import Protocol class IRCProtocol(Protocol.Protocol): def on_connect(self): # we have to tell the client its nick so it will # recognize when we join him into #room self.push(":myhost 001 " + self.source + " :Welcome to this strange example\n") def found_terminator(self): line = self.buffer.strip() self.buffer = "" lsplit = line.split(None, 1) if len(lsplit) > 0: cmd = lsplit[0] if len(lsplit) > 1: target = lsplit[1] if cmd == "PRIVMSG": if target == '#room': self.multiplexer.castmsg(("_message_public", {"_nick" : self.source, "_text" : line.split(":", 1)[1] })) else: self.push("ERROR :sorry, just that single room target supported\n") elif cmd == "PART" or cmd == "QUIT": self.multiplexer.disconnect(self) self.multiplexer.castmsg(("_notice_place_leave", {"_nick" : self.source})) self.close() else: # unsupported command self.push("ERROR: unimplemented " + cmd + " \n") def render(self, (event, vars)): line = "" if event == "_message_public" and vars["_nick"] != self.source: # echo is not yet common in irc line = ":" + vars["_nick"] + "!" + vars["_nick"] line += "@myhost" + " PRIVMSG #room :" + vars["_text"] elif event == "_notice_place_enter": line = ":" + vars["_nick"] + "!" + vars["_nick"] line += "@myhost" + " JOIN #room" elif event == "_notice_place_leave": line = ":" + vars["_nick"] + "!" + vars["_nick"] line += "@myhost" + " PART #room :" line += "messages embedded in state changes are bad protocol design" if line: self.push(line + "\n")