2 # Thou shalt use a fixed tab size of 8
13 def externfunc(c
,list,self
):
15 def deadline(c
,list,bot
):
16 if list[4] == "deadline" and list[3]:
17 c
.privmsg(list[0],"The deadline for SoC has expired.")
23 debug("Scrappy bot started.")
25 self
.nickname
= 'Scrappy'
26 self
.username
= 'scrappy'
27 self
.realname
= 'Scrappy Bot'
31 self
.irc
= '' #this will be the socket
32 self
.c
= '' #Thomas, explain this to me later
34 #testvar, should be gotten rid of
35 self
.configing
= False
37 #Experimental stuff for dynamic modules
39 self
.privmsg_events
= []
40 self
.pubmsg_events
= []
41 self
.onload_events
= []
42 self
.onquit_events
= []
43 #self.what_other_events? = []
45 #load modules(currently in main())
46 self
.load_module("config")
48 self
.onload_events
.append(loadme
)
56 """Parse the commandline args and print a usage message if incorrect."""
57 if len(sys
.argv
) < 3: #Need at least server and nick
61 s
= sys
.argv
[1].split(":", 1)
68 print "Error: Erroneous port."
71 self
.nickname
= sys
.argv
[2]
72 for ch
in sys
.argv
[3:]:
73 self
.chanlist
.append('#%s' % ch
)
75 def print_usage(self
):
76 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys
.argv
[0]
80 self
.irc
= irclib_scrappy
.IRC()
83 self
.c
= self
.irc
.server().connect(self
.server
,
84 self
.port
, self
.nickname
,
85 username
=self
.username
,ircname
=self
.realname
)
86 except irclib_scrappy
.ServerConnectionError
, x
:
90 #if all goes well, register handlers
91 self
.c
.add_global_handler("welcome", self
.on_connect
)
92 self
.c
.add_global_handler("disconnect", self
.on_disconnect
)
93 self
.c
.add_global_handler("privmsg", self
.on_privmsg
)
94 self
.c
.add_global_handler("pubmsg", self
.on_pubmsg
)
96 #register some phony modules for testing
97 # self.register_module("My phony module", 'foo', 'foo')
98 # self.register_module("Another phony module", 'foo', 'foo')
100 #register some events
101 self
.pubmsg_events
.append(externfunc
)
103 self
.privmsg_events
.append(deadline
)
104 self
.pubmsg_events
.append(deadline
)
106 self
.privmsg_events
.append(self
.modload_cmd
)
107 self
.pubmsg_events
.append(self
.modload_cmd
)
109 self
.privmsg_events
.append(self
.modlist_cmd
)
110 self
.pubmsg_events
.append(self
.modlist_cmd
)
114 #nothng after this executes
115 self
.irc
.process_forever()
121 def on_connect(self
, c
, event
):
122 for ch
in self
.chanlist
:
123 if irclib_scrappy
.is_channel(ch
) :
126 def on_pubmsg(self
, c
, event
):
127 arg
= event
.arguments()[0]
130 nick
= irclib_scrappy
.nm_to_n(event
.source())
131 user
= irclib_scrappy
.nm_to_u(event
.source())
132 host
= irclib_scrappy
.nm_to_h(event
.source())
136 if arg
[0] == self
.cmdchar
:
142 #dispatch the command
143 for func
in self
.pubmsg_events
:
144 func(c
,[nick
,user
,host
,iscmd
,cmd
],self
)
146 def on_privmsg(self
, c
, event
):
147 cmd
= event
.arguments()[0]
150 nick
= irclib_scrappy
.nm_to_n(event
.source())
151 user
= irclib_scrappy
.nm_to_u(event
.source())
152 host
= irclib_scrappy
.nm_to_h(event
.source())
154 if cmd
[0] == self
.cmdchar
:
155 c
.privmsg(nick
,"You privmsged me, you don't need to " +
156 "prefix commands with %s" % self
.cmdchar
)
161 #dispatch the command
162 for func
in self
.privmsg_events
:
163 func(c
,[nick
,user
,host
,iscmd
,cmd
],self
)
166 def on_disconnect(self
, c
, event
):
167 for func
in self
.onquit_events
:
173 for func
in self
.onload_events
:
176 def modload_cmd(self
,c
,list,bot
):
179 cmd
= list[4].split(" ")[0]
181 if cmd
== "modload" and list[3]:
182 param
= list[4].split(" ")[1]
183 self
.load_module(param
)
186 def modlist_cmd(self
,c
,list,bot
):
187 if list[4] == "modlist" and list[3]:
188 c
.privmsg(list[0],self
.modulelist
)
193 def load_module(self
,name
):
194 #maybe we should keep the module around?
196 module
= __import__("modules/%s" % name
)
198 #should be error output
199 print "No such module\n"
202 module
.__init
__(self
)
203 self
.register_module(name
,'foo','foo')
205 def register_module(self
, name
, eventlist
, function
):
206 self
.modulelist
.append(name
)
208 def list_modules(self
):
209 """List currently loaded modules."""
210 print "Currently loaded modules:"
211 for mod
in self
.modulelist
:
215 if(__name__
== "__main__"):