Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import sys, socket, shelve
- recv=""
- HOST="irc.freenode.net"
- PORT=6667
- NICK="PedroBot"
- USERNAME="PedroBot"
- REALNAME="PedroBot"
- PASSWORD="(secret)"
- CHANNEL="##fatchat"
- factoids = shelve.open("factoids", writeback=True) # don't edit this manually
- def parsemsg(s) :
- trailing = 0
- prefix = 0
- if s[0] == ":" :
- s = s[1:].split(' ', 1)
- prefix = s[0]
- s = s[1]
- if " :" in s :
- s = s.split(" :")
- trailing = s[1]
- s = s[0]
- args = s.split()
- command = args.pop(0)
- if trailing != 0 :
- args.append(trailing)
- return prefix, command, args
- socket = socket.socket()
- socket.connect((HOST, PORT))
- if PASSWORD != "(secret)" :
- socket.send("PASS %s\r\n" % (PASSWORD))
- socket.send("NICK %s\r\n" % (NICK))
- socket.send("USER %s * * :%s\r\n" % (USERNAME, REALNAME))
- socket.send("JOIN %s\r\n" % (CHANNEL))
- while True :
- recv = recv + socket.recv(4096)
- s = recv.split("\r\n")
- recv = s.pop()
- for msg in s :
- print msg
- prefix, command, args = parsemsg(msg)
- if command == "PING" :
- socket.send("PONG\r\n")
- elif args[-1].split()[0].lower() == "!add" :
- factoids["!" + args[-1].split()[1]] = " ".join(args[-1].split()[3:])
- if factoids["!" + args[-1].split()[1]] == " ".join(args[-1].split()[3:]) :
- socket.send("PRIVMSG %s :Factoid %s added successfully\r\n" % (CHANNEL, args[-1].split()[1]))
- elif args[-1].strip().lower() == "ping %s" % (NICK.lower()) : # args[-1] is the message. check the parsemsg function for clarification
- socket.send("PRIVMSG %s :I'm here\r\n" % (CHANNEL))
- elif args[-1].strip().lower().split()[0] == "!kill" :
- socket.send("PRIVMSG %s :\x01ACTION kills %s unmercifully!\x01\r\n" % (CHANNEL, args[-1].strip().split()[1]))
- elif args[-1].split()[0].lower() == "!factoids" :
- fact_list = []
- for item in factoids :
- fact_list.append(item[1:])
- socket.send("PRIVMSG %s :Available factoids: %s\r\n" % (CHANNEL, ", ".join(fact_list)))
- elif args[-1].split()[0].lower() == "!del" and prefix.split("!")[0] == "pedro3005" :
- del factoids["!" + args[-1].split()[1].lower()]
- try :
- a = factoids["!" + args[-1].split()[1].lower()]
- except :
- socket.send("PRIVMSG %s :Factoid %s deleted\r\n" % (CHANNEL, args[-1].split()[1].lower()))
- else :
- for item in factoids.keys() :
- if item == args[-1].split()[0] :
- socket.send("PRIVMSG %s :%s\r\n" % (CHANNEL, str(factoids[item])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement