-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.py
507 lines (449 loc) · 14.6 KB
/
Bot.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# -*- coding: utf-8 -*-
#
# A simple irc bot in python created from some example found online heavily modified
# Copyright (C) 2009 Matthew Adams and some guy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import socket
import string
import os #not necassary but later on I am going to use a few features from this
import pickle
import random
import time
import urllib
import urllib2
import re
def sorter(a, b):
return cmp(len(b[0]), len(a[0]))
class Bot:
def __init__(self):
self.HOST='irc.ubuntu.com' #The server we want to connect to
self.PORT=8001 #The connection port which is usually 6667
self.NICK='Bot-Nick' #The bot's nickname
self.PASS='Bot-Pass' # If the nick is registered with nickserv
self.IDENT='pybot'
self.REALNAME='s1ash'
self.OWNER='OxDeadC0de' #The bot owner's nick
self.channel='OxDeadC0de' #The default self.channel for the bot to speak on.. changes every time it's spoken to.
self.readbuffer='' #Here we store all the messages from server
self.jobs = []
self.ops = [] # List of room ops, detect +o and -o
self.lastJoin = ''
self.banned = []
self.learned = [] # Learned objects, addarray([key, value])
self.loadBanned()
self.loadLearned()
def getSubmitter(self, text):
return text[text.find(":")+1:text.find("!n")]
def getRest(self, text, marker):
return (text[text.find(marker)+len(marker):]).strip()
def do_learned(self, text):
self.learned.sort(sorter)
self.learnCommand(text)
self.unlearn(text)
self.alllearned(text)
for l in self.learned:
if( text.find(l[0]) != -1):
rest = self.getRest(text, l[0])
rest.strip()
learned = l[1]
if learned.find("/me") == 0 or learned.find("ACTION") == 0:
self.sendm(l[1] + " " + rest)
else:
if(rest is not ""):
rest = rest + ": "
self.sendm(rest + l[1])
return 1
return 0
def alllearned(self, text):
if((text.lower()).find(":!commands") != -1 or (text.lower()).find(":!allcommands") != -1):
self.channel = self.getSubmitter(text)
out = ""
if( not len(self.learned) ):
out = "There are no commands available!"
xc = 0
for l in self.learned:
out = out + (l[0])[1:] + " "
xc = xc + 1
if(xc is 10):
xc = 0
self.sendm(out)
out = ""
self.sendm(out)
def unlearn(self, text):
if(text.find(":!unlearn ") != -1):
if(self.isOp(self.getSubmitter(text))):
rest = ":!"+self.getRest(text, ":!unlearn")
if(not self.is_learned(rest)):
self.sendm(rest + " is not a known command!")
return
de = None
for l in self.learned:
if(l[0] == rest):
de = l
if(de is not None):
self.learned.remove(de)
self.sendm(rest + " was removed")
self.writeLearned()
def is_learned(self, command):
for l in self.learned:
if(l[0] == command): return 1
return 0
def real_learn_command(self, text):
rest = self.getRest(text, ":!learn");
first_space = rest.find(" ")
if(first_space == -1):
self.sendm("Usage: !learn command response")
return
command = ":!" + rest[:first_space]
rest = rest[first_space+1:]
self.learned.append([command, rest])
self.writeLearned()
self.sendm("Yummy Yummy in my Tummy!")
def learnCommand(self, text):
if(text.find(":!learn ") != -1):
submitter = self.getSubmitter(text)
if(self.isOp(submitter)):
self.real_learn_command(text)
def isOp(self, name):
#print "is Op " + name + " "+self.channel
if(name == self.OWNER):
return 1
for op in self.ops:
if(op[0] == name and self.channel == op[1]):
return 1
return 0
def isBanned(self, name):
for b in self.banned:
if(b.lower() == name.lower()):
if(self.isOp(name)):
#print "Warning, " + name + " is banned, but is an op, continueing anyway!"
return 0
return 1
return 0
def ban_user(self, name):
if(not self.isOp(name)):
self.banned.append(name)
#print "Banned " + name
self.writeBanned()
def unban_user(self, name):
banned = None
for b in self.banned:
if(b == name):
banned = b
if(banned is not None):
self.banned.remove(banned)
#print "Unbanned " + name
self.writeBanned()
def joinChannel(self, channel):
# Get list of ops:
output = "names " + channel
self.sock.send(output)
self.lastJoin = channel
def loadJobs(self):
try:
jobfile = open('jobs.pkl', 'r')
self.jobs = pickle.load(jobfile)
jobfile.close()
except IOError:
print "No job file, continue"
def writeJobs(self, ojobs):
jobfile = open('jobs.pkl', 'w')
pickle.dump(ojobs, jobfile)
jobfile.close()
def loadBanned(self):
try:
jobfile = open('banned.pkl', 'r')
self.banned = pickle.load(jobfile)
jobfile.close()
except IOError:
print "No ban file, continue"
def writeBanned(self):
jobfile = open('banned.pkl', 'w')
pickle.dump(self.banned, jobfile)
jobfile.close()
def loadLearned(self):
try:
j = open("learned.pkl", 'r')
self.learned = pickle.load(j)
j.close()
except IOError:
print "No learned file! I don't know anything"
def writeLearned(self):
j = open("learned.pkl", 'w')
pickle.dump(self.learned, j)
j.close()
def sendm(self, msg):
if msg.find("ACTION") == 0:
# msg = msg[1:]
msg = '\001'+msg+'\001'#msg.replace("\\001", "\u0001")
if msg.find("/me ") == 0:
msg = msg[3:]
msg = '\001ACTION'+msg+'\001'#msg.replace("\\001", "\u0001")
self.sock.send('PRIVMSG '+ self.channel + ' :' + str(msg) + '\r\n')
print "Sending: " + 'PRIVMSG '+ self.channel + ' :' + str(msg)
def removeone(self, one):
self.jobs.remove(one)
length = len(self.jobs)
for x in range(length):
self.jobs[x][0] = "%s" % x
def extractChan(self, string):
privmsg = string.find("PRIVMSG ")
chan = string[privmsg+8:string.find(" :", privmsg)]
if self.NICK == chan:
chan = string[string.find(":")+1:string.find("!n")]
return chan
#/** @brief bield room op list from user nick's.op's are designated by @
# *
# * @name nicklist
# *
# */
def nicklist(self, text):
if(text.find(self.NICK + " = " + self.lastJoin + " : ") != -1):
#print "Name list found"
rest = text[text.find(self.NICK + " = " + self.lastJoin + " :"):]
nickList = rest.split(" ")
for nick in nickList:
#print "Nick : " + nick
if(nick.find("@") == 0):
#print "Op found " + nick + " " + self.lastJoin
nick = nick[1:]
self.ops.append([nick, self.lastJoin])
def addOp(self, text):
if re.match(':ChanServ!ChanServ@services\. MODE (.*) \+o', text):
print "adding op!"
name = text[text.find("+o")+3:]
name = name.strip();
if(not self.isOp(name)):
self.ops.append([name, self.channel])
#print "Adding op " + name + " " + self.channel
def remOp(self, text):
if re.match(':ChanServ!ChanServ@services\. MODE (.*) \-o', text):#text.find(':ChanServ!ChanServ@services. MODE #php-freelance -o') == 0 or text.find(':ChanServ!ChanServ@services. MODE #php-freelance -o')== 1:
remOp = None
name = text[text.find("-o")+3:]
name = name.strip();
#print "Trying to remove op: " + name + " " + self.channel
for op in self.ops:
if op[0] == name and op[1] == self.channel:
remOp = op
if remOp is not None:
self.ops.remove(remOp)
#print "Removing op " + name
def google(self, text):
if(text.find("google ") != -1):
rest = text[text.find("google ")+7:]
rest = rest.strip()
temp = { 'q':rest }
if(len(rest) > 0):
self.sendm("http://www.google.com/search?" + urllib.urlencode(temp))
def amiop(self, text):
if(text.find(":!amiop") != -1):
submitter = text[text.find(":")+1:text.find("!n")]
if self.isOp(submitter):
self.sendm("Yes, "+ submitter +" you are an op!")
else:
self.sendm("No, " + submitter +" you are not an op!")
def join(self, text):
if(text.find(":!join ") != -1):
rest = text[text.find(":!join ")+6:]
rest = rest.strip()
submitter = text[text.find(":")+1:text.find("!n")]
if self.isOp(submitter):
#print 'Joining channel: %s' % rest
self.sock.send('JOIN %s\r\n' % rest)
self.joinChannel(rest)
def nickChange(self, text):
if(text.find(":!nick ") != -1):
rest = text[text.find(":!nick ")+6:]
rest = rest.strip()
submitter = self.getSubmitter(text)
if(submitter == self.OWNER):
self.sock.send('NICK %s\r\n' % rest)
def ping(self, text):
if text.find('PING') == 0:
self.sock.send('PONG ' + text.split() [1] + '\r\n')
def quit(self, text):
if text.find(':!quit') != -1:
submitter = text[text.find(":")+1:text.find("!n")]
if(self.isOp(submitter)):
self.sock.send('QUIT :python bot\r\n')
def bot(self, text):
if text.find(':bot') != -1 or text.find("!bot") != -1:
self.sendm('I are no bot . no , you are bot')
def job(self, text):
val = text.find(":job ")
if (val != -1):
ttext = text[val+5:]
ttext = ttext.strip()
leng = len(ttext)
if(leng > 140): leng = 140
ttext = ttext[0:leng]
submitter = text[text.find(":")+1:text.find("!n")]
self.jobs.append(["%s" % len(self.jobs), ttext, submitter])
self.writeJobs(self.jobs)
self.sendm("Job %s added!" % self.jobs[len(self.jobs)-1][0])
def login(self, text):
if text.find('NOTICE %s :This nickname is registered' % self.NICK) != -1:
self.sock.send('PRIVMSG nickserv :identify %s\r\n' % self.PASS )
def date(self, text):
if text.find(':!date') != -1:
self.sendm(''+ time.strftime("%A, %B %d, %Y", time.localtime()))
def usage(self, text):
if text.find(':!usage') != -1 or text.find(":!help") != -1:
self.sendm('I am a very simple bot with simple usage. !job # to display job #, if # is omitted I will display a random job; Add a new one with: ":job your very important job" without quotes; !jobs to see the total amount of jobs; Remove your jobs with !rjob # and view your jobs with !myjobs. !job all for a full job listing! Ops can have me !ignore user and !unignore user. Type !ignored for all ignored users.')
def time(self, text):
if text.find(':!time') != -1:
self.sendm(''+ time.strftime("%H:%M:%S", time.localtime()))
def load(self, text):
if text.find(':!load') != -1:
self.loadJobs()
self.sendm('job file loaded!')
def removeJob(self, text):
val = text.find(':!rjob ')
if val == -1:
val = text.find('::rjob ')
if val != -1:
submitter = text[text.find(":")+1:text.find("!n")]
rnum = text[val+6:]
rnum = rnum.strip()
rjob = None
if(rnum >= 0):
for job in self.jobs:
if(job[0] == rnum):
rjob = job
if rjob is not None:
if(submitter == rjob[2] or self.isOp(submitter)):
self.sendm( "Job %s removed" % (rjob[0]))
self.removeone(rjob)
self.writeJobs(self.jobs)
def listJobs(self, text):
#print "list"
if text.find(':!list') != -1 or text.find(':!jobs') != -1:
#print "Found list again"
self.sendm("Total Jobs: %s " % len(self.jobs))
def myjobs(self, text):
if text.find(':!myjobs') != -1:
submitter = text[text.find(":")+1:text.find("!n")]
self.channel = submitter
myjobs = ""
for job in self.jobs:
if(job[2] == submitter):
myjobs = job[0] + " " + myjobs
if(myjobs == ""):
self.sendm("You haven't submitted any jobs!")
else:
self.sendm("Your jobs are: %s" % myjobs)
def ignored(self, text):
if text.find(':!ignored') != -1:
#submitter = text[text.find(":")+1:text.find("!n")]
# self.channel = submitter
ignoredusers = ""
for ignored in self.banned:
ignoredusers = ignoredusers + " " + ignored
self.sendm("Ignored users: " + ignoredusers)
def ignore(self, text):
if text.find(':!ignore ') != -1 and text.find(":!ignored") == -1:
submitter = text[text.find(":")+1:text.find("!n")]
rest = text[text.find('!ignore')+8:]
rest = rest.strip()
if(self.isOp(submitter)):
self.ban_user(rest)
def unignore(self, text):
if text.find(':!unignore ') != -1:
submitter = text[text.find(":")+1:text.find("!n")]
rest = text[text.find('!unignore')+10:]
rest = rest.strip()
if(self.isOp(submitter)):
self.unban_user(rest)
def mainJob(self, text):
if text.find(':!job\r\n') != -1 or text.find(":!job ") != -1:
submitter = text[text.find(":")+1:text.find("!n")]
rest = text[text.find('!job')+5:]
rest = rest.strip()
if(rest.find('all ') != -1):
# if(text.find('#php-freelance') == -1):
chantemp = self.channel
self.channel = submitter;
top = len(self.jobs)
count = 0
while(count != top):
self.sendm('Job: %s Submitted By: %s Description: %s' % (self.jobs[count][0], self.jobs[count][2], self.jobs[count][1]))
count = count +1
self.channel = chantemp
rnum = 0
doit = 1
try:
i = float(rest)
# numeric
rnum = int(rest)
if rnum >= len(self.jobs) or rnum < 0:
self.sendm('Error, invalid job number %s' % rnum)
doit = 0
except ValueError:
range = len(self.jobs)
if range > 0:
rnum = random.randrange(0,len(self.jobs))
else:
rnum = 0
doit = 0
# not numeric
if (len(self.jobs) == 0) or (len(self.jobs) == -1):
self.sendm('There are no jobs, sorry!\r\n')
else:
if doit == 1:
self.sendm('Job: %s Submitted By: %s Description: %s' % (self.jobs[rnum][0], self.jobs[rnum][2], self.jobs[rnum][1]))
def runQuery(self, text):
print '[GET]', text
self.nicklist( text )
self.addOp( text )
self.remOp( text )
self.google( text )
self.amiop( text )
self.join( text )
self.ping( text )
self.quit( text )
self.bot( text )
self.job( text )
self.login( text )
self.date( text )
self.usage( text )
self.time( text )
self.load( text )
self.removeJob( text )
self.listJobs( text )
self.myjobs( text )
self.ignored( text )
self.ignore( text )
self.unignore( text )
self.mainJob( text )
self.nickChange( text )
def main(self ):
self.loadJobs()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.HOST, self.PORT))
self.sock.send('USER python host servname : Python Bot\r\n')
self.sock.send('NICK %s\r\n' % self.NICK)
while 1:
text=self.sock.recv(2040)
if not text:
break
if(text.find("PRIVMSG ") != -1):
self.channel = self.extractChan(text)
submitter = text[text.find(":")+1:text.find("!n")]
if(self.isBanned(submitter)): continue
self.do_learned(text)
self.runQuery(text)
mybot = Bot()
mybot.main()