Repository navigation
Expand file tree
/
Copy pathLinkBot.py
More file actions
195 lines (166 loc) · 6.33 KB
/
Copy pathLinkBot.py
File metadata and controls
195 lines (166 loc) · 6.33 KB
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
__author__ = 'William'
import socket
import sys
import threading
import time
from urllib.request import urlopen, Request
import re
import urllib.error
try:
s = socket.socket()
except socket.error:
print('Failed to create socket')
sys.exit()
print('Socket Created')
commandchar = '.'
def flush_socket():
f = s.makefile()
f.flush()
class Globals:
linkson = True
boton = True
def send(string):
print('sending ' + string)
s.sendall(str.encode('%s \r\n' % string))
flush_socket()
def connect():
host = 'irc.rizon.net'
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
print('Hostname could not be resolved. Exiting')
sys.exit()
s.connect((remote_ip, 6667))
class ServerListener(threading.Thread):
def run(self):
while 1:
reply = s.recv(4096)
if reply == "":
sys.exit(0)
parse(reply.decode('ascii', 'ignore'))
def pm(room, message):
tosend = 'PRIVMSG ' + room + ' :' + message
send(tosend)
def nick(nickname):
send('NICK ' + nickname)
def user(username):
send('USER ' + username + ' 0 unused :Link Bot')
def login(password):
send('PRIVMSG Nickserv :IDENTIFY ' + password)
def join(room):
send('JOIN #' + room)
def parse(string):
print(string)
split = string.split()
if split[0].startswith(':') is False:
command = split[0]
message = split[1][1:]
param = ''
sender = ''
else:
senderaddress = split[0][1:]
command = split[1]
sendersplit = senderaddress.split('!')
sender = sendersplit[0]
if split[2].startswith(':') is False:
param = split[2]
message = split[3][1:]
for i in range(4, len(split)):
message += ' ' + split[i]
else:
message = split[2][1:]
param = ''
botcom = message.startswith('.')
if param == 'LinkBot':
param = sender
if command == 'PING':
print('sending pong')
s.send(str.encode("PONG :" + message))
if command == 'PRIVMSG':
if len(message.split()) < 1:
return
com = message.split()[0][1:]
if botcom is True:
print('is botcom')
if com == 'bots':
print('is .bots')
pm(param, 'Reporting in! [Python]')
elif sender == 'wiiaamm':
if com == 'linkbot':
if len(message.split()) < 2:
pm(param, 'Commands are: linkson, linksoff, boton, botoff')
return
if message.split()[1] == "linksoff":
Globals.linkson = False
pm(param, 'Links are now off')
if message.split()[1] == "linkson":
Globals.linkson = True
pm(param, 'Links are now on')
if message.split()[1] == "boton":
Globals.boton = True
pm(param, 'Bot is now on')
if message.split()[1] == "botoff":
Globals.boton = False
pm(param, 'Bot is now off')
if Globals.boton is True:
if com == 'g':
print('scanning url')
search = message[3:]
search = re.sub('\s+', '%20', search)
print(search)
urltoopen = 'http://www.google.com/search?btnI=I\'m+Feeling+Lucky&q=' + search
print(urltoopen)
req = Request(urltoopen, None, {'User-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de;'
' rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'})
res = urlopen(req)
finalurl = res.geturl()
pm(param, finalurl)
if com == 'lmgtfy':
search = message[8:]
search = re.sub('\s+', '+', search)
urltoopen = 'https://www.google.com/webhp?hl=en#safe=off&hl=en&q=' + search
pm(param, urltoopen)
pm(param, 'Was that so hard?')
elif Globals.linkson is True:
messagesplit = message.split()
print(messagesplit)
for i in range(0, len(messagesplit)):
if len(messagesplit[i]) > 10:
print('len longer than 10')
if messagesplit[i].startswith('http://') is True or messagesplit[i].startswith('https://') is True:
print('found url')
try:
urltoopen = messagesplit[i]
req = Request(urltoopen, None, {
'User-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; '
'de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'})
res = urlopen(req)
html = res.read().decode('utf-8', 'ignore')
html = html.split('<title')[1]
html = html.split('>')[1]
html = html.split('</title')[0]
html.replace(''','\'')
htmlsplit = html.split()
if len(htmlsplit) < 1:
return
html = '\x03' + htmlsplit[0]
for j in range(1,len(htmlsplit)):
html += ' ' + htmlsplit[j]
if j is 10:
html += ' ...'
break
pm(param,'[\x02URL\x02] ' + html)
except urllib.error.HTTPError:
pm(param, 'Four hundred and four error')
except IndexError:
pm(param, 'I currently don\'t support your shitty url')
except urllib.error.URLError:
pm(param, 'Thats not even a url you autist')
# pw = input('Password to login: \n')
# print(pw)
connect()
ServerListener().start()
nick('LinkBot')
user('LinkBot')
time.sleep(5)
join('pasta')