-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
162 lines (123 loc) · 3.93 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
import socket
import sys
import time
import httplib
from re import escape
from datetime import date
class Bot(object):
host = ''
channel = ''
password = ''
nicks =''
irc = ''
debug = False
loging = False
def __init__(self,host,channel,password,nicks,host_name,debug,log):
self.host = host
self.channel = channel
self.password = password
self.nicks = nicks
self.host_name = host_name
self.debug = debug
self.log= log
def custom_ai(self,text,timestamp):
pass
#extend and put comst ai code here
def tpars(self,txt):
q=txt.split('<span class="temp">')[1]
temp=q.split(' C')[0]
qq=txt.split('<span>')[1]
wind=qq.split('</span>')[0]
return temp, wind
def parsemsg(self,s):
"""Breaks a message from an IRC server into its prefix, command, and arguments."""
try:
prefix = ''
trailing = []
if not s:
raise IRCBadMessage("Empty line.")
if s[0] == ':':
prefix, s = s[1:].split(' ', 1)
if s.find(' :') != -1:
s, trailing = s.split(' :', 1)
args = s.split()
args.append(trailing)
else:
args = s.split()
command = args.pop(0)
return {'channel':args[0],'handle':prefix.split('@')[0],'text':args[1]}
except:
return None
def log_connectivity(self,text):
pass
def archive_message(self,text,timestamp):
pass
def sendm(self,msg):
text = 'PRIVMSG '+ self.channel + ' :' + str(msg) + '\r\n'
if self.log:
timestamp = int(time.time())
self.archive_message(text,timestamp)
self.irc.send(text)
def bot_ai(self,text,timestamp):
if text.find(':KICK') != 1:
self.irc.send('JOIN '+ self.channel +'\r\n')
if text.find(':!date') != -1:
self.sendm('[+] Date: '+ time.strftime("%a, %b %d, %y", time.localtime()))
if text.find(':!time') != -1:
self.sendm('[+] Time: '+ time.strftime("%H:%M:%S", time.localtime()))
if text.find(':!say') != -1:
says = text.split(':!say')
sayse = says[1].strip()
self.sendm('.:: '+ str(sayse) +' ::.')
if text.find(':!voice') != -1:
voice = text.split(':!voice')
voices = voice[1].strip()
self.irc.send('MODE '+ str(channel) +' +v '+ str(voices) +'\r\n')
if text.find(':!devoice') != -1:
devoice = text.split(':!devoice')
devoices = devoice[1].strip()
self.irc.send('MODE '+ str(channel) +' -v '+ str(devoices) +'\r\n')
if text.find(':!image') != -1:
image = text.split(':!image')
images = image[1].strip()
if len(images) < 1:
self.sendm('[+] Error ! Wrote : !image world')
else:
self.sendm('[+] images url : http://images.google.com/images?&q='+ images +'&btnG')
if text.find(':!newyear') != -1:
now = date.today()
newyear = date(2009, 12, 31)
cik = now - newyear
newyears = cik.days
self.sendm('[+] .. ...... .... ........ :'+ str(newyears) +' .... =)')
def run(self):
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.irc.connect((self.host, 6667))
self.irc.send('USER '+self.nicks+' host '+self.host_name+' : Nemus Brand Bot\r\n')
self.irc.send('NICK '+ str(self.nicks) +'\r\n')
self.irc.send('NickServ IDENTIFY '+ str(self.nicks) + ' ' + str(self.password) +'\r\n')
while 1:
try:
text = self.irc.recv(2040)
timestamp = int(time.time())
if text.find('PRIVMSG') != -1 and self.log:
self.archive_message(text,timestamp)
self.log_connectivity(text)
if not text:
break
if self.debug:
print text
if text.find('Message of the Day') != -1:
self.irc.send('JOIN '+ self.channel +'\r\n')
if text.find('+iwR') != -1:
self.irc.send('NickServ IDENTIFY '+ str(self.nicks) + ' ' + str(self.password) +'\r\n')
if text.find('PING') != -1:
print 'PONG ' + text.split() [1] + '\r\n'
self.irc.send('PONG ' + text.split() [1] + '\r\n')
self.bot_ai(text,timestamp)
self.custom_ai(text,timestamp);
except Exception, err:
import traceback, os.path
traceback.print_exc(err)
print err
pass