Created
February 2, 2013 02:23
-
-
Save MooseElkingtons/4695787 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@author: moose | |
''' | |
import Logger | |
import socket | |
class Bot(): | |
logger = None | |
config = None | |
irc = None | |
readbuffer = "" | |
server = "irc.freenode.net" | |
port = 6667 | |
name = "Pycicle" | |
channels = "#Pycicle" | |
server_pwd = None | |
use_pwd = False | |
def __init__(self, config): | |
self.logger = Logger("../logs/logs.dat") | |
self.config = config | |
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
def connect(self): | |
self.reload_config() | |
self.irc.connect(self.server, self.port) | |
self.logger.log('[CONNECT] Connecting to ',self.server,':',self.port) | |
if(self.use_pwd): | |
self.send_raw('QUOTE PASS ',self.server_pwd) | |
self.logger.log('[CONNECT] Setting Nick to ',self.name) | |
self.send_raw('NICK ',self.name) | |
self.run() | |
def disconnect(self): | |
self.logger.log('[DISCONNECT] Disconnected from ',self.server,':',self.port) | |
self.irc.close() | |
def reload_config(self): | |
self.server = self.config.get('irc-server') | |
self.port = int(self.config.get('irc-port')) | |
self.name = self.config.get('bot-name') | |
self.server_pwd = self.config.get('server-pwd') | |
self.channels = self.config.get('auto-join') | |
self.use_pwd = self.server_pwd == None | |
def run(self): | |
while 1: | |
self.readbuffer = self.readbuffer + self.irc.recv(1024) | |
temp = self.readbuffer.split("\n") | |
for l in temp: | |
l = l.rstrip().split() | |
if(l[0] == 'PING'): | |
self.on_pong(l[1:]) | |
elif(l[0].startswith(':') and l[1] == 'PRIVMSG'): | |
pms = l.join() | |
pmdata, pm = pms.split(":", 2) | |
pmsender, pmrec = (pmdata[:1] + pmdata[2:]).split(' ', 2) | |
self.on_message(pmsender, pmrec, pm) | |
elif(l[0] == 'NOTICE'): | |
self.on_notice(l[1:]) | |
elif(l[0] == 'KICK'): | |
ks = l[1:].join() | |
kchan, krec = ks[:2].split(' ', 2) | |
kreason = ks[2:] | |
self.on_kick(kchan, krec, kreason) | |
elif(l[0].startswith(':') and l[1] == 'JOIN'): | |
juser = l[:1].replace(':', '') | |
jchan = l[2:].replace(':', '') | |
self.on_join(juser, jchan) | |
elif(l[0].startswith(':') and l[1] == 'PART'): | |
puser = l[:1].replace(':', '') | |
pchan = l[2:].replace(':', '') | |
self.on_part(puser, pchan) | |
elif(l[0].startswith('MODE')): | |
mdata = l[:2] | |
mchan = l[1] | |
self.on_mode(mdata, mchan) | |
def on_mode(self, data, channel): | |
self.logger.log('[MODE ',channel,'] set mode ',data) | |
def on_join(self, user, channel): | |
self.logger.log('[JOIN ',channel,'] ',user) | |
def on_kick(self, channel, recipient, reason): | |
self.logger.log('[KICK ',channel,'] Kicked ',recipient,' for: ',reason) | |
def on_message(self, sender, recipient, message): | |
self.logger.log('[PRIVMSG ',sender,' -> ',recipient,'] '+message) | |
def on_part(self, user, channel): | |
self.logger.log('[PART ',channel,'] ',user) | |
def on_pong(self, l): | |
self.send_raw('PONG ',l[0]) | |
def send_raw(self, raw): | |
self.irc.send(raw,'\r\n') | |
def send_message(self, message, channel): | |
self.send_raw('PRIVMSG ',channel,' :',self.parse_colors(message)) | |
def parse_controls(self, message): | |
nm = message.replace('^c', '\x03') | |
nm = nm.replace('^b', '\x02') | |
return nm | |
def strip_controls(self, message): | |
nm = message.replace('\x03', '') | |
nm.replace('\x02', '') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@author: Moose Elkingtons | |
''' | |
import os | |
class IRCConfiguration(): | |
def __init__(self, directory): | |
try: | |
dir = directory.replace('%user.home%', os.path) | |
self.file = open(dir) | |
except IOError as e: | |
print 'Invalid or Corrupt File: ', dir | |
print e | |
s = self.file.readline() | |
while s != None: | |
if(not s.startswith('#')): | |
split = s.split('=') | |
map[split[0]] = split[1] | |
def get(self, key): | |
return map[key] | |
def get_all(self): | |
return map | |
def set(self, key, value): | |
map[key] = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@author: Moose Elkingtons | |
''' | |
import Bot | |
import IRCConfiguration | |
class PyBot(Bot): | |
command_prefix = '!' | |
game_running = False | |
game_status = False | |
# Game Variables | |
czar = "NoOne" | |
players = {} | |
white_cards = {} | |
def __init__(self): | |
config = IRCConfiguration('../settings.cfg') | |
self.command_prefix = config.get('command-prefix') | |
self = Bot(config) | |
self.connect() | |
def on_message(self, sender, recipient, message): | |
command = message[0].lower().replace(self.command_prefix, '', 1) | |
if(command == 'join'): | |
if not self.game_status: | |
self.send_message(recipient, sender,': There is no active game at the moment! ', | |
'Type ',self.command_prefix,'start to start a game!') | |
else: | |
if not self.game_running: | |
self.send_message(recipient, '^b',sender,'^b has started a new game of ', | |
'Cards Against Humanity - A Game for Horrible People!') | |
self.send_message(recipient, 'If you do not know how to play, type ', | |
'^b^c04',self.command_prefix,'howto^b^c to know the rules', | |
' of Cards Against Humanity.') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment