aboutsummaryrefslogtreecommitdiffstats
path: root/titlebot
diff options
context:
space:
mode:
Diffstat (limited to 'titlebot')
-rwxr-xr-xtitlebot/commands/clear12
-rwxr-xr-xtitlebot/commands/down2
-rwxr-xr-xtitlebot/commands/help10
-rwxr-xr-xtitlebot/commands/list27
-rwxr-xr-xtitlebot/commands/new19
-rw-r--r--titlebot/commands/poll.py23
-rwxr-xr-xtitlebot/commands/undo27
-rwxr-xr-xtitlebot/commands/up27
-rw-r--r--titlebot/titlebot.py64
9 files changed, 211 insertions, 0 deletions
diff --git a/titlebot/commands/clear b/titlebot/commands/clear
new file mode 100755
index 0000000..e355819
--- /dev/null
+++ b/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/titlebot/commands/down b/titlebot/commands/down
new file mode 100755
index 0000000..8964382
--- /dev/null
+++ b/titlebot/commands/down
@@ -0,0 +1,2 @@
+#!/bin/sh
+echo "not implemented"
diff --git a/titlebot/commands/help b/titlebot/commands/help
new file mode 100755
index 0000000..475ea98
--- /dev/null
+++ b/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/titlebot/commands/list b/titlebot/commands/list
new file mode 100755
index 0000000..3a29919
--- /dev/null
+++ b/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/titlebot/commands/new b/titlebot/commands/new
new file mode 100755
index 0000000..7246a2b
--- /dev/null
+++ b/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/titlebot/commands/poll.py b/titlebot/commands/poll.py
new file mode 100644
index 0000000..595ab26
--- /dev/null
+++ b/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/titlebot/commands/undo b/titlebot/commands/undo
new file mode 100755
index 0000000..bebd57a
--- /dev/null
+++ b/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/titlebot/commands/up b/titlebot/commands/up
new file mode 100755
index 0000000..c9de122
--- /dev/null
+++ b/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/titlebot/titlebot.py b/titlebot/titlebot.py
new file mode 100644
index 0000000..d04789c
--- /dev/null
+++ b/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')
+]
+