Advertisement
Guest User

Untitled

a guest
May 12th, 2017
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys, socket, shelve, time, traceback, random
  5.  
  6. #EDIT the lines that follow
  7. #Also remember to create two files in the same directory as the script. They should be named 'passwd' and 'owners'. 'passwd' should contain the password #for the nick the bot will be using. If you didn't register the nick, please still create a file and in there put (secret) WITH the parenthesis.
  8. #On the 'owners' file, put a single nickname that will have the abilities of owner. To add other owners it is recommended to do it via IRC.
  9. HOST="irc.freenode.net"
  10. PORT=6667
  11. NICK="PedroBot"
  12. USERNAME="PedroBot"
  13. REALNAME="PedroBot"
  14. CHANNEL="##pedro3005"
  15. #COMMENT (add a # to the beginning) the two following lines. they are here to warn a user if he didn't edit the lines properly."
  16. #print "Open the script and edit the first few lines accordingly. Instructions are provided in it."
  17. #exit()
  18. #editing beyond this line is NOT necessary
  19.  
  20.  
  21. tries = 0 #this is related to the roulette function.
  22. recv="" #this is the variable that will receive from the socket
  23. bot_is_opped = 0
  24. passwd = open("passwd", "r")
  25. PASSWORD = passwd.read().split("\n")[0]
  26. owners = []
  27. owners_get = open("owners", "r")
  28. for line in owners_get :
  29.     owners.append(line.split('\n')[0])
  30. log_enabled = 0
  31. factoids = shelve.open("factoids", writeback=True)
  32.  
  33. def parsemsg(s) :
  34.     # this function takes a normal IRC message such as :
  35.     # :pedro3005!~pedro@xxx.xxx.xxx.xxx PRIVMSG ##pedro3005 :yay
  36.     #and defines:
  37.     #prefix = pedro3005!~pedro@xxx.xxx.xxx.xxx
  38.     #command = PRIVMSG
  39.     #args = ['##pedro3005', 'yay']
  40.     #the actual message will always be args[-1], but don't worry since we define that later. use the variable 'message'. also don't worry about the variable trailing.
  41.     trailing = 0
  42.     prefix = 0
  43.     if s[0] == ":" :
  44.         s = s[1:].split(' ', 1)
  45.         prefix = s[0]
  46.         s = s[1]
  47.     if " :" in s :
  48.         s = s.split(" :")
  49.         trailing = s[1]
  50.         s = s[0]
  51.     args = s.split()
  52.     command = args.pop(0)
  53.     if trailing != 0 :
  54.         args.append(trailing)
  55.     return prefix, command, args
  56.  
  57. def chanmsg(*msgs) :
  58.     #this function will make the bot send a message to the channel. in-code, simply call it with chanmsg("message"). you can also call it via IRC with the command !say, but only if you're an owner.
  59.     if nick in owners :
  60.         msg = " ".join(msgs)
  61.         socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, msg))
  62.     else :
  63.         chanmsg("Permission denied.")
  64.  
  65. def chanact(*acts) :
  66.     # similar to the above function but makes the bot act, i.e. /me. in code, call it with chanact("aaaa"), on IRC there is !act. works only for owners.
  67.     if nick in owners :
  68.         act = " ".join(acts)
  69.         socket.send("PRIVMSG %s :\x01ACTION %s\x01\r\n" % (CHANNEL, act))
  70.     else :
  71.         chanmsg("Permission denied.")
  72.  
  73. def kill(nick) :
  74.     if not nick in owners :
  75.         chanact("kills %s unmercifully!" % (nick))
  76.     else :
  77.         chanmsg("I shall not kill my master.")
  78.  
  79. def read_factoids() :
  80.     fact_list = []
  81.     for item in factoids :
  82.         fact_list.append(item[1:]) #this simply takes out the '!' from the name
  83.     chanmsg("Available factoids: %s" % (", ".join(fact_list)))
  84.  
  85. def new_factoid(name, nothing, *definitions) :
  86.     if not name.startswith("!") : #avoids someone trying to create a factoid with ! on the beginning (could confuse the bot)
  87.         definition = " ".join(definitions)
  88.         factoids["!" + name] = definition
  89.         chanmsg("Factoid %s added successfuly" % (name))
  90.     else :
  91.         chanmsg("You're not going to break me")
  92. def del_factoid(*names) :
  93.     factoids_deleted = []
  94.     factoids_notfound = []
  95.     name = " ".join(names).split()
  96.     if nick in owners :
  97.         for factoid in name :
  98.             try :
  99.                 del factoids["!" + factoid]
  100.             except :
  101.                 factoids_notfound.append(factoid)
  102.             else :
  103.                 factoids_deleted.append(factoid)
  104.         if len(factoids_deleted) > 0 :
  105.             chanmsg("Successfuly deleted factoids: %s" % (", ".join(factoids_deleted)))
  106.         if len(factoids_notfound) > 0 :
  107.             chanmsg("Factoids not found: %s" % (", ".join(factoids_notfound)))
  108.  
  109. def add_owner(name) :
  110.     if nick in owners :
  111.         owners.append(name)
  112.         write = open("owners", "a")
  113.         write.write(name + '\n')
  114.     if name in owners :
  115.         chanmsg("Owner %s added successfuly." % (name))
  116.  
  117. def read_owners() :
  118.     rowners = ", ".join(owners) #rowners is from r(ead)owners :P
  119.     chanmsg("Owners: %s" % (rowners))
  120.  
  121. def kick(name, nothing, *reasons) :
  122.     reason = " ".join(reasons)
  123.     if nick in owners :
  124.         socket.send("KICK %s %s :%s\r\n" % (CHANNEL, name, reason))
  125.         if not bot_is_opped == 1 :
  126.             chanmsg("I'm not an OP.")
  127.     else :
  128.         chanmsg("Permission denied.")
  129.  
  130. def ismsg(msg) :
  131.     if command == "PRIVMSG" and args[0] == CHANNEL : #see the parsemsg function if in doubt
  132.         return True
  133.  
  134. def log_on() :
  135.     global log_enabled, log #necessary to work with functions defined elsewhere inside a function
  136.     if nick in owners :
  137.         if log_enabled == 0 : #checks if it isn't already logging
  138.             log = open(CHANNEL, "a")
  139.             log.write(time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime()) + '\n' + '----------------------------' + '\n')
  140.             log_enabled = 1
  141.             chanmsg("Logging started")
  142.             log.flush()
  143.         else :
  144.             chanmsg("I am already logging.")
  145.     else :
  146.         chanmsg("Permission denied.")
  147. def log_off() :
  148.     global log_enabled, log
  149.     if nick in owners :
  150.         if log_enabled == 1 :
  151.             log_enabled = 0
  152.             log.write("-------END OF LOG-----\n")
  153.             log.flush()
  154.             chanmsg("Stopped logging")
  155.         else :
  156.             chanmsg("I am not logging anything.")
  157.  
  158. def ircroulette() :
  159.     global tries
  160.     chance = random.choice(range(1, 5)) #the range is (1, 5) but the possible numbers are [1, 2, 3, 4]. you can increase the range, i'm not sure which is correct
  161.     if tries == 4 or chance == 4 :
  162.         kick(nick, "because", "*BAM*")
  163.         tries = 0
  164.     else :
  165.         tries = tries + 1
  166.         chanmsg("*tick*")
  167.  
  168. def topic(*ntpcs) : #ntpcs is from n(ew)t(o)p(i)cs
  169.     ntpc = " ".join(ntpcs)
  170.     if nick in owners :
  171.         socket.send("TOPIC %s :%s\r\n" % (CHANNEL, ntpc))
  172.         if not bot_is_opped == 1 :
  173.             chanmsg("I'm not an OP.")
  174.     else :
  175.         chanmsg("Permission denied.")
  176.  
  177. commands = { #this dict is extremely important. in here, assign a command which preceded by ! on irc will call the function. ex: "a": a -> if someone types !a it'll call the function a
  178.     "kill": kill,
  179.     "factoids": read_factoids,
  180.     "add": new_factoid,
  181.     "del": del_factoid,
  182.     "add_owner": add_owner,
  183.     "owners": read_owners,
  184.     "kick": kick,
  185.     "log": log_on,
  186.     "log_off": log_off,
  187.     "roulette": ircroulette,
  188.     "say": chanmsg,
  189.     "act": chanact,
  190.     "topic": topic
  191. }
  192.  
  193.  
  194. socket = socket.socket()
  195. socket.connect((HOST, PORT))
  196. if PASSWORD != "(secret)" : #this is to check if the person actually set a password
  197.     socket.send("PASS %s\r\n" % (PASSWORD))
  198. socket.send("NICK %s\r\n" % (NICK))
  199. socket.send("USER %s * * :%s\r\n" % (USERNAME, REALNAME))
  200. socket.send("JOIN %s\r\n" % (CHANNEL))
  201.  
  202. while True :
  203.     recv = recv + socket.recv(4096)
  204.     s = recv.split("\r\n")
  205.     recv = s.pop() #this simply takes the part before the \r\n
  206.     for msg in s :
  207.         print msg
  208.         prefix, command, args = parsemsg(msg)
  209.         try : #here we assign some basic variables. message is whatever someone typed. if someone typed !kill, bot_cmd will be kill. bot_cmd_args will be a list of whatever is after bot_cmd. nick will be the person's nick.
  210.             message = args[-1]
  211.             bot_cmd = message.split()[0][1:]
  212.             bot_cmd_args = message.split()[1:]
  213.             nick = prefix.split("!")[0]
  214.         except :
  215.             pass
  216.         if command == "MODE" and "+o" in args and NICK in args : #detect ops and deops of the bot
  217.             bot_is_opped = 1
  218.         elif command == "MODE" and "-o" in args and NICK in args :
  219.             bot_is_opped = 0
  220.         if command == "KICK" and NICK in args : #detects kicks
  221.             socket.send("JOIN %s\r\n" % (CHANNEL))
  222.         if log_enabled == 1 and ismsg(msg) : #if logging is enabled and the msg is a real message by someone, it writes to the log life.
  223.             log.write("%s: %s\n" % (nick, message))
  224.             log.flush()
  225.         if command == "PING" : #this is to respond for pings, necessary or you will get kicked out of the network eventually.
  226.             socket.send("PONG\r\n")
  227.         elif bot_cmd in commands and message.startswith("!") :
  228.             try :
  229.                 commands[bot_cmd](*bot_cmd_args) #this is necessary or someone can give too many arguments for a function and break the bot
  230.             except :
  231.                 chanmsg("Error encountered. You probably didn't provide enough or provided too much arguments for a function. Try again")
  232.         elif message.startswith("!") and "!" + bot_cmd in factoids.keys() :
  233.             socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, factoids["!" + bot_cmd]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement