-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinprog.py
executable file
·332 lines (244 loc) · 11.9 KB
/
binprog.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
#!/usr/bin/env python3
# 28-Apr-2012
# reallyrose
# Python IRCEirtaBot
# Probably useless, poor thing.
# 03.03.2022: modified by polprog, converted to python3 and tested on espernet/libera
# Most functions operating on data want bytes object, some of them are not yet converted
# I just needed a base for a link title bot so I took this. Fixed up the network code a bit
# This may be easily extended as needed.
# until 07.03.2022: further modifications, refactor code
import time, socket, sys, string, re, random, string
import signal, traceback
from urllib.request import urlopen
from urllib.error import URLError
from collections import defaultdict
from random import choice
from datetime import datetime
import urltitle
import networking
import abspqmcdu
import radiation
# Errors taken up to E006
# E001 permission denied
# E003 urllib error: "+str(e.reason)+"\r\n")
# E002 not a supported uniform resource locator\r\n")
# E004 sorry, cannot find a title tag!\r\n")
# E005 [ general not found in any api response data, like abspqmcdu or radiation data ]
# E006 [ user parameter error ]
# Infos taken up to I006
# INFOS are messages not directly related to api responses
# I001 uptime is
# I002 disconnecting from remote. Bye bye!
# I003 go ping yourself
# I004 binprog version info
# I005 help text
# I006 kitteh!
# I007 command usage
version = '1.0.15 python3.5'
# TODO load all this from a file
TheChosen = eval(open ( 'TheChosen.txt' ).read ()) # People that can do special things
IgnoreUser = eval(open ( 'IgnoreUser.txt' ).read ()) # People to be ignore by the bot
botnick = 'binprog'
channel = '#polprog'
#channel = '#avrs'
password = open('password.txt').read().strip()
port = 6667
server = 'irc.esper.net'
#cmdprefix = '+++'
cmdprefix = '!'
starttime = datetime.now()
print("binprog version", version, "starting on", str(starttime))
print("Bot operators:" + str(TheChosen))
print("Ignores:" + str(IgnoreUser))
print("Bot nickname:", botnick, "command prefix is", cmdprefix)
if password == '':
print("Password is empty, I will not authenticate against NickServ")
# Connecting to a server:
print("Opening socket to IRC server", str(server)+":"+str(port)+"... ", end='')
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( server, port ) )
irc.settimeout(1.0)
print("Done.")
#print(irc.recv ( 4096 ))
# Set nick
running = True
def ircsend(data):
irc.send(data.encode('utf-8'))
print(">", data, end='')
# Many Functions, fun for all the family:
def Volunteer (): # For potential volunteers
ircsend (
'PRIVMSG ' + User +
' : Why not become one of the awesome Green Shirts for xxxxxx! http://xxxxxx.com/volunteer \r\n'
)
ircsend (
'PRIVMSG ' + User +
' : If you have questions, highlight rooose or reallyrose (Just say the name into chat) or if she isn\'t here, email [email protected] \r\n'
)
def Quit (): # only quit when I say so
QUser = data [1:data.find('!')] #Slice
print(QUser, "requested quit...")
if QUser in TheChosen:
ircsend ('PRIVMSG ' + channel +
' :I002 disconnecting from remote. Bye bye! \r\n')
ircsend('QUIT\r\n')
global running
running = False # quit program
else:
ircsend ('PRIVMSG ' + channel + ' :E001 permission denied\r\n') # if not...
def Source():
ircsend ('PRIVMSG ' + channel + ' :I004 https://github.com/polprog/binprog\r\n')
def GreetNew (): # Greet new users
global IgnoreUser
global User
if data.find ( 'JOIN ' ) != -1:
if User in IgnoreUser:
pass
else:
ircsend (
'PRIVMSG ' + channel +
' :Welcome to ' + channel + ', ' + User + '\r\n'
)
def Cat (data): # function to pull a random cat picture from a dictionary of cat pictures in a txt file
cats = ['=^.^=', '~=[,,_,,]:3', '^-.-^J', '>:3', '=^w^=', '__mnm^^']
random.seed(datetime.now())
PickCat = random.choice ( cats )
# post it
ircsend(
'PRIVMSG ' + channel +
' :I006 kitteh! ' + PickCat + '\r\n'
)
def Version(data):
ircsend (
'PRIVMSG ' + channel +
' :I004 binprog version '+version+', cmdprefix is '+cmdprefix+'\r\n')
def Help (data): # Halp!
user = data [1:data.find('!')] #Slice
ircsend ('PRIVMSG ' + user +
' :I005 Command prefix is '+cmdprefix+'; Available commands: ath - hangup (oper only), help - this, t <url]> - print url title, uptime - print uptime, version - bot version, ping - ping back, cat - send a kitteh, abspqmcdu - check polish PSTN number block owner, rad - check polish city radiation level \r\n')
def sigint_handler(signal, frame):
print('Interrupted. Gracefully closing IRC socket...')
ircsend("QUIT binprog closing (caught ^C)\r\n")
while True:
data = irc.recv(4096)
if(data == b''):
print("Server closed connection. Exiting!")
break
print(data)
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
# Core code
state = "connecting"
lasturl = ''
print("Taking nick", botnick)
ircsend('NICK ' + botnick + '\r\n')
ircsend('USER ' + botnick + ' ' + botnick + ' ' + botnick + ' :BeepBeep\r\n')
#TODO: rewrite to process packets line by line.
while running:
#print("Waiting for data...")
data = b''
while True:
try:
data = data + irc.recv(128)
except socket.timeout:
pass
break
data = data.decode('utf-8')
#print("Received", len(data), "bytes in this round...")
User = data [ 1:data.find ('!') ] #Slice user out
print (data, end="")
#GreetNew ()
# Don't join chan til server has finished spamming
if data.find ('End of /MOTD ') != -1 and state == "connecting":
state = "connected"
ircsend('JOIN ' + channel + '\r\n')
if data.find ('End of /NAMES list.' ) != -1: # don't reg til joined chan
if password != '':
print("Attepmting to identify to NickServ...")
ircsend('PRIVMSG NickServ :IDENTIFY '
+ ' ' + password + '\r\n')
Version(data) #send a version string as hello
if data.find ( 'PING :' ) != -1: # ping/pong server
pingloc = data.find( 'PING :' )
pingpayload = data[pingloc:].split()[1]
ircsend('PONG ' + pingpayload + '\r\n')
try:
posted_urls = re.findall("PRIVMSG.*(https?://[^ ]*)", data)
if posted_urls != []:
#print("user", User, "posted url", posted_urls)
lasturl = posted_urls[0]
try:
title = urltitle.UrlTitle(data)
#print("URL TITLE:", title)
ircsend('PRIVMSG ' + channel + " :Title: " + title + "\r\n")
continue
except:
pass
if data.find ( (cmdprefix + "ath") ) != -1:
Quit ()
elif data.find ( (channel + ' :' + cmdprefix + 'help') ) != -1:
Help (data)
elif data.find ( (channel + ' :' + cmdprefix + 'dns') ) != -1:
cmdloc = data.find ( (channel + ' :' + cmdprefix + 'dns') )
addrname = data[cmdloc:].split()[2]
print("addrname=", addrname)
ircsend('PRIVMSG ' + channel + " :" + networking.dns_lookup(addrname) + "\r\n")
elif data.find ( (channel + ' :' + cmdprefix + 'rdns') ) != -1:
cmdloc = data.find ( (channel + ' :' + cmdprefix + 'rdns') )
addrname = data[cmdloc:].split()[2]
print("addrname=", addrname)
ircsend('PRIVMSG ' + channel + " :" + networking.rdns_lookup(addrname) + "\r\n")
elif data.find ( (channel + ' :' + cmdprefix + 'abspqmcdu') ) != -1:
cmdloc = data.find ( (channel + ' :' + cmdprefix + 'abspqmcdu') )
addrname = data[cmdloc:].split()[2]
print("addrname=", addrname)
ircsend('PRIVMSG ' + channel + " :" + abspqmcdu.abspqmcdu(addrname) + "\r\n")
elif data.find ( (channel + ' :' + cmdprefix + 'rad') ) != -1:
cmdloc = data.find ( (channel + ' :' + cmdprefix + 'rad'))
paramstart = cmdloc + len(channel + ' :' + cmdprefix + 'rad')
eol = data.find('\r\n', cmdloc)
print("paramstart = ", paramstart, "eol = ", eol)
city = data[paramstart:eol].strip()
print("City = ", city)
if city == "":
ircsend("PRIVMSG " + channel + " :I007 Usage: !rad [nazwa miasta]\r\n")
else:
ircsend("PRIVMSG " + channel + " :" + radiation.checkRadiation(city) + "\r\n")
elif data.find (( channel + ' :' + cmdprefix + 't')) != -1:
try:
title = urltitle.UrlTitle(data)
except URLError as e:
ircsend('PRIVMSG ' + channel +
" :E003 urllib error: "+str(e.reason)+"\r\n")
continue
if(title == -1):
ircsend('PRIVMSG ' + channel +
" :E002 not a supported uniform resource locator\r\n")
continue
if(title == -2):
ircsend('PRIVMSG ' + channel +
" :E004 sorry, cannot find a title tag!\r\n")
if len(title) > 80:
title = title[:80] + " [trunc'd]"
print(title)
ircsend('PRIVMSG ' + channel + " :Title: " + title + "\r\n")
elif data.find ( (channel + ' :' + cmdprefix + 'uptime') ) != -1:
uptime = datetime.now() - starttime
ircsend('PRIVMSG ' + channel + " :I001 uptime is " + str(uptime) + "\r\n")
elif data.find ( (channel + ' :' + cmdprefix + 'version') ) != -1:
Version(data)
elif data.find ( (channel + ' :' + cmdprefix + 'ping') ) != -1:
ircsend('PRIVMSG ' + channel + " :I003 go ping yourself "+User+"\r\n")
elif data.find ( (channel + ' :' + cmdprefix + 'cat') ) != -1:
Cat(data)
elif data.find ( (channel + ' :' + cmdprefix + 'src') ) != -1:
Source()
#elif data.find ( (channel + ' :' + cmdprefix + 'xxx').encode('utf-8') ) != -1:
# raise ValueError()
except Exception as e:
print("Non critical exception:")
traceback.print_exc()
print("End of stack trace")
ircsend('PRIVMSG ' + channel + " :E999 command failed with uncaught exception\r\n")