Advertisement
Guest User

Untitled

a guest
May 12th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys, socket, shelve
  5.  
  6. recv=""
  7. HOST="irc.freenode.net"
  8. PORT=6667
  9. NICK="PedroBot"
  10. USERNAME="PedroBot"
  11. REALNAME="PedroBot"
  12. PASSWORD="(secret)"
  13. CHANNEL="##fatchat"
  14. factoids = shelve.open("factoids", writeback=True) # don't edit this manually
  15.  
  16. def parsemsg(s) :
  17.     trailing = 0
  18.     prefix = 0
  19.     if s[0] == ":" :
  20.         s = s[1:].split(' ', 1)
  21.         prefix = s[0]
  22.         s = s[1]
  23.     if " :" in s :
  24.         s = s.split(" :")
  25.         trailing = s[1]
  26.         s = s[0]
  27.     args = s.split()
  28.     command = args.pop(0)
  29.     if trailing != 0 :
  30.         args.append(trailing)
  31.     return prefix, command, args
  32.  
  33. socket = socket.socket()
  34. socket.connect((HOST, PORT))
  35. if PASSWORD != "(secret)" :
  36.     socket.send("PASS %s\r\n" % (PASSWORD))
  37. socket.send("NICK %s\r\n" % (NICK))
  38. socket.send("USER %s * * :%s\r\n" % (USERNAME, REALNAME))
  39. socket.send("JOIN %s\r\n" % (CHANNEL))
  40.  
  41. while True :
  42.     recv = recv + socket.recv(4096)
  43.     s = recv.split("\r\n")
  44.     recv = s.pop()
  45.     for msg in s :
  46.         print msg
  47.         prefix, command, args = parsemsg(msg)
  48.         if command == "PING" :
  49.             socket.send("PONG\r\n")
  50.         elif args[-1].split()[0].lower() == "!add" :
  51.             factoids["!" + args[-1].split()[1]] = " ".join(args[-1].split()[3:])
  52.             if factoids["!" + args[-1].split()[1]] == " ".join(args[-1].split()[3:]) :
  53.                 socket.send("PRIVMSG %s :Factoid %s added successfully\r\n" % (CHANNEL, args[-1].split()[1]))
  54.  
  55.         elif args[-1].strip().lower() == "ping %s" % (NICK.lower()) : # args[-1] is the message. check the parsemsg function for clarification
  56.             socket.send("PRIVMSG %s :I'm here\r\n" % (CHANNEL))
  57.  
  58.         elif args[-1].strip().lower().split()[0] == "!kill" :
  59.             socket.send("PRIVMSG %s :\x01ACTION kills %s unmercifully!\x01\r\n" % (CHANNEL, args[-1].strip().split()[1]))
  60.  
  61.         elif args[-1].split()[0].lower() == "!factoids" :
  62.             fact_list = []
  63.             for item in factoids :
  64.                 fact_list.append(item[1:])
  65.             socket.send("PRIVMSG %s :Available factoids: %s\r\n" % (CHANNEL, ", ".join(fact_list)))
  66.  
  67.         elif args[-1].split()[0].lower() == "!del" and prefix.split("!")[0] == "pedro3005" :
  68.             del factoids["!" + args[-1].split()[1].lower()]
  69.             try :
  70.                 a = factoids["!" + args[-1].split()[1].lower()]
  71.             except :
  72.                 socket.send("PRIVMSG %s :Factoid %s deleted\r\n" % (CHANNEL, args[-1].split()[1].lower()))
  73.  
  74.         else :
  75.             for item in factoids.keys() :
  76.                 if item == args[-1].split()[0] :
  77.                     socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, str(factoids[item])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement