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'
36 self
.nickservpass
= ''
41 self
.irc
= '' #this will be the socket
42 self
.c
= '' #Thomas, explain this to me later
44 #testvar, should be gotten rid of
45 self
.configing
= False
47 #Experimental stuff for dynamic modules
49 self
.privmsg_events
= []
50 self
.pubmsg_events
= []
51 self
.onload_events
= []
52 self
.onquit_events
= []
53 #self.what_other_events? = []
55 #load modules(currently in main())
56 self
.load_module("config")
57 self
.load_module("core")
59 self
.register_onload_event(loadme
)
67 """Parse the commandline args and print a usage message if incorrect."""
68 if len(sys
.argv
) < 3: #Need at least server
72 s
= sys
.argv
[1].split(":", 1)
79 print "Error: Erroneous port."
82 self
.nickname
= sys
.argv
[2]
83 for ch
in sys
.argv
[3:]:
84 self
.chanlist
.append('#%s' % ch
)
86 def print_usage(self
):
87 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys
.argv
[0]
91 self
.irc
= irclib_scrappy
.IRC()
94 self
.c
= self
.irc
.server().connect(self
.server
,
95 self
.port
, self
.nickname
,
96 username
=self
.username
,ircname
=self
.realname
)
97 except irclib_scrappy
.ServerConnectionError
, x
:
101 #if all goes well, register handlers
102 self
.c
.add_global_handler("welcome", self
.on_connect
)
103 self
.c
.add_global_handler("disconnect", self
.on_disconnect
)
104 self
.c
.add_global_handler("privmsg", self
.on_privmsg
)
105 self
.c
.add_global_handler("pubmsg", self
.on_pubmsg
)
107 #register some phony modules for testing
108 # self.register_module("My phony module", 'foo', 'foo')
109 # self.register_module("Another phony module", 'foo', 'foo')
111 #register some events
112 self
.register_msg_event(modload_cmd
)
113 self
.register_msg_event(modlist_cmd
)
117 #nothng after this executes
118 self
.irc
.process_forever()
123 def register_privmsg_event(self
,func
):
124 self
.privmsg_events
.append(func
)
126 def register_pubmsg_event(self
,func
):
127 self
.pubmsg_events
.append(func
)
129 #Convenience function, registers the func
130 #for both privmsgs and pubmsgs
131 def register_msg_event(self
,func
):
132 self
.register_privmsg_event(func
)
133 self
.register_pubmsg_event(func
)
135 def register_onload_event(self
,func
):
136 self
.onload_events
.append(func
)
138 def register_onquit_event(self
,func
):
139 self
.onquit_events
.append(func
)
144 def on_connect(self
, c
, event
):
145 for ch
in self
.chanlist
:
146 if irclib_scrappy
.is_channel(ch
) :
149 if self
.identify
== True:
150 self
.c
.privmsg("nickserv", "identify %s" % self
.nickservpass
)
152 def on_pubmsg(self
, c
, event
):
153 arg
= event
.arguments()[0]
156 nick
= irclib_scrappy
.nm_to_n(event
.source())
157 user
= irclib_scrappy
.nm_to_u(event
.source())
158 host
= irclib_scrappy
.nm_to_h(event
.source())
162 if arg
[0] == self
.cmdchar
:
168 #dispatch the command
169 for func
in self
.pubmsg_events
:
170 func(c
,[nick
,user
,host
,iscmd
,cmd
,event
.target()],self
)
172 def on_privmsg(self
, c
, event
):
173 cmd
= event
.arguments()[0]
176 nick
= irclib_scrappy
.nm_to_n(event
.source())
177 user
= irclib_scrappy
.nm_to_u(event
.source())
178 host
= irclib_scrappy
.nm_to_h(event
.source())
180 if cmd
[0] == self
.cmdchar
:
181 c
.privmsg(nick
,"You privmsged me, you don't need to " +
182 "prefix commands with %s" % self
.cmdchar
)
187 #dispatch the command
188 for func
in self
.privmsg_events
:
189 func(c
,[nick
,user
,host
,iscmd
,cmd
,event
.target()],self
)
192 def on_disconnect(self
, c
, event
):
193 for func
in self
.onquit_events
:
199 for func
in self
.onload_events
:
205 def load_module(self
,name
):
206 #for whatever reason, OS X needs your modules folder to be in PYTHONPATH to load
208 sys
.path
.append(os
.path
.join(os
.getcwd(), "modules"))
209 module
= __import__(name
)
211 print "No such module\n"
212 print traceback
.print_exec()
215 module
.__init
__(self
)
216 self
.register_module(name
,'foo','foo')
218 def register_module(self
, name
, eventlist
, function
):
219 self
.modulelist
.append(name
)
221 def list_modules(self
):
222 """List currently loaded modules."""
223 print "Currently loaded modules:"
224 for mod
in self
.modulelist
:
228 if(__name__
== "__main__"):