2 # Thou shalt use a fixed tab size of 8
12 def externfunc(c
,list,self
):
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.")
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
)
38 debug("Scrappy bot started.")
40 self
.nickname
= 'Scrappy'
41 self
.username
= 'scrappy'
42 self
.realname
= 'Scrappy Bot'
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
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
)
71 """Parse the commandline args and print a usage message if incorrect."""
72 if len(sys
.argv
) < 3: #Need at least server and nick
76 s
= sys
.argv
[1].split(":", 1)
83 print "Error: Erroneous port."
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]
95 self
.irc
= irclib_scrappy
.IRC()
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
:
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
)
126 #nothng after this executes
127 self
.irc
.process_forever()
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
)
153 def on_connect(self
, c
, event
):
154 for ch
in self
.chanlist
:
155 if irclib_scrappy
.is_channel(ch
) :
158 def on_pubmsg(self
, c
, event
):
159 arg
= event
.arguments()[0]
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())
168 if arg
[0] == self
.cmdchar
:
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]
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
)
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
:
205 for func
in self
.onload_events
:
211 def load_module(self
,name
):
212 #maybe we should keep the module around?
214 module
= __import__("modules/%s" % name
)
216 #should be error output
217 print "No such module\n"
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
:
233 if(__name__
== "__main__"):