Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # config at the way bottom
- # written in ~30 mins at like 6am
- #
- import socket
- STATE_DISCONNECTED, STATE_HANDSHAKE, STATE_CONNECTED = range(3)
- BUFFER_SIZE = 1024
- class Pynsour:
- def __init__(self):
- self.server = ""
- self.port = -1
- self.nick = ""
- self.altnick = ""
- self.username = ""
- self.realname = ""
- self.server = ""
- self.password = ""
- self.state = STATE_DISCONNECTED
- self.channels = ""
- self.buffer = ""
- def connect(self):
- channels = {}
- for w in self.channels:
- info = w.split(" ")
- if len(info) == 1:
- channel = w
- key = ""
- elif len(info) == 2:
- channel, key = info
- channels[channel] = key
- self.channels = channels
- print ">>> Channels: %s" % str(channels)
- print ">>> Creating socket"
- self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- print ">>> Connecting to %s:%s" % (self.server, self.port)
- self.connection.connect((self.server, self.port))
- print ">>> Running"
- self.run()
- def send(self, msg):
- print "+++ %s" % msg
- self.connection.send("%s\r\n" % msg)
- def run(self):
- while True:
- if not self.parse():
- break
- elif self.state == STATE_DISCONNECTED:
- self.send("NICK %s" % self.nick)
- self.send("USER %s %s %s :%s" %
- (self.username,
- self.hostname,
- self.server,
- self.realname))
- if self.password != "":
- self.connection.send("PASS %s\r\n", self.password)
- self.state = STATE_HANDSHAKE
- def parse(self):
- incoming = self.connection.recv(BUFFER_SIZE)
- self.buffer += incoming
- buffer = self.buffer.split("\r\n")
- print "BUFFER { %s }" % buffer
- if self.buffer[:-2] == "\r\n":
- self.buffer = ""
- else:
- self.buffer = buffer[-1]
- buffer = buffer[:-1]
- for line in buffer:
- if not line:
- continue
- if line[0] == ":":
- line = line[1:]
- if ":" in line:
- first, second = line.split(":", 1)
- print ">>> line: '%s'" % line
- # print " >>> first: '%s'" % first
- print " >>> second: '%s'" % second
- words = first.strip().split(" ")
- words += [second]
- self.irc_parse(words)
- else:
- print ">>> just spaces"
- words = line.split(" ")
- # print "words: %s" % str(words)
- return True
- def irc_parse(self, words):
- if len(words) == 4:
- src, msg_type, dest, msg = words
- if msg_type == "PRIVMSG":
- self.handler_privmsg(src, dest, msg)
- elif msg_type == "001":
- self.state = STATE_CONNECTED
- self.autojoin()
- elif len(words) == 2:
- msg_type, msg = words
- if msg_type == "PING":
- self.handler_pong(msg)
- def autojoin(self):
- for channel, key in self.channels.items():
- self.send("JOIN %s %s" % (channel, key))
- def handler_pong(self, msg):
- self.send("PONG :%s" % msg)
- def handler_privmsg(self, src, dest, msg):
- if dest == self.nick:
- self.script(src, "__self__", msg)
- elif dest in self.channels.keys():
- self.script(src, dest, msg)
- def script(self, src, dest, msg):
- print ">>> script %s %s %s" % tuple(map(str, [src, dest, msg]))
- src_nick = src.split("!")[0]
- if dest == "__self__":
- words = msg.split(" ")
- if words[0] == "hello":
- self.send("PRIVMSG %s :%s" % (src_nick, "oh hy"))
- elif words[0] == ".py":
- self.python_handler(src_nick, msg)
- elif words[0] == "\x01VERSION\x01":
- self.send("NOTICE %s :\x01VERSION %s:%s:%s\x01" %
- (src_nick, "pynsour", "0.1", "your mother"))
- else:
- words = msg.split(" ")
- if words[0] == "hello":
- self.send("PRIVMSG %s :%s" % (dest, "oh hy"))
- elif words[0] == ".py":
- self.python_handler(dest, msg)
- def python_handler(self, dest, msg):
- msg = msg.split(" ", 1)[1].strip()
- print ">>> python handler %s %s" % tuple(map(str, [dest, msg]))
- out = ""
- banned = "eval exit sys os import file open rm rf".split(" ")
- for b in banned:
- if b in msg:
- out = "don't do that."
- print ">>> out = %s" % out
- if out == "":
- try:
- print ">>> eval(%s)" % msg
- out = eval(msg)
- print ">>> out = %s" % str(out)
- except Exception, e:
- out = e
- self.send("PRIVMSG %s :%s" % (dest, str(out)))
- def main():
- bot = Pynsour()
- bot.server = "euclid.poundcs.org"
- bot.port = 6667
- bot.nick = "piefusion"
- bot.altnick = "piefusion"
- bot.username = "piefusion"
- bot.realname = "Pie Fusion"
- bot.hostname = "localhost"
- bot.channels = ["#cs"]
- bot.connect()
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment