diff options
author | makefu <github@syntax-fehler.de> | 2014-04-25 12:06:50 +0200 |
---|---|---|
committer | makefu <github@syntax-fehler.de> | 2014-04-25 12:06:50 +0200 |
commit | 3907535dd48b94b3edd5facde0245719f04effb2 (patch) | |
tree | a25b7826c55cefe3be85f74b978bf0b9f2d19025 | |
parent | 9afc6e2d0237ae84c084cc05492bf0f285f98851 (diff) |
add titlebot Reaktor configuration
-rwxr-xr-x | Reaktor/titlebot/commands/clear | 12 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/down | 2 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/help | 10 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/list | 27 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/new | 19 | ||||
-rw-r--r-- | Reaktor/titlebot/commands/poll.py | 23 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/undo | 27 | ||||
-rwxr-xr-x | Reaktor/titlebot/commands/up | 27 | ||||
-rw-r--r-- | Reaktor/titlebot/titlebot.py | 64 |
9 files changed, 211 insertions, 0 deletions
diff --git a/Reaktor/titlebot/commands/clear b/Reaktor/titlebot/commands/clear new file mode 100755 index 00000000..e3558194 --- /dev/null +++ b/Reaktor/titlebot/commands/clear @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +title=" ".join(sys.argv[1:]) +db = poll.save_db(f,[]) +print("cleared database") diff --git a/Reaktor/titlebot/commands/down b/Reaktor/titlebot/commands/down new file mode 100755 index 00000000..8964382d --- /dev/null +++ b/Reaktor/titlebot/commands/down @@ -0,0 +1,2 @@ +#!/bin/sh +echo "not implemented" diff --git a/Reaktor/titlebot/commands/help b/Reaktor/titlebot/commands/help new file mode 100755 index 00000000..475ea98e --- /dev/null +++ b/Reaktor/titlebot/commands/help @@ -0,0 +1,10 @@ +#!/bin/sh +cat <<EOF +BGT Title Poll Bot: + .new TITLE - suggest a new title + .list <(age|votes)> - list all suggestions + .up NUM (NUM ...) - upvote one or more suggestions from .list + .undo NUM (NUM ...) - undo an upvote + .clear - clear the poll (auth required) +EOF + diff --git a/Reaktor/titlebot/commands/list b/Reaktor/titlebot/commands/list new file mode 100755 index 00000000..3a29919a --- /dev/null +++ b/Reaktor/titlebot/commands/list @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +import poll + +f = 'suggestions.json' +title=" ".join(sys.argv[1:]) +db = poll.load_db(f) +if len(sys.argv) > 1 and ("-h" in sys.argv[1] or "usage" == sys.argv[1]): + print("""usage: list <(age|votes)> + sort by age or by votes (default: age) +""") + sys.exit(0) + +if len(sys.argv) > 1 and ("votes" in sys.argv[1]): + use = poll.sort_by_votes(db) +elif len(sys.argv) > 1 and ("age" in sys.argv[1]) or len(sys.argv) == 1: + pass +else: + print("unknown sorting method") + sys.exit(1) + +for entry in poll.sort_by_votes(db): + print("#%d %s (votes: %d)" % + (db.index(entry),entry['title'],sum(entry['votes'].values()))) diff --git a/Reaktor/titlebot/commands/new b/Reaktor/titlebot/commands/new new file mode 100755 index 00000000..7246a2b2 --- /dev/null +++ b/Reaktor/titlebot/commands/new @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +title=" ".join(sys.argv[1:]) +db = poll.load_db(f) + +suggester = environ['_from'] +if not poll.title_in_db(title,db): + db.append( { 'by': suggester, + 'votes':{},'title': title}) + print("Thank you for your suggestion '%s'!"%environ["_from"]) + print("To vote type '.up %d'"%(len(db)-1)) +poll.save_db(f,db) diff --git a/Reaktor/titlebot/commands/poll.py b/Reaktor/titlebot/commands/poll.py new file mode 100644 index 00000000..595ab269 --- /dev/null +++ b/Reaktor/titlebot/commands/poll.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +import json +def load_db(f): + try: + with open(f) as fl: + return json.load(fl) + except: + #default db is [] + return [] + +def title_in_db(t,d): + for index,entry in enumerate(d): + if t == entry['title']: + print("Title is already in list.") + print("To vote for this type '.up %d'" %index) + return True + return False +def save_db(f,db): + with open(f,"w") as x: + json.dump(db,x) + +def sort_by_votes(db): + return sorted(db,key=lambda entry:sum(entry['votes'].values()),reverse=True) diff --git a/Reaktor/titlebot/commands/undo b/Reaktor/titlebot/commands/undo new file mode 100755 index 00000000..bebd57a3 --- /dev/null +++ b/Reaktor/titlebot/commands/undo @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +db = poll.load_db(f) +votes = [] +try: + votes = sys.argv[1:] +except: + print("""usage: undo number (...) + undos vote of one or more entries based on .list""") + sys.exit(1) +voter = environ['_prefix'] +for vote in votes: + try: + vote = int(vote) + del(db[vote]['votes'][voter] ) + print("undid vote by %s for #%d" %(environ['_from'],vote)) + except: + print("undo voting for #%s failed" %vote) + +poll.save_db(f,db) diff --git a/Reaktor/titlebot/commands/up b/Reaktor/titlebot/commands/up new file mode 100755 index 00000000..c9de1223 --- /dev/null +++ b/Reaktor/titlebot/commands/up @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import json +from os import environ +import sys +import os +# krebs polling +import poll + +f = 'suggestions.json' +db = poll.load_db(f) +votes = [] +try: + votes = sys.argv[1:] +except: + print("""usage: up number (...) + upvotes one or more entries based on .list""") + sys.exit(1) + +voter = environ['_prefix'] +for vote in votes: + try: + vote = int(vote) + db[vote]['votes'][voter] = 1 + except: + print("voting for #%s failed" %vote) +print("Thanks for your votes %s"%environ['_from']) +poll.save_db(f,db) diff --git a/Reaktor/titlebot/titlebot.py b/Reaktor/titlebot/titlebot.py new file mode 100644 index 00000000..d04789c3 --- /dev/null +++ b/Reaktor/titlebot/titlebot.py @@ -0,0 +1,64 @@ +from os import environ,mkdir + +debug = False + +# CAVEAT name should not contains regex magic +name = 'bgt_titlebot' + +workdir = '/tmp/state' + +try: + mkdir(workdir) +except: pass + +irc_alarm_timeout = 300 +irc_hammer_interval = 10 +irc_kill_timeout = 360 +irc_nickname = name +irc_server = 'irc.freenode.org' +irc_port = 6667 +irc_restart_timeout = 5 +irc_channels = [ + '#binaergewitter' +] + +admin_file=workdir+'/'+'admin.lst' +try: + with open(admin_file,"x"): pass +except: pass +auth_file=workdir+'/'+'auth.lst' + +def default_command(cmd): + return { + 'capname': cmd, + 'pattern': '^(?:' + name + '|\\*):\\s*' + cmd + '\\s*(?:\\s+(?P<args>.*))?$', + 'argv': [ 'commands/' + cmd ] } +def dot_command(cmd): + return { + 'capname': cmd, + 'pattern': '\\.' + cmd + '\\s*(?:\\s+(?P<args>.*))?$', + 'argv': [ 'titlebot/commands/' + cmd ] } + +public_commands = [ + default_command('caps'), + default_command('hello'), + default_command('badcommand'), + default_command('rev'), + default_command('uptime'), + default_command('nocommand'), + dot_command('list'), + dot_command('help'), + dot_command('up'), + dot_command('new'), + dot_command('undo'), + dot_command('down'), + # identify via direct connect + { 'capname': 'identify', + 'pattern': 'identify' + '\\s*(?:\\s+(?P<args>.*))?$', + 'argv' : [ 'commands/identify' ]} +] +commands = [ + default_command('reload'), + dot_command('clear') +] + |