diff --git a/main.py b/main.py index a903cb8..67e9ac8 100755 --- a/main.py +++ b/main.py @@ -22,6 +22,8 @@ class Meshenger: os.chdir(os.path.dirname(__file__)) # change present working directory to the one where this file is self.own_ip = self.get_ip_adress().strip() + # this hash is needed in clientserve, so client can generate color + self.own_hash = self.hasj(self.own_ip) if not os.path.exists(self.msg_dir): os.mkdir(self.msg_dir) diff --git a/meshenger_clientserve.py b/meshenger_clientserve.py index d5ae680..579bb46 100644 --- a/meshenger_clientserve.py +++ b/meshenger_clientserve.py @@ -24,6 +24,14 @@ Serve index and messages if self.path == '/index' or self.path.startswith( "/"+self.messageDir ): return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) + elif self.path == '/id': + if meshenger and meshenger.own_hash: + self.send_response(200) + self.send_header('Content-type', 'text-html') + self.end_headers() + self.wfile.write(meshenger.own_hash) + else: + self.send_error(404,'Id not yet available') elif self.path == '/log': self.send_response(200) self.send_header('Content-type', 'text-html') @@ -39,8 +47,7 @@ Serve index and messages self.path = '/'+f return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) - - else: + elif self.path == '/old': self.send_response(200) #serve the webapp on every url requested self.send_header('Content-type', 'text/html') self.end_headers() @@ -48,6 +55,10 @@ Serve index and messages with open( f, 'r') as the_file: self.wfile.write(the_file.read()) + else: + self.path = '/' + os.path.join('web', 'index.html') + return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) + def do_POST(self): """ diff --git a/web/.index.html.swo b/web/.index.html.swo index d158c50..aa83ec3 100644 Binary files a/web/.index.html.swo and b/web/.index.html.swo differ diff --git a/web/index.html b/web/index.html index f708dae..c0313c4 100644 --- a/web/index.html +++ b/web/index.html @@ -15,19 +15,35 @@ + - - - +
+ + + +
- - - - +
+ + +
diff --git a/web/main.js b/web/main.js index bbfd3cd..03363a3 100644 --- a/web/main.js +++ b/web/main.js @@ -1,35 +1,43 @@ localStorage.clear(); + + +//These need to be obtained from the node +var ownId, ownColor; + /* * OUTBOX STUFF */ -document.getElementById( 'send' ).onclick = function() { +document.getElementById('message-form').onsubmit = function(){ + var outStr = localStorage.getItem( 'outbox' ) || ''; + + if (document.getElementById('name').value == ""){ + var namm = "anonymous"; + } + else{ + var namm = document.getElementById('name').value; + } + var mess = document.getElementById('message').value.replace(/\r?\n/g, "
"); + var newMsgs = {}; + var ddata = new Date().getTime(); + var contento = { + "time" : ddata, + "message" : mess, + "name" : namm, + "node" : "local", + "hops" : "0" + } + newMsgs.message = contento; + + localStorage.setItem( 'outbox', JSON.stringify(newMsgs) ); + updateOutboxView(); + checkOutbox(); + document.getElementById('message').value = ''; + + return false; +} - var outStr = localStorage.getItem( 'outbox' ) || ''; - if (document.getElementById('name').value == ""){ - var namm= "anonymous"; - } - else{ - var namm= document.getElementById('name').value; - } - var mess = document.getElementById('message').value.replace(/\r?\n/g, "
"); - var newMsgs ={}; - var ddata= new Date().getTime(); - var contento = { - "time" : ddata, - "message" : mess, - "name" : namm, - "node" : "local", - "hops" : "0" - } - newMsgs.message = contento; - - localStorage.setItem( 'outbox', JSON.stringify(newMsgs) ); - updateOutboxView(); - checkOutbox(); - document.getElementById('message').value = ''; -}; function checkOutbox() { - var outStr = localStorage.getItem( 'outbox' ); + var outStr = localStorage.getItem( 'outbox' ); if ( ! outStr ) { return; } @@ -102,40 +110,86 @@ function updateOutboxView() { function updateInboxView() { - var localStorageArray = new Array(); - var contentString = ''; +var localStorageArray = new Array(); + var contentString = ''; - if (localStorage.length>0) { - for (i=0;i0) { + for (i=0;i'+ + '
On ' + datereadable + + ' ' + orderStorage[i].user +' wrote:
' + + '
' + orderStorage[i].message + '
' + + ' from '+orderStorage[i].node + '' + + ' via '+orderStorage[i].hops+' nodes'; + } + document.getElementById( 'inbox' ).innerHTML = contentString; +} +function getReadableDate( date ) { + var day = date.getDate(); + if (day < 10) day = '0' + day; + var month = date.getMonth()+1; + if (month < 10) month = '0' + month; + var year = date.getFullYear(); + var hrs = date.getHours(); + if (hrs < 10) hrs = '0' + hrs; + var min = date.getMinutes(); + if (min < 10) min = '0' + min; + + return day + '/' + month + '/' + year + ' ' + hrs + ':' + min; +} +function getNodeColor( nodeId ) { + if( nodeId === 'local' ) { + return ownColor || '#fff'; + } + return colorLuminance(nodeId.substr(0,6), 0.5); +} +function colorLuminance(hex, lum) { + + // validate hex string + hex = String(hex).replace(/[^0-9a-f]/gi, ''); + if (hex.length < 6) { + hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2]; } - orderStorage = localStorageArray.sort(function(a,b) { return b.time - a.time } ); + lum = lum || 0; - for(var i in orderStorage) - { - if ( i.length === 0 || i === 'outbox' ) { - continue; - } - var date = new Date(parseInt(orderStorage[i].time)); -// date.setHours(date.getHours() + 2); - var datereadable = date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()+" "+date.getHours()+":"+date.getMinutes(); - contentString += '
  • ' + datereadable + ' ' + ' '+ orderStorage[i].user +'
    '+orderStorage[i].message+' '+orderStorage[i].node+' '+orderStorage[i].hops+'
  • '; + // convert to decimal and change luminosity + var rgb = "#", c, i; + for (i = 0; i < 3; i++) { + c = parseInt(hex.substr(i*2,2), 16); + c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16); + rgb += ("00"+c).substr(c.length); } - document.getElementById( 'inbox' ).innerHTML = contentString; + + return rgb; } function onMessageDownload( msg, filename ) { @@ -170,35 +224,58 @@ updateInboxView(); } function downloadMessage(filename) { - var xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function(){ - if (xhr.readyState == 4 && xhr.status == 200){ - onMessageDownload( xhr.responseText, filename ); - } - } - xhr.open( "GET", 'msg/'+filename, true); - xhr.send(); + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function(){ + if (xhr.readyState == 4 && xhr.status == 200){ + onMessageDownload( xhr.responseText, filename ); + } + } + xhr.open( "GET", 'msg/'+filename, true); + xhr.send(); } function checkInbox() { - var xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function(){ - if (xhr.readyState == 4 && xhr.status == 200){ - onIndex( xhr.responseText ); - } + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function(){ + if (xhr.readyState == 4 && xhr.status == 200){ + onIndex( xhr.responseText ); + } + } + xhr.open( "GET", 'index', true); + xhr.send(); +} +function getOwnId() { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function(){ + if (xhr.readyState == 4 && xhr.status == 200){ + ownId = xhr.responseText; + ownColor = getNodeColor( ownId ); } - xhr.open( "GET", 'index', true); - xhr.send(); + } + xhr.open( "GET", 'id', true); + xhr.send(); } + /* * INIT */ +function update(){ + if ( !ownId ){ + getOwnId(); + } + checkInbox(); + // also check for outbox items on interval, + // necessary in case connection is lost and messages are not yet sent + checkOutbox(); +} + +//update everything to initialize updateInboxView(); updateOutboxView(); -window.setInterval( function(){ - checkInbox(); - checkOutbox(); -}, 7000 ); +update(); + +//check for new messages every 7 seconds +window.setInterval( update, 7000 ); diff --git a/web/style.css b/web/style.css index 367d2c4..3a9cd93 100644 --- a/web/style.css +++ b/web/style.css @@ -5,42 +5,60 @@ body, html{ body{ font-size:20px; - text-shadow: 1px 1px orange; + text-shadow: 1px 1px #ddd; font-family: times, 'times new roman', helvetica,serif; line-height:1.5em; color:#444; background:#fff; - //background-image: url(css/marble.jpg); + padding:20px; } -textarea{ + +#message-form textarea{ -webkit-appearance: none; -moz-appearance: none; - display: block; - margin-left: auto; - margin-right: auto; - width: 90%; height: 50px; - line-height: 40px; font-size: 17px; - border: 1px solid #bbb; - -webkit-border-radius: 3px; - -moz-border-radius: 3px; - border-radius: 3px; - //background-image: url(css/aqua.jpg); + display: block; + outline:none; + width: 98%; + margin: 0 auto; + line-height: 40px; + border:none; + -webkit-border-radius: 13px; + -moz-border-radius: 13px; + border-radius: 13px; font-family: inherit; - font-size: inherit; font-size: 20px; + + -moz-box-shadow:0 0 23px #bbb; + -webkit-box-shadow:0 0 23px #bbb; + box-shadow:0 0 23px #bbb; + } -button { +#message-form .send-button { + -webkit-appearance: none; + -moz-appearance: none; + outline: none; + //border: 1px solid #bbb; + border:0; + -webkit-border-radius: 13px; + -moz-border-radius: 13px; + border-radius: 13px; + height:40px; + width:100%; display: block; - margin-left: auto; - margin-right: auto; - margin: 1.5em 0; font-family: inherit; - font-size: inherit; font-size: 20px; + font-weight:bold; + text-shadow: 0 0 23px #888; + background-color:#fff; + + -moz-box-shadow:0 0 23px #bbb; + -webkit-box-shadow:0 0 23px #bbb; + box-shadow:0 0 23px #bbb; + } select{ @@ -55,10 +73,10 @@ select{ } - - h2{ + margin: 0 0 20px; font-size: 40px; + text-shadow: 0px 0px 23px #bbb; } hr{ @@ -68,16 +86,19 @@ hr{ background-image: url(css/bg1.gif); } -ul{ - width:95%; - padding-left:30px; -} -li{ - word-wrap:break-word; +.messages ul{ + width:100%; + padding:0px; +} +.messages li{ list-style: none; - overflow: visible; + width:100%; +} +.messages .message-block { padding:30px; + word-wrap:break-word; + overflow: visible; -moz-box-shadow:0 0 23px #bbb; -webkit-box-shadow:0 0 23px #bbb; box-shadow:0 0 23px #bbb; @@ -93,14 +114,16 @@ li{ border-top-right-radius:123px; border-bottom-right-radius:123px; border-bottom-left-radius:123px; - left:-35px; - - width:100%; - //overflow:hidden; - position:relative; background:#fff; - //background:-webkit-gradient(linear, left top, left bottom, from(#fff), to(#eee)); - //background:-moz-linear-gradient(top, #fff, #eee); +} +.node, .hops { + font-size:10px; +} +.message-text { + font-size: 25px; +} +.date-sender { + font-size: 15px; } #outbox{ display:none;