Skip to content

Instantly share code, notes, and snippets.

@MooseElkingtons
Created February 2, 2013 00:05
Show Gist options
  • Save MooseElkingtons/4695082 to your computer and use it in GitHub Desktop.
Save MooseElkingtons/4695082 to your computer and use it in GitHub Desktop.
'''
@author: moose
'''
import Logger
import socket
class Bot():
logger = None
config = None
irc = None
queue = None
readbuffer = ""
server = "irc.esper.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, server, port, pwd = None):
self.reload_config()
self.irc.connect(server, port)
self.logger.log('[CONNECT] Connecting to ',server,':',port)
if(self.use_pwd):
self.send_raw('QUOTE PASS ',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')
'''
@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
'''
@author: Moose Elkingtons
'''
from datetime import date
import time
class Logger(object):
file = None
def __init__(self, file):
self.file = open(file)
def log(self, s, ex=None):
ts = '[',date.fromtimestamp(time.time()),']'
st = ts,' ',s
if(ex != None):
st = st,'\n',str(ex)
print st
self.file.write(st,'\n')
'''
@author: Moose Elkingtons
'''
import Bot
import IRCConfiguration
class MainModule():
def __init__(self):
config = IRCConfiguration("../settings.cfg")
bot = Bot(config)
bot.connect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment