Populate the markov chain
[scrappy.git] / scrappy.py
bloba644552a785d9aeb96753d6c5d3617493a8bab8c
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
12 def loadme(self):
13 debug("LOADME")
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)
29 class scrappy:
30 def __init__(self):
31 debug("Scrappy bot started.")
32 self.cmdchar = '!'
33 self.nickname = 'Scrappy'
34 self.username = 'scrappy'
35 self.realname = 'Scrappy Bot'
36 self.server = ''
37 self.port = 6667
38 self.chanlist = []
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
46 self.modulelist = []
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)
59 #on_load event
60 self.on_load()
62 self.__main()
64 def parse_argv(self):
65 """Parse the commandline args and print a usage message if incorrect."""
66 if len(sys.argv) < 3: #Need at least server and nick
67 self.print_usage()
68 sys.exit(1)
70 s = sys.argv[1].split(":", 1)
71 self.server = s[0]
73 if len(s) == 2:
74 try:
75 self.port = int(s[1])
76 except ValueError:
77 print "Error: Erroneous port."
78 sys.exit(1)
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]
87 def __main(self):
88 self.parse_argv()
89 self.irc = irclib_scrappy.IRC()
91 try:
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:
96 print x
97 sys.exit(1)
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)
113 self.list_modules()
115 #nothng after this executes
116 self.irc.process_forever()
118 ##################
119 #Event Processing#
120 ##################
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)
139 ################
140 #Event Handlers#
141 ################
142 def on_connect(self, c, event):
143 for ch in self.chanlist:
144 if irclib_scrappy.is_channel(ch) :
145 c.join(ch)
147 def on_pubmsg(self, c, event):
148 arg = event.arguments()[0]
149 iscmd = False
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())
155 debug(nick)
157 if arg[0] == self.cmdchar:
158 cmd = arg[1:]
159 iscmd = True
160 else:
161 cmd = arg
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]
169 iscmd = False
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)
178 cmd = cmd[1:]
180 iscmd = True
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:
189 func()
191 sys.exit(0)
193 def on_load(self):
194 for func in self.onload_events:
195 func(self)
197 ################
198 #Module Loading#
199 ################
200 def load_module(self,name):
201 #maybe we should keep the module around?
202 try:
203 module = __import__("modules/%s" % name)
204 except ImportError:
205 #should be error output
206 print "No such module\n"
207 return
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:
219 print mod
222 if(__name__ == "__main__"):
223 bot = scrappy()