Fix output format
[scrappy.git] / scrappy.py
blob95871ca04faec9d180f0d47102769b296c5d436d
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.nickservpass = ''
37 self.identify = False
38 self.server = ''
39 self.port = 6667
40 self.chanlist = []
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
48 self.modulelist = []
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)
61 #on_load event
62 self.on_load()
64 self.__main()
66 def parse_argv(self):
67 """Parse the commandline args and print a usage message if incorrect."""
68 if len(sys.argv) < 3: #Need at least server
69 self.print_usage()
70 sys.exit(1)
72 s = sys.argv[1].split(":", 1)
73 self.server = s[0]
75 if len(s) == 2:
76 try:
77 self.port = int(s[1])
78 except ValueError:
79 print "Error: Erroneous port."
80 sys.exit(1)
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]
89 def __main(self):
90 self.parse_argv()
91 self.irc = irclib_scrappy.IRC()
93 try:
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:
98 print x
99 sys.exit(1)
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)
115 self.list_modules()
117 #nothng after this executes
118 self.irc.process_forever()
120 ##################
121 #Event Processing#
122 ##################
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)
141 ################
142 #Event Handlers#
143 ################
144 def on_connect(self, c, event):
145 for ch in self.chanlist:
146 if irclib_scrappy.is_channel(ch) :
147 c.join(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]
154 iscmd = False
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())
160 debug(nick)
162 if arg[0] == self.cmdchar:
163 cmd = arg[1:]
164 iscmd = True
165 else:
166 cmd = arg
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]
174 iscmd = False
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)
183 cmd = cmd[1:]
185 iscmd = True
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:
194 func()
196 sys.exit(0)
198 def on_load(self):
199 for func in self.onload_events:
200 func(self)
202 ################
203 #Module Loading#
204 ################
205 def load_module(self,name):
206 #maybe we should keep the module around?
207 try:
208 module = __import__("modules/%s" % name)
209 except ImportError:
210 #should be error output
211 print "No such module\n"
212 return
214 module.__init__(self)
215 self.register_module(name,'foo','foo')
217 def register_module(self, name, eventlist, function):
218 self.modulelist.append(name)
220 def list_modules(self):
221 """List currently loaded modules."""
222 print "Currently loaded modules:"
223 for mod in self.modulelist:
224 print mod
227 if(__name__ == "__main__"):
228 bot = scrappy()