]>
jfr.im git - erebus.git/blob - erebus.py
3 # Erebus IRC bot - Author: John Runyon
8 import os
, sys
, select
, MySQLdb
, MySQLdb
.cursors
, time
, random
9 import bot
, config
, ctlmod
24 def __init__(self
, nick
, auth
=None):
32 return self
.auth
is not None
34 def authed(self
, auth
):
35 if auth
== '0': self
.auth
= None
36 else: self
.auth
= auth
.lower()
44 if c
.execute("SELECT level FROM users WHERE auth = %s", (self
.auth
,)):
47 self
.glevel
= row
['level']
55 self
.chans
.append(chan
)
57 self
.chans
.remove(chan
)
59 for chan
in self
.chans
:
60 self
.chans
.remove(chan
)
62 def __str__(self
): return self
.nick
63 def __repr__(self
): return "<User %r (%d)>" % (self
.nick
,self
.glevel
)
65 class Channel(object):
66 def __init__(self
, name
, bot
):
76 if c
.execute("SELECT user, level FROM chusers WHERE chan = %s", (self
.name
,)):
78 while row
is not None:
79 self
.levels
[row
['user']] = row
['level']
83 def levelof(self
, auth
):
87 if auth
in self
.levels
:
88 return self
.levels
[auth
]
92 def setlevel(self
, auth
, level
, savetodb
=True):
96 if c
.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self
.name
, auth
, level
)):
97 self
.levels
[auth
] = level
102 def userjoin(self
, user
, level
=None):
103 if user
not in self
.users
: self
.users
.append(user
)
104 if level
== 'op' and user
not in self
.ops
: self
.ops
.append(user
)
105 if level
== 'voice' and user
not in self
.voices
: self
.voices
.append(user
)
106 def userpart(self
, user
):
107 if user
in self
.ops
: self
.ops
.remove(user
)
108 if user
in self
.voices
: self
.voices
.remove(user
)
109 if user
in self
.users
: self
.users
.remove(user
)
111 def userop(self
, user
):
112 if user
in self
.users
and user
not in self
.ops
: self
.ops
.append(user
)
113 def uservoice(self
, user
):
114 if user
in self
.users
and user
not in self
.voices
: self
.voices
.append(user
)
115 def userdeop(self
, user
):
116 if user
in self
.ops
: self
.ops
.remove(user
)
117 def userdevoice(self
, user
):
118 if user
in self
.voices
: self
.voices
.remove(user
)
120 def __str__(self
): return self
.name
121 def __repr__(self
): return "<Channel %r>" % (self
.name
)
123 def __init__(self
, trigger
):
124 self
.trigger
= trigger
125 if os
.name
== "posix":
127 self
.po
= select
.poll()
128 else: # f.e. os.name == "nt" (Windows)
129 self
.potype
= "select"
132 def newbot(self
, nick
, user
, bind
, server
, port
, realname
):
133 if bind
is None: bind
= ''
134 obj
= bot
.Bot(self
, nick
, user
, bind
, server
, port
, realname
)
135 self
.bots
[nick
.lower()] = obj
137 def newfd(self
, obj
, fileno
):
138 self
.fds
[fileno
] = obj
139 if self
.potype
== "poll":
140 self
.po
.register(fileno
, select
.POLLIN
)
141 elif self
.potype
== "select":
142 self
.fdlist
.append(fileno
)
144 def bot(self
, name
): #get Bot() by name (nick)
145 return self
.bots
[name
.lower()]
146 def fd(self
, fileno
): #get Bot() by fd/fileno
147 return self
.fds
[fileno
]
148 def randbot(self
): #get Bot() randomly
149 return random
.choice(self
.bots
)
151 def user(self
, _nick
, justjoined
=False):
153 if nick
in self
.users
:
154 return self
.users
[nick
]
156 user
= self
.User(_nick
)
157 self
.users
[nick
] = user
160 self
.randbot().conn
.send("WHO %s n%%ant,2" % (nick
))
163 def channel(self
, name
): #get Channel() by name
164 if name
.lower() in self
.chans
:
165 return self
.chans
[name
.lower()]
169 def newchannel(self
, bot
, name
):
170 chan
= self
.Channel(name
.lower(), bot
)
171 self
.chans
[name
.lower()] = chan
175 if self
.potype
== "poll":
176 return [fd
for (fd
, ev
) in self
.po
.poll()]
177 elif self
.potype
== "select":
178 return select
.select(self
.fdlist
, [], [])[0]
180 def connectall(self
):
181 for bot
in self
.bots
.itervalues():
182 if bot
.conn
.state
== 0:
185 def module(self
, name
):
186 return ctlmod
.modules
[name
]
189 def hook(self
, word
, handler
):
191 self
.msghandlers
[word
].append(handler
)
193 self
.msghandlers
[word
] = [handler
]
194 def unhook(self
, word
, handler
):
195 if word
in self
.msghandlers
and handler
in self
.msghandlers
[word
]:
196 self
.msghandlers
[word
].remove(handler
)
197 def hashook(self
, word
):
198 return word
in self
.msghandlers
and len(self
.msghandlers
[word
]) != 0
199 def gethook(self
, word
):
200 return self
.msghandlers
[word
]
202 def hooknum(self
, word
, handler
):
204 self
.numhandlers
[word
].append(handler
)
206 self
.numhandlers
[word
] = [handler
]
207 def unhooknum(self
, word
, handler
):
208 if word
in self
.numhandlers
and handler
in self
.numhandlers
[word
]:
209 self
.numhandlers
[word
].remove(handler
)
210 def hasnumhook(self
, word
):
211 return word
in self
.numhandlers
and len(self
.numhandlers
[word
]) != 0
212 def getnumhook(self
, word
):
213 return self
.numhandlers
[word
]
215 def hookchan(self
, chan
, handler
):
217 self
.chanhandlers
[chan
].append(handler
)
219 self
.chanhandlers
[chan
] = [handler
]
220 def unhookchan(self
, chan
, handler
):
221 if chan
in self
.chanhandlers
and handler
in self
.chanhandlers
[chan
]:
222 self
.chanhandlers
[chan
].remove(handler
)
223 def haschanhook(self
, chan
):
224 return chan
in self
.chanhandlers
and len(self
.chanhandlers
[chan
]) != 0
225 def getchanhook(self
, chan
):
226 return self
.chanhandlers
[chan
]
229 class MyCursor(MySQLdb
.cursors
.DictCursor
):
230 def execute(self
, *args
, **kwargs
):
231 print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i
) for i
in args
]), ', '.join([str(key
)+"="+repr(kwargs
[key
]) for key
in kwargs
]))
233 super(self
.__class
__, self
).execute(*args
, **kwargs
)
234 except MySQLdb
.MySQLError
as e
:
235 print "[SQL] [!] MySQL error! %r" % (e
)
243 main
.db
= MySQLdb
.connect(host
=cfg
.dbhost
, user
=cfg
.dbuser
, passwd
=cfg
.dbpass
, db
=cfg
.dbname
, cursorclass
=MyCursor
)
248 cfg
= config
.Config('bot.config')
249 main
= Erebus(cfg
.trigger
)
251 autoloads
= [mod
for mod
, yes
in cfg
.items('autoloads') if int(yes
) == 1]
252 for mod
in autoloads
:
253 print "Loading %s" % (mod
)
254 ctlmod
.load(main
, mod
)
258 if c
.execute("SELECT nick, user, bind FROM bots WHERE active = 1"):
262 main
.newbot(row
['nick'], row
['user'], row
['bind'], cfg
.host
, cfg
.port
, cfg
.realname
)
266 poready
= main
.poll()
267 for fileno
in poready
:
268 for line
in main
.fd(fileno
).getdata():
269 main
.fd(fileno
).parse(line
)
271 if __name__
== '__main__':