Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import os
- import time
- import sys
- class connection():
- nick = "testerPiBot"
- realname = nick
- ident = nick
- channel = "#raspberrypi"
- nickList = []
- away = True
- def __init__(self, address):
- self.address = address
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.connect((socket.gethostbyname(address), 6667))
- self.sock.send("USER %s %s %s :%s\r\n" % (self.ident, address, "UNIX-SERVER", self.realname))
- self.sock.send("NICK %s\r\n" % (self.nick))
- self.sock.send("JOIN %s\r\n" % (self.channel))
- self.away = False
- def server_response(self):
- response = self.sock.recv(1024)
- if "PING :" in response[:7] and "PRIVMSG" not in response:
- self.sock.send( response.replace("PING", "PONG" ))
- return 0
- if "!" in response and ":" in response[response.index(":") + 1:]:
- return response
- def send_message(self, message):
- if not message:
- return False
- if self.channel:
- message_tuple = ( self.nick, self.ident, self.address, self.channel, message)
- return self.sock.send(":%s!%s@%s PRIVMSG %s :%s\r\n" % message_tuple)
- def parse_message(self, message):
- nick = message[message.index(":"):message.index("!")]
- if nick not in self.nickList:
- self.nickList.append(nick)
- message = message[message.index(":")+1:]
- message = message[message.index(":"):]
- return "%s %s" % (nick, message)
- def close(self):
- self.sock.close()
- connected = True
- server = connection( "irc.freenode.net" )
- qouteDict = {
- "are they for sale yet":"Not yet, but hold on because it's not going to be much longer. ;)",
- "can I buy one yet":"Not yet, but hold on because it's not going to be much longer. ;)",
- "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.",
- "when can I buy one":"Likely later in January or early February. Keep an eye on the store!",
- "where can I buy a raspberry pi":"From the raspberry pi store, of course, after it's released, at http://www.raspberrypi.com.",
- "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!",
- "can it run crysis on max":"Of course, and it also flies and plays folky drum and bass.",
- "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."
- }
- trustedNicks = [ "mrdragons", "mrDragnos", "mrdragon1" ]
- try:
- while connected:
- response = server.server_response()
- if response:
- print(response)
- message = server.parse_message(response.lower())
- messageRay = message.split(" ")
- tempMessage = server.parse_message(response)
- tempMessageRay = message.split(" ")
- #print(message + " \n > " + message[len(messageRay[0])+2:])
- print(qouteDict)
- if server.away == False:
- if "kill pibot" in message:
- server.close()
- elif "hello pibot" in message or "hey pibot" in message or "hi pibot" in message:
- server.send_message("Hello thar, %s!" % messageRay[0].strip(":"))
- elif "what's the time?" in message or "what time is it?" in message:
- server.send_message("%s: Adventure time! (And %s.)" % ( messageRay[0].strip(":"), time.strftime("%A, %B %d, %H:%M EST")))
- elif "go away" in message and "pibot" in message:
- server.away = True
- server.send_message("Fine, tell me when you want me back...")
- elif "pibot" in messageRay[1] and "remember" in messageRay[2] and "=" in message:
- if tempMessageRay[0].strip(":") in trustedNicks:
- findPos = message.find("=")
- startPos = len(messageRay[0]) + 2 + len(messageRay[1]) + 1 + len(messageRay[2])
- qouteDict.update({message[startPos:findPos]:message[findPos+1:len(message)-2]})
- #qouteDict.update({message[:findPos]:message[findPos+1:-2]})
- server.send_message("Okay, I'll remember that.")
- else:
- server.send_message("%s: I don't trust you. >_>" % messageRay[0].strip(":"))
- elif "pibot" in message[1:] and "add trusted nick" in message and '\"' in message:
- if tempMessageRay[0].strip(":") in trustedNicks:
- startPos = message.find('\"')+1
- trustedNicks.append(tempMessage[startPos:-3])
- print(trustedNicks)
- server.send_message("User added successfully.")
- else:
- server.send_message("I don't trust you. >_>")
- elif "fuck" in message and "pibot" in message:
- server.send_message("%s: Only if you bring your own condoms." % messageRay[0].strip(":"))
- elif len(messageRay) > 1:
- mostSimilar = 100
- sendQoute = "test"
- for qoute in qouteDict:
- counter=0
- similarity=0
- qouteRay= qoute.split(" ")
- for word in qouteRay:
- if word in message:
- similarity += 1
- counter+=1
- #if counter-similarity < len(qouteRay)/2:
- if counter-similarity < mostSimilar:
- mostSimilar = counter-similarity
- sendQoute = qoute
- if mostSimilar < len(qouteRay)/2:
- server.send_message("%s" % ( qouteDict[sendQoute] ))
- if "come back" in message and "pibot" in message:
- server.send_message("I'ma back")
- server.away = False
- except KeyboardInterrupt:
- server.close()
- print("Exitted.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement