-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircbot.py
257 lines (201 loc) · 7.08 KB
/
ircbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/python
"""
ircbot.py - An IRC Bot Basis in Python
License:
W3C Open Source License; share and enjoy!
http://www.w3.org/Consortium/Legal/copyright-software-19980720
Original:
http://dev.w3.org/cvsweb/2000/scribe-bot/ircAsync.py
Dan Connolly and Tim Berners-Lee
Copyright (c) 2001 W3C (MIT, INRIA, Keio)
Augmentations' Author:
Sean B. Palmer, inamidst.com
"""
import sys, os, re, socket, asyncore, asynchat
protect = True
class Origin(object):
def __init__(self, origin):
self.origin = origin
self.split(origin)
def __str__(self):
return self.origin
def split(self, origin):
if origin and '!' in origin:
self.nick, userHost = origin.split('!', 1)
if '@' in userHost:
self.user, self.host = userHost.split('@', 1)
else: self.user, self.host = userHost, None
else: self.nick, self.user, self.host = origin, None, None
def replyTo(self, nickname, args):
if (not args) or (len(args) < 2): return
target = args[1]
if target == nickname:
self.sender = self.nick
else: self.sender = target
def ctcp(s): return '\x01%s\x01' % s
def me(s): return ctcp('ACTION %s' % s)
class Bot(asynchat.async_chat):
def __init__(self, nick=None, userid=None, name=None, channels=None):
asynchat.async_chat.__init__(self)
self.bufIn = ''
self.set_terminator('\r\n')
self.nick = nick or 'ircbot'
self.userid = userid or 'nobody'
self.name = name or 'ircbot.py user'
self.documentation = {}
self.info = {}
self.dispatch = []
self.msgstack = []
self.channels = channels or ['#test']
self.rule(self.welcome, 'welcome', cmd='001')
self.rule(self.help, 'help', '%s[:,] help (\w+)\??' % self.nick)
def run(self, host, port):
port = port or 6667
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
debug("connecting to...", host, "/", port)
self.connect((host, port))
self.bufIn = ''
asyncore.loop()
def welcome(self, m, origin, args, text):
for chan in self.channels:
self.todo(['JOIN', chan])
def help(self, m, origin, args, text):
command = m.group(1)
if self.documentation.has_key(command):
doc = self.documentation[command]
self.notice(origin.sender, '%s: %s' % (command, doc))
else: self.notice(origin.sender, 'No help available for %s.' % command)
def todo(self, args, *text):
command = ' '.join(args)
if text: command = command + ' :' + ' '.join(text)
self.push(command + '\r\n')
debug("sent/pushed command:", command)
def handle_connect(self):
debug("connected")
self.todo(['NICK', self.nick])
self.todo(['USER', self.userid, "+iw", self.nick], self.name)
# TODO, XXX: Fiks hvis noen gidder
def handle_close(self):
sys.exit(1)
def collect_incoming_data(self, bytes):
self.bufIn = self.bufIn + bytes
def found_terminator(self):
line = self.bufIn
self.bufIn = ''
if line.startswith(':'):
origin, line = line[1:].split(' ', 1)
origin = Origin(origin)
else: origin = None
try: args, text = line.split(' :', 1)
except ValueError:
args, text = line, ''
args = args.split()
if origin: origin.replyTo(self.nick, args)
debug("from::", origin, "|message::", args, "|text::", text)
self.rxdMsg(args, text, origin)
def rule(self, func, name, regexp=None, doc=None, cmd=None):
if isinstance(regexp, basestring):
regexp = re.compile(regexp)
if self.documentation.has_key(name):
raise "DispatchError", "Function %s already added" % name
doc = doc or func.__doc__
if doc and doc.strip():
self.documentation[name] = doc
self.dispatch.append((cmd or 'PRIVMSG', regexp, func))
def bind(self, func, cmd, regexp):
self.dispatch.append((cmd, re.compile(regexp), func))
def rxdMsg(self, args, text, origin):
if args[0] == 'PING':
self.todo(['PONG', text])
for cmd, pat, thunk in self.dispatch:
if args[0] == cmd:
if pat:
m = pat.search(text)
if not m: continue
else: m = None
if protect:
try: thunk(m, origin, args, text)
except Exception, e:
try: self.notice(origin.sender, "%s: %s" % (e.__class__, e))
except: print >> sys.stderr, "%s: %s" % (e.__class__, e)
else: thunk(m, origin, args, text)
def pushMsg(self, msg):
self.msgstack = self.msgstack[-9:]
self.msgstack.append(msg)
def floodProtect(self, text):
if self.msgstack.count(text) >= 5:
text = '...'
if self.msgstack.count('...') >= 2:
if text == '...': return
if len(''.join(self.msgstack[:-3])) > 200:
import time
time.sleep(2)
self.pushMsg(text)
def msg(self, dest, text):
self.floodProtect(text)
self.todo(['PRIVMSG', dest], text)
def safeMsg(self, channel, lines):
import time
for line in lines:
if line: self.msg(channel, line)
time.sleep(1)
if len(line) > 50: time.sleep(0.7)
def notice(self, dest, text):
self.floodProtect(text)
self.todo(['NOTICE', dest], text)
# Add safeNotice as well? safeMsg isn't actually used anywhere yet (mariube)
debugflag = False
def setDebug(val):
global debugflag
debugflag = val
def debug(*args):
if debugflag:
sys.stderr.write("DEBUG: ")
for a in args:
sys.stderr.write(str(a))
sys.stderr.write("\n")
def doubleFork():
# http://swhack.com/logs/2004-05-12#T10-20-11
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
try:
pid = os.fork()
if pid > 0: sys.exit(0)
except OSError, e:
print >> sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
os.chdir("/")
os.setsid()
os.umask(0)
try:
pid = os.fork()
if pid > 0:
debug("Daemon PID %d" % pid)
sys.exit(0)
except OSError, e:
print >> sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
sys.stdin.close()
sys.stdout.close()
sys.stderr.close()
os.close(0)
os.close(1)
os.close(2)
sys.stdin = file("/dev/null","r")
sys.stdout = file("/dev/null", "w")
sys.stderr = file("/dev/null","w")
def test(host, port, channels):
bot = Bot(nick='testIRCbot', channels=channels)
def hi(m, origin, args, text, bot=bot):
bot.msg(origin.sender, "hi %s" % origin.nick)
bot.rule(hi, 'hi', r'hi %s' % bot.nick)
def hmm(m, origin, (cmd, chan), text, bot=bot):
""".test - Do some crazy test thing."""
raise Exception, "blargh"
bot.msg(origin.sender, 'Test')
bot.rule(hmm, 'test', r'^\.test$')
def bye(m, origin, args, text, bot=bot):
bot.todo(['QUIT'], "bye bye!")
bot.rule(bye, 'bye', r'bye bye bot')
bot.run(host, port)
if __name__=='__main__':
test('irc.freenode.net', 6667, ['#d8uv.com'])