Advertisement
Guest User

Untitled

a guest
May 12th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 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.  
  34. def chanmsg(msg) :
  35.     socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, msg))
  36.  
  37. def chanact(act) :
  38.     socket.send("PRIVMSG %s :\x01ACTION %s\x01\r\n" % (CHANNEL, act))
  39.  
  40. def kill(nick) :
  41.     chanact("kills %s unmercifully!" % (nick))
  42.  
  43. def read_factoids() :
  44.     fact_list = []
  45.     for item in factoids :
  46.         fact_list.append(item[1:])
  47.     socket.send("PRIVMSG %s :Available factoids: %s\r\n" % (CHANNEL, ", ".join(fact_list)))
  48.  
  49. def new_factoid(name, nothing, *definitions) :
  50.     definition = " ".join(definitions)
  51.     factoids["!" + name] = definition
  52.     if factoids["!" + name] == definition :
  53.         socket.send("PRIVMSG %s :Factoid %s added successfuly\r\n" % (CHANNEL, name))
  54.  
  55. def del_factoid(name) :
  56.     try :
  57.         del factoids["!" + name]
  58.     except :
  59.         socket.send("PRIVMSG %s :Factoid %s not found\r\n" % (CHANNEL, name))
  60.     else :
  61.         socket.send("PRIVMSG %s :Factoid %s deleted successfuly\r\n" % (CHANNEL, name))
  62.  
  63. commands = {
  64.     "kill": kill,
  65.     "factoids": read_factoids,
  66.     "add": new_factoid,
  67.     "del": del_factoid
  68. }
  69.  
  70.  
  71. socket = socket.socket()
  72. socket.connect((HOST, PORT))
  73. if PASSWORD != "(secret)" :
  74.     socket.send("PASS %s\r\n" % (PASSWORD))
  75. socket.send("NICK %s\r\n" % (NICK))
  76. socket.send("USER %s * * :%s\r\n" % (USERNAME, REALNAME))
  77. socket.send("JOIN %s\r\n" % (CHANNEL))
  78.  
  79. while True :
  80.     recv = recv + socket.recv(4096)
  81.     s = recv.split("\r\n")
  82.     recv = s.pop()
  83.     for msg in s :
  84.         print msg
  85.         prefix, command, args = parsemsg(msg)
  86.         message = args[-1]
  87.         bot_cmd = message.split()[0].lower()[1:]
  88.         bot_cmd_args = message.split()[1:]
  89.         if command == "PING" :
  90.             socket.send("PONG\r\n")
  91.         elif bot_cmd in commands :
  92.             commands[bot_cmd](*bot_cmd_args)
  93.         elif message.startswith("!") and "!" + bot_cmd in factoids.keys() :
  94.             socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, factoids["!" + bot_cmd]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement