-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
151 lines (146 loc) · 5.14 KB
/
parser.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
#Twatbot Plugins
from plugins import *
from heapq import merge
import traceback
import sys, os
from threading import Thread
import MySQLdb
import MySQLdb.cursors
from urllib2 import URLError
def parse(conn):
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
if conn.dataN['msg'] == "\001VERSION\001":
conn.sendNotice('VERSION Twatbot, the tweeting bot 1.2',conn.dataN['fool'])
if conn.dataN['msg'] == "\001PING\001":
conn.sendNotice('PONG',conn.dataN['fool'])
if conn.dataN['fool'] in conn.tells:
conn.sendNot(conn.dataN['fool']+": You have unread messages! Type ^read to read them")
conn.tells.remove(conn.dataN['fool'])
if conn.dataN['cmd'] == 'KICK' and conn.nick in conn.dataN['raw']:
try:
del (conn.chans[conn.dataN['chan']])
except Exception,e:
print "Failed to remove; %s" % (str(e))
# if conn.dataN['cmd'] == 'JOIN' and len(conn.dataN['fool']) > 15:
# conn.sendMsg(".k "+conn.dataN['fool']+' We have a strict no long nick (15 chars) policy here')
if conn.dataN['cmd'] == 'PRIVMSG' and len(conn.dataN['words']) != 0:
if conn.dataN['words'][0] == '^cmds':
trigs = []
for i in list(merge(pluginList, adminPlugins)):
trigs.append(i.triggers.keys())
conn.sendNot(str(trigs))
return
# Run the function for the given command
if conn.dataN['fool'] in conn.admins:
if conn.dataN['words'][0] == '^reload':
try:
g = dict(globals())
for i in g:
if conn.dataN['words'][1] == i:
reload(globals()[conn.dataN['words'][1]])
conn.sendMsg("Module reloaded")
except Exception, e:
conn.sendMsg(str(e))
check(list(merge(pluginList, adminPlugins)),conn)
elif conn.dataN['fool'] not in conn.banned:
check(pluginList,conn)
#everything else is passed to the default plugin
p = Thread(target=PluginRunner,args=(conn,default),name="Default plugin runner")
p.start()
class ircState:
def __init__(self,conn):
self.conn = conn
self.banned = conn.banned
self.api = conn.api
self.tells = conn.tells
self.ignores = conn.ignores
self.uptime = conn.uptime
self.chans = conn.chans
self.dataN = dict(conn.dataN)
self.server = conn.server
def sendNotice(self,msg,fool):
self.conn.sendNotice(msg,fool)
def sendMsg(self,msg,chan = None):
if chan == None:
chan = self.dataN['chan']
self.conn.sendMsg(msg,chan)
def sendNot(self,msg):
self.conn.sendNot(msg)
def decon(self):
self.conn.decon()
def joinChan(self,chan):
self.conn.joinChan(chan)
def close(self):
self.conn.close()
def setName(self,field):
return names.setName(self.conn,field)
def getName(self,field):
return names.getName(self.conn,field)
def PluginRunner(con,plugin):
if con.dataN:
conn = ircState(con)
else:
print "IRC data invalid. Plugin not run"
return
plugin = plugin
try:
# print (conn.dataN['fool']+' '+conn.server+'/'+conn.dataN['chan']+': '+conn.dataN['msg'])
if plugin == default:
plugin.default(conn)
elif conn.dataN['fool'] in (conn.ignores + conn.banned):
return
else:
plugin.triggers[conn.dataN['words'][0]](conn)
except URLError, err :
conn.sendMsg("Plugin failed"+ plugin.__name__ + ': '+str(err))
except Exception, err:
print >> sys.stderr, str(err)
# exc_type, exc_obj, exc_tb = sys.exc_info()
# fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
# conn.sendMsg("Plugin failed: " + plugin.__name__ + ': '+type(err).__name__+" "+(' '.join([str(fname), str(exc_tb.tb_lineno)]))+": "+ str(err) ,conn.dataN['chan'])
fln = traceback.format_exc().splitlines()
# print fln
debugMsg = "Plugin failed: " + plugin.__name__ + ': '+type(err).__name__+" "+str(err)+(' '.join(fln[3:5]))
if conn.conn.debug:
conn.sendMsg(debugMsg)
else:
conn.sendMsg(debugMsg,conn.conn.admins[0])
def check(pl,conn):
for plugin in pl:
if conn.dataN['fool'] not in (conn.ignores+conn.banned) and conn.dataN['words'][0] in plugin.triggers:
if conn.dataN['words'][0] == '^help':
try:
conn.sendNot(plugin.help)
return
except:
conn.sendNot("No help available")
return
else:
p = Thread(target=PluginRunner,args=(conn,plugin),name="Plugin runner:"+str(plugin))
p.start()
return
pluginList = [
web,
stat,
mueval,
amigo,
ban,
chans,
dragon,
help,
tell,
scroll,
tweet,
checkem,
# markov,
lastfm,
# laughter,
fullwidth,
counterstrike,
steam
]
adminPlugins = [
joinpart,
ban,
quit
]