Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import random
- random.seed()
- network = 'irc.darkmyst.net'
- port = 6667
- irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- irc.connect((network,port))
- data = irc.recv(4096)
- nick = "Meisterbot"
- irc.send("NICK {}\r\n".format(nick))
- irc.send("USER {0} 0 {0} :{0}\r\n".format(nick))
- irc.settimeout(0.1)
- quit = False
- listOfkeywordResponses = [
- "responses go here",
- "and are written like this",
- ]
- def ircParse(s): #courtesy of Twisted, http://stackoverflow.com/questions/930700/python-parsing-irc-messages
- prefix = ''
- trailing = []
- if not s:
- return None
- if s[0] == ':':
- try:
- prefix, s = s[1:].split(' ', 1)
- except:
- print "[~] Malformed data received: ",s
- if s.find(' :') != -1:
- s, trailing = s.split(' :', 1)
- args = s.split()
- args.append(trailing)
- else:
- args = s.split()
- command = args.pop(0)
- return prefix, command, args
- def nickParse(s):
- return s.split("!")[0]
- def fncLine(source,command,args):
- knownCallbacks = {
- "PING":fncPing,
- "PRIVMSG":fncPrivMsg,
- }
- try:
- if command in knownCallbacks.keys():
- return (knownCallbacks[command])(source,args)
- else:
- return False
- except Exception as e:
- print e
- return False
- def fncPing(source,args):
- irc.send("PONG "+args[0]+"\r\n")
- return False
- def google(components):
- response = ''
- terms = components['arguments'].split('!google ')
- if 2 == len(terms) and 1 <= len(terms[1].lstrip()):
- service = build("customsearch", "v1",
- developerKey="AIzaSyCy6tveUHlfNQDUtH0TJrF6PtU0h894S2I")
- res = service.cse().list(
- q = terms[1].lstrip(),
- cx = '005983647730461686104:qfayqkczxfg',
- ).execute()
- if 1 <= res['queries']['request'][0]['totalResults']:
- result = res['items'][0]
- response = result['link'] + '\r\n' + result['snippet']
- else:
- response = 'Not found: ' + terms[1]
- else:
- response = 'Usage: !google <search term>'
- return str(response.encode('utf8'))
- def fncPrivMsg(source,args):
- channel = args[0]
- text = args[1].replace("\r\n","")
- if source == "TPM" or source == "alt" or source == "alt2":
- #bot control stuff goes here
- if text == "=join":
- irc.send("JOIN #channel1,#channel2,#channel3\r\n")
- if text.startswith("=quit"):
- msg = text.replace("=quit ","")
- irc.send("QUIT :"+msg+"\r\n")
- if text.startswith("!x"):
- text = text[3:]
- irc.send(text+"\r\n")
- quit = True
- return True
- if text.startswith("=join"):
- msg = text.replace("=join ","")
- irc.send("JOIN :"+msg+"\r\n")
- if text.startswith("=part"):
- msg = text.replace("=part ","")
- irc.send("PART :"+msg+"\r\n")
- if text.startswith("=nick"):
- msg = text.replace("=nick ","")
- irc.send("NICK :"+msg+"\r\n")
- #fun stuff goes here
- pass
- if text.startswith("=say"):
- msg = text.replace("=say ","")
- irc.send("PRIVMSG "+channel+" :"+msg+"\r\n")
- if text.find("=hi") != -1:
- msg = text.split("=hi")
- to = t[hello].strip()
- irc.send("PRIVMSG "+channel+" :"+msg+"\r\n")
- if text.lower().find("keyword") != -1:
- msg = random.choice(listOfkeywordResponses)
- irc.send("PRIVMSG "+channel+" :"+msg+"\r\n")
- return False
- while True: # main loop
- try:
- data = irc.recv(8192)
- except:
- continue
- lines = []
- if data:
- lines = data.split("\r\n")
- for l in lines:
- line = ircParse(l)
- if line == None:
- continue
- print line
- source = nickParse(line[0])
- if source == line[0]: #from server (no nick), might as well blank
- source == ""
- command = line[1]
- args = line[2]
- if fncLine(source,command,args): # True when quitting
- quit = True
- break
- if quit:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement