Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.54 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 = "Night"
  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.             if str(recv).find ( "PING" ) != -1:
  67.                 self.irc_sock.send ( "PONG ".encode() + recv.split() [ 1 ] + "\r\n".encode() )
  68.             if str(recv).find ( "PRIVMSG" ) != -1:
  69.                 irc_user_nick = str(recv).split ( '!' ) [ 0 ] . split ( ":")[1]
  70.                 irc_user_host = str(recv).split ( '@' ) [ 1 ] . split ( ' ' ) [ 0 ]
  71.                 irc_user_message = self.data_to_message(str(recv))
  72.                 print ( irc_user_nick + ": " + irc_user_message)
  73.                 # "!" Indicated a command
  74.                 if ( str(irc_user_message[0]) == "!" ):
  75.                     self.command = str(irc_user_message[1:])
  76.                     # (str(recv)).split()[2] ) is simply the channel the command was heard on.
  77.                     self.process_command(irc_user_nick, ( (str(recv)).split()[2] ) )
  78.         if self.should_reconnect:
  79.             self.connect()
  80.  
  81.     def data_to_message(self,data):
  82.         data = data[data.find(':')+1:len(data)]
  83.         data = data[data.find(':')+1:len(data)]
  84.         data = str(data[0:len(data)-5])
  85.         return data
  86.  
  87.     # This function sends a message to a channel, which must start with a #.
  88.     def send_message_to_channel(self,data,channel):
  89.         print ( ( "%s: %s") % (self.irc_nick, data) )
  90.         self.irc_sock.send( (("PRIVMSG %s :%s\r\n") % (channel, data)).encode() )
  91.  
  92.     # This function takes a channel, which must start with a #.
  93.     def join_channel(self,channel):
  94.         if (channel[0] == "#"):
  95.             str_buff = ( "JOIN %s \r\n" ) % (channel)
  96.             self.irc_sock.send (str_buff.encode())
  97.             # This needs to test if the channel is full
  98.             # This needs to modify the list of active channels
  99.  
  100.     # This function takes a channel, which must start with a #.
  101.     def quit_channel(self,channel):
  102.         if (channel[0] == "#"):
  103.             str_buff = ( "PART %s \r\n" ) % (channel)
  104.             self.irc_sock.send (str_buff.encode())
  105.             # This needs to modify the list of active channels
  106.  
  107.  
  108.     # This nice function here runs ALL the commands.
  109.     # For now, we only have 2: root admin, and anyone.
  110.     def process_command(self, user, channel):
  111.         # This line makes sure an actual command was sent, not a plain "!"
  112.         if ( len(self.command.split()) == 0):
  113.             return
  114.         # So the command isn't case sensitive
  115.         command = (self.command).lower()
  116.         # Break the command into pieces, so we can interpret it with arguments
  117.         command = command.split()
  118.  
  119.         # All admin only commands go in here.
  120.         if (user == root_admin):
  121.             # The first set of commands are ones that don't take parameters
  122.             if ( len(command) == 1):
  123.  
  124.                 #This command shuts the bot down.
  125.                 if (command[0] == "quit"):
  126.                     str_buff = ( "QUIT %s \r\n" ) % (channel)
  127.                     self.irc_sock.send (str_buff.encode())
  128.                     self.irc_sock.close()
  129.                     self.is_connected = False
  130.                     self.should_reconnect = False
  131.  
  132.             # These commands take parameters
  133.             else:
  134.  
  135.                 # This command makes the bot join a channel
  136.                 # This needs to be rewritten in a better way, to catch multiple channels
  137.                 if (command[0] == "join"):
  138.                     if ( (command[1])[0] == "#"):
  139.                         irc_channel = command[1]
  140.                     else:
  141.                         irc_channel = "#" + command[1]
  142.                     self.join_channel(irc_channel)
  143.  
  144.                 # This command makes the bot part a channel
  145.                 # This needs to be rewritten in a better way, to catch multiple channels
  146.                 if (command[0] == "part"):
  147.                     if ( (command[1])[0] == "#"):
  148.                         irc_channel = command[1]
  149.                     else:
  150.                         irc_channel = "#" + command[1]
  151.                     self.quit_channel(irc_channel)
  152.  
  153.  
  154.         # All public commands go here
  155.         # The first set of commands are ones that don't take parameters
  156.         if ( len(command) == 1):
  157.  
  158.             if (command[0] == "hi"):
  159.                 self.send_message_to_channel( ("Hello to you too, " + user), channel )
  160.             if (command[0] == "moo"):
  161.                 self.send_message_to_channel( ("MOO yourself, " + user), channel )
  162.             if (command[0] == "train"):
  163.                 self.send_message_to_channel( ("Choo Choo! It's the MysteryTrain!"), channel )
  164.             if (command[0] == "poo"):
  165.                 self.send_message_to_channel( ("Don't be a potty mouth"), channel )
  166.             if (command[0] == "readnext"):
  167.                 self.send_message_to_channel( ("Visit whatshouldIreadnext.com"), channel )
  168.         else:
  169.             if (command[0] == "bop"):
  170.                 self.send_message_to_channel( ("\x01ACTION bopz " + str(command[1]) + "\x01"), channel )
  171.  
  172.  
  173. # Here begins the main programs flow:
  174.  
  175. test = IRC_Server("127.0.0.1", 6667, "masbot", "#schoentoon")
  176. run_test = threading.Thread(None, test.connect)
  177. run_test.start()
  178.  
  179. while (test.should_reconnect):
  180.     time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement