]>
jfr.im git - erebus.git/blob - erebus.py
3 # Erebus IRC bot - Author: John Runyon
8 import os
, sys
, select
, MySQLdb
, MySQLdb
.cursors
, time
9 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 def __str__(self
): return self
.nick
58 def __repr__(self
): return "<User %r (%d)>" % (self
.nick
,self
.glevel
)
60 class Channel(object):
61 def __init__(self
, name
, bot
):
71 if c
.execute("SELECT user, level FROM chusers WHERE chan = %s", (self
.name
,)):
73 while row
is not None:
74 self
.levels
[row
['user']] = row
['level']
78 def levelof(self
, auth
):
80 if auth
in self
.levels
:
81 return self
.levels
[auth
]
85 def setlevel(self
, auth
, level
, savetodb
=True):
89 if c
.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self
.name
, auth
, level
)):
90 self
.levels
[auth
] = level
95 def userjoin(self
, user
, level
=None):
96 if user
not in self
.users
: self
.users
.append(user
)
97 if level
== 'op' and user
not in self
.ops
: self
.ops
.append(user
)
98 if level
== 'voice' and user
not in self
.voices
: self
.voices
.append(user
)
99 def userpart(self
, user
):
100 if user
in self
.ops
: self
.ops
.remove(user
)
101 if user
in self
.voices
: self
.voices
.remove(user
)
102 if user
in self
.users
: self
.users
.remove(user
)
104 def userop(self
, user
):
105 if user
in self
.users
and user
not in self
.ops
: self
.ops
.append(user
)
106 def uservoice(self
, user
):
107 if user
in self
.users
and user
not in self
.voices
: self
.voices
.append(user
)
108 def userdeop(self
, user
):
109 if user
in self
.ops
: self
.ops
.remove(user
)
110 def userdevoice(self
, user
):
111 if user
in self
.voices
: self
.voices
.remove(user
)
113 def __str__(self
): return self
.name
114 def __repr__(self
): return "<Channel %r>" % (self
.name
)
116 def __init__(self
, trigger
):
117 self
.trigger
= trigger
118 if os
.name
== "posix":
120 self
.po
= select
.poll()
121 else: # f.e. os.name == "nt" (Windows)
122 self
.potype
= "select"
125 def newbot(self
, nick
, user
, bind
, server
, port
, realname
):
126 if bind
is None: bind
= ''
127 obj
= bot
.Bot(self
, nick
, user
, bind
, server
, port
, realname
)
128 self
.bots
[nick
.lower()] = obj
130 def newfd(self
, obj
, fileno
):
131 self
.fds
[fileno
] = obj
132 if self
.potype
== "poll":
133 self
.po
.register(fileno
, select
.POLLIN
)
134 elif self
.potype
== "select":
135 self
.fdlist
.append(fileno
)
137 def bot(self
, name
): #get Bot() by name (nick)
138 return self
.bots
[name
.lower()]
139 def fd(self
, fileno
): #get Bot() by fd/fileno
140 return self
.fds
[fileno
]
141 def randbot(self
): #get Bot() randomly
142 for b
in self
.bots
.itervalues(): return b
#TODO
144 def user(self
, nick
, justjoined
=False):
146 if nick
in self
.users
:
147 return self
.users
[nick
]
149 user
= self
.User(nick
)
150 self
.users
[nick
] = user
153 self
.randbot().conn
.send("WHO %s %%ant,2" % (nick
))
156 def channel(self
, name
): #get Channel() by name
157 if name
.lower() in self
.chans
:
158 return self
.chans
[name
.lower()]
162 def newchannel(self
, bot
, name
):
163 chan
= self
.Channel(name
.lower(), bot
)
164 self
.chans
[name
.lower()] = chan
168 if self
.potype
== "poll":
169 return [fd
for (fd
, ev
) in self
.po
.poll()]
170 elif self
.potype
== "select":
171 return select
.select(self
.fdlist
, [], [])[0]
173 def connectall(self
):
174 for bot
in self
.bots
.itervalues():
175 if bot
.conn
.state
== 0:
179 def hook(self
, word
, handler
):
181 self
.msghandlers
[word
].append(handler
)
183 self
.msghandlers
[word
] = [handler
]
184 def unhook(self
, word
, handler
):
185 if word
in self
.msghandlers
and handler
in self
.msghandlers
[word
]:
186 self
.msghandlers
[word
].remove(handler
)
187 def hashook(self
, word
):
188 return word
in self
.msghandlers
and len(self
.msghandlers
[word
]) != 0
189 def gethook(self
, word
):
190 return self
.msghandlers
[word
]
192 def hooknum(self
, word
, handler
):
194 self
.numhandlers
[word
].append(handler
)
196 self
.numhandlers
[word
] = [handler
]
197 def unhooknum(self
, word
, handler
):
198 if word
in self
.numhandlers
and handler
in self
.numhandlers
[word
]:
199 self
.numhandlers
[word
].remove(handler
)
200 def hasnumhook(self
, word
):
201 return word
in self
.numhandlers
and len(self
.numhandlers
[word
]) != 0
202 def getnumhook(self
, word
):
203 return self
.numhandlers
[word
]
205 def hookchan(self
, chan
, handler
):
207 self
.chanhandlers
[chan
].append(handler
)
209 self
.chanhandlers
[chan
] = [handler
]
210 def unhookchan(self
, chan
, handler
):
211 if chan
in self
.chanhandlers
and handler
in self
.chanhandlers
[chan
]:
212 self
.chanhandlers
[chan
].remove(handler
)
213 def haschanhook(self
, chan
):
214 return chan
in self
.chanhandlers
and len(self
.chanhandlers
[chan
]) != 0
215 def getchanhook(self
, chan
):
216 return self
.chanhandlers
[chan
]
219 class MyCursor(MySQLdb
.cursors
.DictCursor
):
220 def execute(self
, *args
, **kwargs
):
221 print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i
) for i
in args
]), ', '.join([str(key
)+"="+repr(kwargs
[key
]) for key
in kwargs
]))
223 super(self
.__class
__, self
).execute(*args
, **kwargs
)
224 except MySQLdb
.MySQLError
as e
:
225 print "[SQL] [!] MySQL error! %r" % (e
)
233 main
.db
= MySQLdb
.connect(host
=cfg
.dbhost
, user
=cfg
.dbuser
, passwd
=cfg
.dbpass
, db
=cfg
.dbname
, cursorclass
=MyCursor
)
238 cfg
= config
.Config('bot.config')
239 main
= Erebus(cfg
.trigger
)
241 autoloads
= [mod
for mod
, yes
in cfg
.items('autoloads') if int(yes
) == 1]
242 for mod
in autoloads
:
243 print "Loading %s" % (mod
)
244 ctlmod
.load(main
, mod
)
248 if c
.execute("SELECT nick, user, bind FROM bots WHERE active = 1"):
252 main
.newbot(row
['nick'], row
['user'], row
['bind'], cfg
.host
, cfg
.port
, cfg
.realname
)
256 poready
= main
.poll()
257 for fileno
in poready
:
258 for line
in main
.fd(fileno
).getdata():
259 main
.fd(fileno
).parse(line
)
261 if __name__
== '__main__':