Last active
December 12, 2015 02:08
-
-
Save MooseElkingtons/4696042 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 | |
| ''' | |
| from Logger import Logger | |
| from twisted.internet import reactor, protocol | |
| class ircprotocol(protocol): | |
| logger = None | |
| config = None | |
| readbuffer = "" | |
| server = "irc.freenode.net" | |
| port = 6667 | |
| name = "Pycicle" | |
| channels = "#Pycicle" | |
| server_pwd = None | |
| use_pwd = False | |
| def connect(self): | |
| self.reload_config() | |
| self.doStart() | |
| 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) | |
| def disconnect(self): | |
| self.logger.log('[DISCONNECT] Disconnected from ',self.server,':',self.port) | |
| self.doStop() | |
| 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 dataReceived(self, data): | |
| l = data.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.transport.write(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', '') | |
| class ircfactory(protocol.ReconnectingClientFactory): | |
| protocol = ircprotocol() | |
| class Bot(object): | |
| config = None | |
| logger = None | |
| def __init__(self, config): | |
| self.logger = Logger("~/workspace/com.mooseelkingtons.pycicle.main/logs/logs.dat") | |
| self.config = config | |
| self.protocol.reload_config() | |
| self.reactor.connectTCP(self.server, self.port, ircfactory()) | |
| self.connect() |
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(object): | |
| map = {} | |
| file = None | |
| def __init__(self, directory): | |
| try: | |
| dire = os.path.normpath(os.path.expanduser(directory)) | |
| self.file = open(dire) | |
| except IOError as e: | |
| print 'Invalid or Corrupt File: ', str(dire) | |
| 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): | |
| try: | |
| return map[key] | |
| except KeyError: | |
| return None | |
| 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 | |
| ''' | |
| from PyBot import PyBot | |
| pybot = PyBot() |
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 | |
| ''' | |
| from Bot import Bot | |
| from IRCConfiguration 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('~/workspace/com.mooseelkingtons.pycicle.main/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