Advertisement
Scr7

python

Mar 22nd, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. import socket
  2. import random
  3. random.seed()
  4. network = 'irc.darkmyst.net'
  5. port = 6667
  6. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. irc.connect((network,port))
  8. data = irc.recv(4096)
  9. nick = "Meisterbot"
  10. irc.send("NICK {}\r\n".format(nick))
  11. irc.send("USER {0} 0 {0} :{0}\r\n".format(nick))
  12. irc.settimeout(0.1)
  13. quit = False
  14.  
  15. listOfkeywordResponses = [
  16. "responses go here",
  17. "and are written like this",
  18. ]
  19.  
  20. def ircParse(s): #courtesy of Twisted, http://stackoverflow.com/questions/930700/python-parsing-irc-messages
  21. prefix = ''
  22. trailing = []
  23. if not s:
  24. return None
  25. if s[0] == ':':
  26. try:
  27. prefix, s = s[1:].split(' ', 1)
  28. except:
  29. print "[~] Malformed data received: ",s
  30. if s.find(' :') != -1:
  31. s, trailing = s.split(' :', 1)
  32. args = s.split()
  33. args.append(trailing)
  34. else:
  35. args = s.split()
  36. command = args.pop(0)
  37. return prefix, command, args
  38.  
  39. def nickParse(s):
  40. return s.split("!")[0]
  41.  
  42. def fncLine(source,command,args):
  43. knownCallbacks = {
  44. "PING":fncPing,
  45. "PRIVMSG":fncPrivMsg,
  46. }
  47. try:
  48. if command in knownCallbacks.keys():
  49. return (knownCallbacks[command])(source,args)
  50. else:
  51. return False
  52. except Exception as e:
  53. print e
  54. return False
  55.  
  56. def fncPing(source,args):
  57. irc.send("PONG "+args[0]+"\r\n")
  58. return False
  59.  
  60. def google(components):
  61. response = ''
  62.  
  63. terms = components['arguments'].split('!google ')
  64.  
  65. if 2 == len(terms) and 1 <= len(terms[1].lstrip()):
  66. service = build("customsearch", "v1",
  67. developerKey="AIzaSyCy6tveUHlfNQDUtH0TJrF6PtU0h894S2I")
  68.  
  69. res = service.cse().list(
  70. q = terms[1].lstrip(),
  71. cx = '005983647730461686104:qfayqkczxfg',
  72. ).execute()
  73.  
  74. if 1 <= res['queries']['request'][0]['totalResults']:
  75. result = res['items'][0]
  76. response = result['link'] + '\r\n' + result['snippet']
  77.  
  78. else:
  79. response = 'Not found: ' + terms[1]
  80.  
  81. else:
  82. response = 'Usage: !google <search term>'
  83.  
  84. return str(response.encode('utf8'))
  85.  
  86. def fncPrivMsg(source,args):
  87. channel = args[0]
  88. text = args[1].replace("\r\n","")
  89. if source == "TPM" or source == "alt" or source == "alt2":
  90. #bot control stuff goes here
  91. if text == "=join":
  92. irc.send("JOIN #channel1,#channel2,#channel3\r\n")
  93. if text.startswith("=quit"):
  94. msg = text.replace("=quit ","")
  95. irc.send("QUIT :"+msg+"\r\n")
  96. if text.startswith("!x"):
  97. text = text[3:]
  98. irc.send(text+"\r\n")
  99. quit = True
  100. return True
  101. if text.startswith("=join"):
  102. msg = text.replace("=join ","")
  103. irc.send("JOIN :"+msg+"\r\n")
  104. if text.startswith("=part"):
  105. msg = text.replace("=part ","")
  106. irc.send("PART :"+msg+"\r\n")
  107. if text.startswith("=nick"):
  108. msg = text.replace("=nick ","")
  109. irc.send("NICK :"+msg+"\r\n")
  110. #fun stuff goes here
  111. pass
  112. if text.startswith("=say"):
  113. msg = text.replace("=say ","")
  114. irc.send("PRIVMSG "+channel+" :"+msg+"\r\n")
  115. if text.find("=hi") != -1:
  116. msg = text.split("=hi")
  117. to = t[hello].strip()
  118. irc.send("PRIVMSG "+channel+" :"+msg+"\r\n")
  119. if text.lower().find("keyword") != -1:
  120. msg = random.choice(listOfkeywordResponses)
  121. irc.send("PRIVMSG "+channel+" :"+msg+"\r\n")
  122.  
  123. return False
  124.  
  125. while True: # main loop
  126. try:
  127. data = irc.recv(8192)
  128. except:
  129. continue
  130.  
  131. lines = []
  132. if data:
  133. lines = data.split("\r\n")
  134. for l in lines:
  135. line = ircParse(l)
  136. if line == None:
  137. continue
  138. print line
  139. source = nickParse(line[0])
  140. if source == line[0]: #from server (no nick), might as well blank
  141. source == ""
  142. command = line[1]
  143. args = line[2]
  144. if fncLine(source,command,args): # True when quitting
  145. quit = True
  146. break
  147. if quit:
  148. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement