Better way to grab the command
[scrappy.git] / scrappy.py
blobf221374e095f276f9123658f25d18d3365d401e9
1 #!/usr/bin/env python
2 # Thou shalt use a fixed tab size of 8
4 import irclib_scrappy
5 import sys
7 DEBUG = True
9 def debug(msg):
10 if DEBUG:
11 print msg
13 class scrappy:
14 def __init__(self):
15 debug("Scrappy bot started.")
16 self.cmdchar = '!'
17 self.nickname = 'Scrappy'
18 self.username = 'scrappy'
19 self.realname = 'Scrappy Bot'
20 self.server = ''
21 self.port = 6667
22 self.chanlist = []
23 self.irc = '' #this will be the socket
24 self.c = '' #Thomas, explain this to me later
26 #Experimental stuff for dynamic modules
27 self.modulelist = []
28 #self.privmsg_events = []
29 #self.pubmsg_events = []
30 #self.what_other_events? = []
32 self.main()
34 def parseArgv(self):
35 """Parse the commandline args and print a usage message if incorrect."""
36 if len(sys.argv) < 3: #Need at least server and nick
37 self.printUsage()
38 sys.exit(1)
40 s = sys.argv[1].split(":", 1)
41 self.server = s[0]
43 if len(s) == 2:
44 try:
45 self.port = int(s[1])
46 except ValueError:
47 print "Error: Erroneous port."
48 sys.exit(1)
50 self.nickname = sys.argv[2]
51 for ch in sys.argv[3:]:
52 self.chanlist.append('#%s' % ch)
54 def printUsage(self):
55 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys.argv[0]
57 def main(self):
58 self.parseArgv()
59 self.irc = irclib_scrappy.IRC()
60 try:
61 self.c = self.irc.server().connect(self.server, self.port, self.nickname)
62 except irclib.ServerConnectionError, x:
63 print x
64 sys.exit(1)
65 #if all goes well, register handlers
66 self.c.add_global_handler("welcome", self.on_connect)
67 self.c.add_global_handler("disconnect", self.on_disconnect)
68 self.c.add_global_handler("privmsg", self.on_privmsg)
69 self.c.add_global_handler("pubmsg", self.on_pubmsg)
71 #register some phony modules for testing
72 self.register_module("My phony module", 'foo', 'foo')
73 self.register_module("Another phony module", 'foo', 'foo')
74 self.list_modules()
76 #nothng after this executes
77 self.irc.process_forever()
80 ################
81 #Event Handlers#
82 ################
83 def on_connect(self, c, event):
84 for ch in self.chanlist:
85 if irclib_scrappy.is_channel(ch) :
86 c.join(ch)
88 def on_pubmsg(self, c, event):
89 arg = event.arguments()[0]
90 nick = irclib_scrappy.nm_to_n(event.source())
91 user = irclib_scrappy.nm_to_u(event.source())
92 host = irclib_scrappy.nm_to_h(event.source())
94 if arg[0] == self.cmdchar:
95 cmd = arg[1:]
96 print cmd;
98 #dispatch the command
100 def on_privmsg(self, c, event):
101 cmd = event.arguments()[0]
102 nick = irclib_scrappy.nm_to_n(event.source())
103 user = irclib_scrappy.nm_to_u(event.source())
104 host = irclib_scrappy.nm_to_h(event.source())
106 #dispatch the command
108 def on_disconnect(self, c, event):
109 sys.exit(0)
111 ################
112 #Module Loading#
113 ################
114 def register_module(self, name, eventlist, function):
115 self.modulelist.append(name)
117 def list_modules(self):
118 """List currently loaded modules."""
119 for mod in self.modulelist:
120 print mod
123 if(__name__ == "__main__"):
124 bot = scrappy()