]>
jfr.im git - erebus.git/blob - erebus.py
3 # Erebus IRC bot - Author: John Runyon
6 import os
, sys
, select
, MySQLdb
, MySQLdb
.cursors
, time
, random
7 import bot
, config
, ctlmod
22 def __init__(self
, nick
, auth
=None):
30 return self
.auth
is not None
32 def authed(self
, auth
):
33 if auth
== '0': self
.auth
= None
34 else: self
.auth
= auth
.lower()
42 if c
.execute("SELECT level FROM users WHERE auth = %s", (self
.auth
,)):
45 self
.glevel
= row
['level']
53 self
.chans
.append(chan
)
55 self
.chans
.remove(chan
)
57 for chan
in self
.chans
:
58 self
.chans
.remove(chan
)
60 def __str__(self
): return self
.nick
61 def __repr__(self
): return "<User %r (%d)>" % (self
.nick
,self
.glevel
)
63 class Channel(object):
64 def __init__(self
, name
, bot
):
74 if c
.execute("SELECT user, level FROM chusers WHERE chan = %s", (self
.name
,)):
76 while row
is not None:
77 self
.levels
[row
['user']] = row
['level']
81 def msg(self
, *args
, **kwargs
):
82 self
.bot
.msg(self
.name
, *args
, **kwargs
)
84 def levelof(self
, auth
):
88 if auth
in self
.levels
:
89 return self
.levels
[auth
]
93 def setlevel(self
, auth
, level
, savetodb
=True):
97 if c
.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self
.name
, auth
, level
)):
98 self
.levels
[auth
] = level
103 def userjoin(self
, user
, level
=None):
104 if user
not in self
.users
: self
.users
.append(user
)
105 if level
== 'op' and user
not in self
.ops
: self
.ops
.append(user
)
106 if level
== 'voice' and user
not in self
.voices
: self
.voices
.append(user
)
107 def userpart(self
, user
):
108 if user
in self
.ops
: self
.ops
.remove(user
)
109 if user
in self
.voices
: self
.voices
.remove(user
)
110 if user
in self
.users
: self
.users
.remove(user
)
112 def userop(self
, user
):
113 if user
in self
.users
and user
not in self
.ops
: self
.ops
.append(user
)
114 def uservoice(self
, user
):
115 if user
in self
.users
and user
not in self
.voices
: self
.voices
.append(user
)
116 def userdeop(self
, user
):
117 if user
in self
.ops
: self
.ops
.remove(user
)
118 def userdevoice(self
, user
):
119 if user
in self
.voices
: self
.voices
.remove(user
)
121 def __str__(self
): return self
.name
122 def __repr__(self
): return "<Channel %r>" % (self
.name
)
124 def __init__(self
, cfg
):
126 self
.trigger
= cfg
.trigger
127 if os
.name
== "posix":
129 self
.po
= select
.poll()
130 else: # f.e. os.name == "nt" (Windows)
131 self
.potype
= "select"
134 def newbot(self
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
):
135 if bind
is None: bind
= ''
136 obj
= bot
.Bot(self
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
)
137 self
.bots
[nick
.lower()] = obj
139 def newfd(self
, obj
, fileno
):
140 self
.fds
[fileno
] = obj
141 if self
.potype
== "poll":
142 self
.po
.register(fileno
, select
.POLLIN
)
143 elif self
.potype
== "select":
144 self
.fdlist
.append(fileno
)
146 def bot(self
, name
): #get Bot() by name (nick)
147 return self
.bots
[name
.lower()]
148 def fd(self
, fileno
): #get Bot() by fd/fileno
149 return self
.fds
[fileno
]
150 def randbot(self
): #get Bot() randomly
151 return self
.bots
[random
.choice(self
.bots
.keys())]
153 def user(self
, _nick
, justjoined
=False):
155 if nick
in self
.users
:
156 return self
.users
[nick
]
158 user
= self
.User(_nick
)
159 self
.users
[nick
] = user
162 self
.randbot().conn
.send("WHO %s n%%ant,2" % (nick
))
165 def channel(self
, name
): #get Channel() by name
166 if name
.lower() in self
.chans
:
167 return self
.chans
[name
.lower()]
171 def newchannel(self
, bot
, name
):
172 chan
= self
.Channel(name
.lower(), bot
)
173 self
.chans
[name
.lower()] = chan
177 if self
.potype
== "poll":
178 return [fd
for (fd
, ev
) in self
.po
.poll()]
179 elif self
.potype
== "select":
180 return select
.select(self
.fdlist
, [], [])[0]
182 def connectall(self
):
183 for bot
in self
.bots
.itervalues():
184 if bot
.conn
.state
== 0:
187 def module(self
, name
):
188 return ctlmod
.modules
[name
]
191 def hook(self
, word
, handler
):
193 self
.msghandlers
[word
].append(handler
)
195 self
.msghandlers
[word
] = [handler
]
196 def unhook(self
, word
, handler
):
197 if word
in self
.msghandlers
and handler
in self
.msghandlers
[word
]:
198 self
.msghandlers
[word
].remove(handler
)
199 def hashook(self
, word
):
200 return word
in self
.msghandlers
and len(self
.msghandlers
[word
]) != 0
201 def gethook(self
, word
):
202 return self
.msghandlers
[word
]
204 def hooknum(self
, word
, handler
):
206 self
.numhandlers
[word
].append(handler
)
208 self
.numhandlers
[word
] = [handler
]
209 def unhooknum(self
, word
, handler
):
210 if word
in self
.numhandlers
and handler
in self
.numhandlers
[word
]:
211 self
.numhandlers
[word
].remove(handler
)
212 def hasnumhook(self
, word
):
213 return word
in self
.numhandlers
and len(self
.numhandlers
[word
]) != 0
214 def getnumhook(self
, word
):
215 return self
.numhandlers
[word
]
217 def hookchan(self
, chan
, handler
):
219 self
.chanhandlers
[chan
].append(handler
)
221 self
.chanhandlers
[chan
] = [handler
]
222 def unhookchan(self
, chan
, handler
):
223 if chan
in self
.chanhandlers
and handler
in self
.chanhandlers
[chan
]:
224 self
.chanhandlers
[chan
].remove(handler
)
225 def haschanhook(self
, chan
):
226 return chan
in self
.chanhandlers
and len(self
.chanhandlers
[chan
]) != 0
227 def getchanhook(self
, chan
):
228 return self
.chanhandlers
[chan
]
231 class MyCursor(MySQLdb
.cursors
.DictCursor
):
232 def execute(self
, *args
, **kwargs
):
233 print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i
) for i
in args
]), ', '.join([str(key
)+"="+repr(kwargs
[key
]) for key
in kwargs
]))
235 super(self
.__class
__, self
).execute(*args
, **kwargs
)
236 except MySQLdb
.MySQLError
as e
:
237 print "[SQL] [!] MySQL error! %r" % (e
)
245 main
.db
= MySQLdb
.connect(host
=cfg
.dbhost
, user
=cfg
.dbuser
, passwd
=cfg
.dbpass
, db
=cfg
.dbname
, cursorclass
=MyCursor
)
250 cfg
= config
.Config('bot.config')
253 autoloads
= [mod
for mod
, yes
in cfg
.items('autoloads') if int(yes
) == 1]
254 for mod
in autoloads
:
255 print "Loading %s" % (mod
)
256 ctlmod
.load(main
, mod
)
260 if c
.execute("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1"):
264 main
.newbot(row
['nick'], row
['user'], row
['bind'], row
['authname'], row
['authpass'], cfg
.host
, cfg
.port
, cfg
.realname
)
268 poready
= main
.poll()
269 for fileno
in poready
:
270 for line
in main
.fd(fileno
).getdata():
271 main
.fd(fileno
).parse(line
)
273 if __name__
== '__main__':