Guest User

Untitled

a guest
Mar 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # config at the way bottom
  4. # written in ~30 mins at like 6am
  5. #
  6.  
  7. import socket
  8.  
  9. STATE_DISCONNECTED, STATE_HANDSHAKE, STATE_CONNECTED = range(3)
  10. BUFFER_SIZE = 1024
  11.  
  12. class Pynsour:
  13. def __init__(self):
  14. self.server = ""
  15. self.port = -1
  16. self.nick = ""
  17. self.altnick = ""
  18. self.username = ""
  19. self.realname = ""
  20. self.server = ""
  21. self.password = ""
  22. self.state = STATE_DISCONNECTED
  23. self.channels = ""
  24.  
  25. self.buffer = ""
  26.  
  27. def connect(self):
  28. channels = {}
  29. for w in self.channels:
  30. info = w.split(" ")
  31. if len(info) == 1:
  32. channel = w
  33. key = ""
  34. elif len(info) == 2:
  35. channel, key = info
  36.  
  37. channels[channel] = key
  38.  
  39. self.channels = channels
  40.  
  41. print ">>> Channels: %s" % str(channels)
  42.  
  43. print ">>> Creating socket"
  44. self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  45. print ">>> Connecting to %s:%s" % (self.server, self.port)
  46. self.connection.connect((self.server, self.port))
  47.  
  48. print ">>> Running"
  49. self.run()
  50.  
  51. def send(self, msg):
  52. print "+++ %s" % msg
  53. self.connection.send("%s\r\n" % msg)
  54.  
  55. def run(self):
  56. while True:
  57. if not self.parse():
  58. break
  59. elif self.state == STATE_DISCONNECTED:
  60. self.send("NICK %s" % self.nick)
  61. self.send("USER %s %s %s :%s" %
  62. (self.username,
  63. self.hostname,
  64. self.server,
  65. self.realname))
  66.  
  67. if self.password != "":
  68. self.connection.send("PASS %s\r\n", self.password)
  69.  
  70. self.state = STATE_HANDSHAKE
  71.  
  72.  
  73. def parse(self):
  74. incoming = self.connection.recv(BUFFER_SIZE)
  75.  
  76. self.buffer += incoming
  77. buffer = self.buffer.split("\r\n")
  78.  
  79. print "BUFFER { %s }" % buffer
  80.  
  81. if self.buffer[:-2] == "\r\n":
  82. self.buffer = ""
  83. else:
  84. self.buffer = buffer[-1]
  85. buffer = buffer[:-1]
  86.  
  87. for line in buffer:
  88. if not line:
  89. continue
  90.  
  91. if line[0] == ":":
  92. line = line[1:]
  93.  
  94. if ":" in line:
  95. first, second = line.split(":", 1)
  96. print ">>> line: '%s'" % line
  97. # print " >>> first: '%s'" % first
  98. print " >>> second: '%s'" % second
  99. words = first.strip().split(" ")
  100. words += [second]
  101.  
  102. self.irc_parse(words)
  103. else:
  104. print ">>> just spaces"
  105. words = line.split(" ")
  106.  
  107. # print "words: %s" % str(words)
  108.  
  109. return True
  110.  
  111. def irc_parse(self, words):
  112. if len(words) == 4:
  113. src, msg_type, dest, msg = words
  114.  
  115. if msg_type == "PRIVMSG":
  116. self.handler_privmsg(src, dest, msg)
  117. elif msg_type == "001":
  118. self.state = STATE_CONNECTED
  119. self.autojoin()
  120.  
  121. elif len(words) == 2:
  122. msg_type, msg = words
  123.  
  124. if msg_type == "PING":
  125. self.handler_pong(msg)
  126.  
  127. def autojoin(self):
  128. for channel, key in self.channels.items():
  129. self.send("JOIN %s %s" % (channel, key))
  130.  
  131. def handler_pong(self, msg):
  132. self.send("PONG :%s" % msg)
  133.  
  134. def handler_privmsg(self, src, dest, msg):
  135. if dest == self.nick:
  136. self.script(src, "__self__", msg)
  137. elif dest in self.channels.keys():
  138. self.script(src, dest, msg)
  139.  
  140. def script(self, src, dest, msg):
  141. print ">>> script %s %s %s" % tuple(map(str, [src, dest, msg]))
  142.  
  143. src_nick = src.split("!")[0]
  144.  
  145. if dest == "__self__":
  146. words = msg.split(" ")
  147. if words[0] == "hello":
  148. self.send("PRIVMSG %s :%s" % (src_nick, "oh hy"))
  149. elif words[0] == ".py":
  150. self.python_handler(src_nick, msg)
  151. elif words[0] == "\x01VERSION\x01":
  152. self.send("NOTICE %s :\x01VERSION %s:%s:%s\x01" %
  153. (src_nick, "pynsour", "0.1", "your mother"))
  154. else:
  155. words = msg.split(" ")
  156. if words[0] == "hello":
  157. self.send("PRIVMSG %s :%s" % (dest, "oh hy"))
  158. elif words[0] == ".py":
  159. self.python_handler(dest, msg)
  160.  
  161. def python_handler(self, dest, msg):
  162. msg = msg.split(" ", 1)[1].strip()
  163.  
  164. print ">>> python handler %s %s" % tuple(map(str, [dest, msg]))
  165.  
  166. out = ""
  167. banned = "eval exit sys os import file open rm rf".split(" ")
  168. for b in banned:
  169. if b in msg:
  170. out = "don't do that."
  171.  
  172. print ">>> out = %s" % out
  173.  
  174. if out == "":
  175. try:
  176. print ">>> eval(%s)" % msg
  177. out = eval(msg)
  178. print ">>> out = %s" % str(out)
  179. except Exception, e:
  180. out = e
  181.  
  182. self.send("PRIVMSG %s :%s" % (dest, str(out)))
  183.  
  184. def main():
  185. bot = Pynsour()
  186. bot.server = "euclid.poundcs.org"
  187. bot.port = 6667
  188. bot.nick = "piefusion"
  189. bot.altnick = "piefusion"
  190. bot.username = "piefusion"
  191. bot.realname = "Pie Fusion"
  192. bot.hostname = "localhost"
  193.  
  194. bot.channels = ["#cs"]
  195.  
  196. bot.connect()
  197.  
  198. if __name__ == "__main__":
  199. main()
Add Comment
Please, Sign In to add comment