Don't say anything when increasing/decreasing karam
[scrappy.git] / scrappy.py
blob7c87e1bcb19e41e052edd908a5d952e133489ae8
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
12 def externfunc(c,list,self):
13 """externfunc"""
14 print "Externfunc"
15 def deadline(c,list,bot):
16 """deadline - Testing command"""
17 if list[4] == "deadline" and list[3]:
18 c.privmsg(list[0],"The deadline for SoC has expired.")
19 def loadme(self):
20 debug("LOADME")
22 def modload_cmd(c,list,bot):
23 """modload - Loads a module"""
24 cmd = list[4].split(" ")[0]
26 if cmd == "modload" and list[3]:
27 param = list[4].split(" ")[1]
28 bot.load_module(param)
30 def modlist_cmd(c,list,bot):
31 """modlist - Lists loaded modules"""
32 if list[4] == "modlist" and list[3]:
33 c.privmsg(list[0],bot.modulelist)
36 class scrappy:
37 def __init__(self):
38 debug("Scrappy bot started.")
39 self.cmdchar = '!'
40 self.nickname = 'Scrappy'
41 self.username = 'scrappy'
42 self.realname = 'Scrappy Bot'
43 self.server = ''
44 self.port = 6667
45 self.chanlist = []
46 self.irc = '' #this will be the socket
47 self.c = '' #Thomas, explain this to me later
49 #testvar, should be gotten rid of
50 self.configing = False
52 #Experimental stuff for dynamic modules
53 self.modulelist = []
54 self.privmsg_events = []
55 self.pubmsg_events = []
56 self.onload_events = []
57 self.onquit_events = []
58 #self.what_other_events? = []
60 #load modules(currently in main())
61 self.load_module("config")
63 self.register_onload_event(loadme)
65 #on_load event
66 self.on_load()
68 self.__main()
70 def parse_argv(self):
71 """Parse the commandline args and print a usage message if incorrect."""
72 if len(sys.argv) < 3: #Need at least server and nick
73 self.print_usage()
74 sys.exit(1)
76 s = sys.argv[1].split(":", 1)
77 self.server = s[0]
79 if len(s) == 2:
80 try:
81 self.port = int(s[1])
82 except ValueError:
83 print "Error: Erroneous port."
84 sys.exit(1)
86 self.nickname = sys.argv[2]
87 for ch in sys.argv[3:]:
88 self.chanlist.append('#%s' % ch)
90 def print_usage(self):
91 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys.argv[0]
93 def __main(self):
94 self.parse_argv()
95 self.irc = irclib_scrappy.IRC()
97 try:
98 self.c = self.irc.server().connect(self.server,
99 self.port, self.nickname,
100 username=self.username,ircname=self.realname)
101 except irclib_scrappy.ServerConnectionError, x:
102 print x
103 sys.exit(1)
105 #if all goes well, register handlers
106 self.c.add_global_handler("welcome", self.on_connect)
107 self.c.add_global_handler("disconnect", self.on_disconnect)
108 self.c.add_global_handler("privmsg", self.on_privmsg)
109 self.c.add_global_handler("pubmsg", self.on_pubmsg)
111 #register some phony modules for testing
112 # self.register_module("My phony module", 'foo', 'foo')
113 # self.register_module("Another phony module", 'foo', 'foo')
115 #register some events
116 self.register_pubmsg_event(externfunc)
118 self.register_msg_event(deadline)
120 self.register_msg_event(modload_cmd)
122 self.register_msg_event(modlist_cmd)
124 self.list_modules()
126 #nothng after this executes
127 self.irc.process_forever()
129 ##################
130 #Event Processing#
131 ##################
132 def register_privmsg_event(self,func):
133 self.privmsg_events.append(func)
135 def register_pubmsg_event(self,func):
136 self.pubmsg_events.append(func)
138 #Convenience function, registers the func
139 #for both privmsgs and pubmsgs
140 def register_msg_event(self,func):
141 self.register_privmsg_event(func)
142 self.register_pubmsg_event(func)
144 def register_onload_event(self,func):
145 self.onload_events.append(func)
147 def register_onquit_event(self,func):
148 self.onquit_events.append(func)
150 ################
151 #Event Handlers#
152 ################
153 def on_connect(self, c, event):
154 for ch in self.chanlist:
155 if irclib_scrappy.is_channel(ch) :
156 c.join(ch)
158 def on_pubmsg(self, c, event):
159 arg = event.arguments()[0]
160 iscmd = False
162 nick = irclib_scrappy.nm_to_n(event.source())
163 user = irclib_scrappy.nm_to_u(event.source())
164 host = irclib_scrappy.nm_to_h(event.source())
166 debug(nick)
168 if arg[0] == self.cmdchar:
169 cmd = arg[1:]
170 iscmd = True
171 else:
172 cmd = arg
174 #dispatch the command
175 for func in self.pubmsg_events:
176 func(c,[nick,user,host,iscmd,cmd,event.target()],self)
178 def on_privmsg(self, c, event):
179 cmd = event.arguments()[0]
180 iscmd = False
182 nick = irclib_scrappy.nm_to_n(event.source())
183 user = irclib_scrappy.nm_to_u(event.source())
184 host = irclib_scrappy.nm_to_h(event.source())
186 if cmd[0] == self.cmdchar:
187 c.privmsg(nick,"You privmsged me, you don't need to " +
188 "prefix commands with %s" % self.cmdchar)
189 cmd = cmd[1:]
191 iscmd = True
193 #dispatch the command
194 for func in self.privmsg_events:
195 func(c,[nick,user,host,iscmd,cmd,event.target()],self)
198 def on_disconnect(self, c, event):
199 for func in self.onquit_events:
200 func()
202 sys.exit(0)
204 def on_load(self):
205 for func in self.onload_events:
206 func(self)
208 ################
209 #Module Loading#
210 ################
211 def load_module(self,name):
212 #maybe we should keep the module around?
213 try:
214 module = __import__("modules/%s" % name)
215 except ImportError:
216 #should be error output
217 print "No such module\n"
218 return
220 module.__init__(self)
221 self.register_module(name,'foo','foo')
223 def register_module(self, name, eventlist, function):
224 self.modulelist.append(name)
226 def list_modules(self):
227 """List currently loaded modules."""
228 print "Currently loaded modules:"
229 for mod in self.modulelist:
230 print mod
233 if(__name__ == "__main__"):
234 bot = scrappy()