summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
l---------[-rw-r--r--]autoexec.py69
l---------[-rw-r--r--]bouncer.py336
l---------[-rw-r--r--]cannon.py31
l---------[-rw-r--r--]figlet.py21
l---------irc.py1
l---------[-rw-r--r--]logger.py337
l---------[-rw-r--r--]wallet.py38
7 files changed, 7 insertions, 826 deletions
diff --git a/autoexec.py b/autoexec.py
index 8a8b7e7..8ed73e0 100644..120000
--- a/autoexec.py
+++ b/autoexec.py
@@ -1,68 +1 @@
-#!/usr/bin/python
-import re
-
-class Autoexec(object):
- def __init__(self):
- self.networks={}
- def onModuleAdd(self, IRC, label, onconnect=None, onregister=None, autojoin=None, usermodes=None, wallet=None, opername=None, opermodes=None, snomasks=None, operexec=None, operjoin=None):
- labels=[v[0] for v in self.networks.values()]
- if label in labels:
- raise BaseException, "Label already exists"
- if IRC in self.networks.keys():
- raise BaseException, "Network already exists"
- self.networks[IRC]=(label, onconnect, onregister, autojoin, usermodes, wallet, opername, opermodes, snomasks, operexec, operjoin)
- def onModuleRem(self, IRC):
- del self.networks[IRC]
- def onConnect(self, IRC):
- (label, onconnect, onregister, autojoin, usermodes, wallet, opername, opermodes, snomasks, operexec, operjoin)=self.networks[IRC]
- if onconnect:
- for line in onconnect:
- IRC.raw(line, origin=self)
- def onRegistered(self, IRC):
- (label, onconnect, onregister, autojoin, usermodes, wallet, opername, opermodes, snomasks, operexec, operjoin)=self.networks[IRC]
- if onregister:
- for line in onregister:
- IRC.raw(line, origin=self)
- if usermodes:
- IRC.raw("MODE %s %s"%(IRC.identity.nick, usermodes), origin=self)
- if opername and wallet and "%s/opers/%s"%(label, opername) in wallet.keys():
- IRC.raw("OPER %s %s"%(opername, wallet["%s/opers/%s"%(label, opername)]), origin=self)
- if autojoin:
- IRC.raw("JOIN %s"%(",".join(autojoin)), origin=self)
- def onRecv(self, IRC, line, data):
- if data==None:
- return
- (label, onconnect, onregister, autojoin, usermodes, wallet, opername, opermodes, snomasks, operexec, operjoin)=self.networks[IRC]
- (origin, ident, host, cmd, target, params, extinfo)=data
- if cmd=="381" and opermodes:
- if operexec:
- for line in operexec:
- IRC.raw(line, origin=self)
- if opermodes:
- IRC.raw("MODE %s %s"%(IRC.identity.nick, opermodes), origin=self)
- if snomasks:
- IRC.raw("MODE %s +s %s"%(IRC.identity.nick, snomasks), origin=self)
- if operjoin:
- IRC.raw("JOIN %s"%(",".join(operjoin)), origin=self)
-
-class NickServ(object):
- def __init__(self):
- self.networks={}
- def onModuleAdd(self, IRC, label, wallet=None, autojoin=None):
- labels=[v[0] for v in self.networks.values()]
- #print labels
- if label in labels:
- raise BaseException, "Label already exists"
- if IRC in self.networks.keys():
- raise BaseException, "Network already exists"
- self.networks[IRC]=(label, wallet, autojoin)
- def onModuleRem(self, IRC):
- del self.networks[IRC]
- def onRecv(self, IRC, line, data):
- if data==None: return
- (origin, ident, host, cmd, target, params, extinfo)=data
- label, wallet, autojoin=self.networks[IRC]
- if target==IRC.identity.nick and origin=="NickServ" and re.match("This nickname is registered and protected.", extinfo) and wallet and "%s/NickServ/%s"%(label, target.lower()) in wallet.keys():
- IRC.user("NickServ").msg("identify %s" % wallet["%s/NickServ/%s"%(label, target.lower())])
- if cmd=="900" and autojoin:
- IRC.raw("JOIN %s"%(",".join(autojoin)), origin=self)
+../autoexec.py \ No newline at end of file
diff --git a/bouncer.py b/bouncer.py
index be4231d..d8ddf7e 100644..120000
--- a/bouncer.py
+++ b/bouncer.py
@@ -1,335 +1 @@
-#!/usr/bin/python
-import socket, ssl, os, re, time, sys, string
-from threading import Thread
-import Queue
-
-class Bouncer (Thread):
- def __init__(self, addr, port, servers, ssl=False, certfile=None, keyfile=None, ignore=None):
- self.__name__="Bouncer for pyIRC"
- self.__version__="1.0.0alpha1"
- self.__author__="Brian Sherson"
- self.__date__="Apr 21, 2013"
- #print "Initializing ListenThread..."
- self.addr=addr
- self.port=port
- self.servers=servers
- self.socket=s=socket.socket()
- self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- self.ssl=ssl
- self.certfile=certfile
- self.keyfile=keyfile
- s.bind((self.addr,self.port))
- self.connections=[]
- for server in servers.values(): server.modules.append(self)
- self.ignore=ignore
- self.whoexpected=[]
- Thread.__init__ ( self )
-
- def run(self):
- self.socket.listen(5)
- #print ((self,"Now listening on port "+str(self.port)))
- while True:
- try:
- (connection,addr)=self.socket.accept()
- if self.ssl:
- connection=ssl.wrap_socket(connection, server_side=True, certfile=self.certfile, keyfile=self.keyfile, ssl_version=ssl.PROTOCOL_SSLv23)
- try:
- hostname, aliaslist, addresslist = socket.gethostbyaddr(addr[0])
- addr = (hostname, addr[1])
- except:
- pass
- #print ((self,"New client connecting from %s:%s"%addr))
- except socket.error:
- print "Shutting down Listener"
- self.socket.close()
- #raise
- sys.exit()
- bouncer=BouncerConnection(self, connection, addr)
- bouncer.daemon=True
- #self.connections.append(bouncer)
- bouncer.start()
- #ccrecv.start()
- time.sleep(0.5)
- try:
- self.socket.close()
- except: pass
- def onRecv(self, IRC, line, data):
- if type(self.ignore) not in (list, tuple) or all([not re.match(pattern, line) for pattern in self.ignore]):
- if data:
- (origin, ident, host, cmd, target, params, extinfo)=data
- #print data
- if cmd=="352": ### Who reply
- if len(self.whoexpected) and self.whoexpected[0] in self.connections and self.whoexpected[0].IRC==IRC:
- self.whoexpected[0].connection.send(line+"\n")
- elif cmd=="315": ### End of Who reply
- if len(self.whoexpected) and self.whoexpected[0] in self.connections and self.whoexpected[0].IRC==IRC:
- self.whoexpected[0].connection.send(line+"\n")
- del self.whoexpected[0]
- else:
- for bouncer in self.connections:
- #print bouncer.IRC
- #print IRC
- #print line
- if bouncer.IRC==IRC: bouncer.connection.send(line+"\n")
- def onSend(self, IRC, line, data, origin):
- #print "Bouncer onSend"
- if type(self.ignore) not in (list, tuple) or all([not re.match(pattern, line) for pattern in self.ignore]):
- (cmd, target, params, extinfo)=data
- if cmd in ("PRIVMSG", "NOTICE"):
- for bouncer in self.connections:
- if bouncer==origin: continue
- #print bouncer.IRC
- #print IRC
- if bouncer.IRC==IRC: bouncer.connection.send(":%s!%s@%s %s\n" % (bouncer.IRC.identity.nick, bouncer.IRC.identity.idnt, bouncer.IRC.identity.host, line))
- elif cmd=="WHO":
- #print origin, line
- self.whoexpected.append(origin)
- def stop(self):
- self.socket.shutdown(0)
- def onDisconnect(self, IRC):
- for bouncer in self.connections:
- if bouncer.IRC==IRC:
- quitmsg="Bouncer has been disconnected from IRC"
- try:
- bouncer.connection.send("ERROR :Closing link: (%s@%s) [%s]\n" % (self.nick, self.addr[0], quitmsg))
- bouncer.connection.close()
- bouncer.connection.shutdown(0)
- except:
- pass
- if bouncer in self.connections:
- self.connections.remove(bouncer)
-
-class BouncerConnection (Thread):
- def __init__(self, bouncerlistener, connection, addr):
- #print "Initializing ListenThread..."
- self.bouncerlistener=bouncerlistener
- self.connection=connection
- self.addr=addr
- self.IRC=None
- self.pwd=None
- self.nick=None
- self.idnt=None
- self.realname=None
- self.addr=addr
- Thread.__init__ ( self )
-
- def stop(self):
- self.connection.shutdown(0)
-
- def run(self):
- #print "Bouncer Connection Started"
- r=self.connection.makefile("r")
- w=self.connection.makefile("w")
- k=0
- while self.pwd==None or self.nick==None or self.idnt==None:
- line=r.readline().rstrip()
- match=re.findall("^PASS :?(.*)$", line, re.I)
- if match:
- self.pwd=match[0]
- match=re.findall("^NICK :?(.*)$", line, re.I)
- if match:
- self.nick=match[0]
- match=re.findall("^USER\\s+(.+?)\\s+(.+?)\\s+(.+?):(.*)$", line, re.I)
- if match:
- self.idnt, a, b, self.realname=match[0]
- if k>10:
- self.connection.send("ERROR :Closing link: (%s@%s) [Access Denied]\n" % (self.nick, self.addr[0]))
- self.connection.close()
- sys.exit()
- k+=1
- for idnt, pwd in self.bouncerlistener.servers.keys():
- if idnt.lower()==self.idnt.lower() and pwd==self.pwd:
- self.IRC=self.bouncerlistener.servers[idnt, pwd]
- if self.IRC==None or not self.IRC.registered:
- self.connection.send("ERROR :Closing link: (%s@%s) [Access Denied]\n" % (self.nick, self.addr[0]))
- self.connection.close()
- sys.exit()
-
- for bouncer in self.bouncerlistener.connections:
- try:
- bouncer.connection.send(":%s!%s@%s NOTICE %s :*** Bouncer Connection to %s originated from %s\n" % (self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, bouncer.IRC.identity.nick, self.IRC, self.addr[0]))
- except:
- self.bouncerlistener.connections.remove(bouncer)
-
- self.connection.send(":%s 001 %s :%s\n" % (self.IRC.serv, self.IRC.identity.nick, self.IRC.welcome))
- self.connection.send(":%s 002 %s :%s\n" % (self.IRC.serv, self.IRC.identity.nick, self.IRC.hostinfo))
- self.connection.send(":%s 003 %s :%s\n" % (self.IRC.serv, self.IRC.identity.nick, self.IRC.servinfo))
- self.connection.send(":%s 004 %s %s\n" % (self.IRC.serv, self.IRC.identity.nick, self.IRC.serv004))
- for params in self.IRC.serv005:
- self.connection.send(":%s 005 %s %s :are supported by this server\n" % (self.IRC.serv, self.IRC.identity.nick, params))
-
- self.connection.send(":%s 375 %s :%s\n" % (self.IRC.serv, self.IRC.identity.nick, self.IRC.motdgreet))
- for motdline in self.IRC.motd:
- self.connection.send(":%s 372 %s :%s\n" % (self.IRC.serv, self.IRC.identity.nick, motdline))
- self.connection.send(":%s 376 %s :%s\n" % (self.IRC.serv, self.IRC.identity.nick, self.IRC.motdend))
-
- self.connection.send(":%s 221 %s +%s\n" % (self.IRC.serv, self.IRC.identity.nick, self.IRC.identity.modes))
- if "s" in self.IRC.identity.modes and self.IRC.identity.snomask:
- self.connection.send(":%s 008 %s +%s :Server notice mask\n" % (self.IRC.server, self.IRC.identity.nick, self.IRC.identity.snomask))
-
- for channel in self.IRC.identity.channels:
- self.connection.send(":%s!%s@%s JOIN :%s\n" % (self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, channel.name))
- self.connection.send(":%s 332 %s %s :%s\n" % (self.IRC.serv, self.IRC.identity.nick, channel.name, channel.topic))
- self.connection.send(":%s 333 %s %s %s %s\n" % (self.IRC.serv, self.IRC.identity.nick, channel.name, channel.topicsetby, channel.topictime))
- secret="s" in channel.modes.keys() and channel.modes["s"]
- private="p" in channel.modes.keys() and channel.modes["p"]
- namesusers=[]
- modes, symbols=self.IRC.supports["PREFIX"]
- self.connection.send(":%s 353 %s %s %s :%s\n" % (
- self.IRC.serv,
- self.IRC.identity.nick,
- "@" if secret else ("*" if private else "="),
- channel.name,
- string.join([string.join([symbols[k] if modes[k] in channel.modes.keys() and user in channel.modes[modes[k]] else "" for k in xrange(len(modes))],"")+user.nick for user in channel.users]))
- )
-
- self.bouncerlistener.connections.append(self)
-
- quitmsg="Connection Closed"
- readbuf=""
- linebuf=[]
-
- while True:
- while len(linebuf)==0:
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in time.localtime()[0:6]])
- try:
- read=self.connection.recv(512)
- except:
- exc,excmsg,tb=sys.exc_info()
- print >>sys.stderr, "%(timestamp)s *** %(exc)s: %(excmsg)s" % vars()
- server, port=self.addr
- #server=self.addr[0]
- #port=self.port
- print >>sys.stderr, "%(timestamp)s *** Connection from %(server)s:%(port)s terminated" % vars()
- self.bouncerlistener.connections.remove(self)
- sys.stderr.flush()
- raise
- if read=="":
- server, port=self.addr
- #port=self.port
- print >>sys.stderr, "%(timestamp)s *** Connection from %(server)s:%(port)s terminated" % vars()
- self.bouncerlistener.connections.remove(self)
- sys.stderr.flush()
- sys.exit()
- readbuf+=read
- lastlf=readbuf.rfind("\n")
- if lastlf>=0:
- linebuf.extend(string.split(readbuf[0:lastlf], "\n"))
- readbuf=readbuf[lastlf+1:]
-
- line=string.rstrip(linebuf.pop(0))
-
- match=re.findall("^(.+?)(?:\\s+(.+?)(?:\\s+(.+?))??)??(?:\\s+:(.*))?$", line, re.I)
- if len(match)==0: continue
- (cmd, target, params, extinfo)=match[0]
-
- if cmd.upper()=="QUIT":
- quitmsg=extinfo
- break
- elif cmd.upper()=="PING":
- try:
- self.connection.send(":%s PONG %s :%s\n" % (self.IRC.serv, self.IRC.serv, self.IRC.identity.nick))
- except:
- sys.exit()
- continue
- elif cmd.upper() in ("PRIVMSG", "NOTICE"):
- #print line
- ctcp=re.findall("^\x01(.*?)(?:\\s+(.*?)\\s*)?\x01$",extinfo)
- if ctcp:
- (ctcptype,ext)=ctcp[0]
- if ctcptype=="LAGCHECK":
- try:
- self.connection.send(":%s!%s@%s %s\n" % (self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, line))
- except:
- self.connection.remove(self)
- sys.exit()
- elif ctcptype=="ACTION":
- self.IRC.raw(line, origin=self)
- #for bouncer in self.bouncerlistener.connections:
- # if bouncer!=self and bouncer.IRC==self.IRC:
- # try:
- # bouncer.connection.send(":%s!%s@%s %s\n"%(self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, line))
- # except:
- # self.bouncerlistener.connections.remove(bouncer)
- else:
- self.IRC.raw(line, origin=self)
- else:
- self.IRC.raw(line, origin=self)
- #for bouncer in self.bouncerlistener.connections:
- # if bouncer!=self and bouncer.IRC==self.IRC:
- # try:
- # bouncer.connection.send(":%s!%s@%s %s\n"%(self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, line))
- # except:
- # self.bouncerlistener.connections.remove(bouncer)
- elif cmd.upper() == "MODE":
- #print "ddd", target, params, self.IRC.supports["CHANTYPES"]
- if target and target[0] in self.IRC.supports["CHANTYPES"]:
- if params=="":
- channel=self.IRC.channel(target)
- modes=channel.modes.keys()
- modestr="".join([mode for mode in modes if mode not in self.IRC.supports["CHANMODES"][0]+self.IRC.supports["PREFIX"][0] and channel.modes[mode]])
- params=" ".join([channel.modes[mode] for mode in modes if mode in self.IRC.supports["CHANMODES"][1]+self.IRC.supports["CHANMODES"][2] and channel.modes[mode]])
- self.connection.send(":%s 324 %s %s +%s %s\n" % (self.IRC.server, self.IRC.identity.nick, channel.name, modestr, params))
- elif re.match("^\\+?[%s]+$"%self.IRC.supports["CHANMODES"][0], params) and extinfo=="":
- #print "ddd Mode List Request", params
- channel=self.IRC.channel(target)
- listnumerics=dict(b=(367, 368, "channel ban list"), e=(348, 349, "Channel Exception List"), I=(346, 347, "Channel Invite Exception List"), w=(910, 911, "Channel Access List"), g=(941, 940, "chanel spamfilter list"), X=(954, 953, "channel exemptchanops list"))
- redundant=[]
- for mode in params.lstrip("+"):
- if mode in redundant or mode not in listnumerics.keys(): continue
- i,e,l=listnumerics[mode]
- if mode in channel.modes.keys():
- for (mask, setby, settime) in channel.modes[mode]:
- self.connection.send(":%s %d %s %s %s %s %s\n" % (self.IRC.server, i, channel.context.identity.nick, channel.name, mask, setby, settime))
- self.connection.send(":%s %d %s %s :End of %s\n" % (self.IRC.server, e, channel.context.identity.nick, channel.name, l))
- redundant.append(mode)
- else:
- self.IRC.raw(line, origin=self)
- elif params=="" and target.lower()==self.IRC.identity.lower():
- self.connection.send(":%s 221 %s +%s\n" % (self.IRC.server, self.IRC.identity.nick, channel.name, self.IRC.identity.modes))
- if "s" in self.IRC.identity.modes and self.IRC.identity.snomask:
- self.connection.send(":%s 008 %s +%s :Server notice mask\n" % (self.IRC.server, self.IRC.identity.nick, channel.name, self.IRC.identity.snomask))
- else:
- self.IRC.raw(line, origin=self)
- else:
- self.IRC.raw(line, origin=self)
-
-
-
- continue
- #print line
- match=re.findall("^quit(?:\\s:?(.*))?$", line, re.I)
- #print match
- if match:
- quitmsg=match[0]
- break
- else:
- #match=re.findall("^ping(?:\\s.*:?(.*))?$", line)
- self.IRC.raw(line)
- match=re.findall("^PRIVMSG (\\S+) :(.*)$", line, re.I)
- #print match
- if match:
- (origin, ident, host, cmd, params)=(self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, "PRIVMSG", "")
- target, extinfo=match[0]
- data=(origin, ident, host, cmd, target, params, extinfo)
- modules=list(self.IRC.modules)
- for channel in self.IRC.channels:
- for module in channel.modules:
- if module not in modules: modules.append(module)
- for module in set(modules):
- if module!=self.bouncerlistener:
- #print ":%s!%s@%s %s"%(self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, line)
- try:
- module.process(self.IRC, ":%s!%s@%s %s"%(self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, line), data)
- except:
- pass
- else:
- for bouncer in self.bouncerlistener.connections:
- if bouncer!=self and bouncer.IRC==self.IRC:
- try:
- bouncer.connection.send(":%s!%s@%s %s\n"%(self.IRC.identity.nick, self.IRC.identity.idnt, self.IRC.identity.host, line))
- except:
- self.bouncerlistener.connections.remove(bouncer)
- self.connection.send("ERROR :Closing link: (%s@%s) [%s]\n" % (self.nick, self.addr[0], quitmsg))
- self.connection.close()
- self.bouncerlistener.connections.remove(self)
+../bouncer4.py \ No newline at end of file
diff --git a/cannon.py b/cannon.py
index 3af725e..1770271 100644..120000
--- a/cannon.py
+++ b/cannon.py
@@ -1,30 +1 @@
-#!/usr/bin/python
-
-import re, os
-
-class Cannon(object):
- def __init__(self):
- self.firecount={}
- def onRecv(self, IRC, line, data):
- if data==None: return
- (origin, ident, host, cmd, target, params, extinfo)=data
- if len(target) and target[0]=="#" and cmd=="PRIVMSG":
- channel=IRC.channel(target)
- matches=re.findall("^!fire\\s+(.*)$",extinfo)
- if matches:
- nickname=matches[0]
- if any([nickname.lower()==user.nick.lower() for user in channel.users]):
- user=IRC.user(nickname)
- if user in self.firecount.keys():
- count=self.firecount[user]+1
- else:
- count=1
- self.firecount[user]=count
- if 10<=count%100<20: ordinal="th"
- elif count%10==1: ordinal="st"
- elif count%10==2: ordinal="nd"
- elif count%10==3: ordinal="rd"
- else: ordinal="th"
- channel.me("fires %s out of a cannon for the %d%s time."%(user.nick, count, ordinal))
- else:
- channel.msg("%s: I cannot fire %s out of a cannon, as he or she is not here."%(origin, nickname))
+../cannon.py \ No newline at end of file
diff --git a/figlet.py b/figlet.py
index 8f88416..7d63417 100644..120000
--- a/figlet.py
+++ b/figlet.py
@@ -1,20 +1 @@
-#!/usr/bin/python
-import re, os
-
-class Figlet(object):
- def onRecv(self, IRC, line, data):
- if data==None: return
- (origin, ident, host, cmd, target, params, extinfo)=data
- if len(target) and target[0]=="#" and cmd=="PRIVMSG":
- channel=IRC.channel(target)
- matches=re.findall("^!figlet\\s+(.*)$",extinfo)
- if matches:
- gif,fig=os.popen2("figlet")
- gif.write(matches[0])
- gif.close()
- while True:
- line=fig.readline()
- if line=="": break
- if re.match("^\\s+$", line.rstrip()): continue
- channel.msg(line.rstrip())
- fig.close()
+../figlet.py \ No newline at end of file
diff --git a/irc.py b/irc.py
new file mode 120000
index 0000000..f2bc457
--- /dev/null
+++ b/irc.py
@@ -0,0 +1 @@
+../irc5.py \ No newline at end of file
diff --git a/logger.py b/logger.py
index 92d5157..e1dc8ce 100644..120000
--- a/logger.py
+++ b/logger.py
@@ -1,336 +1 @@
-#!/usr/bin/python
-
-from threading import Thread, Lock
-import re, time, sys, string, socket, os, platform, traceback, Queue, ssl, urllib2
-
-class LogRotate(Thread):
- def __init__(self, logger):
- self.logger=logger
- Thread.__init__(self)
- self.daemon=True
- self.start()
- def run(self):
- Y, M, D, h, m, s, w, d, dst=time.localtime()
- nextrotate=int(time.mktime((Y, M, D+1, 0, 0, 0, 0, 0, -1)))
- #print time.time()-nextrotate
- #print time.localtime(), time.localtime(nextrotate)
- while True:
- #print "LogRotate will sleep until %d/%d/%d %d:%02d:%02d"%(time.localtime(nextrotate)[:6])
- while nextrotate>time.time(): ### May need to do this in a loop in case the following time.sleep command wakes up a second too early.
- time.sleep(max(0.1, min((nextrotate-time.time(), 3600))))
- self.logger.rotatelock.acquire()
- if all([not log or log.closed for log in self.logger.consolelogs.values()+self.logger.channellogs.values()]):
- ### If there are no logs to rotate
- self.logger.logrotate=None
- self.logger.rotatelock.release()
- break
- self.logger.rotatelock.release()
- #print "Rotating Logs"
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- for IRC in self.logger.labels.keys():
- #IRC.loglock.acquire()
- if IRC.connected:
- IRC.lock.acquire()
- try:
- self.logger.rotateConsoleLog(IRC)
- if IRC.identity:
- for channel in IRC.identity.channels:
- self.logger.rotateChannelLog(channel)
- finally:
- IRC.lock.release()
- nextrotate+=3600*24
-
-class Logger(object):
- def __init__(self, logroot):
- self.logroot=logroot
- path=[logroot]
- #print path
- while not os.path.isdir(path[0]):
- split=os.path.split(path[0])
- path.insert(1,split[1])
- path[0]=split[0]
- #print path
- while len(path)>1:
- path[0]=os.path.join(*path[:2])
- del path[1]
- #print path
- os.mkdir(path[0])
- #return
- self.consolelogs={}
- self.channellogs={}
- self.labels={}
- self.rotatelock=Lock()
- self.logrotate=None
- def addNetworks(self, **IRCs): ### Deprecated
- for (label,IRC) in IRCs.items():
- if not os.path.isdir(os.path.join(self.logroot, label)):
- os.mkdir(os.path.join(self.logroot, label))
- if label in self.labels.values():
- raise BaseException, "Label already exists"
- if IRC in self.IRCs.keys():
- raise BaseException, "Network already exists"
- for (label,IRC) in IRCs.items():
- self.labels[IRC]=label
- IRC.lock.acquire()
- IRC.modules.append(self)
- if IRC.connected:
- self.openConsoleLog(IRC)
- IRC.lock.release()
- def removeNetworks(self, *IRCs): ### Deprecated
- for IRC in IRCs:
- if IRC not in self.IRCs.keys():
- raise BaseException, "Network not added"
- for IRC in IRCs:
- IRC.lock.acquire()
- IRC.modules.append(self)
- if IRC.connected:
- self.closeConsoleLog(IRC)
- IRC.lock.release()
- del self.labels[IRC]
- del self.consolelogs[IRC]
-
- def onModuleAdd(self, IRC, label):
- if label in self.labels.values():
- raise BaseException, "Label already exists"
- if IRC in self.labels.keys():
- raise BaseException, "Network already exists"
- if not os.path.isdir(os.path.join(self.logroot, label)):
- os.mkdir(os.path.join(self.logroot, label))
- IRC.lock.acquire()
- self.labels[IRC]=label
- if IRC.connected:
- self.openConsoleLog(IRC)
- if IRC.identity:
- for channel in IRC.identity.channels:
- self.openChannelLog(channel)
- IRC.lock.release()
-
- def onModuleRem(self, IRC):
- IRC.lock.acquire()
- if IRC.connected:
- for channel in self.channellogs.keys():
- if channel in IRC.channels:
- if not self.channellogs[channel].closed: self.closeChannelLog(channel)
- del self.channellogs[channel]
- if not self.consolelogs[IRC].closed: self.closeConsoleLog(IRC)
- IRC.lock.release()
- del self.labels[IRC]
- del self.consolelogs[IRC]
-
- def openConsoleLog(self, IRC):
- self.rotatelock.acquire()
- if not self.logrotate or not self.logrotate.isAlive():
- self.logrotate=LogRotate(self)
- self.rotatelock.release()
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- self.consolelogs[IRC]=open(os.path.join(self.logroot, self.labels[IRC], "console-%04d.%02d.%02d.log"%now[:3]), "a")
- print >>self.consolelogs[IRC], "%s %s ### Log session started" % (timestamp, time.tzname[now[-1]])
- self.consolelogs[IRC].flush()
- def closeConsoleLog(self, IRC):
- if IRC in self.consolelogs.keys() and type(self.consolelogs[IRC])==file and not self.consolelogs[IRC].closed:
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- print >>self.consolelogs[IRC], "%s %s ### Log session ended" % (timestamp, time.tzname[now[-1]])
- self.consolelogs[IRC].close()
- def rotateConsoleLog(self, IRC):
- self.closeConsoleLog(IRC)
- self.openConsoleLog(IRC)
-
- def openChannelLog(self, channel):
- self.rotatelock.acquire()
- if not self.logrotate or not self.logrotate.isAlive():
- self.logrotate=LogRotate(self)
- self.logrotate.daemon=True
- self.logrotate.start()
- self.rotatelock.release()
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- label=self.labels[channel.context]
- self.channellogs[channel]=open(os.path.join(self.logroot, label, "channel-%s-%04d.%02d.%02d.log"%((urllib2.quote(channel.name.lower()).replace("/","%2f"),)+now[:3])), "a")
- print >>self.channellogs[channel], "%s %s ### Log session started" % (timestamp, time.tzname[now[-1]])
- self.channellogs[channel].flush()
- if channel.context.identity in channel.users:
- if channel.topic: print >>self.channellogs[channel], "%s %s <<< :%s 332 %s %s :%s" % (timestamp, time.tzname[now[-1]], channel.context.serv, channel.context.identity.nick, channel.name, channel.topic)
- if channel.topicsetby and channel.topictime: print >>self.channellogs[channel], "%s %s <<< :%s 333 %s %s %s %s" % (timestamp, time.tzname[now[-1]], channel.context.serv, channel.context.identity.nick, channel.name, channel.topicsetby, channel.topictime)
- if channel.users:
- secret="s" in channel.modes.keys() and channel.modes["s"]
- private="p" in channel.modes.keys() and channel.modes["p"]
- namesusers=[]
- modes, symbols=channel.context.supports["PREFIX"]
- print >>self.channellogs[channel], "%s %s <<< :%s 353 %s %s %s :%s" % (timestamp, time.tzname[now[-1]],
- channel.context.serv,
- channel.context.identity.nick,
- "@" if secret else ("*" if private else "="),
- channel.name,
- " ".join(["".join([symbols[k] if modes[k] in channel.modes.keys() and user in channel.modes[modes[k]] else "" for k in xrange(len(modes))])+user.nick for user in channel.users]))
- if channel.modes:
- modes=channel.modes.keys()
- modestr="".join([mode for mode in modes if mode not in channel.context.supports["CHANMODES"][0]+channel.context.supports["PREFIX"][0] and channel.modes[mode]])
- params=" ".join([channel.modes[mode] for mode in modes if mode in channel.context.supports["CHANMODES"][1]+channel.context.supports["CHANMODES"][2] and channel.modes[mode]])
- print >>self.channellogs[channel], "%s %s <<< :%s 324 %s %s +%s %s" % (timestamp, time.tzname[now[-1]], channel.context.server, channel.context.identity.nick, channel.name, modestr, params)
- if channel.created: print >>self.channellogs[channel], "%s %s <<< :%s 329 %s %s %s" % (timestamp, time.tzname[now[-1]], channel.context.serv, channel.context.identity.nick, channel.name, channel.created)
- self.channellogs[channel].flush()
- def closeChannelLog(self, channel):
- if channel in self.channellogs.keys() and type(self.channellogs[channel])==file and not self.channellogs[channel].closed:
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- print >>self.channellogs[channel], "%s %s ### Log session ended" % (timestamp, time.tzname[now[-1]])
- self.channellogs[channel].close()
- def rotateChannelLog(self, channel):
- self.closeChannelLog(channel)
- self.openChannelLog(channel)
- def onRecv(self, IRC, line, data):
- modemapping=dict(Y="ircop", q="owner", a="admin", o="op", h="halfop", v="voice")
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- if data==None:
- print >>self.consolelogs[IRC], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.consolelogs[IRC].flush()
- return
- (origin, ident, host, cmd, target, params, extinfo)=data
- if re.match("^\\d+$",cmd): cmd=int(cmd)
- if cmd in (324, 329):
- modeparams=params.split()
- channame=modeparams[0]
- channel=IRC.channel(channame)
- if channel in self.channellogs.keys() and not self.channellogs[channel].closed: log=self.channellogs[channel]
- else: log=self.consolelogs[IRC]
- print >>log, "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- log.flush()
- elif cmd == 332:
- channel=IRC.channel(params)
- if channel in self.channellogs.keys() and not self.channellogs[channel].closed: log=self.channellogs[channel]
- else: log=self.consolelogs[IRC]
- print >>log, "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- log.flush()
- elif cmd == 333:
- (channame,nick,dt)=params.split()
- channel=IRC.channel(channame)
- if not self.channellogs[channel].closed:
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- elif cmd == 353:
- (flag, channame)=params.split()
- channel=IRC.channel(channame)
- if not self.channellogs[channel].closed:
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- elif cmd=="JOIN":
- user=IRC.user(origin)
- channel=IRC.channel(target if len(target) else extinfo)
- if user==IRC.identity:
- self.openChannelLog(channel)
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- elif cmd == "PRIVMSG":
- if target and target[0] in IRC.supports["CHANTYPES"]:
- channel=IRC.channel(target)
- if ident and host:
- user=IRC.user(origin)
- classes=" ".join([modemapping[mode] for mode in IRC.supports["PREFIX"][0] if mode in channel.modes.keys() and user in channel.modes[mode]])
- else:
- classes="server"
- if classes: print >>self.channellogs[channel], "%s %s %s <<< %s" % (timestamp, time.tzname[now[-1]], classes, line)
- else: print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- elif cmd == "NOTICE":
- if target and (target[0] in IRC.supports["CHANTYPES"] or (len(target)>1 and target[0] in IRC.supports["PREFIX"][1] and target[1] in IRC.supports["CHANTYPES"])):
- if target[0] in IRC.supports["PREFIX"][1]:
- channel=IRC.channel(target[1:])
- else:
- channel=IRC.channel(target)
- if ident and host:
- user=IRC.user(origin)
- classes=" ".join([modemapping[mode] for mode in IRC.supports["PREFIX"][0] if mode in channel.modes.keys() and user in channel.modes[mode]])
- else:
- classes="server"
- if classes: print >>self.channellogs[channel], "%s %s %s <<< %s" % (timestamp, time.tzname[now[-1]], classes, line)
- else: print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- elif target.lower()==IRC.identity.nick.lower() and not ident and not host:
- print >>self.consolelogs[IRC], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.consolelogs[IRC].flush()
- elif cmd == "TOPIC":
- channel=IRC.channel(target)
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- elif cmd == "PART":
- user=IRC.user(origin)
- channel=IRC.channel(target)
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- if user==IRC.identity:
- self.closeChannelLog(channel)
- elif cmd == "KICK":
- kicked=IRC.user(params)
- channel=IRC.channel(target)
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- if kicked==IRC.identity:
- self.closeChannelLog(channel)
- elif cmd == "MODE":
- if target and target[0] in IRC.supports["CHANTYPES"]:
- channel=IRC.channel(target)
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- else:
- print >>self.consolelogs[IRC], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.consolelogs[IRC].flush()
- elif cmd in ("NICK", "QUIT"):
- user=IRC.user(origin)
- for channel in user.channels:
- print >>self.channellogs[channel], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.channellogs[channel].flush()
- else:
- print >>self.consolelogs[IRC], "%s %s <<< %s" % (timestamp, time.tzname[now[-1]], line)
- self.consolelogs[IRC].flush()
- def onConnectAttempt(self, IRC):
- if IRC not in self.consolelogs.keys() or (not self.consolelogs[IRC]) or self.consolelogs[IRC].closed:
- self.openConsoleLog(IRC)
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- print >>self.consolelogs[IRC], "%s %s *** Attempting connection to %s:%s." % (timestamp, time.tzname[now[-1]], IRC.server, IRC.port)
- def onConnect(self, IRC):
- if IRC not in self.consolelogs.keys() or (not self.consolelogs[IRC]) or self.consolelogs[IRC].closed:
- self.openConsoleLog(IRC)
- #self.openConsoleLog(IRC)
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- print >>self.consolelogs[IRC], "%s %s *** Connection to %s:%s established." % (timestamp, time.tzname[now[-1]], IRC.server, IRC.port)
- def onDisconnect(self, IRC):
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- for channel in IRC.identity.channels:
- print >>self.channellogs[channel], "%s %s *** Connection to %s:%s terminated." % (timestamp, time.tzname[now[-1]], IRC.server, IRC.port)
- self.channellogs[channel].flush()
- self.closeChannelLog(channel)
- print >>self.consolelogs[IRC], "%s %s *** Connection %s:%s terminated." % (timestamp, time.tzname[now[-1]], IRC.server, IRC.port)
- self.consolelogs[IRC].flush()
- self.closeConsoleLog(IRC)
- def onSend(self, IRC, line, data, origin):
- modemapping=dict(Y="ircop", q="owner", a="admin", o="op", h="halfop", v="voice")
- now=time.localtime()
- timestamp=reduce(lambda x,y: x+":"+y,[str(t).rjust(2,"0") for t in now[0:6]])
- (cmd, target, params, extinfo)=data
- if IRC.registered and cmd=="PRIVMSG" and "CHANTYPES" in IRC.supports.keys() and len(target) and target[0] in IRC.supports["CHANTYPES"]:
- channel=IRC.channel(target)
- if channel in IRC.identity.channels:
- classes=" ".join([modemapping[mode] for mode in IRC.supports["PREFIX"][0] if mode in channel.modes.keys() and IRC.identity in channel.modes[mode]])
- if classes: print >>self.channellogs[channel], "%s %s %s >>> :%s!%s@%s %s" % (timestamp, time.tzname[now[-1]], classes, IRC.identity.nick, IRC.identity.idnt, IRC.identity.host, line)
- else: print >>self.channellogs[channel], "%s %s >>> :%s!%s@%s %s" % (timestamp, time.tzname[now[-1]], IRC.identity.nick, IRC.identity.idnt, IRC.identity.host, line)
- self.channellogs[channel].flush()
- else:
- print >>self.consolelogs[IRC], "%s %s >>> :%s!%s@%s %s" % (timestamp, time.tzname[now[-1]], IRC.identity.nick, IRC.identity.idnt, IRC.identity.host, line)
- self.consolelogs[IRC].flush()
- if IRC.registered and len(target) and (target[0] in IRC.supports["CHANTYPES"] or (len(target)>1 and target[0] in IRC.supports["PREFIX"][1] and target[1] in IRC.supports["CHANTYPES"])) and cmd=="NOTICE":
- channel=IRC.channel(target[1:] if target[0] in IRC.supports["PREFIX"][1] else target)
- if channel in IRC.identity.channels:
- classes=" ".join([modemapping[mode] for mode in IRC.supports["PREFIX"][0] if mode in channel.modes.keys() and IRC.identity in channel.modes[mode]])
- if classes: print >>self.channellogs[channel], "%s %s %s >>> :%s!%s@%s %s" % (timestamp, time.tzname[now[-1]], classes, IRC.identity.nick, IRC.identity.idnt, IRC.identity.host, line)
- else: print >>self.channellogs[channel], "%s %s >>> :%s!%s@%s %s" % (timestamp, time.tzname[now[-1]], IRC.identity.nick, IRC.identity.idnt, IRC.identity.host, line)
- self.channellogs[channel].flush()
- else:
- print >>self.consolelogs[IRC], "%s %s >>> :%s!%s@%s %s" % (timestamp, time.tzname[now[-1]], IRC.identity.nick, IRC.identity.idnt, IRC.identity.host, line)
- self.consolelogs[IRC].flush()
+../logger.py \ No newline at end of file
diff --git a/wallet.py b/wallet.py
index 05321f0..eba4a72 100644..120000
--- a/wallet.py
+++ b/wallet.py
@@ -1,37 +1 @@
-#!/usr/bin/python
-import pickle, Crypto.Cipher.Blowfish, os, getpass
-from threading import Lock
-
-class Wallet(dict):
- def __init__(self, filename):
- self.lock=Lock()
- if os.path.isfile(filename):
- self.f=open(filename,"rb+")
- self.passwd=getpass.getpass()
- self.crypt=Crypto.Cipher.Blowfish.new(self.passwd)
- contents_encrypted=self.f.read()
- contents=self.crypt.decrypt(contents_encrypted+"\x00"*((-len(contents_encrypted))%8))
- if contents.startswith(self.passwd):
- self.update(dict(pickle.loads(contents[len(self.passwd):])))
- else:
- self.f.close()
- raise BaseException, "Incorrect Password"
- else:
- self.f=open(filename,"wb+")
- passwd=self.passwd=None
- while passwd==None or passwd!=self.passwd:
- passwd=getpass.getpass("Enter new password: ")
- self.passwd=getpass.getpass("Confirm new password: ")
- if passwd!=self.passwd: print "Passwords do not match!"
- self.crypt=Crypto.Cipher.Blowfish.new(self.passwd)
- self.flush()
- def flush(self):
- contents=self.passwd+pickle.dumps(self.items(), protocol=2)
- self.lock.acquire()
- try:
- self.f.seek(0)
- self.f.write(self.crypt.encrypt(contents+"\x00"*((-len(contents))%8)))
- self.f.truncate()
- self.f.flush()
- finally:
- self.lock.release()
+../wallet.py \ No newline at end of file