diff --git a/badmath/Gyopi.py b/badmath/Gyopi.py index 2eb26f4..6fd2eb7 100644 --- a/badmath/Gyopi.py +++ b/badmath/Gyopi.py @@ -1,4 +1,3 @@ -import irc import badmath import time import os @@ -6,7 +5,13 @@ import traceback import pickle from hashlib import sha256 -import Flagger +try: + from ctf import irc + from ctf.flagd import Flagger +except: + import sys + sys.path.append('/home/pflarr/repos/gctf/') + from ctf.flagd import Flagger, irc class Gyopi(irc.Bot): STATE_FN = 'pi.state' @@ -20,8 +25,8 @@ class Gyopi(irc.Bot): FLAG_HOST = b'ctf1.lanl.gov' # FLAG_HOST = b'localhost' - def __init__(self, host, dataPath, flagger): - irc.Bot.__init__(self, host, ['gyupi'], 'Gyupi', ['#badmath']) + def __init__(self, host, channels, dataPath, flagger): + irc.Bot.__init__(self, host, ['gyopi'], 'Gyopi', channels) self._dataPath = dataPath @@ -96,15 +101,15 @@ class Gyopi(irc.Bot): stateFile = open(statePath + '.tmp', 'wb') pickle.dump(state, stateFile) stateFile.close() - os.move( statePath + '.tmp', statePath) + os.rename( statePath + '.tmp', statePath) def _tellFlag(self, forum): """Announce who owns the flag.""" forum.msg('%s has the flag.' % (self._flag.flag)) - forum.msg('Difficulty level is %d' % self._lvl) def _tellPuzzle(self, forum): """Announce the current puzzle.""" + forum.msg('Difficulty level is %d' % self._lvl) forum.msg('The problem is: %s' % ' '.join( map(str, self._puzzle))) def _getStations(self): @@ -173,8 +178,8 @@ class Gyopi(irc.Bot): elif cmd.startswith('h'): # Help forum.msg('Goal: Help me with my math homework, FROM ANOTHER DIMENSION!') - forum.msg('Goal: The current winner gets to control the contest music.') - forum.msg('Commands: !help, !flag, !register [TEAM], !solve SOLUTION,!? EQUATION, !ops, !problem', '!who') + #forum.msg('Goal: The current winner gets to control the contest music.') + forum.msg('Commands: !help, !flag, !register [TEAM], !solve SOLUTION,!? EQUATION, !ops, !problem, !who') elif cmd.startswith('prob'): self._tellPuzzle(forum) elif cmd.startswith('solve') and args: @@ -238,18 +243,21 @@ if __name__ == '__main__': import optparse p = optparse.OptionParser() - p.add_option('-h', '--host', dest='ircHost', default='localhost', - 'IRC Host to connect to.') + p.add_option('-i', '--irc', dest='ircHost', default='localhost', + help='IRC Host to connect to.') p.add_option('-f', '--flagd', dest='flagd', default='localhost', - 'Flag Server to connect to') + help='Flag Server to connect to') p.add_option('-p', '--password', dest='password', default='badmath:::a41c6753210c0bdafd84b3b62d7d1666', help='Flag server password') p.add_option('-d', '--path', dest='path', default='/var/lib/badmath', - 'Path to where we can store state info.') + help='Path to where we can store state info.') + p.add_option('-c', '--channel', dest='channel', default='+badmath', + help='Which channel to join') opts, args = p.parse_args() + channels = [opts.channel] - flagger = Flagger.Flagger(opts.flagd, opts.password.encode('utf-8')) - gyopi = Gyopi((opts.ircHost, 6667), opts.path, flagger) + flagger = Flagger(opts.flagd, opts.password.encode('utf-8')) + gyopi = Gyopi((opts.ircHost, 6667), channels, opts.path, flagger) irc.run_forever() diff --git a/badmath/pi.state b/badmath/pi.state new file mode 100644 index 0000000..43d3747 Binary files /dev/null and b/badmath/pi.state differ diff --git a/badmath/test b/badmath/test new file mode 100755 index 0000000..d1d8b7e --- /dev/null +++ b/badmath/test @@ -0,0 +1,10 @@ +#! /bin/sh + +[ -f /var/lib/ctf/disabled/badmath ] && exit 0 + +DATA_PATH= +if $DATA_PATH; then + mkdir -p $DATA_PATH; +fi + +python3.0 Gyopi.py --path=$DATA_PATH --irc=irc.lanl.gov --flagd=ctf1.lanl.gov --channel=#badmath diff --git a/badmath/test.py b/badmath/test.py deleted file mode 100644 index be736dc..0000000 --- a/badmath/test.py +++ /dev/null @@ -1,4 +0,0 @@ -import Pi, irc - -pi = Pi.pi(('irc.lanl.gov', 6667), '') -irc.run_forever() diff --git a/ctf/__init__.py b/ctf/__init__.py index 467e41a..05a7e62 100755 --- a/ctf/__init__.py +++ b/ctf/__init__.py @@ -1,33 +1,3 @@ #! /usr/bin/env python3 -import asynchat -import asyncore -import socket -class Flagger(asynchat.async_chat): - """Connection to flagd""" - - def __init__(self, addr, auth): - asynchat.async_chat.__init__(self) - self.create_socket(socket.AF_INET, socket.SOCK_STREAM) - self.connect((addr, 6668)) - self.push(auth + b'\n') - self.flag = None - - def handle_read(self): - msg = self.recv(4096) - raise ValueError("Flagger died: %r" % msg) - - def handle_error(self): - # If we lose the connection to flagd, nobody can score any - # points. Terminate everything. - asyncore.close_all() - asynchat.async_chat.handle_error(self) - - def set_flag(self, team): - if team: - eteam = team.encode('utf-8') - else: - eteam = b'' - self.push(eteam + b'\n') - self.flag = team diff --git a/ctf/flagd.py b/ctf/flagd.py index 24bceab..6ae444b 100755 --- a/ctf/flagd.py +++ b/ctf/flagd.py @@ -20,6 +20,33 @@ def hexdigest(data): flags_dir = config.get('global', 'flags_dir') +class Flagger(asynchat.async_chat): + """Use to connect to flagd and submit the current flag holder.""" + + def __init__(self, addr, auth): + asynchat.async_chat.__init__(self) + self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + self.connect((addr, 6668)) + self.push(auth + b'\n') + self.flag = None + + def handle_read(self): + msg = self.recv(4096) + raise ValueError("Flagger died: %r" % msg) + + def handle_error(self): + # If we lose the connection to flagd, nobody can score any + # points. Terminate everything. + asyncore.close_all() + asynchat.async_chat.handle_error(self) + + def set_flag(self, team): + if team: + eteam = team.encode('utf-8') + else: + eteam = b'' + self.push(eteam + b'\n') + self.flag = team class Submitter(asyncore.dispatcher): def __init__(self, host='127.0.0.1', port=6667): asyncore.dispatcher.__init__(self) diff --git a/ctf/game.py b/ctf/game.py index 8346735..3bcd8e8 100755 --- a/ctf/game.py +++ b/ctf/game.py @@ -8,7 +8,7 @@ import traceback import time from errno import EPIPE from . import teams -from . import Flagger +from . import flagd # Heartbeat frequency (in seconds) @@ -439,7 +439,7 @@ class TurnBasedGame(Game): ## def start(game_factory, port, auth, minplayers, maxplayers=None): - flagger = Flagger(('localhost', 6668), auth) + flagger = flagd.Flagger(('localhost', 6668), auth) manager = Manager(game_factory, flagger, minplayers, maxplayers) listener = Listener(('', port), Player, manager) return (flagger, manager, listener) diff --git a/kevin/irc.py b/ctf/irc.py similarity index 100% rename from kevin/irc.py rename to ctf/irc.py diff --git a/kevin/kevin.py b/kevin/kevin.py index 1548858..d0ba26b 100755 --- a/kevin/kevin.py +++ b/kevin/kevin.py @@ -1,13 +1,14 @@ #! /usr/bin/env python3 -import irc import os import optparse import asynchat import socket import asyncore from urllib.parse import quote_plus as quote -from ctf import Flagger + +from ctf import irc +from ctf.flagd import Flagger nobody = '\002[nobody]\002' @@ -122,6 +123,8 @@ def main(): p.add_option('-p', '--password', dest='password', default='kevin:::7db3e44d53d4a466f8facd7b7e9aa2b7', help='Flag server password') + p.add_option('-c', '--channel', dest='channel', + help='Channel to join') opts, args = p.parse_args() f = Flagger(opts.flagd, opts.password.encode('utf-8')) diff --git a/roshambo.py b/roshambo/roshambo.py similarity index 100% rename from roshambo.py rename to roshambo/roshambo.py diff --git a/roshambocli.py b/roshambo/roshambocli.py similarity index 100% rename from roshambocli.py rename to roshambo/roshambocli.py