2 #Let's keep this file in particular as clean and neatly organized as possible.
3 #If this is nice and organized, then writing new modules will be a snap and this
4 #file should rarely have to be edited.
11 ################################################################################
12 #set to False to turn off debugging to stdout
19 ################################################################################
22 #this is our main bot class. Once scrappy.py is called, an instance of this
23 #class (our bot) gets created and some initialization is done. The real work is
24 #done via modules that get loaded here.
27 debug("Scrappy bot started.")
28 #hard-code these for now
29 #then write a config loading module
31 self
.nickname
= 'scrappy'
32 self
.username
= 'scrappy'
33 self
.realname
= 'Scrappy Bot'
37 self
.ircsock
= '' #this will be the socket
38 self
.connection
= '' #Thomas, explain this to me later
42 #each module adds functions to be called to these events.
43 #each event handler calls all of the functions within its list.
44 self
.events
= ["connect", "disconnect", "error", "invite",
45 "join", "kick", "load", "mode", "msg", "part", "ping", "pong",
46 "privmsg", "privnotice", "pubmsg", "pubnotice", "quit"]
48 self
.connect_events
= []
49 self
.disconnect_events
= []
50 self
.error_events
= []
51 self
.invite_events
= []
60 self
.privmsg_events
= []
61 self
.privnotice_events
= []
62 self
.pubmsg_events
= []
63 self
.pubnotice_events
= []
64 self
.quit_events
= [] #for other users quitting, not the bot
65 #self.what_other_events? = []
67 sys
.path
.append(os
.path
.join(os
.getcwd(), "modules"))
68 #need to load a few modules here - the rest are done elsewhere
69 self
.load_module("modmanage")
70 self
.load_module("core")
71 self
.load_module("config")
73 #self.register_onload_event(loadme)
80 ########################################################################
82 """Parse the commandline args and print a usage message if incorrect."""
83 if len(sys
.argv
) < 3: #Need at least server
87 #split out the port if specified
88 s
= sys
.argv
[1].split(":", 1)
96 print "Error: Erroneous port."
99 self
.nickname
= sys
.argv
[2]
100 #add channels to chanlist
101 for ch
in sys
.argv
[3:]:
102 self
.chanlist
.append('#%s' % ch
)
104 def print_usage(self
):
105 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys
.argv
[0]
107 ########################################################################
109 """The real work. Initialize our connection and register events."""
110 #parse comamnd line and create a new socket
112 self
.ircsock
= irclib_scrappy
.IRC()
114 #attempt to create a socket and connect to the server
116 self
.connection
= self
.ircsock
.server().connect(self
.server
,
117 self
.port
, self
.nickname
,
118 username
=self
.username
,ircname
=self
.realname
)
119 #connection failed, print error and exit
120 except irclib_scrappy
.ServerConnectionError
, x
:
124 #if all goes well, register handlers
125 self
.connection
.add_global_handler("welcome", self
.on_connect
)
126 self
.connection
.add_global_handler("disconnect", self
.on_disconnect
)
127 self
.connection
.add_global_handler("error", self
.on_error
)
128 self
.connection
.add_global_handler("invite", self
.on_invite
)
129 self
.connection
.add_global_handler("join", self
.on_join
)
130 self
.connection
.add_global_handler("kick", self
.on_kick
)
131 self
.connection
.add_global_handler("mode", self
.on_mode
)
132 self
.connection
.add_global_handler("part", self
.on_part
)
133 self
.connection
.add_global_handler("ping", self
.on_ping
)
134 self
.connection
.add_global_handler("pong", self
.on_pong
)
135 self
.connection
.add_global_handler("privmsg", self
.on_privmsg
)
136 self
.connection
.add_global_handler("privnotice", self
.on_privnotice
)
137 self
.connection
.add_global_handler("pubmsg", self
.on_privmsg
)
138 self
.connection
.add_global_handler("quit", self
.on_quit
)
143 #enter main event loop after this
146 self
.ircsock
.process_forever()
147 except KeyboardInterrupt:
148 self
.connection
.quit("Keyboard interrupt!")
151 ########################################################################
156 def register_event(self
, event_type
, func
):
157 """Call this with an event_type and a function to call when that event_type happens."""
158 #list of legal event types
161 if not event_type
in self
.events
:
162 debug("I don't know what an %s event is." % event_type
)
165 #event type is good. Add it to appropriate event list
168 #BUGGY NONWORKING STUFF
170 listname
= "self."+event_type
+"_events"
171 #debug(func.__name__)
172 if func
.__name
__ in eval(listname
):
173 debug("%s already exists in %s! Removing old copy and inserting this..." % (func
, listname
))
174 eval(listname
).remove(func
.__name
__)
176 eval(listname
).append(func
)
178 if event_type
== "msg":
179 self
.privmsg_events
.append(func
)
180 self
.pubmsg_events
.append(func
)
183 def unregister_event(self
, event_type
, func
):
187 ########################################################################
192 def on_connect(self
, conn
, eventlist
):
193 """Called when bot makes a connection to the server."""
194 #do all of our events
195 for func
in self
.connect_events
:
196 thread
.start_new_thread(func
)
199 for chan
in self
.chanlist
:
200 if irclib_scrappy
.is_channel(chan
):
203 ########################################################################
204 def on_disconnect(self
, conn
, eventlist
):
205 """Called when the connection to the server is closed."""
206 for func
in self
.disconnect_events
:
207 thread
.start_new_thread(func
)
208 conn
.quit("Scrappy bot signing off.")
209 #do we need to clean stuff up?
212 ########################################################################
213 def on_error(self
, conn
, eventlist
):
214 debug("Error received: %s" % eventlist
.arguments())
215 for func
in self
.error_events
:
216 thread
.start_new_thread(func
)
219 ########################################################################
220 def on_invite(self
, conn
, eventlist
):
221 debug("Received an invite: %s" % eventlist
.arguments())
222 for func
in self
.invite_events
:
223 thread
.start_new_thread(func
)
225 ########################################################################
226 def on_join(self
, conn
, eventlist
):
227 debug("User joined: %s" % eventlist
.arguments())
228 for func
in self
.join_events
:
229 thread
.start_new_thread(func
)
231 ########################################################################
232 def on_kick(self
, conn
, eventlist
):
233 debug("Someone was kicked: %s" % eventlist
.arguments())
234 for func
in self
.kick_events
:
235 thread
.start_new_thread(func
)
237 ########################################################################
238 def on_mode(self
, conn
, eventlist
):
239 debug("Mode change: %s" % eventlist
.arguments())
240 for func
in self
.mode_events
:
241 thread
.start_new_thread(func
)
243 ########################################################################
244 def on_part(self
, conn
, eventlist
):
245 debug("Part: %s" % eventlist
.arguments())
246 for func
in self
.part_events
:
247 thread
.start_new_thread(func
)
249 ########################################################################
250 def on_ping(self
, conn
, eventlist
):
251 debug("Ping: %s" % eventlist
.arguments())
252 for func
in self
.ping_events
:
253 thread
.start_new_thread(func
)
255 ########################################################################
256 def on_pong(self
, conn
, eventlist
):
257 debug("Pong: %s" % eventlist
.arguments())
258 for func
in self
.pong_events
:
259 thread
.start_new_thread(func
)
261 ########################################################################
262 def on_privmsg(self
, conn
, eventlist
):
263 """Called when bot receives a private or channel (public) message."""
264 #eventlist.arguments() = the message body
265 arg
= eventlist
.arguments()[0]
269 nick
= irclib_scrappy
.nm_to_n(eventlist
.source())
270 user
= irclib_scrappy
.nm_to_u(eventlist
.source())
271 host
= irclib_scrappy
.nm_to_h(eventlist
.source())
273 if arg
[0] == self
.cmdchar
:
279 #how can we make the list that's passed to functions more friendly?
280 #we end up parsing the list again in the called function...
281 for func
in self
.privmsg_events
:
282 thread
.start_new_thread(func
, (conn
, [nick
, user
, host
, iscmd
, cmd
, eventlist
.target()], self
))
286 ########################################################################
287 def on_privnotice(self
, conn
, eventlist
):
288 debug("Privnotice: %s" % eventlist
.arguments())
289 for func
in self
.privnotice_events
:
290 thread
.start_new_thread(func
)
294 ########################################################################
295 #right now this isn't used because it's assumed that privmsg == pubmsg
296 #this should probably be changed...
297 def on_pubmsg(self
, conn
, eventlist
):
298 debug("Pubmsg: % " % eventlist
.arguments())
299 for func
in pubmsg_events
:
302 ########################################################################
303 def on_quit(self
, conn
, eventlist
):
304 debug("Quit: %s" % eventlist
.arguments())
305 for func
in quit_events
:
306 thread
.start_new_thread(func
)
312 def load_module(self
,name
):
314 #for whatever reason, OS X needs your modules folder to be in PYTHONPATH to load
316 #sys.path.append(os.path.join(os.getcwd(), "modules"))
317 exec("from %s import %s" % (name
, name
))
318 #module = __import__(name)
320 #should be error output
321 print "No such module\n"
322 print traceback
.print_exc()
323 return "Sorry, there was an error loading %s." % name
325 eval(name
).init(self
)
326 self
.register_module(name
,'foo','foo')
327 return "Loaded %s." % name
329 def unload_module(self
, name
):
331 self
.modulelist
.index(name
)
333 print "No such module!"
334 return "Sorry, no module named %s is loaded." % name
335 self
.unregister_module(name
)
337 return "%s unloaded. (but not really)" % name
339 def register_module(self
, name
, eventlist
, function
):
340 self
.modulelist
.append(name
)
342 def unregister_module(self
, name
):
343 self
.modulelist
.remove(name
)
347 def list_modules(self
):
348 """List currently loaded modules."""
349 print "Currently loaded modules:"
350 for mod
in self
.modulelist
:
354 if(__name__
== "__main__"):