Advertisement
Guest User

Untitled

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