Moved irc.py into ctf.

Moved Flagger into ctf.flagd
Moved Roshambo stuff into its own directory.
This commit is contained in:
Paul S. Ferrell 2009-10-05 15:30:37 -06:00
parent 3892cd0a7d
commit 5a27122cec
11 changed files with 66 additions and 52 deletions

View File

@ -1,4 +1,3 @@
import irc
import badmath import badmath
import time import time
import os import os
@ -6,7 +5,13 @@ import traceback
import pickle import pickle
from hashlib import sha256 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): class Gyopi(irc.Bot):
STATE_FN = 'pi.state' STATE_FN = 'pi.state'
@ -20,8 +25,8 @@ class Gyopi(irc.Bot):
FLAG_HOST = b'ctf1.lanl.gov' FLAG_HOST = b'ctf1.lanl.gov'
# FLAG_HOST = b'localhost' # FLAG_HOST = b'localhost'
def __init__(self, host, dataPath, flagger): def __init__(self, host, channels, dataPath, flagger):
irc.Bot.__init__(self, host, ['gyupi'], 'Gyupi', ['#badmath']) irc.Bot.__init__(self, host, ['gyopi'], 'Gyopi', channels)
self._dataPath = dataPath self._dataPath = dataPath
@ -96,15 +101,15 @@ class Gyopi(irc.Bot):
stateFile = open(statePath + '.tmp', 'wb') stateFile = open(statePath + '.tmp', 'wb')
pickle.dump(state, stateFile) pickle.dump(state, stateFile)
stateFile.close() stateFile.close()
os.move( statePath + '.tmp', statePath) os.rename( statePath + '.tmp', statePath)
def _tellFlag(self, forum): def _tellFlag(self, forum):
"""Announce who owns the flag.""" """Announce who owns the flag."""
forum.msg('%s has the flag.' % (self._flag.flag)) forum.msg('%s has the flag.' % (self._flag.flag))
forum.msg('Difficulty level is %d' % self._lvl)
def _tellPuzzle(self, forum): def _tellPuzzle(self, forum):
"""Announce the current puzzle.""" """Announce the current puzzle."""
forum.msg('Difficulty level is %d' % self._lvl)
forum.msg('The problem is: %s' % ' '.join( map(str, self._puzzle))) forum.msg('The problem is: %s' % ' '.join( map(str, self._puzzle)))
def _getStations(self): def _getStations(self):
@ -173,8 +178,8 @@ class Gyopi(irc.Bot):
elif cmd.startswith('h'): elif cmd.startswith('h'):
# Help # Help
forum.msg('Goal: Help me with my math homework, FROM ANOTHER DIMENSION!') 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('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('Commands: !help, !flag, !register [TEAM], !solve SOLUTION,!? EQUATION, !ops, !problem, !who')
elif cmd.startswith('prob'): elif cmd.startswith('prob'):
self._tellPuzzle(forum) self._tellPuzzle(forum)
elif cmd.startswith('solve') and args: elif cmd.startswith('solve') and args:
@ -238,18 +243,21 @@ if __name__ == '__main__':
import optparse import optparse
p = optparse.OptionParser() p = optparse.OptionParser()
p.add_option('-h', '--host', dest='ircHost', default='localhost', p.add_option('-i', '--irc', dest='ircHost', default='localhost',
'IRC Host to connect to.') help='IRC Host to connect to.')
p.add_option('-f', '--flagd', dest='flagd', default='localhost', 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', p.add_option('-p', '--password', dest='password',
default='badmath:::a41c6753210c0bdafd84b3b62d7d1666', default='badmath:::a41c6753210c0bdafd84b3b62d7d1666',
help='Flag server password') help='Flag server password')
p.add_option('-d', '--path', dest='path', default='/var/lib/badmath', 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() opts, args = p.parse_args()
channels = [opts.channel]
flagger = Flagger.Flagger(opts.flagd, opts.password.encode('utf-8')) flagger = Flagger(opts.flagd, opts.password.encode('utf-8'))
gyopi = Gyopi((opts.ircHost, 6667), opts.path, flagger) gyopi = Gyopi((opts.ircHost, 6667), channels, opts.path, flagger)
irc.run_forever() irc.run_forever()

BIN
badmath/pi.state Normal file

Binary file not shown.

10
badmath/test Executable file
View File

@ -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

View File

@ -1,4 +0,0 @@
import Pi, irc
pi = Pi.pi(('irc.lanl.gov', 6667), '')
irc.run_forever()

View File

@ -1,33 +1,3 @@
#! /usr/bin/env python3 #! /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

View File

@ -20,6 +20,33 @@ def hexdigest(data):
flags_dir = config.get('global', 'flags_dir') 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): class Submitter(asyncore.dispatcher):
def __init__(self, host='127.0.0.1', port=6667): def __init__(self, host='127.0.0.1', port=6667):
asyncore.dispatcher.__init__(self) asyncore.dispatcher.__init__(self)

View File

@ -8,7 +8,7 @@ import traceback
import time import time
from errno import EPIPE from errno import EPIPE
from . import teams from . import teams
from . import Flagger from . import flagd
# Heartbeat frequency (in seconds) # Heartbeat frequency (in seconds)
@ -439,7 +439,7 @@ class TurnBasedGame(Game):
## ##
def start(game_factory, port, auth, minplayers, maxplayers=None): 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) manager = Manager(game_factory, flagger, minplayers, maxplayers)
listener = Listener(('', port), Player, manager) listener = Listener(('', port), Player, manager)
return (flagger, manager, listener) return (flagger, manager, listener)

View File

@ -1,13 +1,14 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
import irc
import os import os
import optparse import optparse
import asynchat import asynchat
import socket import socket
import asyncore import asyncore
from urllib.parse import quote_plus as quote 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' nobody = '\002[nobody]\002'
@ -122,6 +123,8 @@ def main():
p.add_option('-p', '--password', dest='password', p.add_option('-p', '--password', dest='password',
default='kevin:::7db3e44d53d4a466f8facd7b7e9aa2b7', default='kevin:::7db3e44d53d4a466f8facd7b7e9aa2b7',
help='Flag server password') help='Flag server password')
p.add_option('-c', '--channel', dest='channel',
help='Channel to join')
opts, args = p.parse_args() opts, args = p.parse_args()
f = Flagger(opts.flagd, opts.password.encode('utf-8')) f = Flagger(opts.flagd, opts.password.encode('utf-8'))