]>
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 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 levelof(self
, auth
):
83 if auth
in self
.levels
:
84 return self
.levels
[auth
]
88 def setlevel(self
, auth
, level
, savetodb
=True):
92 if c
.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self
.name
, auth
, level
)):
93 self
.levels
[auth
] = level
98 def userjoin(self
, user
, level
=None):
99 if user
not in self
.users
: self
.users
.append(user
)
100 if level
== 'op' and user
not in self
.ops
: self
.ops
.append(user
)
101 if level
== 'voice' and user
not in self
.voices
: self
.voices
.append(user
)
102 def userpart(self
, user
):
103 if user
in self
.ops
: self
.ops
.remove(user
)
104 if user
in self
.voices
: self
.voices
.remove(user
)
105 if user
in self
.users
: self
.users
.remove(user
)
107 def userop(self
, user
):
108 if user
in self
.users
and user
not in self
.ops
: self
.ops
.append(user
)
109 def uservoice(self
, user
):
110 if user
in self
.users
and user
not in self
.voices
: self
.voices
.append(user
)
111 def userdeop(self
, user
):
112 if user
in self
.ops
: self
.ops
.remove(user
)
113 def userdevoice(self
, user
):
114 if user
in self
.voices
: self
.voices
.remove(user
)
116 def __str__(self
): return self
.name
117 def __repr__(self
): return "<Channel %r>" % (self
.name
)
119 def __init__(self
, trigger
):
120 self
.trigger
= trigger
121 if os
.name
== "posix":
123 self
.po
= select
.poll()
124 else: # f.e. os.name == "nt" (Windows)
125 self
.potype
= "select"
128 def newbot(self
, nick
, user
, bind
, server
, port
, realname
):
129 if bind
is None: bind
= ''
130 obj
= bot
.Bot(self
, nick
, user
, bind
, server
, port
, realname
)
131 self
.bots
[nick
.lower()] = obj
133 def newfd(self
, obj
, fileno
):
134 self
.fds
[fileno
] = obj
135 if self
.potype
== "poll":
136 self
.po
.register(fileno
, select
.POLLIN
)
137 elif self
.potype
== "select":
138 self
.fdlist
.append(fileno
)
140 def bot(self
, name
): #get Bot() by name (nick)
141 return self
.bots
[name
.lower()]
142 def fd(self
, fileno
): #get Bot() by fd/fileno
143 return self
.fds
[fileno
]
144 def randbot(self
): #get Bot() randomly
145 for b
in self
.bots
.itervalues(): return b
#TODO
147 def user(self
, _nick
, justjoined
=False):
149 if nick
in self
.users
:
150 return self
.users
[nick
]
152 user
= self
.User(_nick
)
153 self
.users
[nick
] = user
156 self
.randbot().conn
.send("WHO %s %%ant,2" % (nick
))
159 def channel(self
, name
): #get Channel() by name
160 if name
.lower() in self
.chans
:
161 return self
.chans
[name
.lower()]
165 def newchannel(self
, bot
, name
):
166 chan
= self
.Channel(name
.lower(), bot
)
167 self
.chans
[name
.lower()] = chan
171 if self
.potype
== "poll":
172 return [fd
for (fd
, ev
) in self
.po
.poll()]
173 elif self
.potype
== "select":
174 return select
.select(self
.fdlist
, [], [])[0]
176 def connectall(self
):
177 for bot
in self
.bots
.itervalues():
178 if bot
.conn
.state
== 0:
182 def hook(self
, word
, handler
):
184 self
.msghandlers
[word
].append(handler
)
186 self
.msghandlers
[word
] = [handler
]
187 def unhook(self
, word
, handler
):
188 if word
in self
.msghandlers
and handler
in self
.msghandlers
[word
]:
189 self
.msghandlers
[word
].remove(handler
)
190 def hashook(self
, word
):
191 return word
in self
.msghandlers
and len(self
.msghandlers
[word
]) != 0
192 def gethook(self
, word
):
193 return self
.msghandlers
[word
]
195 def hooknum(self
, word
, handler
):
197 self
.numhandlers
[word
].append(handler
)
199 self
.numhandlers
[word
] = [handler
]
200 def unhooknum(self
, word
, handler
):
201 if word
in self
.numhandlers
and handler
in self
.numhandlers
[word
]:
202 self
.numhandlers
[word
].remove(handler
)
203 def hasnumhook(self
, word
):
204 return word
in self
.numhandlers
and len(self
.numhandlers
[word
]) != 0
205 def getnumhook(self
, word
):
206 return self
.numhandlers
[word
]
208 def hookchan(self
, chan
, handler
):
210 self
.chanhandlers
[chan
].append(handler
)
212 self
.chanhandlers
[chan
] = [handler
]
213 def unhookchan(self
, chan
, handler
):
214 if chan
in self
.chanhandlers
and handler
in self
.chanhandlers
[chan
]:
215 self
.chanhandlers
[chan
].remove(handler
)
216 def haschanhook(self
, chan
):
217 return chan
in self
.chanhandlers
and len(self
.chanhandlers
[chan
]) != 0
218 def getchanhook(self
, chan
):
219 return self
.chanhandlers
[chan
]
222 class MyCursor(MySQLdb
.cursors
.DictCursor
):
223 def execute(self
, *args
, **kwargs
):
224 print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i
) for i
in args
]), ', '.join([str(key
)+"="+repr(kwargs
[key
]) for key
in kwargs
]))
226 super(self
.__class
__, self
).execute(*args
, **kwargs
)
227 except MySQLdb
.MySQLError
as e
:
228 print "[SQL] [!] MySQL error! %r" % (e
)
236 main
.db
= MySQLdb
.connect(host
=cfg
.dbhost
, user
=cfg
.dbuser
, passwd
=cfg
.dbpass
, db
=cfg
.dbname
, cursorclass
=MyCursor
)
241 cfg
= config
.Config('bot.config')
242 main
= Erebus(cfg
.trigger
)
244 autoloads
= [mod
for mod
, yes
in cfg
.items('autoloads') if int(yes
) == 1]
245 for mod
in autoloads
:
246 print "Loading %s" % (mod
)
247 ctlmod
.load(main
, mod
)
251 if c
.execute("SELECT nick, user, bind FROM bots WHERE active = 1"):
255 main
.newbot(row
['nick'], row
['user'], row
['bind'], cfg
.host
, cfg
.port
, cfg
.realname
)
259 poready
= main
.poll()
260 for fileno
in poready
:
261 for line
in main
.fd(fileno
).getdata():
262 main
.fd(fileno
).parse(line
)
264 if __name__
== '__main__':