Skip to content

Commit

Permalink
Added the Pyramid-specific files to the project
Browse files Browse the repository at this point in the history
Added credits to README.txt for the inspiration of how to create the
chat window.
  • Loading branch information
Kirk Strauser committed May 20, 2011
1 parent db6d69e commit 8f7b4b1
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.txt
Expand Up @@ -111,6 +111,13 @@ interfering with each other.
Seshat is also the name of the Egyption goddess of wisdom, knowledge, and
writing. This is pure coincidence.

# Credits

Inspiration for building the chat window (and bits of HTML and CSS) came
from:

http://net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-simple-web-based-chat-application/

# License

The standalone broker bot uses the GPL-licensed xmpppy library, and is thus
Expand Down
10 changes: 10 additions & 0 deletions pyramid/__init__sample.py
@@ -0,0 +1,10 @@
#!/usr/bin/env python

def main(global_config, **settings):

# ...

# These routes handle viewing the chat window and sending/receiving messages
config.add_route('chat', '/chat', view='myapp.views.chat.chat', renderer='myapp:templates/chatwindow.pt')
config.add_route('chatrecv', '/chat/recvmessage/{chatid}', view='myapp.views.chat.recvmessage', renderer='json')
config.add_route('chatsend', '/chat/sendmessage/{chatid}', view='myapp.views.chat.sendmessage', renderer='json')
7 changes: 7 additions & 0 deletions pyramid/development_sample.ini
@@ -0,0 +1,7 @@
[app:MyApp]
# All the normal application settings...
reload_templates = true
debug_authorization = false
debug_notfound = false
# ...plus:
seshat_sqlitedb = /tmp/seshat.db
34 changes: 34 additions & 0 deletions pyramid/static/chatstyle.css
@@ -0,0 +1,34 @@
/* CSS Document */
body {
font:12px arial;
color: #222;
text-align:center;
padding:35px; }

#wrapper {
margin:0 auto;
padding-bottom:25px;
padding-top:25px;
background:#EBF4FB;
width:504px;
border:1px solid #ACD8F0; }

#chatbox {
text-align:left;
margin:0 auto;
margin-bottom:25px;
padding:10px;
background:#fff;
height:270px;
width:430px;
border:1px solid #ACD8F0;
overflow:auto; }

#sendtext {
width:395px;
border:1px solid #ACD8F0; }

#sendbutton { width: 60px; }

span.tovisitor { color: red; }
span.fromvisitor { color: blue; }
69 changes: 69 additions & 0 deletions pyramid/templates/chatwindow.pt
@@ -0,0 +1,69 @@
<!DOCTYPE html PUBLIC "- //W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat - Customer Module</title>
<link type="text/css" rel="stylesheet" href="/static/chatstyle.css" />
</head>
<body>
<div id="wrapper">
<div id="menu">
</div>

<div id="chatbox">Messages you send will <span class="fromvisitor">look like this</span>, and messages you receive will <span class="tovisitor">look like this</span>.</div>

<form name="message" action="">
<input name="sendtext" type="text" id="sendtext" size="63" />
<input name="action" type="submit" id="sendbutton" value="Send" />
</form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){

// Append the new message to the bottom of the chat window, and scroll
// the window to display it if needed
function updateChat(direction, message){
if(message == '') { return false; }
// Let the browser's DOM escape any HTML entities contained in the message
var encoder = document.createElement("div");
encoder.innerText = encoder.textContent = message;
message = encoder.innerHTML;
delete encoder;
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
$("#chatbox").append('<br /><span class="' + direction + 'visitor">' + message + '</span>');
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
}

// User presses "enter" or clicks the "Send" button
$("#sendbutton").click(function(){
var sendtext = $("#sendtext").val();
// Don't pester the server with empty messages
if(sendtext == '') { return false; }
$.post("/chat/sendmessage/${chatid}", {text: sendtext});
$("#sendtext").attr("value", "");
updateChat('from', sendtext);
return false;
});

// Poll the server for new messages to display
function getNewMessage(){
$.ajax({
url: "/chat/recvmessage/${chatid}",
cache: false,
success: function(response){
updateChat('to', response);
},
});
};

setInterval (getNewMessage, 1000);

$("#sendtext").focus();
});
</script>
</body>
</html>
33 changes: 33 additions & 0 deletions pyramid/views/chat.py
@@ -0,0 +1,33 @@
#!/usr/bin/env python

"""Allow web visitors to chat with local users logged into a Jabber server"""

from pyramid.security import authenticated_userid
from seshat import client

def getseshatvalues(request):
"""Return a configured Seshat client and the username of the visitor"""
user = authenticated_userid(request)
return (client.SeshatClient(request.registry.settings['seshat_sqlitedb']),
user if user is not None else 'Anonymous')

def chat(request):
"""Get a new chatid and open the chat window"""
message = 'Coming from page %s' % request.referer if request.referer is not None else None
seshatclient, user = getseshatvalues(request)
return {'chatid': seshatclient.startchat(user, message)}

def recvmessage(request):
"""Poll the server for the first queued message in this chat"""
chatid = int(request.matchdict['chatid'])
seshatclient, user = getseshatvalues(request)
message = seshatclient.getmessage(chatid, user)
if message is None:
return ''
return message

def sendmessage(request):
"""Queue the visitor's message for delivery to the chat's localuser"""
chatid = int(request.matchdict['chatid'])
seshatclient, user = getseshatvalues(request)
seshatclient.sendmessage(chatid, user, request.params['text'])

0 comments on commit 8f7b4b1

Please sign in to comment.