Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 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 = ["##bodsda"]
  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.             command = message()
  65.  
  66.         if os.path.isfile("factoids"):
  67.             file = open("factoids", 'rb')
  68.             factoids = pickle.load(file)
  69.             file.close()
  70.          
  71.  
  72.         elif not os.path.isfile("factoids"):
  73.             factoids  = {}
  74.  
  75.         if factoids.has_key(command):
  76.             if sendto != "":
  77.                 irc_funcs.privmsg(channel, "%s: %s" % (sendto, factoids[command]))
  78.  
  79.             elif sendto == "":
  80.                 irc_funcs.privmsg(channel, factoids[command])
  81.  
  82.         else:
  83.             irc_funcs.privmsg(channel, "%s: %s" % (sender, notfound))
  84.  
  85.  
  86.        
  87.  
  88.     def auto_response(self, message, channel, sender):
  89.         pass
  90.                    
  91.  
  92.  
  93.  
  94. class irc_functions():
  95.  
  96.     def join(self, channel):
  97.         user_funcs.raw_send("JOIN %s" % channel)
  98.         print "Joined: %s" % channel
  99.  
  100.     def privmsg(self, channel, message):
  101.         user_funcs.raw_send("PRIVMSG %s :%s" % (channel, message))
  102.         print "Sent, %s: \"%s\"" % (channel, message)
  103.  
  104.  
  105. irc_funcs = irc_functions()
  106. user_funcs = user_functions()
  107.  
  108. user_funcs.raw_send("PASS %s" % PASS)
  109. user_funcs.raw_send("NICK %s" % USERNAME)
  110. user_funcs.raw_send("USER %s NULL NULL :%s" %(USERNAME, REALNAME))
  111.  
  112. received = ""
  113. while "Thank you" not in received:
  114.     received = user_funcs.receive()
  115.     print received
  116.  
  117. for i in CHANNELLIST:
  118.     irc_funcs.join(i)
  119.  
  120.  
  121. while True:
  122.     try:
  123.         received = user_funcs.receive()
  124.         if RAW_LOG:
  125.             raw = open("./logs/raw.log", 'a')
  126.             raw.write(received + "\n")
  127.             raw.close()
  128.         else:
  129.             pass
  130.  
  131.         if "PING :" in received:
  132.             server = received.split(":")[1]
  133.             user_funcs.raw_send("PONG %s" % server)
  134.  
  135.         if "PRIVMSG" in received:
  136.             channel, message, sender = user_funcs.msg_split(received)
  137.             log = open("./logs/%s.log" % channel, 'a')
  138.             log.write("%s\t: %s\n" % (sender, message))
  139.             log.close()
  140.  
  141.             if message.startswith(DELIMITER):
  142.                 user_funcs.factoids(message, channel, sender)
  143.  
  144.             if message.endswith("!!!"):
  145.                 user_funcs.auto_response(message, channel, sender)
  146.  
  147.     except KeyboardInterrupt:
  148.         try:
  149.             raw.close()
  150.             log.close()
  151.         except NameError:
  152.             pass
  153.  
  154.         irc_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement