Advertisement
Guest User

Untitled

a guest
May 12th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys, socket, shelve, time, traceback, random
  5.  
  6.  
  7. HOST="irc.freenode.net"
  8. PORT=6667
  9. NICK="PedroBot"
  10. USERNAME="PedroBot"
  11. REALNAME="PedroBot"
  12. CHANNEL="##pedro3005"
  13. #editing beyond this line is NOT necessary
  14. recv=""
  15. bot_is_opped = 0
  16. passwd = open("passwd", "r")
  17. PASSWORD = passwd.read().split("\n")[0]
  18. owners = []
  19. owners_get = open("owners", "r")
  20. for line in owners_get :
  21.     owners.append(line.split('\n')[0])
  22. log_enabled = 0
  23. factoids = shelve.open("factoids", writeback=True) # don't edit this manually
  24.  
  25. def parsemsg(s) :
  26.     trailing = 0
  27.     prefix = 0
  28.     if s[0] == ":" :
  29.         s = s[1:].split(' ', 1)
  30.         prefix = s[0]
  31.         s = s[1]
  32.     if " :" in s :
  33.         s = s.split(" :")
  34.         trailing = s[1]
  35.         s = s[0]
  36.     args = s.split()
  37.     command = args.pop(0)
  38.     if trailing != 0 :
  39.         args.append(trailing)
  40.     return prefix, command, args
  41.  
  42. def chanmsg(*msgs) :
  43.     if nick in owners :
  44.         msg = " ".join(msgs)
  45.         socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, msg))
  46.     else :
  47.         chanmsg("Permission denied.")
  48.  
  49. def chanact(*acts) :
  50.     if nick in owners :
  51.         act = " ".join(acts)
  52.         socket.send("PRIVMSG %s :\x01ACTION %s\x01\r\n" % (CHANNEL, act))
  53.     else :
  54.         chanmsg("Permission denied.")
  55.  
  56. def kill(nick) :
  57.     if not nick in owners :
  58.         chanact("kills %s unmercifully!" % (nick))
  59.     else :
  60.         chanmsg("I shall not kill my master.")
  61.  
  62. def read_factoids() :
  63.     fact_list = []
  64.     for item in factoids :
  65.         fact_list.append(item[1:])
  66.     socket.send("PRIVMSG %s :Available factoids: %s\r\n" % (CHANNEL, ", ".join(fact_list)))
  67.  
  68. def new_factoid(name, nothing, *definitions) :
  69.     if not name.startswith("!") :
  70.         definition = " ".join(definitions)
  71.         factoids["!" + name] = definition
  72.         if factoids["!" + name] == definition :
  73.             socket.send("PRIVMSG %s :Factoid %s added successfuly\r\n" % (CHANNEL, name))
  74.     else :
  75.         chanmsg("You're not going to break me, asshole")
  76. def del_factoid(name) :
  77.     if nick in owners :
  78.         try :
  79.             del factoids["!" + name]
  80.         except :
  81.             socket.send("PRIVMSG %s :Factoid %s not found\r\n" % (CHANNEL, name))
  82.         else :
  83.             socket.send("PRIVMSG %s :Factoid %s deleted successfuly\r\n" % (CHANNEL, name))
  84.  
  85. def add_owner(name) :
  86.     if nick in owners :
  87.         owners.append(name)
  88.         write = open("owners", "a")
  89.         write.write(name + '\n')
  90.     if name in owners :
  91.         chanmsg("Owner %s added successfuly." % (name))
  92.  
  93. def read_owners() :
  94.     rowners = ", ".join(owners)
  95.     chanmsg("Owners: %s" % (rowners))
  96.  
  97. def kick(name, nothing, *reasons) :
  98.     reason = " ".join(reasons)
  99.     if nick in owners :
  100.         socket.send("KICK %s %s :%s\r\n" % (CHANNEL, name, reason))
  101.         if not bot_is_opped == 1 :
  102.             chanmsg("I'm not an OP.")
  103.     else :
  104.         chanmsg("Permission denied.")
  105.  
  106. def ismsg(msg) :
  107.     if command == "PRIVMSG" and args[0] == CHANNEL :
  108.         return True
  109.  
  110. def log_on() :
  111.     global log_enabled, log
  112.     if nick in owners :
  113.         if log_enabled == 0 :
  114.             log = open(CHANNEL, "a")
  115.             log.write(time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime()) + '\n' + '----------------------------' + '\n')
  116.             log_enabled = 1
  117.             chanmsg("Logging started")
  118.             log.flush()
  119.         else :
  120.             chanmsg("I am already logging.")
  121.     else :
  122.         chanmsg("Permission denied.")
  123. def log_off() :
  124.     global log_enabled, log
  125.     if nick in owners :
  126.         if log_enabled == 1 :
  127.             log_enabled = 0
  128.             log.write("-------END OF LOG-----\n")
  129.             log.flush()
  130.             chanmsg("Stopped logging")
  131.         else :
  132.             chanmsg("I am not logging anything.")
  133.  
  134. def ircroulette() :
  135.     chance = random.choice(range(1, 4))
  136.     if chance == 3 :
  137.         kick(nick, "because", "*BAM*")
  138.     else :
  139.         chanmsg("*tick*")
  140.  
  141. def topic(*ntpcs) :
  142.     ntpc = " ".join(ntpcs)
  143.     if nick in owners :
  144.         socket.send("TOPIC %s :%s\r\n" % (CHANNEL, ntpc))
  145.         if not bot_is_opped == 1 :
  146.             chanmsg("I'm not an OP.")
  147.     else :
  148.         chanmsg("Permission denied.")
  149.  
  150. commands = {
  151.     "kill": kill,
  152.     "factoids": read_factoids,
  153.     "add": new_factoid,
  154.     "del": del_factoid,
  155.     "add_owner": add_owner,
  156.     "owners": read_owners,
  157.     "kick": kick,
  158.     "log": log_on,
  159.     "log_off": log_off,
  160.     "roulette": ircroulette,
  161.     "say": chanmsg,
  162.     "act": chanact,
  163.     "topic": topic
  164. }
  165.  
  166.  
  167. socket = socket.socket()
  168. socket.connect((HOST, PORT))
  169. if PASSWORD != "(secret)" :
  170.     socket.send("PASS %s\r\n" % (PASSWORD))
  171. socket.send("NICK %s\r\n" % (NICK))
  172. socket.send("USER %s * * :%s\r\n" % (USERNAME, REALNAME))
  173. socket.send("JOIN %s\r\n" % (CHANNEL))
  174.  
  175. while True :
  176.     recv = recv + socket.recv(4096)
  177.     s = recv.split("\r\n")
  178.     recv = s.pop()
  179.     for msg in s :
  180.         print msg
  181.         prefix, command, args = parsemsg(msg)
  182.         try :
  183.             message = args[-1]
  184.             bot_cmd = message.split()[0][1:]
  185.             bot_cmd_args = message.split()[1:]
  186.             nick = prefix.split("!")[0]
  187.         except :
  188.             pass
  189.         if command == "MODE" and "+o" in args and "PedroBot" in args :
  190.             bot_is_opped = 1
  191.         if log_enabled == 1 and ismsg(msg) :
  192.             log.write("%s: %s\n" % (nick, message))
  193.             log.flush()
  194.         if command == "PING" :
  195.             socket.send("PONG\r\n")
  196.         elif bot_cmd in commands and message.startswith("!") :
  197.             try :
  198.                 commands[bot_cmd](*bot_cmd_args)
  199.             except :
  200.                 chanmsg("Error encountered. You probably didn't provide enough or provided too much arguments for a function. Try again")
  201.         elif message.startswith("!") and "!" + bot_cmd in factoids.keys() :
  202.             socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, factoids["!" + bot_cmd]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement