Fix typo
[scrappy.git] / scrappy.py
blob9aeaf12c5d3251d65bd75499bc263c9613f6d1a2
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):
14 print "Externfunc"
15 def deadline(c,list):
16 if list[4] == "deadline" and list[3]:
17 c.privmsg(list[0],"The deadline for SoC has expired.")
18 def loadme():
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 #Experimental stuff for dynamic modules
35 self.modulelist = []
36 self.privmsg_events = []
37 self.pubmsg_events = []
38 self.onload_events = []
39 self.onquit_events = []
40 #self.what_other_events? = []
42 #load modules(currently in main())
44 self.onload_events.append(loadme)
46 #on_load event
47 self.on_load()
49 self.main()
51 def parse_argv(self):
52 """Parse the commandline args and print a usage message if incorrect."""
53 if len(sys.argv) < 3: #Need at least server and nick
54 self.print_usage()
55 sys.exit(1)
57 s = sys.argv[1].split(":", 1)
58 self.server = s[0]
60 if len(s) == 2:
61 try:
62 self.port = int(s[1])
63 except ValueError:
64 print "Error: Erroneous port."
65 sys.exit(1)
67 self.nickname = sys.argv[2]
68 for ch in sys.argv[3:]:
69 self.chanlist.append('#%s' % ch)
71 def print_usage(self):
72 print 'Usage: %s <server[:port]> <nickname> [channel 1 channel 2 ... channelN]' % sys.argv[0]
74 def main(self):
75 self.parse_argv()
76 self.irc = irclib_scrappy.IRC()
77 try:
78 self.c = self.irc.server().connect(self.server,
79 self.port, self.nickname,
80 username=self.username,ircname=self.realname)
81 except irclib_scrappy.ServerConnectionError, x:
82 print x
83 sys.exit(1)
84 #if all goes well, register handlers
85 self.c.add_global_handler("welcome", self.on_connect)
86 self.c.add_global_handler("disconnect", self.on_disconnect)
87 self.c.add_global_handler("privmsg", self.on_privmsg)
88 self.c.add_global_handler("pubmsg", self.on_pubmsg)
90 #register some phony modules for testing
91 self.register_module("My phony module", 'foo', 'foo')
92 self.register_module("Another phony module", 'foo', 'foo')
93 self.pubmsg_events.append(externfunc)
94 self.pubmsg_events.append(deadline)
95 self.privmsg_events.append(deadline)
96 self.list_modules()
98 #nothng after this executes
99 self.irc.process_forever()
102 ################
103 #Event Handlers#
104 ################
105 def on_connect(self, c, event):
106 for ch in self.chanlist:
107 if irclib_scrappy.is_channel(ch) :
108 c.join(ch)
110 def on_pubmsg(self, c, event):
111 arg = event.arguments()[0]
112 iscmd = False
114 nick = irclib_scrappy.nm_to_n(event.source())
115 user = irclib_scrappy.nm_to_u(event.source())
116 host = irclib_scrappy.nm_to_h(event.source())
118 debug(nick)
120 if arg[0] == self.cmdchar:
121 cmd = arg[1:]
122 iscmd = True
123 else:
124 cmd = arg
126 #dispatch the command
127 for func in self.pubmsg_events:
128 func(c,[nick,user,host,iscmd,cmd])
130 def on_privmsg(self, c, event):
131 cmd = event.arguments()[0]
132 iscmd = False
134 nick = irclib_scrappy.nm_to_n(event.source())
135 user = irclib_scrappy.nm_to_u(event.source())
136 host = irclib_scrappy.nm_to_h(event.source())
138 if cmd[0] == self.cmdchar:
139 c.privmsg(nick,"You privmsged me, you don't need to " +
140 "prefix commands with %s" % self.cmdchar)
141 cmd = cmd[1:]
143 iscmd = True
145 #dispatch the command
146 for func in self.privmsg_events:
147 func(c,[nick,user,host,iscmd,cmd])
150 def on_disconnect(self, c, event):
151 for func in self.onquit_events:
152 func()
154 sys.exit(0)
156 def on_load(self):
157 for func in self.onload_events:
158 func()
160 ################
161 #Module Loading#
162 ################
163 def register_module(self, name, eventlist, function):
164 self.modulelist.append(name)
166 def list_modules(self):
167 """List currently loaded modules."""
168 for mod in self.modulelist:
169 print mod
172 if(__name__ == "__main__"):
173 bot = scrappy()