Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import sys, socket, shelve
- recv=""
- HOST="irc.freenode.net"
- PORT=6667
- NICK="PedroBot"
- USERNAME="PedroBot"
- REALNAME="PedroBot"
- PASSWORD="21223445"
- CHANNEL="##fatchat"
- factoids = shelve.open("factoids", writeback=True) # don't edit this manually
- def parsemsg(s) :
- trailing = 0
- prefix = 0
- if s[0] == ":" :
- s = s[1:].split(' ', 1)
- prefix = s[0]
- s = s[1]
- if " :" in s :
- s = s.split(" :")
- trailing = s[1]
- s = s[0]
- args = s.split()
- command = args.pop(0)
- if trailing != 0 :
- args.append(trailing)
- return prefix, command, args
- def chanmsg(msg) :
- socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, msg))
- def chanact(act) :
- socket.send("PRIVMSG %s :\x01ACTION %s\x01\r\n" % (CHANNEL, act))
- def kill(nick) :
- chanact("kills %s unmercifully!" % (nick))
- def read_factoids() :
- fact_list = []
- for item in factoids :
- fact_list.append(item[1:])
- socket.send("PRIVMSG %s :Available factoids: %s\r\n" % (CHANNEL, ", ".join(fact_list)))
- def new_factoid(name, nothing, definition) :
- factoids["!" + name] = definition
- if factoids["!" + name] == definition :
- socket.send("PRIVMSG %s :Factoid %s added successfuly\r\n" % (CHANNEL, name))
- def del_factoid(name) :
- try :
- del factoids["!" + name]
- except :
- socket.send("PRIVMSG %s :Factoid %s not found\r\n" % (CHANNEL, name))
- else :
- socket.send("PRIVMSG %s :Factoid %s deleted successfuly\r\n" % (CHANNEL, name))
- commands = {
- "kill": kill,
- "factoids": read_factoids,
- "add": new_factoid,
- "del": del_factoid
- }
- socket = socket.socket()
- socket.connect((HOST, PORT))
- if PASSWORD != "(secret)" :
- socket.send("PASS %s\r\n" % (PASSWORD))
- socket.send("NICK %s\r\n" % (NICK))
- socket.send("USER %s * * :%s\r\n" % (USERNAME, REALNAME))
- socket.send("JOIN %s\r\n" % (CHANNEL))
- while True :
- recv = recv + socket.recv(4096)
- s = recv.split("\r\n")
- recv = s.pop()
- for msg in s :
- print msg
- prefix, command, args = parsemsg(msg)
- message = args[-1]
- bot_cmd = message.split()[0].lower()[1:]
- bot_cmd_args = message.split()[1:]
- if command == "PING" :
- socket.send("PONG\r\n")
- elif bot_cmd in commands :
- commands[bot_cmd](*bot_cmd_args)
- elif message.startswith("!") and "!" + bot_cmd in factoids.keys() :
- socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, factoids["!" + bot_cmd]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement