Advertisement
Guest User

Untitled

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