forked from Taiiwo/IRCLinkBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
158 lines (144 loc) · 4.91 KB
/
util.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
#This file contains fuctions available for the plugin developers.
import socket
import sys
import re
import urllib2
import time
import os
import random
import json
import htmlentitydefs
import collections
from BeautifulSoup import BeautifulSoup
def modeCheck(mode, data):
args = argv('@', data['recv'])
for user in data['config']['settings']['userModes']:
if args['nick'] == user['nick'] and mode in user['modes'] and user['isAuth']:
if user['channel'] == args['channel'] or 'g' in user['modes']:
return True
return False
def saveConfigChanges(config):
try:
f = open('linkbot.conf','w')
f.write(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))
f.close()
except:
print "[E] Could not save to config file"
def say(channel, message):# A quick way to make the bot say something. Use return say(argv['channel'],message)
if len(message) > 430:
messages = [message[i:i+430] for i in range(0, len(message), 430)]
retme = []
for message in messages:
retme.append('PRIVMSG ' + channel + ' :' + message + '\r\n')
return '\n'.join(retme)
else:
return "PRIVMSG " + channel + " :" + message + "\r\n"
def html_strip(s):
return re.sub('<[^<]+?>', '', s)
def html_decode(text):
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
if text[1:-1] == "apos":
text = u"'"
else:
text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
text = makeByte(text)
fixup = makeByte(fixup)
return re.sub("&#?\w+;", fixup, text)
def makeByte(s):
if isinstance(s, unicode):
return s.encode('utf-8', 'ignore')
else:
return s
def textbetween(str1,str2,text):# returns the text between str1 and str2 in text. This is useful for parsing data.
posstr1 = text.find(str1)
posstr2 = text.find(str2)
between = text[posstr1 + len(str1):posstr2]
return between
def command(command,recv): #finds the argument for a command.
if command in recv and command != '':
num = recv.find(command)
message = recv[num + len(command) + 1:]
if message == '':
return None
else:
return message
else:
return None
def argv(user_com,recv):# returns a named, multidimensional array of on recv
# info [nick, user, channel, [argv[0], argv[1], etc..]] (Args are in
# a separate array)
m = re.match(
"^:([^!]*)!~?([^@]*)@([^\s]*)\s([^\s]*)\s([^\s:]*)\s?:?(.*)",
recv
)
if m is not None:
nick = m.group(1)
ident = m.group(2)
host = m.group(3)
com = m.group(4)
target = m.group(5)
message = m.group(6)
else:
return {'nick': 'Unknown', 'channel': 'Unknown'}
if com.lower() == "privmsg":
argc = command(user_com, message)
argv = [user_com,]
if argc is not None:
argv += argc.split()
else:
argv = []
return {'nick' : nick,'channel' : target, 'argv' : argv,
'ident': ident, 'hostname': host, 'message': message, "command": com}
def gettitle(url):#get the page title of an URL
try:
soup = BeautifulSoup(urllib2.urlopen(url).read(1024 * 1024))
return soup.title.string
except:
return False
def geturl(recv):#parse an URL from a string
url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', recv)
if url and url != '':
return url
else:
return False
def maketiny(url):# make a tinyurl from a string
try:
html = urllib2.urlopen("http://tinyurl.com/api-create.php?url=" + url)
tiny = str(html.read())
tiny = tiny.replace("http", "https")
return tiny
except:
return False
def dictUpdate(d, u):
for k, v in u.iteritems():
if isinstance(v, collections.Mapping):
r = dictUpdate(d.get(k, {}), v)
d[k] = r
else:
d[k] = u[k]
return d
def dictUnicodeToByte(input):# recurively change unicode values of a dict to byte
if isinstance(input, dict):
return {dictUnicodeToByte(key):dictUnicodeToByte(value) for key,value in input.iteritems()}
elif isinstance(input, list):
return [dictUnicodeToByte(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input