Add an 'iscmd' field to the list passed to registered events.
[scrappy.git] / scrappy.py
blob79017b3c382e3e551dfa42e69fb19045c45f1350
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 def externfunc(c,list):
14 print "Externfunc"
15 def deadline(c,list):
16 if list[4] == "deadline" and list[3]:
17 c.privmsg(list[0],"The deadline for SoC has expired.")
19 class scrappy:
20 def __init__(self):
21 debug("Scrappy bot started.")
22 self.cmdchar = '!'
23 self.nickname = 'Scrappy'
24 self.username = 'scrappy'
25 self.realname = 'Scrappy Bot'
26 self.server = ''
27 self.port = 6667
28 self.chanlist = []
29 self.irc = '' #this will be the socket
30 self.c = '' #Thomas, explain this to me later
32 #Experimental stuff for dynamic modules
33 self.modulelist = []
34 self.privmsg_events = []
35 self.pubmsg_events = []
36 #self.what_other_events? = []
38 self.main()
40 def parseArgv(self):
41 """Parse the commandline args and print a usage message if incorrect."""
42 if len(sys.argv) < 3: #Need at least server and nick
43 self.printUsage()
44 sys.exit(1)
46 s = sys.argv[1].split(":", 1)
47 self.server = s[0]
49 if len(s) == 2:
50 try:
51 self.port = int(s[1])
52 except ValueError:
53 print "Error: Erroneous port."
54 sys.exit(1)
56 self.nickname = sys.argv[2]
57 for ch in sys.argv[3:]:
58 self.chanlist.append('#%s' % ch)
60 def printUsage(self):
61 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys.argv[0]
63 def main(self):
64 self.parseArgv()
65 self.irc = irclib_scrappy.IRC()
66 try:
67 self.c = self.irc.server().connect(self.server,
68 self.port, self.nickname,
69 username=self.username,ircname=self.realname)
70 except irclib.ServerConnectionError, x:
71 print x
72 sys.exit(1)
73 #if all goes well, register handlers
74 self.c.add_global_handler("welcome", self.on_connect)
75 self.c.add_global_handler("disconnect", self.on_disconnect)
76 self.c.add_global_handler("privmsg", self.on_privmsg)
77 self.c.add_global_handler("pubmsg", self.on_pubmsg)
79 #register some phony modules for testing
80 self.register_module("My phony module", 'foo', 'foo')
81 self.register_module("Another phony module", 'foo', 'foo')
82 self.pubmsg_events.append(externfunc)
83 self.pubmsg_events.append(deadline)
84 self.privmsg_events.append(deadline)
85 self.list_modules()
87 #nothng after this executes
88 self.irc.process_forever()
91 ################
92 #Event Handlers#
93 ################
94 def on_connect(self, c, event):
95 for ch in self.chanlist:
96 if irclib_scrappy.is_channel(ch) :
97 c.join(ch)
99 def on_pubmsg(self, c, event):
100 arg = event.arguments()[0]
101 iscmd = False
103 nick = irclib_scrappy.nm_to_n(event.source())
104 user = irclib_scrappy.nm_to_u(event.source())
105 host = irclib_scrappy.nm_to_h(event.source())
107 if arg[0] == self.cmdchar:
108 cmd = arg[1:]
109 iscmd = True
110 else:
111 cmd = arg
113 #dispatch the command
114 for func in self.pubmsg_events:
115 func(c,[nick,user,host,iscmd,cmd])
117 def on_privmsg(self, c, event):
118 cmd = event.arguments()[0]
119 iscmd = False
121 nick = irclib_scrappy.nm_to_n(event.source())
122 user = irclib_scrappy.nm_to_u(event.source())
123 host = irclib_scrappy.nm_to_h(event.source())
125 if cmd[0] == self.cmdchar:
126 c.privmsg(nick,"You privmsged me, you don't need to " +
127 "prefix commands with %s" % self.cmdchar)
128 cmd = cmd[1:]
129 iscmd = True
131 #dispatch the command
132 for func in self.privmsg_events:
133 func(c,[nick,user,host,iscmd,cmd])
136 def on_disconnect(self, c, event):
137 sys.exit(0)
139 ################
140 #Module Loading#
141 ################
142 def register_module(self, name, eventlist, function):
143 self.modulelist.append(name)
145 def list_modules(self):
146 """List currently loaded modules."""
147 for mod in self.modulelist:
148 print mod
151 if(__name__ == "__main__"):
152 bot = scrappy()