2 # Thou shalt use a fixed tab size of 8
15 def modload_cmd(c
,list,bot
):
16 """modload - Loads a module"""
17 cmd
= list[4].split(" ")[0]
19 if cmd
== "modload" and list[3]:
20 param
= list[4].split(" ")[1]
21 bot
.load_module(param
)
23 def modlist_cmd(c
,list,bot
):
24 """modlist - Lists loaded modules"""
25 if list[4] == "modlist" and list[3]:
26 c
.privmsg(list[0],bot
.modulelist
)
31 debug("Scrappy bot started.")
33 self
.nickname
= 'Scrappy'
34 self
.username
= 'scrappy'
35 self
.realname
= 'Scrappy Bot'
39 self
.irc
= '' #this will be the socket
40 self
.c
= '' #Thomas, explain this to me later
42 #testvar, should be gotten rid of
43 self
.configing
= False
45 #Experimental stuff for dynamic modules
47 self
.privmsg_events
= []
48 self
.pubmsg_events
= []
49 self
.onload_events
= []
50 self
.onquit_events
= []
51 #self.what_other_events? = []
53 #load modules(currently in main())
54 self
.load_module("config")
55 self
.load_module("core")
57 self
.register_onload_event(loadme
)
65 """Parse the commandline args and print a usage message if incorrect."""
66 if len(sys
.argv
) < 3: #Need at least server and nick
70 s
= sys
.argv
[1].split(":", 1)
77 print "Error: Erroneous port."
80 self
.nickname
= sys
.argv
[2]
81 for ch
in sys
.argv
[3:]:
82 self
.chanlist
.append('#%s' % ch
)
84 def print_usage(self
):
85 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys
.argv
[0]
89 self
.irc
= irclib_scrappy
.IRC()
92 self
.c
= self
.irc
.server().connect(self
.server
,
93 self
.port
, self
.nickname
,
94 username
=self
.username
,ircname
=self
.realname
)
95 except irclib_scrappy
.ServerConnectionError
, x
:
99 #if all goes well, register handlers
100 self
.c
.add_global_handler("welcome", self
.on_connect
)
101 self
.c
.add_global_handler("disconnect", self
.on_disconnect
)
102 self
.c
.add_global_handler("privmsg", self
.on_privmsg
)
103 self
.c
.add_global_handler("pubmsg", self
.on_pubmsg
)
105 #register some phony modules for testing
106 # self.register_module("My phony module", 'foo', 'foo')
107 # self.register_module("Another phony module", 'foo', 'foo')
109 #register some events
110 self
.register_msg_event(modload_cmd
)
111 self
.register_msg_event(modlist_cmd
)
115 #nothng after this executes
116 self
.irc
.process_forever()
121 def register_privmsg_event(self
,func
):
122 self
.privmsg_events
.append(func
)
124 def register_pubmsg_event(self
,func
):
125 self
.pubmsg_events
.append(func
)
127 #Convenience function, registers the func
128 #for both privmsgs and pubmsgs
129 def register_msg_event(self
,func
):
130 self
.register_privmsg_event(func
)
131 self
.register_pubmsg_event(func
)
133 def register_onload_event(self
,func
):
134 self
.onload_events
.append(func
)
136 def register_onquit_event(self
,func
):
137 self
.onquit_events
.append(func
)
142 def on_connect(self
, c
, event
):
143 for ch
in self
.chanlist
:
144 if irclib_scrappy
.is_channel(ch
) :
147 def on_pubmsg(self
, c
, event
):
148 arg
= event
.arguments()[0]
151 nick
= irclib_scrappy
.nm_to_n(event
.source())
152 user
= irclib_scrappy
.nm_to_u(event
.source())
153 host
= irclib_scrappy
.nm_to_h(event
.source())
157 if arg
[0] == self
.cmdchar
:
163 #dispatch the command
164 for func
in self
.pubmsg_events
:
165 func(c
,[nick
,user
,host
,iscmd
,cmd
,event
.target()],self
)
167 def on_privmsg(self
, c
, event
):
168 cmd
= event
.arguments()[0]
171 nick
= irclib_scrappy
.nm_to_n(event
.source())
172 user
= irclib_scrappy
.nm_to_u(event
.source())
173 host
= irclib_scrappy
.nm_to_h(event
.source())
175 if cmd
[0] == self
.cmdchar
:
176 c
.privmsg(nick
,"You privmsged me, you don't need to " +
177 "prefix commands with %s" % self
.cmdchar
)
182 #dispatch the command
183 for func
in self
.privmsg_events
:
184 func(c
,[nick
,user
,host
,iscmd
,cmd
,event
.target()],self
)
187 def on_disconnect(self
, c
, event
):
188 for func
in self
.onquit_events
:
194 for func
in self
.onload_events
:
200 def load_module(self
,name
):
201 #maybe we should keep the module around?
203 module
= __import__("modules/%s" % name
)
205 #should be error output
206 print "No such module\n"
209 module
.__init
__(self
)
210 self
.register_module(name
,'foo','foo')
212 def register_module(self
, name
, eventlist
, function
):
213 self
.modulelist
.append(name
)
215 def list_modules(self
):
216 """List currently loaded modules."""
217 print "Currently loaded modules:"
218 for mod
in self
.modulelist
:
222 if(__name__
== "__main__"):