Permalink
Browse files

Initial checkin of IRC COMET demo. Not fully working yet.

git-svn-id: http://chiral.j4cbo.com/svn/trunk@71 a827fe29-2235-0410-a7a9-cacdfe24a5d3
  • Loading branch information...
jacob
jacob committed Sep 3, 2007
1 parent 8b5d1b9 commit 9cba4f07308bb705798b78a6256643292613044d
Showing with 161 additions and 0 deletions.
  1. +36 −0 irc-static/index.html
  2. +125 −0 irctest.py
View
@@ -0,0 +1,36 @@
+<html>
+<head>
+<script type="text/javascript">
+chiral = {
+ _comet_handler: function(str) {
+ var ul = document.getElementById("ul");
+ var tn = document.createTextNode(str + "\n");
+ var li = document.createElement("li");
+ li.appendChild(tn);
+ ul.appendChild(li);
+ if (ul.childNodes.length > 10) {
+ ul.removeChild(ul.firstChild);
+ }
+ }
+};
+</script>
+<body>
+IRC
+<ul style="margin: 0; padding: 0; list-style: none" id="ul"></ul>
+<iframe src="/irc/" style="width:0;height:0;visibility:hidden;overflow:hidden"></iframe>
+<script type="text/javascript">
+var ul = document.getElementById("ul");
+var xhr = new XMLHttpRequest();
+xhr.open("GET", "/last/", false)
+xhr.send('')
+rows = xhr.responseText.split("\n");
+for(i = 0; i < rows.length; i++) {
+ r = rows[i];
+ var tn = document.createTextNode(rows[i]);
+ var li = document.createElement("li");
+ li.appendChild(tn);
+ ul.appendChild(li);
+}
+</script>
+</body>
+</html>
View
@@ -0,0 +1,125 @@
+#/usr/bin/env python2.5
+
+print "Loading modules..."
+
+from chiral.core import stats, coroutine
+from chiral.net import reactor
+
+from chiral.messaging.irc import IRCConnection
+
+from chiral.web.httpd import HTTPServer
+from paste.urlmap import URLMap
+from paste.httpexceptions import HTTPNotFound
+from paste.pony import PonyMiddleware
+from chiral.web.introspector import Introspector
+from chiral.web.comet import CometPage
+from chiral.web.servers import StaticFileServer
+from chiral.web.framework import *
+
+class CometIRCReaderPage(CometPage):
+ def __init__(self, environ, start_resp, irc_connection):
+ """Initialize the CometIRCReader"""
+ self.irc_connection = irc_connection
+ CometPage.__init__(
+ self, environ, start_resp,
+ content_type="text/plain", method=self.METHOD_JS
+ )
+ irc_connection.add_line_handler(self.line)
+
+ def run(self):
+ """Main loop"""
+ yield coroutine.WaitForCallback()
+ pass
+
+ def line(self, data):
+ self.send_chunk(repr(data))
+
+
+class CometIRCReader(object):
+ def __init__(self, irc_connection):
+ self.irc_connection = irc_connection
+
+ def __call__(self, environ, start_response):
+ path_info = environ.get('PATH_INFO', '')
+
+ if path_info == '/':
+ return CometIRCReaderPage(environ, start_response, self.irc_connection)
+ else:
+ start_response('404 Not Found', [('Content-Type', 'text/html')])
+ return [ "404 Not Found" ]
+
+last_messages = []
+
+class IRCBot(IRCConnection):
+
+ def add_line_handler(self, handler):
+ self.line_handlers.append(handler)
+
+ def display_line(self, msg):
+ # Add to last_messages
+ last_messages.append(msg)
+ if len(last_messages) > 10: last_messages.pop(0)
+
+ for lh in self.line_handlers:
+ try:
+ lh(msg)
+ except Exception:
+ pass
+
+ def handle_message(self, sender, message):
+ print "Message from %s: %s" % (sender, message)
+
+ if sender.identifier == "j4cbo" and message.startswith("!") and " " in message:
+ command, target = message.split(" ", 1)
+ if command == "!join":
+ print "joining %s" % target
+ self.join_room(target)
+ elif command == "!part":
+ print "leaving %s" % target
+ self.leave_room(target)
+ else:
+ self.display_line("<%s> %s" % (sender.identifier, message))
+
+
+ def handle_room_message(self, room, sender, message):
+ print "Message from %s in %s: %s" % (sender, room, message)
+
+ self.display_line("%s: <%s> %s" % (room, sender.identifier, message))
+
+ def __init__(self, server, nick, username, realname):
+ IRCConnection.__init__(self, server, nick, username, realname)
+ self.line_handlers = []
+
+irc_conn = IRCBot(
+ server=("irc.freenode.net", 6667),
+ nick="j4bot",
+ username="j4cbo",
+ realname="jacob"
+)
+
+@coroutine_page()
+def last_page():
+ yield
+ print "returning: %s" % repr("\n".join(last_messages))
+ raise StopIteration("\n".join(last_messages))
+
+
+application = URLMap()
+application.update({
+ "/pony": PonyMiddleware(HTTPNotFound()),
+ "/introspector": Introspector(),
+ "/irc": CometIRCReader(irc_conn),
+ "/last": last_page,
+ "/static": StaticFileServer("/home/jacob/code/chiral/irc-static")
+})
+
+HTTPServer(
+ bind_addr = ('', 8081),
+ application = application
+).start()
+
+print "Running..."
+
+reactor.run()
+
+stats.dump()

0 comments on commit 9cba4f0

Please sign in to comment.