oonbotti2/botcmd.py

273 lines
8.0 KiB
Python
Raw Normal View History

2013-06-29 22:18:56 +00:00
import eliza
2013-07-06 21:21:27 +00:00
import threading
2013-09-03 12:50:54 +00:00
import random
2013-06-29 22:18:56 +00:00
2013-09-25 15:23:38 +00:00
concmd=['/q','/lt','/st','/lg']
2013-06-29 22:18:56 +00:00
doctor=eliza.eliza()
trusted=[]
trustedlock=threading.Lock()
2013-09-25 15:23:38 +00:00
gods=[]
godslock=threading.Lock()
msgs={}
2013-07-06 21:21:27 +00:00
msglock=threading.Lock()
authcmds={}
authcmdlock=threading.Lock()
2013-09-25 15:23:38 +00:00
authfuncs={}
authfunclock=threading.Lock()
2013-06-29 22:01:33 +00:00
msglock.acquire()
f=open('msgs.txt','r')
for line in f:
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()
2013-09-25 15:23:38 +00:00
def addtrusted(nick):
trustedlock.acquire()
if nick not in trusted:
trusted.append(nick)
trustedlock.release()
def rmtrusted(nick):
trustedlock.acquire()
if nick in trusted:
2013-09-25 15:27:48 +00:00
trusted.remove(nick)
2013-09-25 15:23:38 +00:00
trustedlock.release()
def loadtrusted():
trustedlock.acquire()
while len(trusted)>0: trusted.pop() #I'm really sorry but trusted=[] created trusted as local variable
2013-09-25 15:23:38 +00:00
trustedlock.release()
f=open('trusted.txt','r')
for line in f:
while len(line)>0 and line[-1]=='\n': line=line[:-1]
if len(line)>0:
2013-09-25 15:23:38 +00:00
addtrusted(line)
f.close()
2013-09-25 15:23:38 +00:00
def loadgods():
godslock.acquire()
while len(gods)>0: gods.pop() #See above
f=open('gods.txt','r')
for line in f:
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():
trustedlock.acquire()
f=open('trusted.txt','w')
for i in trusted:
f.write(i+'\n')
f.close
trustedlock.release()
loadtrusted()
2013-09-25 15:23:38 +00:00
loadgods()
def addauthcmd(nick,cmd):
authcmdlock.acquire()
trustedlock.acquire()
if nick in trusted:
if nick not in authcmds:
authcmds[nick]=[]
authcmds[nick].append(cmd)
trustedlock.release()
authcmdlock.release()
2013-09-25 15:23:38 +00:00
def addauthfunc(nick,f):
authfunclock.acquire()
trustedlock.acquire()
if nick in trusted:
if nick not in authfuncs:
authfuncs[nick]=[]
authfuncs[nick].append(f)
trustedlock.release()
authfunclock.release()
2013-07-21 16:56:44 +00:00
def chmode(irc,chan,nick,mode,args):
if len(args)==0:
addauthcmd(nick,'MODE %s %s %s'%(chan,mode,nick))
else:
for name in args:
addauthcmd(nick,'MODE %s %s %s'%(chan,mode,name))
irc.send('PRIVMSG NickServ :ACC '+nick)
2013-06-29 22:01:33 +00:00
def parse((line,irc)):
line=line.split(' ')
nick=line[0].split('!')[0][1:]
chan=line[2] if line[2][0]=='#' else nick
2013-06-29 22:18:56 +00:00
if line[1]=='PRIVMSG':
if line[3]==':#echo':
irc.send('PRIVMSG %s :%s'%(chan,' '.join(line[4:])))
2013-06-29 22:18:56 +00:00
elif line[3]==':#op':
2013-07-21 16:56:44 +00:00
chmode(irc,chan,nick,'+o',line[4:])
elif line[3]==':#deop':
2013-07-21 16:56:44 +00:00
chmode(irc,chan,nick,'-o',line[4:])
elif line[3]==':#voice':
chmode(irc,chan,nick,'+v',line[4:])
elif line[3]==':#devoice':
chmode(irc,chan,nick,'-v',line[4:])
elif line[3]==':#kick':
if len(line)>4:
2014-03-17 21:40:22 +00:00
if line[4].lower()=='oonbotti2':
2013-09-03 12:50:54 +00:00
irc.send('KICK %s %s :Fuck you'%(chan,nick))
elif random.randint(0,9)==0 and len(line)==5:
2013-09-03 12:50:54 +00:00
irc.send('KICK %s %s :Bam'%(chan,nick))
else:
addauthcmd(nick,'KICK %s %s :%s'%(chan,line[4],' '.join(line[5:])))
irc.send('PRIVMSG NickServ :ACC '+nick)
else:
irc.send('PRIVMSG %s :Usage #kick nick reason'%chan)
2013-06-29 22:18:56 +00:00
elif line[3]==':#src':
irc.send('PRIVMSG %s :https://github.com/JuEeHa/oonbotti2'%chan)
elif line[3]==':#msg':
if len(line)>5:
2013-07-06 21:21:27 +00:00
msglock.acquire()
if line[4] not in msgs:
msgs[line[4]]=[]
msgs[line[4]].append((nick,' '.join(line[5:])))
2013-07-06 21:21:27 +00:00
msglock.release()
else:
irc.send('PRIVMSG %s :Usage: #msg nick message'%chan)
elif line[3]==':#trusted?':
addauthcmd(nick,'PRIVMSG %s :%s: You are trusted'%(chan,nick))
irc.send('PRIVMSG NickServ :ACC '+nick)
2013-09-25 15:23:38 +00:00
elif line[3]==':#trust':
if len(line)==5:
addauthfunc(nick,(lambda :addtrusted(line[4])))
irc.send('PRIVMSG NickServ :ACC '+nick)
else:
irc.send('PRIVMSG %s :Usage #trust nick'%chan)
elif line[3]==':#untrust':
if len(line)==5:
godslock.acquire()
if line[4] not in gods:
addauthfunc(nick,(lambda :rmtrusted(line[4])))
irc.send('PRIVMSG NickServ :ACC '+nick)
godslock.release()
2013-09-25 15:23:38 +00:00
else:
irc.send('PRIVMSG %s :Usage #trust nick'%chan)
elif line[3]==':#ls-trusted':
trustedlock.acquire()
2013-11-13 19:12:05 +00:00
irc.send('PRIVMSG %s :%s'%(nick,', '.join(trusted)))
2013-09-25 15:23:38 +00:00
trustedlock.release()
2014-01-04 18:25:09 +00:00
elif line[3]==':#invite':
if len(line)==5:
addauthcmd(nick, 'INVITE %s %s'%(line[4], chan))
irc.send('PRIVMSG NickServ :ACC '+nick)
else:
irc.send('PRIVMSG %s :Usage #invite nick'%chan)
2013-06-30 11:42:14 +00:00
elif line[3]==':#help':
irc.send('PRIVMSG %s :%s'%(chan,help(' '.join(line[4:]))))
elif line[3]==':#esoteric' and chan=='#esoteric':
irc.send('PRIVMSG %s :Nothing here'%chan)
2013-06-29 22:18:56 +00:00
elif line[3][1:] in ('oonbotti:', 'oonbotti', 'oonbotti,', 'oonbotti2', 'oonbotti2:', 'oonbotti2,'):
irc.send('PRIVMSG %s :%s: %s'%(chan,nick,doctor.respond(' '.join(line[4:]))))
2013-06-29 22:01:33 +00:00
elif line[1]=='NOTICE' and line[0].split('!')[0]==':NickServ' and line[4]=='ACC':
2013-09-25 15:23:38 +00:00
authfunclock.acquire()
authcmdlock.acquire()
2013-09-25 15:23:38 +00:00
if line[5]=='3':
trustedlock.acquire()
if line[3][1:] in trusted:
trustedlock.release()
if line[3][1:] in authcmds:
for i in authcmds.pop(line[3][1:]):
irc.send(i)
if line[3][1:] in authfuncs:
for i in authfuncs.pop(line[3][1:]):
i()
else:
trustedlock.release()
else:
if line[3][1:] in authcmds:
authcmds.pop(line[3][1:])
2013-09-25 15:23:38 +00:00
if line[3][1:] in authfuncs:
authfuncs.pop(line[3][1:])
2013-08-09 08:08:18 +00:00
if line[5]=='0':
irc.send('PRIVMSG %s :Register account with NickServ'%line[3][1:])
elif line[5]=='1':
irc.send('PRIVMSG %s :Identify with NickServ'%line[3][1:])
2013-09-25 15:23:38 +00:00
else:
2013-08-09 08:08:18 +00:00
irc.send('PRIVMSG %s :WTF, NickServ returned %s'%(line[3][1:],line[5]))
authcmdlock.release()
2013-09-25 15:23:38 +00:00
authfunclock.release()
elif line[1]=='482':
irc.send('PRIVMSG %s :Not op'%line[3])
2013-08-09 08:08:18 +00:00
#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
2013-07-06 21:21:27 +00:00
msglock.acquire()
if (line[1]=='PRIVMSG' or line[1]=='JOIN') and nick in msgs:
for sender,msg in msgs.pop(nick):
irc.send('PRIVMSG %s :<%s> %s'%(nick,sender,msg))
2013-07-06 21:21:27 +00:00
msglock.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()
savetrusted()
elif cmdline[0]=='/lt':
loadtrusted()
2013-09-25 15:23:38 +00:00
elif cmdline[0]=='/st':
savetrusted()
elif cmdline[0]=='/lg':
loadgods()
def help(cmd):
if cmd=='':
2014-01-04 18:25:09 +00:00
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':
2013-07-21 16:56:44 +00:00
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 (added due to irrational demand by shikhin and sortiecat, nick support added for same reason)'
2013-07-21 16:56:44 +00:00
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. messages can be read with #readmsg'
elif cmd=='#trusted?':
return '#trusted? tell you if you are trusted by oonbotti'
2013-09-25 15:23:38 +00:00
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':
2013-10-11 19:53:56 +00:00
return '#ls-trusted list nicks that are trusted. use only in a query'
2014-01-04 18:25:09 +00:00
elif cmd=='#invite':
return '#invite nick invites nick to channel'
elif cmd=='#help':
return '#help [command] give short info of command or list commands'
2014-03-11 19:05:17 +00:00
elif cmd=='me':
return 'I shall.'
else:
return 'Not found'