Make trustiness be per-channel

This commit is contained in:
Juhani Haverinen 2015-03-03 17:46:19 +02:00
parent 7e0203ab75
commit 1d1b23c4b9
1 changed files with 42 additions and 31 deletions

View File

@ -10,9 +10,9 @@ blacklist = ['bslsk05']
doctor = eliza.eliza() doctor = eliza.eliza()
trusted = [] trusted = {}
trustedlock = threading.Lock() trustedlock = threading.Lock()
gods = [] gods = {}
godslock = threading.Lock() godslock = threading.Lock()
msgs = {} msgs = {}
@ -98,23 +98,26 @@ def savemessages():
loadmessages() loadmessages()
def addtrusted(account): def addtrusted(chan, account):
global trusted, trustedlock global trusted, trustedlock
trustedlock.acquire() trustedlock.acquire()
if account not in trusted: if chan not in trusted:
trusted.append(account) trusted[chan] = []
if account not in trusted[chan]:
trusted[chan].append(account)
trustedlock.release() trustedlock.release()
def rmtrusted(account): def rmtrusted(chan, account):
global trusted, trustedlock global trusted, trustedlock
trustedlock.acquire() trustedlock.acquire()
if account in trusted: if chan in trusted and account in trusted[chan]:
trusted.remove(account) trusted[chan].remove(account)
trustedlock.release() trustedlock.release()
@ -122,7 +125,7 @@ def loadtrusted():
global trusted, trustedlock global trusted, trustedlock
trustedlock.acquire() trustedlock.acquire()
trusted = [] trusted = {}
trustedlock.release() trustedlock.release()
f=open('trusted.txt', 'r') f=open('trusted.txt', 'r')
@ -131,7 +134,8 @@ def loadtrusted():
while len(line) > 0 and line[-1] == '\n': while len(line) > 0 and line[-1] == '\n':
line = line[:-1] line = line[:-1]
if len(line) > 0: if len(line) > 0:
addtrusted(line) chan, account = line.split()
addtrusted(chan, account)
f.close() f.close()
@ -139,15 +143,20 @@ def loadgods():
global gods, godslock global gods, godslock
godslock.acquire() godslock.acquire()
gods = [] gods = {}
f=open('gods.txt', 'r') f=open('gods.txt', 'r')
for line in f: for line in f:
while len(line) > 0 and line[-1] == '\n': while len(line) > 0 and line[-1] == '\n':
line = line[:-1] line = line[:-1]
if len(line) > 0: if len(line) > 0:
gods.append(line) chan, account = line.split()
addtrusted(line)
if chan not in gods:
gods[chan] = []
gods[chan].append(account)
addtrusted(chan, account)
f.close() f.close()
godslock.release() godslock.release()
@ -158,8 +167,9 @@ def savetrusted():
trustedlock.acquire() trustedlock.acquire()
f=open('trusted.txt', 'w') f=open('trusted.txt', 'w')
for i in trusted: for chan in trusted:
f.write(i+'\n') for account in trusted[chan]:
f.write('%s %s\n' % (chan, account))
f.close f.close
trustedlock.release() trustedlock.release()
@ -169,16 +179,16 @@ loadgods()
def chmode(irc, chan, nick, mode, args): def chmode(irc, chan, nick, mode, args):
if args == ['']: if args == ['']:
if isauthorized(irc, nick): if isauthorized(irc, chan, nick):
irc.send('MODE %s %s %s' % (chan, mode, nick)) irc.send('MODE %s %s %s' % (chan, mode, nick))
else: else:
for name in args: for name in args:
if isauthorized(irc, nick): if isauthorized(irc, chan, nick):
irc.send('MODE %s %s %s' % (chan, mode, name)) irc.send('MODE %s %s %s' % (chan, mode, name))
def istrusted(account): def istrusted(chan, account):
trustedlock.acquire() trustedlock.acquire()
if account in trusted: if chan in trusted and account in trusted[chan]:
trustedlock.release() trustedlock.release()
return True return True
else: else:
@ -234,10 +244,10 @@ def getaccount(irc, nick):
else: else:
return account return account
def isauthorized(irc, nick): def isauthorized(irc, chan, nick):
account = getaccount(irc, nick) account = getaccount(irc, nick)
if account: if account:
return istrusted(account) return istrusted(chan, account)
else: else:
irc.msg(nick, 'Identify with NickServ') irc.msg(nick, 'Identify with NickServ')
@ -400,7 +410,7 @@ def parse((line, irc)):
elif random.randint(0,9) == 0 and len(line) == 5: elif random.randint(0,9) == 0 and len(line) == 5:
irc.send('KICK %s %s :Bam' % (chan, nick)) irc.send('KICK %s %s :Bam' % (chan, nick))
else: else:
if isauthorized(irc, nick): if isauthorized(irc, chan, nick):
irc.send('KICK %s %s :%s'%(chan, kicknick, kickreason)) irc.send('KICK %s %s :%s'%(chan, kicknick, kickreason))
else: else:
irc.msg(chan, 'Usage #kick nick reason') irc.msg(chan, 'Usage #kick nick reason')
@ -425,7 +435,7 @@ def parse((line, irc)):
trustnick = nick trustnick = nick
account = getaccount(irc, trustnick) account = getaccount(irc, trustnick)
if account: if account:
if istrusted(account): if istrusted(chan, account):
irc.msg(chan, '%s is trusted' % trustnick) irc.msg(chan, '%s is trusted' % trustnick)
else: else:
irc.msg(chan, '%s is not trusted' % trustnick) irc.msg(chan, '%s is not trusted' % trustnick)
@ -436,10 +446,10 @@ def parse((line, irc)):
elif matchcmd(cmdline, '#trust'): elif matchcmd(cmdline, '#trust'):
if matchcmd(cmdline, '#trust', 'nick'): if matchcmd(cmdline, '#trust', 'nick'):
trustnick = parsecmd(cmdline, 'nick') trustnick = parsecmd(cmdline, 'nick')
if isauthorized(irc, nick): if isauthorized(irc, chan, nick):
account = getaccount(irc, trustnick) account = getaccount(irc, trustnick)
if account: if account:
addtrusted(account) addtrusted(chan, account)
else: else:
irc.msg(chan, 'Failed to get account for %s' % trustnick) irc.msg(chan, 'Failed to get account for %s' % trustnick)
else: else:
@ -447,12 +457,12 @@ def parse((line, irc)):
elif matchcmd(cmdline, '#untrust'): elif matchcmd(cmdline, '#untrust'):
if matchcmd(cmdline, '#untrust', 'nick'): if matchcmd(cmdline, '#untrust', 'nick'):
untrustnick = parsecmd(cmdline, 'nick') untrustnick = parsecmd(cmdline, 'nick')
if isauthorized(irc, nick): if isauthorized(irc, chan, nick):
account = getaccount(irc, untrustnick) account = getaccount(irc, untrustnick)
if account: if account:
godslock.acquire() godslock.acquire()
if account not in gods: if chan not in gods or account not in gods[chan]:
rmtrusted(untrustnick) rmtrusted(chan, untrustnick)
godslock.release() godslock.release()
else: else:
irc.msg(chan, 'Failed to get account for %s' % untrustnick) irc.msg(chan, 'Failed to get account for %s' % untrustnick)
@ -460,12 +470,13 @@ def parse((line, irc)):
irc.msg(chan, 'Usage #untrust nick') irc.msg(chan, 'Usage #untrust nick')
elif matchcmd(cmdline, '#ls-trusted'): elif matchcmd(cmdline, '#ls-trusted'):
trustedlock.acquire() trustedlock.acquire()
irc.msg(nick, ', '.join(trusted)) if chan in trusted:
irc.msg(nick, '%s: %s' % (chan, ', '.join(trusted[chan])))
trustedlock.release() trustedlock.release()
elif matchcmd(cmdline, '#invite'): elif matchcmd(cmdline, '#invite'):
if matchcmd(cmdline, '#invite', 'nick'): if matchcmd(cmdline, '#invite', 'nick'):
invitenick = parsecmd(cmdline, 'nick') invitenick = parsecmd(cmdline, 'nick')
if isauthorized(irc, nick): if isauthorized(irc, chan, nick):
irc.send('INVITE %s %s' % (invitenick, chan)) irc.send('INVITE %s %s' % (invitenick, chan))
else: else:
irc.msg(chan, 'Usage #invite nick') irc.msg(chan, 'Usage #invite nick')
@ -511,7 +522,7 @@ def parse((line, irc)):
if getaccountcheckvalue(whoisnick) == None: if getaccountcheckvalue(whoisnick) == None:
setaccountcheckvalue(whoisnick, '') # Mark as failed, '' is used because None is already reserved setaccountcheckvalue(whoisnick, '') # Mark as failed, '' is used because None is already reserved
elif line[1] == 'INVITE' and line[2] == irc.nick and line[3][1:] in irc.chan.split(' '): elif line[1] == 'INVITE' and line[2] == irc.nick and line[3][1:] in irc.chan.split(' '):
if isauthorized(irc, nick): if isauthorized(irc, line[3], nick):
irc.send('JOIN ' + line[3]) irc.send('JOIN ' + line[3])
elif line[1] == '482': elif line[1] == '482':
irc.msg(line[3], 'Not op') irc.msg(line[3], 'Not op')