]>
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
, cfg
):
125 self
.trigger
= cfg
.trigger
126 if os
.name
== "posix":
128 self
.po
= select
.poll()
129 else: # f.e. os.name == "nt" (Windows)
130 self
.potype
= "select"
133 def newbot(self
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
):
134 if bind
is None: bind
= ''
135 obj
= bot
.Bot(self
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
)
136 self
.bots
[nick
.lower()] = obj
138 def newfd(self
, obj
, fileno
):
139 self
.fds
[fileno
] = obj
140 if self
.potype
== "poll":
141 self
.po
.register(fileno
, select
.POLLIN
)
142 elif self
.potype
== "select":
143 self
.fdlist
.append(fileno
)
145 def bot(self
, name
): #get Bot() by name (nick)
146 return self
.bots
[name
.lower()]
147 def fd(self
, fileno
): #get Bot() by fd/fileno
148 return self
.fds
[fileno
]
149 def randbot(self
): #get Bot() randomly
150 return self
.bots
[random
.choice(self
.bots
.keys())]
152 def user(self
, _nick
, justjoined
=False):
154 if nick
in self
.users
:
155 return self
.users
[nick
]
157 user
= self
.User(_nick
)
158 self
.users
[nick
] = user
161 self
.randbot().conn
.send("WHO %s n%%ant,2" % (nick
))
164 def channel(self
, name
): #get Channel() by name
165 if name
.lower() in self
.chans
:
166 return self
.chans
[name
.lower()]
170 def newchannel(self
, bot
, name
):
171 chan
= self
.Channel(name
.lower(), bot
)
172 self
.chans
[name
.lower()] = chan
176 if self
.potype
== "poll":
177 return [fd
for (fd
, ev
) in self
.po
.poll()]
178 elif self
.potype
== "select":
179 return select
.select(self
.fdlist
, [], [])[0]
181 def connectall(self
):
182 for bot
in self
.bots
.itervalues():
183 if bot
.conn
.state
== 0:
186 def module(self
, name
):
187 return ctlmod
.modules
[name
]
190 def hook(self
, word
, handler
):
192 self
.msghandlers
[word
].append(handler
)
194 self
.msghandlers
[word
] = [handler
]
195 def unhook(self
, word
, handler
):
196 if word
in self
.msghandlers
and handler
in self
.msghandlers
[word
]:
197 self
.msghandlers
[word
].remove(handler
)
198 def hashook(self
, word
):
199 return word
in self
.msghandlers
and len(self
.msghandlers
[word
]) != 0
200 def gethook(self
, word
):
201 return self
.msghandlers
[word
]
203 def hooknum(self
, word
, handler
):
205 self
.numhandlers
[word
].append(handler
)
207 self
.numhandlers
[word
] = [handler
]
208 def unhooknum(self
, word
, handler
):
209 if word
in self
.numhandlers
and handler
in self
.numhandlers
[word
]:
210 self
.numhandlers
[word
].remove(handler
)
211 def hasnumhook(self
, word
):
212 return word
in self
.numhandlers
and len(self
.numhandlers
[word
]) != 0
213 def getnumhook(self
, word
):
214 return self
.numhandlers
[word
]
216 def hookchan(self
, chan
, handler
):
218 self
.chanhandlers
[chan
].append(handler
)
220 self
.chanhandlers
[chan
] = [handler
]
221 def unhookchan(self
, chan
, handler
):
222 if chan
in self
.chanhandlers
and handler
in self
.chanhandlers
[chan
]:
223 self
.chanhandlers
[chan
].remove(handler
)
224 def haschanhook(self
, chan
):
225 return chan
in self
.chanhandlers
and len(self
.chanhandlers
[chan
]) != 0
226 def getchanhook(self
, chan
):
227 return self
.chanhandlers
[chan
]
230 class MyCursor(MySQLdb
.cursors
.DictCursor
):
231 def execute(self
, *args
, **kwargs
):
232 print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i
) for i
in args
]), ', '.join([str(key
)+"="+repr(kwargs
[key
]) for key
in kwargs
]))
234 super(self
.__class
__, self
).execute(*args
, **kwargs
)
235 except MySQLdb
.MySQLError
as e
:
236 print "[SQL] [!] MySQL error! %r" % (e
)
244 main
.db
= MySQLdb
.connect(host
=cfg
.dbhost
, user
=cfg
.dbuser
, passwd
=cfg
.dbpass
, db
=cfg
.dbname
, cursorclass
=MyCursor
)
249 cfg
= config
.Config('bot.config')
252 autoloads
= [mod
for mod
, yes
in cfg
.items('autoloads') if int(yes
) == 1]
253 for mod
in autoloads
:
254 print "Loading %s" % (mod
)
255 ctlmod
.load(main
, mod
)
259 if c
.execute("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1"):
263 main
.newbot(row
['nick'], row
['user'], row
['bind'], row
['authname'], row
['authpass'], cfg
.host
, cfg
.port
, cfg
.realname
)
267 poready
= main
.poll()
268 for fileno
in poready
:
269 for line
in main
.fd(fileno
).getdata():
270 main
.fd(fileno
).parse(line
)
272 if __name__
== '__main__':