]>
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 msg(self
, *args
, **kwargs
):
84 self
.bot
.msg(self
.name
, *args
, **kwargs
)
86 def levelof(self
, auth
):
90 if auth
in self
.levels
:
91 return self
.levels
[auth
]
95 def setlevel(self
, auth
, level
, savetodb
=True):
99 if c
.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self
.name
, auth
, level
)):
100 self
.levels
[auth
] = level
105 def userjoin(self
, user
, level
=None):
106 if user
not in self
.users
: self
.users
.append(user
)
107 if level
== 'op' and user
not in self
.ops
: self
.ops
.append(user
)
108 if level
== 'voice' and user
not in self
.voices
: self
.voices
.append(user
)
109 def userpart(self
, user
):
110 if user
in self
.ops
: self
.ops
.remove(user
)
111 if user
in self
.voices
: self
.voices
.remove(user
)
112 if user
in self
.users
: self
.users
.remove(user
)
114 def userop(self
, user
):
115 if user
in self
.users
and user
not in self
.ops
: self
.ops
.append(user
)
116 def uservoice(self
, user
):
117 if user
in self
.users
and user
not in self
.voices
: self
.voices
.append(user
)
118 def userdeop(self
, user
):
119 if user
in self
.ops
: self
.ops
.remove(user
)
120 def userdevoice(self
, user
):
121 if user
in self
.voices
: self
.voices
.remove(user
)
123 def __str__(self
): return self
.name
124 def __repr__(self
): return "<Channel %r>" % (self
.name
)
126 def __init__(self
, cfg
):
128 self
.trigger
= cfg
.trigger
129 if os
.name
== "posix":
131 self
.po
= select
.poll()
132 else: # f.e. os.name == "nt" (Windows)
133 self
.potype
= "select"
136 def newbot(self
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
):
137 if bind
is None: bind
= ''
138 obj
= bot
.Bot(self
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
)
139 self
.bots
[nick
.lower()] = obj
141 def newfd(self
, obj
, fileno
):
142 self
.fds
[fileno
] = obj
143 if self
.potype
== "poll":
144 self
.po
.register(fileno
, select
.POLLIN
)
145 elif self
.potype
== "select":
146 self
.fdlist
.append(fileno
)
148 def bot(self
, name
): #get Bot() by name (nick)
149 return self
.bots
[name
.lower()]
150 def fd(self
, fileno
): #get Bot() by fd/fileno
151 return self
.fds
[fileno
]
152 def randbot(self
): #get Bot() randomly
153 return self
.bots
[random
.choice(self
.bots
.keys())]
155 def user(self
, _nick
, justjoined
=False):
157 if nick
in self
.users
:
158 return self
.users
[nick
]
160 user
= self
.User(_nick
)
161 self
.users
[nick
] = user
164 self
.randbot().conn
.send("WHO %s n%%ant,2" % (nick
))
167 def channel(self
, name
): #get Channel() by name
168 if name
.lower() in self
.chans
:
169 return self
.chans
[name
.lower()]
173 def newchannel(self
, bot
, name
):
174 chan
= self
.Channel(name
.lower(), bot
)
175 self
.chans
[name
.lower()] = chan
179 if self
.potype
== "poll":
180 return [fd
for (fd
, ev
) in self
.po
.poll()]
181 elif self
.potype
== "select":
182 return select
.select(self
.fdlist
, [], [])[0]
184 def connectall(self
):
185 for bot
in self
.bots
.itervalues():
186 if bot
.conn
.state
== 0:
189 def module(self
, name
):
190 return ctlmod
.modules
[name
]
193 def hook(self
, word
, handler
):
195 self
.msghandlers
[word
].append(handler
)
197 self
.msghandlers
[word
] = [handler
]
198 def unhook(self
, word
, handler
):
199 if word
in self
.msghandlers
and handler
in self
.msghandlers
[word
]:
200 self
.msghandlers
[word
].remove(handler
)
201 def hashook(self
, word
):
202 return word
in self
.msghandlers
and len(self
.msghandlers
[word
]) != 0
203 def gethook(self
, word
):
204 return self
.msghandlers
[word
]
206 def hooknum(self
, word
, handler
):
208 self
.numhandlers
[word
].append(handler
)
210 self
.numhandlers
[word
] = [handler
]
211 def unhooknum(self
, word
, handler
):
212 if word
in self
.numhandlers
and handler
in self
.numhandlers
[word
]:
213 self
.numhandlers
[word
].remove(handler
)
214 def hasnumhook(self
, word
):
215 return word
in self
.numhandlers
and len(self
.numhandlers
[word
]) != 0
216 def getnumhook(self
, word
):
217 return self
.numhandlers
[word
]
219 def hookchan(self
, chan
, handler
):
221 self
.chanhandlers
[chan
].append(handler
)
223 self
.chanhandlers
[chan
] = [handler
]
224 def unhookchan(self
, chan
, handler
):
225 if chan
in self
.chanhandlers
and handler
in self
.chanhandlers
[chan
]:
226 self
.chanhandlers
[chan
].remove(handler
)
227 def haschanhook(self
, chan
):
228 return chan
in self
.chanhandlers
and len(self
.chanhandlers
[chan
]) != 0
229 def getchanhook(self
, chan
):
230 return self
.chanhandlers
[chan
]
233 class MyCursor(MySQLdb
.cursors
.DictCursor
):
234 def execute(self
, *args
, **kwargs
):
235 print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i
) for i
in args
]), ', '.join([str(key
)+"="+repr(kwargs
[key
]) for key
in kwargs
]))
237 super(self
.__class
__, self
).execute(*args
, **kwargs
)
238 except MySQLdb
.MySQLError
as e
:
239 print "[SQL] [!] MySQL error! %r" % (e
)
247 main
.db
= MySQLdb
.connect(host
=cfg
.dbhost
, user
=cfg
.dbuser
, passwd
=cfg
.dbpass
, db
=cfg
.dbname
, cursorclass
=MyCursor
)
252 cfg
= config
.Config('bot.config')
255 autoloads
= [mod
for mod
, yes
in cfg
.items('autoloads') if int(yes
) == 1]
256 for mod
in autoloads
:
257 print "Loading %s" % (mod
)
258 ctlmod
.load(main
, mod
)
262 if c
.execute("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1"):
266 main
.newbot(row
['nick'], row
['user'], row
['bind'], row
['authname'], row
['authpass'], cfg
.host
, cfg
.port
, cfg
.realname
)
270 poready
= main
.poll()
271 for fileno
in poready
:
272 for line
in main
.fd(fileno
).getdata():
273 main
.fd(fileno
).parse(line
)
275 if __name__
== '__main__':