Skip to content

Instantly share code, notes, and snippets.

@haykuro
Created Apr 24, 2015
Embed
What would you like to do?
Written by Steve Birstok
# Actively under development
import random
import re
import socket
import string
import sys
HOST = PORT = NICK = None
for arg in sys.argv[1:]:
res = re.compile('--server=(.+)').match(arg)
if res:
port = re.compile('(.+):(\d+)').match(res.group(1))
if port:
HOST = port.group(1)
PORT = port.group(2)
else:
HOST = res.group(1)
print "[i] App Start"
def get_random_name(N):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))
class IRCBot:
def send_data(self, txt):
print "..>>| %s" % txt
return self.socket.send('%s\r\n' % txt)
def connect(self):
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.socket = socket.socket(af, socktype, proto)
except OSError as msg:
self.socket = None
continue
try:
self.socket.connect(sa)
except OSError as msg:
self.socket.close()
self.socket = None
continue
break
if self.socket is None:
raise Exception('Could not connect.')
return self.socket
def disconnect(self):
self.send_data("quit :QUITTING (haykuro-bot)")
return self.socket.close()
def get_data_from_socket(self, buff_size=1024):
# pull the data
data = self.socket.recv(buff_size)
# split by carriage return.
for line_data in data.split("\r\n"):
if(len(line_data) > 0):
print "<<..| %s" % line_data
# check line_data here and process
self.check_cmd(line_data)
def check_cmd(self, data):
print "Testing if 'Found' is in text: %s" % data
res = re.compile("Found").match(data)
print res
if res:
print res
self.disconnect()
sys.exit(0)
def __init__(self):
self.socket = None
self.connect()
while True:
try:
self.get_data_from_socket()
except KeyboardInterrupt:
print
self.disconnect()
print "Goodbye!"
sys.exit(1)
self.disconnect()
if HOST is None:
HOST = 'irc.freenode.net'
if PORT is None:
PORT = 6667
if NICK is None:
NICK = get_random_name(8)
try:
irc_bot = IRCBot()
except Exception as e:
print e
sys.exit(1)
print "[e] END OF APP."
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment