Advertisement
Guest User

Untitled

a guest
May 12th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys, socket, shelve, time
  5.  
  6. recv=""
  7. HOST="irc.freenode.net"
  8. PORT=6667
  9. NICK="PedroBot"
  10. USERNAME="PedroBot"
  11. REALNAME="PedroBot"
  12. passwd = open("passwd", "r")
  13. PASSWORD = passwd.read().split("\n")[0]
  14. CHANNEL="##devil"
  15. owners = []
  16. owners_get = open("owners", "r")
  17. for line in owners_get :
  18.     owners.append(line.split('\n')[0])
  19. log_enabled = 0
  20. factoids = shelve.open("factoids", writeback=True) # don't edit this manually
  21.  
  22. def parsemsg(s) :
  23.     trailing = 0
  24.     prefix = 0
  25.     if s[0] == ":" :
  26.         s = s[1:].split(' ', 1)
  27.         prefix = s[0]
  28.         s = s[1]
  29.     if " :" in s :
  30.         s = s.split(" :")
  31.         trailing = s[1]
  32.         s = s[0]
  33.     args = s.split()
  34.     command = args.pop(0)
  35.     if trailing != 0 :
  36.         args.append(trailing)
  37.     return prefix, command, args
  38.  
  39.  
  40. def chanmsg(msg) :
  41.     socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, msg))
  42.  
  43. def chanact(act) :
  44.     socket.send("PRIVMSG %s :\x01ACTION %s\x01\r\n" % (CHANNEL, act))
  45.  
  46. def kill(nick) :
  47.     chanact("kills %s unmercifully!" % (nick))
  48.  
  49. def read_factoids() :
  50.     fact_list = []
  51.     for item in factoids :
  52.         fact_list.append(item[1:])
  53.     socket.send("PRIVMSG %s :Available factoids: %s\r\n" % (CHANNEL, ", ".join(fact_list)))
  54.  
  55. def new_factoid(name, nothing, *definitions) :
  56.     if not name.startswith("!") :
  57.         definition = " ".join(definitions)
  58.         factoids["!" + name] = definition
  59.         if factoids["!" + name] == definition :
  60.             socket.send("PRIVMSG %s :Factoid %s added successfuly\r\n" % (CHANNEL, name))
  61.     else :
  62.         chanmsg("You're not going to break me, asshole")
  63. def del_factoid(name) :
  64.     if nick in owners :
  65.         try :
  66.             del factoids["!" + name]
  67.         except :
  68.             socket.send("PRIVMSG %s :Factoid %s not found\r\n" % (CHANNEL, name))
  69.         else :
  70.             socket.send("PRIVMSG %s :Factoid %s deleted successfuly\r\n" % (CHANNEL, name))
  71.  
  72. def add_owner(name) :
  73.     if nick in owners :
  74.         owners.append(name)
  75.         write = open("owners", "a")
  76.         write.write(name + '\n')
  77.     if name in owners :
  78.         chanmsg("Owner %s added successfuly." % (name))
  79.  
  80. def read_owners() :
  81.     rowners = ", ".join(owners)
  82.     chanmsg("Owners: %s" % (rowners))
  83.  
  84. def kick(name, nothing, *reasons) :
  85.     reason = " ".join(reasons)
  86.     if nick in owners :
  87.         socket.send("KICK %s %s :%s\r\n" % (CHANNEL, name, reason))
  88.  
  89. def ismsg(msg) :
  90.     if command == "PRIVMSG" and args[0] == CHANNEL :
  91.         return True
  92.  
  93. def log_on() :
  94.     global log_enabled, log
  95.     if nick in owners :
  96.         if log_enabled == 0 :
  97.             log = open(CHANNEL, "a")
  98.             log.write(time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime()) + '\n')
  99.             log_enabled = 1
  100.             chanmsg("Logging started")
  101.         else :
  102.             chanmsg("I am already logging.")
  103.     else :
  104.         chanmsg("Permission denied.")
  105. def log_off() :
  106.     global log_enabled, log
  107.     if nick in owners :
  108.         if log_enabled == 1 :
  109.             log_enabled == 0
  110.             log.write("-------END OF LOG-----\n")
  111.             chanmsg("Stopped logging")
  112.         else :
  113.             chanmsg("I am not logging anything.")
  114.  
  115. commands = {
  116.     "kill": kill,
  117.     "factoids": read_factoids,
  118.     "add": new_factoid,
  119.     "del": del_factoid,
  120.     "add_owner": add_owner,
  121.     "owners": read_owners,
  122.     "kick": kick,
  123.     "log": log_on,
  124.     "log_off": log_off
  125. }
  126.  
  127.  
  128. socket = socket.socket()
  129. socket.connect((HOST, PORT))
  130. if PASSWORD != "(secret)" :
  131.     socket.send("PASS %s\r\n" % (PASSWORD))
  132. socket.send("NICK %s\r\n" % (NICK))
  133. socket.send("USER %s * * :%s\r\n" % (USERNAME, REALNAME))
  134. socket.send("JOIN %s\r\n" % (CHANNEL))
  135.  
  136. while True :
  137.     recv = recv + socket.recv(4096)
  138.     s = recv.split("\r\n")
  139.     recv = s.pop()
  140.     for msg in s :
  141.         print msg
  142.         prefix, command, args = parsemsg(msg)
  143.         try :
  144.             message = args[-1]
  145.             bot_cmd = message.split()[0][1:]
  146.             bot_cmd_args = message.split()[1:]
  147.             nick = prefix.split("!")[0]
  148.         except :
  149.             pass
  150.         if log_enabled == 1 and ismsg(msg) :
  151.             log.write("%s: %s\n" % (nick, message))
  152.         if command == "PING" :
  153.             socket.send("PONG\r\n")
  154.         elif bot_cmd in commands :
  155.             commands[bot_cmd](*bot_cmd_args)
  156.         elif message.startswith("!") and "!" + bot_cmd in factoids.keys() :
  157.             socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, factoids["!" + bot_cmd]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement