Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- from socket import *
- import pickle
- import os
- HOST = "irc.freenode.net"
- PORT = 6667
- USERNAME = "Bubblebot"
- REALNAME = "Bubble bot"
- PASS = "Bubblebot"
- CHANNELLIST = ["#ubuntu-beginners-dev"]
- RAW_LOG = False
- DELIMITER = "*"
- irc_socket = socket(AF_INET, SOCK_STREAM)
- irc_socket.connect((HOST, PORT))
- if os.path.isdir("./logs"):
- pass
- else:
- os.mkdir("./logs")
- class user_functions():
- def raw_send(self, data):
- irc_socket.send(data + "\r\n")
- print "Sent: \"%s\" \r\n" % data
- def receive(self, buffer_size = 1024):
- return irc_socket.recv(buffer_size)
- def msg_split(self, message):
- try:
- channel = message.split("PRIVMSG")[1].split(":")[0].strip()
- print "Channel stripped\t: %s" % channel
- msg = message.split("PRIVMSG")[1].split(":", 1)[1].rstrip()
- print "Message stripped\t: %s" % msg
- sender = message.split("PRIVMSG")[0].split(":")[1].split("!")[0]
- print "Sender stripped\t\t: %s\n" % sender
- return channel, msg, sender
- except IndexError:
- print "Not a valid PRIVMSG"
- return 1
- def say(self, message, channel):
- irc_funcs.privmsg(channel, message)
- print "Told to say: %s" % message
- def factoids(self, message, channel, sender):
- sendto = ""
- notfound = "You hear that Mr. Anderson?... That is the sound of inevitability..... Or maybe its the sound of me not having the factoid :)"
- try:
- command = message.split(DELIMITER)[1]
- if '|' in message:
- sendto = message.split("|")[1].lstrip()
- except IndexError:
- print "Excpeted index error"
- command = message()
- if os.path.isfile("factoids"):
- file = open("factoids", 'rb')
- factoids = pickle.load(file)
- file.close()
- elif not os.path.isfile("factoids"):
- factoids = {}
- try:
- if message.split()[1] == "is":
- message = message.split()
- message.pop(0)
- message.pop(0)
- new_factoid = ""
- for i in message:
- new_factoid += i
- factoids[command] = new_factoid
- print "%s: is %s" % (command, new_factoid)
- return 0
- except IndexError:
- pass
- if factoids.has_key(command):
- print "command is:", command
- if command == "quit":
- irc_funcs.privmsg(channel, "Im off, my bits are out of sync.")
- quit()
- if sendto != "":
- irc_funcs.privmsg(channel, "%s: %s" % (sendto, factoids[command]))
- return 0
- elif sendto == "":
- irc_funcs.privmsg(channel, factoids[command])
- return 0
- else:
- irc_funcs.privmsg(channel, "%s: %s" % (sender, notfound))
- def auto_response(self, message, channel, sender):
- pass
- class irc_functions():
- def join(self, channel):
- user_funcs.raw_send("JOIN %s" % channel)
- print "Joined: %s" % channel
- def privmsg(self, channel, message):
- user_funcs.raw_send("PRIVMSG %s :%s" % (channel, message))
- print "Sent, %s: \"%s\"" % (channel, message)
- irc_funcs = irc_functions()
- user_funcs = user_functions()
- user_funcs.raw_send("PASS %s" % PASS)
- user_funcs.raw_send("NICK %s" % USERNAME)
- user_funcs.raw_send("USER %s NULL NULL :%s" %(USERNAME, REALNAME))
- received = ""
- while "Thank you" not in received:
- received = user_funcs.receive()
- print received
- for i in CHANNELLIST:
- irc_funcs.join(i)
- while True:
- try:
- received = user_funcs.receive()
- if RAW_LOG:
- raw = open("./logs/raw.log", 'a')
- raw.write(received + "\n")
- raw.close()
- else:
- pass
- if "PING :" in received:
- server = received.split(":")[1]
- user_funcs.raw_send("PONG %s" % server)
- if "PRIVMSG" in received:
- channel, message, sender = user_funcs.msg_split(received)
- log = open("./logs/%s.log" % channel, 'a')
- log.write("%s\t: %s\n" % (sender, message))
- log.close()
- if message.startswith(DELIMITER):
- user_funcs.factoids(message, channel, sender)
- if message.endswith("!!!"):
- user_funcs.auto_response(message, channel, sender)
- except KeyboardInterrupt:
- try:
- raw.close()
- log.close()
- except NameError:
- pass
- irc_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement