Advertisement
Guest User

PircBot v0.1(?)

a guest
Jan 19th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. import socket
  2. import os
  3. import time
  4. import sys
  5.  
  6. class connection():
  7.  
  8.     nick = "testerPiBot"
  9.     realname = nick
  10.     ident = nick
  11.     channel = "#raspberrypi"
  12.     nickList = []
  13.     away = True
  14.  
  15.     def __init__(self, address):
  16.         self.address = address
  17.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  18.        
  19.         self.sock.connect((socket.gethostbyname(address), 6667))
  20.         self.sock.send("USER %s %s %s :%s\r\n" % (self.ident, address, "UNIX-SERVER", self.realname))
  21.         self.sock.send("NICK %s\r\n" % (self.nick))
  22.         self.sock.send("JOIN %s\r\n" % (self.channel))
  23.         self.away = False
  24.     def server_response(self):
  25.         response = self.sock.recv(1024)
  26.         if "PING :" in response[:7] and "PRIVMSG" not in response:
  27.             self.sock.send( response.replace("PING", "PONG" ))
  28.             return 0
  29.         if "!" in response and ":" in response[response.index(":") + 1:]:
  30.             return response
  31.     def send_message(self, message):
  32.         if not message:
  33.             return False
  34.         if self.channel:
  35.             message_tuple = ( self.nick, self.ident, self.address, self.channel, message)
  36.             return self.sock.send(":%s!%s@%s PRIVMSG %s :%s\r\n" % message_tuple)
  37.     def parse_message(self, message):
  38.         nick = message[message.index(":"):message.index("!")]
  39.         if nick not in self.nickList:
  40.             self.nickList.append(nick)
  41.         message = message[message.index(":")+1:]
  42.         message = message[message.index(":"):]
  43.         return "%s %s" % (nick, message)
  44.     def close(self):
  45.         self.sock.close()
  46.  
  47.  
  48. connected = True
  49. server = connection( "irc.freenode.net" )
  50. qouteDict = {
  51.     "are they for sale yet":"Not yet, but hold on because it's not going to be much longer. ;)",
  52.     "can I buy one yet":"Not yet, but hold on because it's not going to be much longer. ;)",
  53.     "when can I buy a raspberry pi":"Likely later in January or early Febuary. Keep an eye on the store, http://www.raspberrypi.com, and subscribe to the mailing list.",
  54.     "when can I buy one":"Likely later in January or early February. Keep an eye on the store!",
  55.     "where can I buy a raspberry pi":"From the raspberry pi store, of course, after it's released, at http://www.raspberrypi.com.",
  56.     "will it run windows":"Currently no, and likely not in the foreseeable future, due to the limited resources and the processor achetecture. Feel free to take a look around linux!",
  57.     "can it run crysis on max":"Of course, and it also flies and plays folky drum and bass.",
  58.     "how much is a raspberry pi":"Model A will be $25, a model B will be $35. This is of course not including any peripherals."
  59.     }
  60. trustedNicks = [ "mrdragons", "mrDragnos", "mrdragon1" ]
  61.  
  62. try:
  63.     while connected:
  64.         response = server.server_response()
  65.         if response:
  66.             print(response)
  67.             message = server.parse_message(response.lower())
  68.             messageRay = message.split(" ")
  69.             tempMessage = server.parse_message(response)
  70.             tempMessageRay = message.split(" ")
  71.  
  72.             #print(message + " \n > " + message[len(messageRay[0])+2:])
  73.             print(qouteDict)
  74.             if server.away == False:
  75.                 if "kill pibot" in message:
  76.                     server.close()
  77.                 elif "hello pibot" in message or "hey pibot" in message or "hi pibot" in message:
  78.                     server.send_message("Hello thar, %s!" % messageRay[0].strip(":"))
  79.                 elif "what's the time?" in message or "what time is it?" in message:
  80.                     server.send_message("%s: Adventure time! (And %s.)" % ( messageRay[0].strip(":"), time.strftime("%A, %B %d, %H:%M EST")))
  81.                 elif "go away" in message and "pibot" in message:
  82.                     server.away = True
  83.                     server.send_message("Fine, tell me when you want me back...")
  84.                
  85.                 elif "pibot" in messageRay[1] and "remember" in messageRay[2] and "=" in message:
  86.                     if tempMessageRay[0].strip(":") in trustedNicks:
  87.                         findPos = message.find("=")
  88.                         startPos = len(messageRay[0]) + 2 + len(messageRay[1]) + 1 + len(messageRay[2])
  89.                         qouteDict.update({message[startPos:findPos]:message[findPos+1:len(message)-2]})
  90.                         #qouteDict.update({message[:findPos]:message[findPos+1:-2]})
  91.                         server.send_message("Okay, I'll remember that.")
  92.                     else:
  93.                         server.send_message("%s: I don't trust you. >_>" % messageRay[0].strip(":"))
  94.                 elif "pibot" in message[1:] and "add trusted nick" in message and '\"' in message:
  95.                     if tempMessageRay[0].strip(":") in trustedNicks:
  96.                         startPos = message.find('\"')+1
  97.                         trustedNicks.append(tempMessage[startPos:-3])
  98.                         print(trustedNicks)
  99.                         server.send_message("User added successfully.")
  100.                     else:
  101.                         server.send_message("I don't trust you. >_>")
  102.                 elif "fuck" in message and "pibot" in message:
  103.                     server.send_message("%s: Only if you bring your own condoms." % messageRay[0].strip(":"))
  104.                 elif len(messageRay) > 1:
  105.                     mostSimilar = 100
  106.                     sendQoute = "test"
  107.                     for qoute in qouteDict:
  108.                         counter=0
  109.                         similarity=0
  110.                         qouteRay= qoute.split(" ")
  111.                         for word in qouteRay:
  112.                             if word in message:
  113.                                 similarity += 1
  114.                             counter+=1
  115.                         #if counter-similarity < len(qouteRay)/2:
  116.                         if counter-similarity < mostSimilar:
  117.                             mostSimilar = counter-similarity
  118.                             sendQoute = qoute
  119.                     if mostSimilar < len(qouteRay)/2:
  120.                         server.send_message("%s" % ( qouteDict[sendQoute] ))
  121.  
  122.             if "come back" in message and "pibot" in message:
  123.                 server.send_message("I'ma back")
  124.                 server.away = False
  125.  
  126. except KeyboardInterrupt:
  127.     server.close()
  128.     print("Exitted.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement