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, time
- recv=""
- HOST="irc.freenode.net"
- PORT=6667
- NICK="PedroBot"
- USERNAME="PedroBot"
- REALNAME="PedroBot"
- passwd = open("passwd", "r")
- PASSWORD = passwd.read().split("\n")[0]
- CHANNEL="##devil"
- owners = []
- owners_get = open("owners", "r")
- for line in owners_get :
- owners.append(line.split('\n')[0])
- log_enabled = 0
- 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, *definitions) :
- if not name.startswith("!") :
- definition = " ".join(definitions)
- factoids["!" + name] = definition
- if factoids["!" + name] == definition :
- socket.send("PRIVMSG %s :Factoid %s added successfuly\r\n" % (CHANNEL, name))
- else :
- chanmsg("You're not going to break me, asshole")
- def del_factoid(name) :
- if nick in owners :
- 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))
- def add_owner(name) :
- if nick in owners :
- owners.append(name)
- write = open("owners", "a")
- write.write(name + '\n')
- if name in owners :
- chanmsg("Owner %s added successfuly." % (name))
- def read_owners() :
- rowners = ", ".join(owners)
- chanmsg("Owners: %s" % (rowners))
- def kick(name, nothing, *reasons) :
- reason = " ".join(reasons)
- if nick in owners :
- socket.send("KICK %s %s :%s\r\n" % (CHANNEL, name, reason))
- def ismsg(msg) :
- if command == "PRIVMSG" and args[0] == CHANNEL :
- return True
- def log_on() :
- global log_enabled, log
- if nick in owners :
- if log_enabled == 0 :
- log = open(CHANNEL, "a")
- log.write(time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime()) + '\n')
- log_enabled = 1
- chanmsg("Logging started")
- else :
- chanmsg("I am already logging.")
- else :
- chanmsg("Permission denied.")
- def log_off() :
- global log_enabled, log
- if nick in owners :
- if log_enabled == 1 :
- log_enabled == 0
- log.write("-------END OF LOG-----\n")
- chanmsg("Stopped logging")
- else :
- chanmsg("I am not logging anything.")
- commands = {
- "kill": kill,
- "factoids": read_factoids,
- "add": new_factoid,
- "del": del_factoid,
- "add_owner": add_owner,
- "owners": read_owners,
- "kick": kick,
- "log": log_on,
- "log_off": log_off
- }
- 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)
- try :
- message = args[-1]
- bot_cmd = message.split()[0][1:]
- bot_cmd_args = message.split()[1:]
- nick = prefix.split("!")[0]
- except :
- pass
- if log_enabled == 1 and ismsg(msg) :
- log.write("%s: %s\n" % (nick, message))
- 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