Guest User

Untitled

a guest
Jul 21st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # This code was written for Python 3.1.1
  3. # version 0.101
  4.  
  5. # Changelog:
  6. # version 0.100
  7. # Basic framework
  8. #
  9. # version 0.101
  10. # Fixed an error if an admin used a command with an argument, that wasn't an admin-only command
  11.  
  12. import socket, sys, threading, time
  13.  
  14. # Hardcoding the root admin - it seems the best way for now
  15. root_admin = "Phurl"
  16.  
  17. # Defining a class to run the server. One per connection. This class will do most of our work.
  18. class IRC_Server:
  19.  
  20. # The default constructor - declaring our global variables
  21. # channel should be rewritten to be a list, which then loops to connect, per channel.
  22. # This needs to support an alternate nick.
  23. def __init__(self, host, port, nick, channel , password =""):
  24. self.irc_host = host
  25. self.irc_port = port
  26. self.irc_nick = nick
  27. self.irc_channel = channel
  28. self.irc_sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
  29. self.is_connected = False
  30. self.should_reconnect = False
  31. self.command = ""
  32.  
  33.  
  34. # This is the bit that controls connection to a server & channel.
  35. # It should be rewritten to allow multiple channels in a single server.
  36. # This needs to have an "auto identify" as part of its script, or support a custom connect message.
  37. def connect(self):
  38. self.should_reconnect = True
  39. try:
  40. self.irc_sock.connect ((self.irc_host, self.irc_port))
  41. except:
  42. print ("Error: Could not connect to IRC; Host: " + str(self.irc_host) + "Port: " + str(self.irc_port))
  43. exit(1) # We should make it recconect if it gets an error here
  44. print ("Connected to: " + str(self.irc_host) + ":" + str(self.irc_port))
  45.  
  46. str_buff = ("NICK %s \r\n") % (self.irc_nick)
  47. self.irc_sock.send (str_buff.encode())
  48. print ("Setting bot nick to " + str(self.irc_nick) )
  49.  
  50. str_buff = ("USER %s 8 * :X\r\n") % (self.irc_nick)
  51. self.irc_sock.send (str_buff.encode())
  52. print ("Setting User")
  53. # Insert Alternate nick code here.
  54.  
  55. # Insert Auto-Identify code here.
  56.  
  57. str_buff = ( "JOIN %s \r\n" ) % (self.irc_channel)
  58. self.irc_sock.send (str_buff.encode())
  59. print ("Joining channel " + str(self.irc_channel) )
  60. self.is_connected = True
  61. self.listen()
  62.  
  63. def listen(self):
  64. while self.is_connected:
  65. recv = self.irc_sock.recv( 4096 )
  66. print ("Got mesg %s " % str(recv))
  67. if str(recv).find ( "PING" ) != -1:
  68. self.irc_sock.send ( "PONG ".encode() + recv.split() [ 1 ] + "\r\n".encode() )
  69. if str(recv).find ( "PRIVMSG" ) != -1:
  70. irc_user_nick = str(recv).split ( '!' ) [ 0 ] . split ( ":")[1]
  71. irc_user_host = str(recv).split ( '@' ) [ 1 ] . split ( ' ' ) [ 0 ]
  72. irc_user_message = self.data_to_message(str(recv))
  73. print ( irc_user_nick + ": " + irc_user_message)
  74. # "!" Indicated a command
  75. if ( str(irc_user_message[0]) == "!" ):
  76. self.command = str(irc_user_message[1:])
  77. # (str(recv)).split()[2] ) is simply the channel the command was heard on.
  78. self.process_command(irc_user_nick, ( (str(recv)).split()[2] ) )
  79. if self.should_reconnect:
  80. self.connect()
  81.  
  82. def data_to_message(self,data):
  83. data = data[data.find(':')+1:len(data)]
  84. data = data[data.find(':')+1:len(data)]
  85. # print ("Got data %s " % str(data))
  86. # data = str(data[0:len(data)-5])
  87. # print ("Got data2 %s " % str(data))
  88. return data
  89.  
  90. # This function sends a message to a channel, which must start with a #.
  91. def send_message_to_channel(self,data,channel):
  92. print ( ( "%s: %s") % (self.irc_nick, data) )
  93. self.irc_sock.send( (("PRIVMSG %s :%s\r\n") % (channel, data)).encode() )
  94.  
  95. # This function takes a channel, which must start with a #.
  96. def join_channel(self,channel):
  97. if (channel[0] == "#"):
  98. str_buff = ( "JOIN %s \r\n" ) % (channel)
  99. self.irc_sock.send (str_buff.encode())
  100. # This needs to test if the channel is full
  101. # This needs to modify the list of active channels
  102.  
  103. # This function takes a channel, which must start with a #.
  104. def quit_channel(self,channel):
  105. if (channel[0] == "#"):
  106. str_buff = ( "PART %s \r\n" ) % (channel)
  107. self.irc_sock.send (str_buff.encode())
  108. # This needs to modify the list of active channels
  109.  
  110.  
  111. # This nice function here runs ALL the commands.
  112. # For now, we only have 2: root admin, and anyone.
  113. def process_command(self, user, channel):
  114. print ("Got command %s " % self.command)
  115. # This line makes sure an actual command was sent, not a plain "!"
  116. if ( len(self.command.split()) == 0):
  117. return
  118. # So the command isn't case sensitive
  119. command = (self.command).lower()
  120. # Break the command into pieces, so we can interpret it with arguments
  121. command = command.split()
  122.  
  123. print ("Got command length %d " % len(command) )
  124.  
  125. print ("Got user %s " % user )
  126.  
  127. if ( len(command) > 0):
  128. print ("Got command name %s " % command[0])
  129.  
  130. if ( len(command) > 1):
  131. print ("Got command arg %s " % command[1])
  132.  
  133. # All admin only commands go in here.
  134. if (user == root_admin):
  135. # The first set of commands are ones that don't take parameters
  136. if ( len(command) == 1):
  137.  
  138. #This command shuts the bot down.
  139. if (command[0] == "quit"):
  140. str_buff = ( "QUIT %s \r\n" ) % (channel)
  141. self.irc_sock.send (str_buff.encode())
  142. self.irc_sock.close()
  143. self.is_connected = False
  144. self.should_reconnect = False
  145.  
  146. # These commands take parameters
  147. else:
  148.  
  149. # This command makes the bot join a channel
  150. # This needs to be rewritten in a better way, to catch multiple channels
  151. if (command[0] == "join"):
  152. if ( (command[1])[0] == "#"):
  153. irc_channel = command[1]
  154. else:
  155. irc_channel = "#" + command[1]
  156. self.join_channel(irc_channel)
  157.  
  158. # This command makes the bot part a channel
  159. # This needs to be rewritten in a better way, to catch multiple channels
  160. if (command[0] == "part"):
  161. if ( (command[1])[0] == "#"):
  162. irc_channel = command[1]
  163. else:
  164. irc_channel = "#" + command[1]
  165. self.quit_channel(irc_channel)
  166.  
  167.  
  168. # All public commands go here
  169. # The first set of commands are ones that don't take parameters
  170. if ( len(command) == 1):
  171.  
  172. if (command[0] == "hi"):
  173. self.send_message_to_channel( ("Hello to you too, " + user), channel )
  174. if (command[0] == "moo"):
  175. self.send_message_to_channel( ("MOO yourself, " + user), channel )
  176. if (command[0] == "train"):
  177. self.send_message_to_channel( ("Choo Choo! It's the MysteryTrain!"), channel )
  178. if (command[0] == "poo"):
  179. self.send_message_to_channel( ("Don't be a potty mouth"), channel )
  180. if (command[0] == "readnext"):
  181. self.send_message_to_channel( ("Visit whatshouldIreadnext.com"), channel )
  182. else:
  183. if (command[0] == "bop"):
  184. self.send_message_to_channel( ("\x01ACTION bopz " + str(command[1]) + "\x01"), channel )
  185.  
  186.  
  187. # Here begins the main programs flow:
  188.  
  189. test = IRC_Server("irc.freenode.net", 6667, "mikebot", "#osmbot")
  190. run_test = threading.Thread(None, test.connect)
  191. run_test.start()
  192.  
  193. while (test.should_reconnect):
  194. time.sleep(5)
Add Comment
Please, Sign In to add comment