Advertisement
Guest User

Untitled

a guest
May 10th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #! /usr/bin/env python
  2. from socket import *
  3. import pickle
  4. import os
  5.  
  6. HOST = "irc.freenode.net"
  7. PORT = 6667
  8. USERNAME = "Bubblebot"
  9. REALNAME = "Bubble bot"
  10. PASS = "Bubblebot"
  11. CHANNELLIST = ["#ubuntu-beginners-dev"]
  12. RAW_LOG = False
  13. DELIMITER = "*"
  14.  
  15. irc_socket = socket(AF_INET, SOCK_STREAM)
  16. irc_socket.connect((HOST, PORT))
  17.  
  18. if os.path.isdir("./logs"):
  19. pass
  20. else:
  21. os.mkdir("./logs")
  22.  
  23. class user_functions():
  24.  
  25. def raw_send(self, data):
  26. irc_socket.send(data + "\r\n")
  27. print "Sent: \"%s\" \r\n" % data
  28.  
  29. def receive(self, buffer_size = 1024):
  30. return irc_socket.recv(buffer_size)
  31.  
  32. def msg_split(self, message):
  33. try:
  34. channel = message.split("PRIVMSG")[1].split(":")[0].strip()
  35. print "Channel stripped\t: %s" % channel
  36.  
  37. msg = message.split("PRIVMSG")[1].split(":", 1)[1].rstrip()
  38. print "Message stripped\t: %s" % msg
  39.  
  40. sender = message.split("PRIVMSG")[0].split(":")[1].split("!")[0]
  41. print "Sender stripped\t\t: %s\n" % sender
  42.  
  43. return channel, msg, sender
  44.  
  45. except IndexError:
  46. print "Not a valid PRIVMSG"
  47. return 1
  48.  
  49.  
  50. def say(self, message, channel):
  51. irc_funcs.privmsg(channel, message)
  52. print "Told to say: %s" % message
  53.  
  54. def factoids(self, message, channel, sender):
  55. sendto = ""
  56. notfound = "You hear that Mr. Anderson?... That is the sound of inevitability..... Or maybe its the sound of me not having the factoid :)"
  57. try:
  58. command = message.split(DELIMITER)[1]
  59.  
  60. if '|' in message:
  61. sendto = message.split("|")[1].lstrip()
  62.  
  63. except IndexError:
  64. print "Excpeted index error"
  65. command = message()
  66.  
  67. if os.path.isfile("factoids"):
  68. file = open("factoids", 'rb')
  69. factoids = pickle.load(file)
  70. file.close()
  71.  
  72.  
  73. elif not os.path.isfile("factoids"):
  74. factoids = {}
  75.  
  76. try:
  77. if message.split()[1] == "is":
  78. message = message.split()
  79. message.pop(0)
  80. message.pop(0)
  81. new_factoid = ""
  82.  
  83. for i in message:
  84. new_factoid += i
  85.  
  86. factoids[command] = new_factoid
  87. print "%s: is %s" % (command, new_factoid)
  88. return 0
  89. except IndexError:
  90. pass
  91.  
  92. if factoids.has_key(command):
  93. print "command is:", command
  94. if command == "quit":
  95. irc_funcs.privmsg(channel, "Im off, my bits are out of sync.")
  96. quit()
  97.  
  98. if sendto != "":
  99. irc_funcs.privmsg(channel, "%s: %s" % (sendto, factoids[command]))
  100. return 0
  101.  
  102. elif sendto == "":
  103. irc_funcs.privmsg(channel, factoids[command])
  104. return 0
  105.  
  106. else:
  107. irc_funcs.privmsg(channel, "%s: %s" % (sender, notfound))
  108.  
  109.  
  110.  
  111.  
  112. def auto_response(self, message, channel, sender):
  113. pass
  114.  
  115.  
  116.  
  117.  
  118. class irc_functions():
  119.  
  120. def join(self, channel):
  121. user_funcs.raw_send("JOIN %s" % channel)
  122. print "Joined: %s" % channel
  123.  
  124. def privmsg(self, channel, message):
  125. user_funcs.raw_send("PRIVMSG %s :%s" % (channel, message))
  126. print "Sent, %s: \"%s\"" % (channel, message)
  127.  
  128.  
  129. irc_funcs = irc_functions()
  130. user_funcs = user_functions()
  131.  
  132. user_funcs.raw_send("PASS %s" % PASS)
  133. user_funcs.raw_send("NICK %s" % USERNAME)
  134. user_funcs.raw_send("USER %s NULL NULL :%s" %(USERNAME, REALNAME))
  135.  
  136. received = ""
  137. while "Thank you" not in received:
  138. received = user_funcs.receive()
  139. print received
  140.  
  141. for i in CHANNELLIST:
  142. irc_funcs.join(i)
  143.  
  144.  
  145. while True:
  146. try:
  147. received = user_funcs.receive()
  148. if RAW_LOG:
  149. raw = open("./logs/raw.log", 'a')
  150. raw.write(received + "\n")
  151. raw.close()
  152. else:
  153. pass
  154.  
  155. if "PING :" in received:
  156. server = received.split(":")[1]
  157. user_funcs.raw_send("PONG %s" % server)
  158.  
  159. if "PRIVMSG" in received:
  160. channel, message, sender = user_funcs.msg_split(received)
  161. log = open("./logs/%s.log" % channel, 'a')
  162. log.write("%s\t: %s\n" % (sender, message))
  163. log.close()
  164.  
  165. if message.startswith(DELIMITER):
  166. user_funcs.factoids(message, channel, sender)
  167.  
  168. if message.endswith("!!!"):
  169. user_funcs.auto_response(message, channel, sender)
  170.  
  171. except KeyboardInterrupt:
  172. try:
  173. raw.close()
  174. log.close()
  175. except NameError:
  176. pass
  177.  
  178. irc_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement