Add core module
[scrappy.git] / scrappy.py
bloba64e91a0ca76e2ca5d1244500a39beef31096723
1 #!/usr/bin/env python
2 # Thou shalt use a fixed tab size of 8
4 import irclib_scrappy
5 import sys
7 DEBUG = True
9 def debug(msg):
10 if DEBUG:
11 print msg
13 def externfunc(c,list,self):
14 print "Externfunc"
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.")
18 def loadme(self):
19 debug("LOADME")
21 class scrappy:
22 def __init__(self):
23 debug("Scrappy bot started.")
24 self.cmdchar = '!'
25 self.nickname = 'Scrappy'
26 self.username = 'scrappy'
27 self.realname = 'Scrappy Bot'
28 self.server = ''
29 self.port = 6667
30 self.chanlist = []
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
38 self.modulelist = []
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)
50 #on_load event
51 self.on_load()
53 self.main()
55 def parse_argv(self):
56 """Parse the commandline args and print a usage message if incorrect."""
57 if len(sys.argv) < 3: #Need at least server and nick
58 self.print_usage()
59 sys.exit(1)
61 s = sys.argv[1].split(":", 1)
62 self.server = s[0]
64 if len(s) == 2:
65 try:
66 self.port = int(s[1])
67 except ValueError:
68 print "Error: Erroneous port."
69 sys.exit(1)
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]
78 def main(self):
79 self.parse_argv()
80 self.irc = irclib_scrappy.IRC()
82 try:
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:
87 print x
88 sys.exit(1)
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)
112 self.list_modules()
114 #nothng after this executes
115 self.irc.process_forever()
118 ################
119 #Event Handlers#
120 ################
121 def on_connect(self, c, event):
122 for ch in self.chanlist:
123 if irclib_scrappy.is_channel(ch) :
124 c.join(ch)
126 def on_pubmsg(self, c, event):
127 arg = event.arguments()[0]
128 iscmd = False
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())
134 debug(nick)
136 if arg[0] == self.cmdchar:
137 cmd = arg[1:]
138 iscmd = True
139 else:
140 cmd = arg
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]
148 iscmd = False
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)
157 cmd = cmd[1:]
159 iscmd = True
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:
168 func()
170 sys.exit(0)
172 def on_load(self):
173 for func in self.onload_events:
174 func(self)
176 def modload_cmd(self,c,list,bot):
177 debug("DO STUFF")
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)
190 ################
191 #Module Loading#
192 ################
193 def load_module(self,name):
194 #maybe we should keep the module around?
195 try:
196 module = __import__("modules/%s" % name)
197 except ImportError:
198 #should be error output
199 print "No such module\n"
200 return
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:
212 print mod
215 if(__name__ == "__main__"):
216 bot = scrappy()