Cleaned up the code
This commit is contained in:
parent
49a485c347
commit
ad48a0695b
194
botcmd.py
194
botcmd.py
|
@ -9,12 +9,15 @@ concmd=['/q','/lt','/st','/lg']
|
|||
blacklist = ['bslsk05']
|
||||
|
||||
doctor = eliza.eliza()
|
||||
|
||||
trusted = []
|
||||
trustedlock = threading.Lock()
|
||||
gods = []
|
||||
godslock = threading.Lock()
|
||||
|
||||
msgs = {}
|
||||
msglock=threading.Lock()
|
||||
msgslock = threading.Lock()
|
||||
|
||||
authcheck = {}
|
||||
authchecklock = threading.Lock()
|
||||
|
||||
|
@ -27,14 +30,17 @@ class Cron(threading.Thread):
|
|||
self.cronctrl = []
|
||||
self.cronctrllock = threading.Lock()
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def queuejob(self, time, fn):
|
||||
self.timedjobslock.acquire()
|
||||
self.timedjobs.append((time, fn))
|
||||
self.timedjobslock.release()
|
||||
|
||||
def ctrl(self, cmd):
|
||||
self.cronctrllock.acquire()
|
||||
self.cronctrl.append(cmd)
|
||||
self.cronctrllock.release()
|
||||
|
||||
def run(self):
|
||||
run = True
|
||||
while run:
|
||||
|
@ -56,61 +62,105 @@ class Cron(threading.Thread):
|
|||
for fn in torun:
|
||||
fn()
|
||||
|
||||
msglock.acquire()
|
||||
cron=Cron()
|
||||
cron.start()
|
||||
|
||||
def loadmessages():
|
||||
global msgs, msgslock
|
||||
|
||||
msgslock.acquire()
|
||||
f = open('msgs.txt', 'r')
|
||||
|
||||
for line in f:
|
||||
while len(line)>0 and line[-1]=='\n': line=line[:-1]
|
||||
while len(line) > 0 and line[-1] == '\n':
|
||||
line = line[:-1]
|
||||
if len(line) > 0:
|
||||
receiver, sender, msg = line.split('\t')
|
||||
if receiver not in msgs:
|
||||
msgs[receiver] = []
|
||||
msgs[receiver].append((sender, msg))
|
||||
f.close()
|
||||
msglock.release()
|
||||
|
||||
cron=Cron()
|
||||
cron.start()
|
||||
f.close()
|
||||
msgslock.release()
|
||||
|
||||
def savemessages():
|
||||
global msgs, msgslock
|
||||
|
||||
msgslock.acquire()
|
||||
f=open('msgs.txt', 'w')
|
||||
|
||||
for receiver in msgs:
|
||||
for sender, msg in msgs[receiver]:
|
||||
f.write('%s\t%s\t%s\n' % (receiver, sender, msg))
|
||||
|
||||
f.close()
|
||||
msgslock.release()
|
||||
|
||||
loadmessages()
|
||||
|
||||
def addtrusted(nick):
|
||||
global trusted, trustedlock
|
||||
|
||||
trustedlock.acquire()
|
||||
|
||||
if nick not in trusted:
|
||||
trusted.append(nick)
|
||||
|
||||
trustedlock.release()
|
||||
|
||||
def rmtrusted(nick):
|
||||
global trusted, trustedlock
|
||||
|
||||
trustedlock.acquire()
|
||||
|
||||
if nick in trusted:
|
||||
trusted.remove(nick)
|
||||
|
||||
trustedlock.release()
|
||||
|
||||
def loadtrusted():
|
||||
global trusted, trustedlock
|
||||
|
||||
trustedlock.acquire()
|
||||
while len(trusted)>0: trusted.pop() #I'm really sorry but trusted=[] created trusted as local variable
|
||||
trusted = []
|
||||
trustedlock.release()
|
||||
|
||||
f=open('trusted.txt', 'r')
|
||||
|
||||
for line in f:
|
||||
while len(line)>0 and line[-1]=='\n': line=line[:-1]
|
||||
while len(line) > 0 and line[-1] == '\n':
|
||||
line = line[:-1]
|
||||
if len(line) > 0:
|
||||
addtrusted(line)
|
||||
|
||||
f.close()
|
||||
|
||||
def loadgods():
|
||||
global gods, godslock
|
||||
|
||||
godslock.acquire()
|
||||
while len(gods)>0: gods.pop() #See above
|
||||
gods = []
|
||||
f=open('gods.txt', 'r')
|
||||
|
||||
for line in f:
|
||||
while len(line)>0 and line[-1]=='\n': line=line[:-1]
|
||||
while len(line) > 0 and line[-1] == '\n':
|
||||
line = line[:-1]
|
||||
if len(line) > 0:
|
||||
gods.append(line)
|
||||
addtrusted(line)
|
||||
|
||||
f.close()
|
||||
godslock.release()
|
||||
|
||||
def savetrusted():
|
||||
global trusted, trustedlock
|
||||
|
||||
trustedlock.acquire()
|
||||
f=open('trusted.txt', 'w')
|
||||
|
||||
for i in trusted:
|
||||
f.write(i+'\n')
|
||||
|
||||
f.close
|
||||
trustedlock.release()
|
||||
|
||||
|
@ -137,12 +187,14 @@ def istrusted(nick):
|
|||
|
||||
def initauthcheck(nick):
|
||||
global authcheck, authchecklock
|
||||
|
||||
authchecklock.acquire()
|
||||
authcheck[nick] = None
|
||||
authchecklock.release()
|
||||
|
||||
def setauthcheckstate(nick, state):
|
||||
global authcheck, authchecklock
|
||||
|
||||
authchecklock.acquire()
|
||||
if nick in authcheck:
|
||||
authcheck[nick] = state
|
||||
|
@ -150,14 +202,17 @@ def setauthcheckstate(nick, state):
|
|||
|
||||
def getauthcheckstate(nick):
|
||||
global authcheck, authchecklock
|
||||
|
||||
authchecklock.acquire()
|
||||
if nick in authcheck:
|
||||
state = authcheck[nick]
|
||||
authchecklock.release()
|
||||
|
||||
return state
|
||||
|
||||
def removeauthcheck(nick):
|
||||
global authcheck, authchecklock
|
||||
|
||||
authchecklock.acquire()
|
||||
if nick in authcheck:
|
||||
del authcheck[nick]
|
||||
|
@ -294,6 +349,11 @@ def parsecmd(line, args):
|
|||
return out
|
||||
|
||||
def parse((line, irc)):
|
||||
global blacklist
|
||||
global msgs, msgslock
|
||||
global trusted, trustedlock, gods, godslock
|
||||
global doctor, die_expr
|
||||
|
||||
line = line.split(' ')
|
||||
nick = line[0].split('!')[0][1:]
|
||||
chan = line[2] if line[2][0] == '#' else nick
|
||||
|
@ -309,9 +369,9 @@ def parse((line,irc)):
|
|||
cmdline = [line[3][1:]] + line[4:]
|
||||
while '' in cmdline:
|
||||
cmdline.remove('')
|
||||
|
||||
if matchcmd(cmdline, '#echo'):
|
||||
text = parsecmd(cmdline, '{text}')
|
||||
print text #debg
|
||||
irc.msg(chan, zwsp+text)
|
||||
elif matchcmd(cmdline, '#op'):
|
||||
args = parsecmd(cmdline, '{args}')
|
||||
|
@ -344,11 +404,11 @@ def parse((line,irc)):
|
|||
elif matchcmd(cmdline, '#msg'):
|
||||
if matchcmd(cmdline, '#msg', 'nick {message}'):
|
||||
msgnick, message = parsecmd(cmdline, 'nick {message}')
|
||||
msglock.acquire()
|
||||
msgslock.acquire()
|
||||
if msgnick not in msgs:
|
||||
msgs[msgnick] = []
|
||||
msgs[msgnick].append((nick, message))
|
||||
msglock.release()
|
||||
msgslock.release()
|
||||
else:
|
||||
irc.msg(chan, 'Usage: #msg nick message')
|
||||
elif matchcmd(cmdline, '#trusted?'):
|
||||
|
@ -431,38 +491,24 @@ def parse((line,irc)):
|
|||
if line[5] == '0':
|
||||
irc.msg(line[3][1:], 'Register account with NickServ')
|
||||
elif line[5] == '1':
|
||||
irc.msg(line[3][1:], 'PRIVMSG %s :Identify with NickServ')
|
||||
irc.msg(line[3][1:], 'Identify with NickServ')
|
||||
else:
|
||||
irc.msg(line[3][1:], 'WTF, NickServ returned %s'+line[5])
|
||||
irc.msg(line[3][1:], 'WTF, NickServ returned %s' % line[5])
|
||||
elif line[1] == 'INVITE' and line[2] == irc.nick and line[3][1:] in irc.chan.split(' '):
|
||||
if isauthorized(irc, nick):
|
||||
irc.send('JOIN ' + line[3])
|
||||
elif line[1] == '482':
|
||||
irc.msg(line[3], 'Not op')
|
||||
#elif line[1]=='332' or line[1]=='TOPIC':
|
||||
# if line[1]=='332':
|
||||
# ch=line[3]
|
||||
# tp=' '.join(line[4:])[1:]
|
||||
# elif line[1]=='TOPIC':
|
||||
# ch=line[2]
|
||||
# tp=' '.join(line[3:])[1:]
|
||||
# #Do the magic here
|
||||
|
||||
msglock.acquire()
|
||||
msgslock.acquire()
|
||||
if (line[1] == 'PRIVMSG' or line[1] == 'JOIN') and nick in msgs:
|
||||
for sender, msg in msgs.pop(nick):
|
||||
irc.msg(nick, '<%s> %s' % (sender, msg))
|
||||
msglock.release()
|
||||
msgslock.release()
|
||||
|
||||
def execcmd(cmdline):
|
||||
if cmdline[0] == '/q':
|
||||
msglock.acquire()
|
||||
f=open('msgs.txt','w')
|
||||
for receiver in msgs:
|
||||
for sender, msg in msgs[receiver]:
|
||||
f.write('%s\t%s\t%s\n'%(receiver,sender,msg))
|
||||
f.close()
|
||||
msglock.release()
|
||||
savemessages()
|
||||
savetrusted()
|
||||
|
||||
cron.ctrl('QUIT')
|
||||
|
@ -473,38 +519,54 @@ def execcmd(cmdline):
|
|||
elif cmdline[0] == '/lg':
|
||||
loadgods()
|
||||
|
||||
def help(cmd):
|
||||
if cmd=='':
|
||||
return '#echo #op #deop #voice #devoice #kick #src #msg #trusted? #trust #untrust #ls-trusted #invite #help'
|
||||
elif cmd=='#echo':
|
||||
return '#echo text echo text back'
|
||||
elif cmd=='#op':
|
||||
return '#op [nick] give nick or yourself op rights in case you are trusted by oonbotti2 and identified with NickServ'
|
||||
elif cmd=='#deop':
|
||||
return '#deop [nick] remove your/nick\'s op rights'
|
||||
elif cmd=='#voice':
|
||||
return '#voice [nick] give nick or yourself voice in case you are trusted by oonbotti2 and identified with NickServ'
|
||||
elif cmd=='#devoice':
|
||||
return '#devoice [nick] remove your or nick\'s voice in case you are trusted by oonbotti2 and identified with NickServ'
|
||||
elif cmd=='#kick':
|
||||
return '#kick nick reason kicks nick with specified reason'
|
||||
elif cmd=='#src':
|
||||
return '#src paste a link to oonbotti2\'s git repo'
|
||||
elif cmd=='#msg':
|
||||
return '#msg nick message send a message to nick'
|
||||
elif cmd=='#trusted?':
|
||||
return '#trusted? [nick] tell you if nick or yourself is trusted by oonbotti2'
|
||||
elif cmd=='#trust':
|
||||
return '#trust nick add nick to trusted list'
|
||||
elif cmd=='#untrust':
|
||||
return '#untrust nick remove nick from trusted list'
|
||||
elif cmd=='#ls-trusted':
|
||||
return '#ls-trusted list nicks that are trusted. use only in a query'
|
||||
elif cmd=='#invite':
|
||||
return '#invite nick invites nick to channel'
|
||||
elif cmd=='#help':
|
||||
return '#help [command] give short info of command or list commands'
|
||||
elif cmd=='me':
|
||||
return 'I shall.'
|
||||
def usage(cmd, message = True):
|
||||
usage = {'#echo': 'text',
|
||||
'#op': '[nick]',
|
||||
'#deop': '[nick]',
|
||||
'#voice': '[nick]',
|
||||
'#devoice': '[nick]',
|
||||
'#kick': 'nick [reason]',
|
||||
'#src': '',
|
||||
'#msg': 'nick message',
|
||||
'#trusted?': 'nick',
|
||||
'#trust': 'nick',
|
||||
'#untrust': 'nick',
|
||||
'#ls-trusted': '',
|
||||
'#invite': 'nick',
|
||||
'#help': '[command]'}
|
||||
|
||||
if cmd in usage:
|
||||
if message:
|
||||
return 'Usage: %s %s' % (cmd, usage[cmd])
|
||||
else:
|
||||
return usage[cmd]
|
||||
else:
|
||||
return None
|
||||
|
||||
def help(cmd):
|
||||
helptext = {'#echo': '#echo text back',
|
||||
'#op': 'give nick or yourself op rights in case you are trusted by oonbotti2 and identified with NickServ',
|
||||
'#deop': 'remove your/nick\'s op rights',
|
||||
'#voice': 'give nick or yourself voice in case you are trusted by oonbotti2 and identified with NickServ',
|
||||
'#devoice': 'remove your or nick\'s voice in case you are trusted by oonbotti2 and identified with NickServ',
|
||||
'#kick': 'kicks nick with specified reason',
|
||||
'#src': 'paste a link to oonbotti2\'s git repo',
|
||||
'#msg': 'send a message to nick',
|
||||
'#trusted?': 'tell you if nick or yourself is trusted by oonbotti2',
|
||||
'#trust': 'add nick to trusted list',
|
||||
'#untrust': 'remove nick from trusted list',
|
||||
'#ls-trusted': 'list nicks that are trusted. use only in a query',
|
||||
'#invite': 'invites nick to channel',
|
||||
'#help': 'give short info of command or list commands'}
|
||||
|
||||
if cmd=='':
|
||||
return '#echo #op #deop #voice #devoice #kick #src #msg #trusted? #trust #untrust #ls-trusted #invite #help'
|
||||
elif cmd=='me':
|
||||
return 'I shall.'
|
||||
elif cmd in helptext:
|
||||
if helptext[cmd]:
|
||||
return '%s %s %s' % (cmd, usage(cmd, False), helptext[cmd])
|
||||
else:
|
||||
return '%s %s' % (cmd, usage(cmd, False))
|
||||
else:
|
||||
return None
|
||||
|
|
47
ircbot.py
47
ircbot.py
|
@ -10,20 +10,25 @@ class Channel:
|
|||
def __init__(self):
|
||||
self.lock = threading.Lock()
|
||||
self.msg = []
|
||||
|
||||
def send(self, msg):
|
||||
self.lock.acquire()
|
||||
self.msg.append(msg)
|
||||
self.lock.release()
|
||||
|
||||
def recv(self, wait = True):
|
||||
while True:
|
||||
self.lock.acquire()
|
||||
|
||||
if len(self.msg) > 0:
|
||||
msg = self.msg.pop(0)
|
||||
self.lock.release()
|
||||
return msg
|
||||
|
||||
if not wait:
|
||||
self.lock.release()
|
||||
return None
|
||||
|
||||
self.lock.release()
|
||||
time.sleep(0.1)
|
||||
|
||||
|
@ -45,6 +50,7 @@ class Irc:
|
|||
class Connhandler(threading.Thread):
|
||||
def __init__(self, server, port, chan, nick, botname, inpc, logc):
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
self.server = server
|
||||
self.port = port
|
||||
self.nick = nick
|
||||
|
@ -52,12 +58,16 @@ class Connhandler(threading.Thread):
|
|||
self.chan = chan
|
||||
self.inpc = inpc
|
||||
self.logc = logc
|
||||
|
||||
def send(self, s):
|
||||
s = s.replace('\n', '\\n').replace('\r', '\\r') # Sanitize output
|
||||
if len(s)>512: s=s[:512]
|
||||
if len(s) > 512:
|
||||
s = s[:512]
|
||||
|
||||
self.sock.send(s + '\r\n')
|
||||
if s.split(' ')[0] != 'PONG':
|
||||
self.logc.send(s + '\n')
|
||||
|
||||
def check(self, line):
|
||||
args = line.split(' ')
|
||||
if args[0] == 'PING':
|
||||
|
@ -80,20 +90,25 @@ class Connhandler(threading.Thread):
|
|||
self.sock = None
|
||||
continue
|
||||
break
|
||||
|
||||
if self.sock is None:
|
||||
self.logc.send('QUIT');
|
||||
sys.exit(1);
|
||||
|
||||
self.sock.settimeout(0.1)
|
||||
|
||||
self.send('NICK %s' % self.nick)
|
||||
self.send('USER %s a a :%s' % (self.nick, self.name))
|
||||
|
||||
f = open('startcmd.txt', 'r')
|
||||
for i in f:
|
||||
if i[-1]=='\n': i=i[:-1]
|
||||
if i[-1] == '\n':
|
||||
i = i[:-1]
|
||||
self.send(i)
|
||||
f.close()
|
||||
|
||||
for i in self.chan.split(' '):
|
||||
self.send('JOIN %s'%(i))
|
||||
self.send('JOIN %s' % i)
|
||||
|
||||
buf = ''
|
||||
while True:
|
||||
|
@ -103,6 +118,7 @@ class Connhandler(threading.Thread):
|
|||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
cmd = self.inpc.recv(wait = False)
|
||||
if cmd == 'QUIT':
|
||||
data = None
|
||||
|
@ -110,33 +126,43 @@ class Connhandler(threading.Thread):
|
|||
break
|
||||
elif cmd:
|
||||
self.send(cmd)
|
||||
|
||||
time.sleep(0.1)
|
||||
if not data: break
|
||||
|
||||
if not data:
|
||||
break
|
||||
|
||||
buf += data
|
||||
buf = buf.split('\n')
|
||||
for line in buf[:-1]:
|
||||
if line[-1]=='\r': line=line[:-1]
|
||||
if line[-1] == '\r':
|
||||
line = line[:-1]
|
||||
self.check(line)
|
||||
buf = buf[-1]
|
||||
|
||||
self.sock.close()
|
||||
|
||||
class Keyhandler(threading.Thread):
|
||||
def __init__(self, outc):
|
||||
self.outc = outc
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
line = raw_input()
|
||||
c=line.split(' ')
|
||||
if c == '':
|
||||
if line == '':
|
||||
continue
|
||||
|
||||
c = line.split(' ')
|
||||
if c[0] in botcmd.concmd:
|
||||
botcmd.execcmd(c)
|
||||
if c[0] == '/j' and len(c) == 2:
|
||||
self.outc.send('JOIN ' + c[1])
|
||||
elif c[0] == '/m' and len(c) > 2:
|
||||
self.outc.send('PRIVMSG %s :%s' % (c[1], ' '.join(c[2:])))
|
||||
elif c[0]=='/q' and len(c)==1:
|
||||
elif c[0] == '/q':
|
||||
if len(c) > 1:
|
||||
self.outc.send('QUIT :%s' % ' '.join(c[1:]))
|
||||
self.outc.send('QUIT')
|
||||
break
|
||||
elif c[0][0] == '/' and c[0] not in botcmd.concmd:
|
||||
|
@ -146,10 +172,12 @@ class Loghandler(threading.Thread):
|
|||
def __init__(self, inpc):
|
||||
self.inpc = inpc
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
s = self.inpc.recv()
|
||||
if s=='QUIT': break
|
||||
if s == 'QUIT':
|
||||
break
|
||||
sys.stdout.write(''.join([i if ord(i)>=32 or i=='\n' else '^'+chr(ord(i)+64) for i in s]))
|
||||
|
||||
class Threadwrapper(threading.Thread):
|
||||
|
@ -157,6 +185,7 @@ class Threadwrapper(threading.Thread):
|
|||
self.func = func
|
||||
self.arg = arg
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def run(self):
|
||||
self.func(self.arg)
|
||||
|
||||
|
|
Loading…
Reference in New Issue