2 # Thou shalt use a fixed tab size of 8
17 def modload_cmd(c
,list,bot
):
18 """modload - Loads a module"""
19 cmd
= list[4].split(" ")[0]
21 if cmd
== "modload" and list[3]:
22 param
= list[4].split(" ")[1]
23 bot
.load_module(param
)
25 def modlist_cmd(c
,list,bot
):
26 """modlist - Lists loaded modules"""
27 if list[4] == "modlist" and list[3]:
28 c
.privmsg(list[0],bot
.modulelist
)
33 debug("Scrappy bot started.")
35 self
.nickname
= 'scrappy'
36 self
.username
= 'scrappy'
37 self
.realname
= 'Scrappy Bot'
38 self
.nickservpass
= ''
43 self
.irc
= '' #this will be the socket
44 self
.c
= '' #Thomas, explain this to me later
46 #testvar, should be gotten rid of
47 self
.configing
= False
49 #Experimental stuff for dynamic modules
51 self
.privmsg_events
= []
52 self
.pubmsg_events
= []
53 self
.onload_events
= []
54 self
.onquit_events
= []
55 #self.what_other_events? = []
57 #load modules(currently in main())
58 self
.load_module("config")
59 self
.load_module("core")
60 #self.load_module("markov")
62 self
.register_onload_event(loadme
)
70 """Parse the commandline args and print a usage message if incorrect."""
71 if len(sys
.argv
) < 3: #Need at least server
75 s
= sys
.argv
[1].split(":", 1)
82 print "Error: Erroneous port."
85 self
.nickname
= sys
.argv
[2]
86 for ch
in sys
.argv
[3:]:
87 self
.chanlist
.append('#%s' % ch
)
89 def print_usage(self
):
90 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys
.argv
[0]
94 self
.irc
= irclib_scrappy
.IRC()
97 self
.c
= self
.irc
.server().connect(self
.server
,
98 self
.port
, self
.nickname
,
99 username
=self
.username
,ircname
=self
.realname
)
100 except irclib_scrappy
.ServerConnectionError
, x
:
104 #if all goes well, register handlers
105 self
.c
.add_global_handler("welcome", self
.on_connect
)
106 self
.c
.add_global_handler("disconnect", self
.on_disconnect
)
107 self
.c
.add_global_handler("privmsg", self
.on_privmsg
)
108 self
.c
.add_global_handler("pubmsg", self
.on_pubmsg
)
110 #register some phony modules for testing
111 # self.register_module("My phony module", 'foo', 'foo')
112 # self.register_module("Another phony module", 'foo', 'foo')
114 #register some events
115 self
.register_msg_event(modload_cmd
)
116 self
.register_msg_event(modlist_cmd
)
120 #nothng after this executes
121 self
.irc
.process_forever()
126 def register_privmsg_event(self
,func
):
127 self
.privmsg_events
.append(func
)
129 def register_pubmsg_event(self
,func
):
130 self
.pubmsg_events
.append(func
)
132 #Convenience function, registers the func
133 #for both privmsgs and pubmsgs
134 def register_msg_event(self
,func
):
135 self
.register_privmsg_event(func
)
136 self
.register_pubmsg_event(func
)
138 def register_onload_event(self
,func
):
139 self
.onload_events
.append(func
)
141 def register_onquit_event(self
,func
):
142 self
.onquit_events
.append(func
)
147 def on_connect(self
, c
, event
):
148 for ch
in self
.chanlist
:
149 if irclib_scrappy
.is_channel(ch
) :
152 if self
.identify
== True:
153 self
.c
.privmsg("nickserv", "identify %s" % self
.nickservpass
)
155 def on_pubmsg(self
, c
, event
):
156 arg
= event
.arguments()[0]
159 nick
= irclib_scrappy
.nm_to_n(event
.source())
160 user
= irclib_scrappy
.nm_to_u(event
.source())
161 host
= irclib_scrappy
.nm_to_h(event
.source())
165 if arg
[0] == self
.cmdchar
:
171 #dispatch the command
172 for func
in self
.pubmsg_events
:
173 thread
.start_new_thread(func
, (c
,[nick
,user
,host
,iscmd
,cmd
,event
.target()],self
))
175 def on_privmsg(self
, c
, event
):
176 cmd
= event
.arguments()[0]
179 nick
= irclib_scrappy
.nm_to_n(event
.source())
180 user
= irclib_scrappy
.nm_to_u(event
.source())
181 host
= irclib_scrappy
.nm_to_h(event
.source())
183 if cmd
[0] == self
.cmdchar
:
184 c
.privmsg(nick
,"You privmsged me, you don't need to " +
185 "prefix commands with %s" % self
.cmdchar
)
190 #dispatch the command
191 for func
in self
.privmsg_events
:
192 thread
.start_new_thread(func
, (c
,[nick
,user
,host
,iscmd
,cmd
,event
.target()],self
))
195 def on_disconnect(self
, c
, event
):
196 for func
in self
.onquit_events
:
202 for func
in self
.onload_events
:
208 def load_module(self
,name
):
209 #for whatever reason, OS X needs your modules folder to be in PYTHONPATH to load
211 sys
.path
.append(os
.path
.join(os
.getcwd(), "modules"))
212 module
= __import__(name
)
214 #should be error output
215 print "No such module\n"
216 print traceback
.print_exc()
219 module
.__init
__(self
)
220 self
.register_module(name
,'foo','foo')
222 def register_module(self
, name
, eventlist
, function
):
223 self
.modulelist
.append(name
)
225 def list_modules(self
):
226 """List currently loaded modules."""
227 print "Currently loaded modules:"
228 for mod
in self
.modulelist
:
232 if(__name__
== "__main__"):