LOC-18 Deploy server with ansible
LOC-18 Deploy server with ansible

It could do with some tidying and some conditionals, but it's now possible to deploy the server with the ansible playbook.

file:b/LICENSE (new)
--- /dev/null
+++ b/LICENSE
@@ -1,1 +1,339 @@
-
+GNU GENERAL PUBLIC LICENSE
+                       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                            Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+                    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+                            NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+                     END OF TERMS AND CONDITIONS
+
+            How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    {description}
+    Copyright (C) {year}  {fullname}
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  {signature of Ty Coon}, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.

file:b/README.md (new)
--- /dev/null
+++ b/README.md
@@ -1,1 +1,153 @@
+# LocalChat
 
+
+## About
+
+Localchat is a simple and lightweight chat application. It's primary purpose is to provide a means to have a multi-user Off-The-Record transient chat, minimising the likelihood that anyone but the chat participants has even a record that the chat took place.
+
+It is not designed to be exposed to the internet at large. Instead, the primary intended means of use is to deploy it onto a new system (such as an EC2 instance), have user's SSH tunnel in (or use other supported methods) to use it and then discard the system once that chat has completed.
+
+In other words, it's not designed as a generic chat application, but as one to be used for clandestine chats that are hard to monitor/intercept.
+
+The provided client uses basic End To End encryption (currently using PGP as the encryption mechanism), and the server holds encrypted payloads in memory only (to ensure the ciphertext doesn't end up captured on the hosting provider's SAN for a time). Message payloads are purged after a short interval to help reduce the potential exposure were someone to be monitoring the server's memory.
+
+The default behaviour is a Multi-User Chat (MUC), however direct messaging between participants within a room is also supported.
+
+
+## Project Management
+
+Planned features, bugfixes etc can all be viewed at [project.bentasker.co.uk](https://projects.bentasker.co.uk/jira_projects/browse/LOC.html). Release notes are at [bentasker.co.uk](https://www.bentasker.co.uk/documentation/release-notes/91-localchat)
+
+
+
+
+
+
+## Dependancies
+
+### Server
+
+See [server/README.md](server/README.md)
+
+
+### Client
+
+See [client/README.md](client/README.md)
+
+
+
+
+
+## Deployment Options
+
+LocalChat is not designed to be publicly accessible and discoverable. It's been written to be relatively light-weight for ease of deployment (and subsequent destruction).
+
+Under no circumstances should you configure it to bind to `0.0.0.0` as doing so would allow adversaries to not only discover it, but to start probing it in order to try and establish whether it's in use (and who's using it). There should *always* be some additional step (i.e. being able to SSH into a server, or knowing exactly where to find it) required before access is available.
+
+Over time, it will likely be hardened further, but it's unlikely it will ever be considered safe for completely unrestricted access - allowing discoverability would allow adversaries to establish likely meeting places in advance.
+
+
+
+### Direct Communication
+
+The most simple deployment method is to run the server component somewhere, and then simply run the client on the same system. The problem with this is it means that the E2E encryption keys are in memory on the same system as the server.
+
+    ./server/LocalChat.py
+    ./client/LocalChatClient.py
+
+
+So, it's recommended that you run the client on a seperate system and use a SSH tunnel to allow communication
+
+    user@server1 $ ./server/LocalChat.py
+
+    remuser@mymachine $ ssh -L 8090:127.0.0.1:8090 user@server1
+    remuser@mymachine $ ./client/LocalChatClient.py
+
+This ensures that anyone able to observe memory on the server cannot see the E2E keys.
+
+As a variation of this, if you do not want the server component to appear on the server's filesystem (you will still need to install dependancies though) then you can also create a reverse tunnel back to another machine. Just be aware that if the connection drops, the server will be unavailable, so ensure you've a reliable connection
+
+
+    user@server2 $ ./server/LocalChat.py
+    user@server2 $ ssh -R 127.0.0.1:8090:127.0.0.1:8090 user@server1
+
+    remuser@mymachine $ ssh -L 8090:127.0.0.1:8090 user@server1
+    remuser@mymachine $ ./client/LocalChatClient.py
+    
+
+    
+### Proxied Communications
+
+It may be that you decide it's better to "hide" the server within an existing website, so that chat connections are mixed in with traffic to that site.
+
+To do this, you simply need to proxy a path on that website back to the server. So you may have an existing Nginx server block like this:
+
+    server {
+            listen 443;
+
+            root /usr/share/nginx/example.com/;
+            index index.html;
+
+            server_name example.com;
+
+            ssl                  on;
+            ssl_certificate      /etc/pki/tls/certs/example.com.crt;
+            ssl_certificate_key  /etc/pki/tls/private/example.com.key;
+
+            ssl_session_timeout  5m;
+
+            location / {
+                try_files $uri $uri/ =404;
+            }
+
+    }
+
+You'd then add a `location` statement with a hard to guess path to handle the chat client
+
+    server {
+            listen 443;
+
+            root /usr/share/nginx/example.com/;
+            index index.html;
+
+            server_name example.com;
+
+            ssl                  on;
+            ssl_certificate      /etc/pki/tls/certs/example.com.crt;
+            ssl_certificate_key  /etc/pki/tls/private/example.com.key;
+
+            ssl_session_timeout  5m;
+
+            location / {
+                try_files $uri $uri/ =404;
+            }
+            
+            
+            location /SM9vbtNrnZ0d6WQa1ByLjZEX/ {
+                proxy_pass https://127.0.0.1:8090/;
+            }        
+
+    }
+
+And make the server component available on that port (either by running directly, or using a SSH reverse tunnel as described above).
+
+Assuming `example.com` serves a publicly signed and trusted certificate (you'd hope it does), we'll also want to re-enable cert verification. So the client is called with verification enabled and being passed the URL to use for requests
+
+    ./client/LocalChatClient.py --verify https://example.com/SM9vbtNrnZ0d6WQa1ByLjZEX/
+ 
+Later versions will implement the ability to include auth headers in the request so that you can 404 unauthorised requests to the 'hidden' path. Until then, unless there's a particularly strong reason not to, the SSH methods described above are the recommended routes of access.
+
+
+
+
+## Copyright
+
+LocalChat is Copyright (C) 2018 B Tasker. All Rights Reserved. 
+
+Released Under GNU GPL V2 License, see LICENSE.
+
+
+
+
+
+

file:b/ansible/README.md (new)
--- /dev/null
+++ b/ansible/README.md
@@ -1,1 +1,9 @@
+LocalChat Ansible Playbook Instructions
+=========================================
 
+
+
+Dependancies
+----------------
+
+Requires ansible >= 2.2

--- /dev/null
+++ b/ansible/group_vars/localchat-servers.yml
@@ -1,1 +1,3 @@
+ansible_user: ansible
+ansible_become: true
 

--- /dev/null
+++ b/ansible/inventory/hosts
@@ -1,1 +1,4 @@
+[localchat-servers]
+192.168.7.253
 
+

--- /dev/null
+++ b/ansible/locaLchat.service
@@ -1,1 +1,15 @@
+[Unit]
+Description=LocalChat
+After=multi-user.target
 
+[Service]
+WorkingDirectory=/usr/local/localchat
+Type=idle
+ExecStart=/usr/bin/python LocalChat.py
+Restart=always
+RestartSec=3
+
+[Install]
+WantedBy=multi-user.target
+
+

--- /dev/null
+++ b/ansible/localchat-servers.retry
@@ -1,1 +1,2 @@
+192.168.7.253
 

--- /dev/null
+++ b/ansible/localchat-servers.yml
@@ -1,1 +1,7 @@
+---
 
+-   hosts: localchat-servers
+    roles:
+        - localchat-server
+
+

--- /dev/null
+++ b/ansible/roles/localchat-server/defaults/main.yml
@@ -1,1 +1,4 @@
+localchat_server_install_path: /usr/local/src/localchat
+localchat_server_install_ver: 0.0.2.1
 
+

--- /dev/null
+++ b/ansible/roles/localchat-server/handlers/main.yml
@@ -1,1 +1,14 @@
+---
 
+# Handlers
+
+
+
+- name: Start service
+  # See above.
+  command: systemctl start localchat.service
+
+- name: reload systemd
+  command: systemctl daemon-reload
+
+

--- /dev/null
+++ b/ansible/roles/localchat-server/tasks/main.yml
@@ -1,1 +1,46 @@
+---
 
+- name: Install Utils
+  apt: name={{item}} state=installed
+  with_items:
+    - python-pip
+    - python-sqlite
+    - unzip
+  tags: deps
+
+- name: Install Python Dependancies
+  pip: name={{item}} state=present
+  with_items:
+    - flask
+    - werkzeug
+    - bcrypt
+    - gnupg
+    - pyopenssl
+  tags: deps
+
+
+- name: Create install dir
+  file: path={{localchat_server_install_path}} state=directory
+  
+- name: Download and install Server
+  unarchive:
+    src: "https://github.com/bentasker/LocalChat/archive/v{{localchat_server_install_ver}}.zip"
+    dest: "{{localchat_server_install_path}}"
+    remote_src: yes
+  
+- name: Symlink latest
+  file:
+    src: "{{localchat_server_install_path}}/LocalChat-{{localchat_server_install_ver}}/server/LocalChat.py"
+    dest: "{{localchat_server_install_path}}/LocalChat.py"
+    state: link
+    
+- name: Create Unit File
+  template: src=localchat.service dest=/etc/systemd/system/localchat.service
+  notify: reload systemd
+
+
+- name: Start server
+  command: systemctl start localchat
+
+
+

--- /dev/null
+++ b/ansible/roles/localchat-server/templates/localchat.service
@@ -1,1 +1,14 @@
+[Unit]
+Description=LocalChat
+After=multi-user.target
 
+[Service]
+WorkingDirectory={{localchat_server_install_path}}
+Type=idle
+ExecStart=/usr/bin/python LocalChat.py
+Restart=always
+RestartSec=3
+
+[Install]
+WantedBy=multi-user.target
+

file:b/ansible/site.yml (new)
--- /dev/null
+++ b/ansible/site.yml
@@ -1,1 +1,2 @@
+- include: localchat-servers.yml
 

--- a/client/LocalChatClient.py
+++ b/client/LocalChatClient.py
@@ -18,18 +18,15 @@
 import ssl
 import string
 import random
-
+import sys
 import datetime as dt
 
 import gnupg
 
 
 
-# We'll get these from the commandline later
-USER='ben2'
+# This can be changed from the commandline
 SERVER='https://127.0.0.1:8090'
-ROOMNAME='BenTest'
-
 
 class msgHandler(object):
     
@@ -41,6 +38,7 @@
         self.roompass = False
         self.sesskey = False
         self.syskey = False
+        self.verifycert = False
         self.gpg = gnupg.GPG()
     
     
@@ -386,12 +384,12 @@
         data = json.dumps(data)
         
         try:
-            # The cert the other end will be considered invalid
-            #
-            # Ignore it
-            ctx = ssl.create_default_context()
-            ctx.check_hostname = False
-            ctx.verify_mode = ssl.CERT_NONE
+            
+            ctx = ssl.create_default_context()            
+            
+            if not self.verifycert:
+                ctx.check_hostname = False
+                ctx.verify_mode = ssl.CERT_NONE
 
             req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'})
             f = urllib2.urlopen(req,context=ctx)
@@ -461,7 +459,7 @@
 similar to cmd.Cmd in standard library
 just extend with do_something  method to handle your commands"""
 
-    def __init__(self,quit_commands=['q','quit','exit'], help_commands=['help','?', 'h']):
+    def __init__(self,quit_commands=['/q','/quit','/exit'], help_commands=['/help','/?', '/h']):
         self._quit_cmd=quit_commands
         self._help_cmd=help_commands
         
@@ -596,11 +594,15 @@
                     c.output('To join the room, they should do /join %s %s:%s %s' %(n[0],n[1],n[2],n[3]))
                     return                                        
                     
+            if cmd in ["help",'h','?']:
+                return self.help(args[0] if args else None)
+            
+            if cmd in ["exit",'q','quit']:
+                return Commander.Exit
+            
 
         if cmd in self._quit_cmd:
             return Commander.Exit
-        elif cmd in self._help_cmd:
-            return self.help(args[0] if args else None)
         elif hasattr(self, 'do_'+cmd):
             return getattr(self, 'do_'+cmd)(*args)
         else:
@@ -616,6 +618,9 @@
             hc ='|'.join(self._help_cmd)
             res='Type [%s] to quit program\n' % qc
             res += """Available commands: 
+
+            /h                                                          Print this help text
+
             
             /join [room] [password] [username]                          Join a room
             /leave                                                      Leave current room
@@ -624,6 +629,7 @@
             
             /room invite [user]                                         Invite a user into the current room
             /me [string]                                                Send an 'action' instead of a message
+            /msg [user] message                                         Send a direct message to [user]
             
             Room Admin commands:
             
@@ -796,11 +802,25 @@
         
 
 
+def handle_cmdline_opts(argv,msg):
+    ''' Process  any commandline options
+    '''
+    
+    if argv[-1][0:2] == 'ht':
+        msg.server=argv[-1]
+        
+    if "--verify" in argv:
+        msg.verifycert = True
+
+    return msg
+
     
 if __name__=='__main__':
-    
-    
+        
     msg = msgHandler()
+    
+    if len(sys.argv) > 1:
+        msg = handle_cmdline_opts(sys.argv,msg)
     
     
     class TestCmd(Command):

file:b/client/README.md (new)
--- /dev/null
+++ b/client/README.md
@@ -1,1 +1,85 @@
+# LocalChat Client
 
+
+## About
+
+Localchat is a simple and lightweight chat application. It's primary purpose is to provide a means to have a multi-user Off-The-Record transient chat, minimising the likelihood that anyone but the chat participants has even a record that the chat took place.
+
+This client uses basic End To End encryption (currently using PGP as the encryption mechanism), and the server holds encrypted payloads in memory only (to ensure the ciphertext doesn't end up captured on the hosting provider's SAN for a time). 
+
+The default behaviour is a Multi-User Chat (MUC), however direct messaging between participants within a room is also supported.
+
+
+
+
+## Dependancies
+
+The following Python modules are required (other client dependancies should all already be present so long as you have python installed)
+
+* urllib2
+* urwid
+* gnupg
+
+
+
+
+
+## Usage Instructions
+
+Commands are IRC like:
+
+            /help                                                       Print Usage information
+
+            /join [room] [password] [username]                          Join a room
+            /leave                                                      Leave current room
+            /room create [roomname] [roompass] [admin user]             New room management
+
+
+            /room invite [user]                                         Invite a user into the current room
+            /me [string]                                                Send an 'action' instead of a message
+            /msg [user] message                                         Send a direct message to [user]
+
+            Room Admin commands:
+
+            /kick [user]                                                Kick a user out of the room (they can return)
+            /ban [user]                                                 Kick a user out and disinvite them (they cannot return)
+            /room close [roompass]                                      Kick all users out and close the room
+
+
+            /exit                                                       Quit the client
+
+
+
+## Commandline Arguments
+
+The client takes a limited number of commandline arguments. By default, none are needed, but depending on your deployment methodology, some of the following may be required.
+
+
+    ./LocalChatClient.py [--verify] [server]
+    
+
+When `--verify` is present, SSL certificate verification will be enabled (which means the server must present a certificate trusted by your system and valid for the server's address. By default that's not the case).
+
+If specified, `server` should be the last argument and must be of the format `https://[servername/ip[:port]]/[path]` (port is optional, default is `8090`).
+
+See the main [README](../README.md) for examples of deployments where these flags may be required.
+
+
+
+
+## Client Message API
+
+To send and receive messages, the client uses the [Server API](../docs/server-api.md), however, messages are contained within an encrypted payload that the server cannot see into. In order to be compatible with this client, message payloads need to be formed as a JSON encapsulated string with the following structure
+
+    {
+        "text":"[message text]",
+        "verb":[message verb]
+    }
+
+Message verbs were defined in [LOC-16](https://projects.bentasker.co.uk/jira_projects/browse/LOC-16.html). The default is `say` and any unrecognised verbs will be treated as such - an ordinary message. 
+
+The verb `do` will cause the client to print a `/me` message:
+
+            ** ben writes documentation **
+
+Once the JSON string has been generated, it should be PGP encrypted using the room key as a passphrase. The client uses Symmetric AES-256 encryption when encrypting, but will support decrypting anything that the Python PGP bindings support.

--- /dev/null
+++ b/docs/server-api.md
@@ -1,1 +1,323 @@
-
+Server API
+============
+
+All requests to the server are placed with the `POST` method, using a JSON request body
+
+The basic structure is
+
+    {
+        "action":"[api action]",
+        "payload": [json payload]
+    }
+
+Which, if successful, will result in a `HTTP 200` and a JSON encapsulated response body.
+
+----
+
+Response Codes
+---------------
+
+The following HTTP statuses may be returned
+
+* `200` - Request was successful
+* `400` - Invalid request
+* `403` - Authentication Invalid/Permission Denied
+* `500` - Server had an issue
+
+----
+
+Authentication
+---------------
+
+Authentication is performed on a per-room basis, so certain calls (such as `createRoom`) are currently entirely unauthenticated (a form of auth may be introduced for those later).
+
+User's are required to authenticate themselves (via password) when they attempt to enter a room with a `joinRoom` call. If that call is successful then a session key will be provided in the response. This must be embedded into every payload (as attribute `sesskey`) after that.
+
+The server uses `bcrypt` for storing authentication credentials.
+
+----
+
+Supported Actions
+-------------------
+
+
+### banUser
+
+Can only be called by the room admin, kicks a user out of the room, and does not allow them to return (unless re-invited)
+
+    {
+        "action":"banUser",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "user": user,
+                    "kick": [user to kick],
+                    "sesskey": sessionkey
+        }
+    }
+
+*Response*
+
+If successful, the value of `status` will be `ok` and the kicked user's session will be terminated and their user record removed. A message will be pushed to the room by `SYSTEM` to note that that user has been kicked and banned.
+
+If the requesting user lacks the privileges to kick a user, `SYSTEM` will push a message to warn that `user` tried to ban `tokick`.
+
+----
+
+### closeRoom
+
+This can only be successfully called by the room's owner. `closeRoom` will close the current room, remove all associated messages, sessions and user accounts.
+
+
+    {
+        "action":"closeRoom",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "user": user,
+                    "sesskey": sessionkey
+        }
+    }
+
+*Response*
+
+If successful, the value of `status` will be `ok`.
+
+Rooms should always be closed when they are no longer required. The server will (by default) automatically close inactive rooms after a period, but this is intended as a safety net (in case the admin gets disconnected and cannot reconnect for some reason).
+
+----
+
+### createRoom
+
+This is used to create a room. Calls are currently unauthenticated because user accounts are tied to a room rather than existing in a global namespace (this is done to limit the risk of a list of possible users sitting on the server until they're likely to be needed).
+
+    {
+        "action":"createRoom",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "owner": [name of user],
+                    "pass": password
+        }
+    }
+
+When creating a room, we specify the username of the user who will be the room owner/admin, and the password they will use to join the room. In the background this will set up the requisite user account.
+
+
+*Response*
+
+If the room is successfully created, the value of `status` will be `ok`. The roomname will also be confirmed back as the attribute `name`.
+
+The user will then need to call `joinRoom` in order to enter the room (the client could do this automatically, but the current client version does not).
+
+----
+
+### inviteUser
+
+Used to invite a user into the current room. Can be called by admin's and users alike (`SYSTEM` will push a message into the room to notify others of the invite)
+
+    {
+        "action":"inviteUser",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "user": user,
+                    "invite": [user to invite],
+                    "pass": [new users pass]
+                    "sesskey": sessionkey
+        }
+    }
+
+When inviting a user, we need to specify their username as `invite` and their authentication password as `pass`
+
+*Response*
+
+If successful, the value of `status` will be `ok`. `SYSTEM` will also push a message into the room that `user` invited `invite`
+
+----
+
+### joinRoom
+
+Used to enter a room.
+
+When the user was invited to the room, a password will have been set for them (the supplied client currently auto-generates that password), it will need to be provided for the user to authenticate.
+
+    {
+        "action":"joinRoom",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "userpass": password,
+                    "user": user
+        }
+    }
+
+*Response*
+
+If authentication is successful, the response will include a number of details that the client will need to take note of for future requests
+
+    {
+        "status": "ok",
+        "last": last,
+        "session": sessionkey,
+        "syskey": systemkey
+    }
+    
+The value `last` is the ID of the latest message in the room you've just joined. It should be included in `pollMsg` calls.
+
+The value `sessionkey` is a server generated sessionkey. It must be included in the payload of all future requests (it's used to help authenticate those requests).
+
+The value `syskey` is a decryption passphrase. The server's internal user `SYSTEM` will E2E encrypt any messages it pushes into rooms, this is the key you should use to decrypt those messages.
+
+----
+
+### kickUser
+
+Can only be called by the room admin, kicks a user out of the room (but allows them to return). Useful where you think a user's session has hung (for example)
+
+    {
+        "action":"kickUser",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "user": user,
+                    "kick": [user to kick],
+                    "sesskey": sessionkey
+        }
+    }
+
+*Response*
+
+If successful, the value of `status` will be `ok` and the kicked user's session will be terminated. A message will be pushed to the room by `SYSTEM` to note that that user has been kicked.
+
+If the requesting user lacks the privileges to kick a user, `SYSTEM` will push a message to warn that `user` tried to kick `tokick`.
+
+----
+
+### leaveRoom
+
+Used to leave the current room
+
+    {
+        "action":"leaveRoom",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "user": user,
+                    "sesskey": sessionkey
+        }
+    }
+
+*Response*
+
+If successful, the value of `status` will be `ok`. Client's should then disable any automated message polling they are running.
+
+----
+
+### pollMsg
+
+Used to check whether there are any messages since the last the client received. It will return room-wide messages and direct messages addressed to the polling user.
+
+Client provides the ID of the last message they've received, as well as the room they're polling. Where this is the first request, `last` should be 0
+
+    {
+        "action":"pollMsg",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "mylast": "[last]",
+                    "user": user,
+                    "sesskey": sessionkey
+        }
+    }
+
+
+*Response*
+
+If there are no messages, then the response status will be `unchanged`.
+If a specific error is being returned, the response status will be `errmessage` - you should disable any automatic polling and the print the content of `text`
+
+If status is `updated` then messages have been returned:
+
+    {
+        "status": "updated",
+        "messages" : [
+                        [msgid,msgtext,timestamp,fromuser,touser],
+                        [msgid,msgtext,timestamp,fromuser,touser]    
+        ]
+
+    }
+
+Once the client has processed the received messages, the value of `last` in the next `pollMsg` request should be the maximum ID in the resultset.
+
+----
+
+### sendDirectMsg
+
+Used to send a message to a specific user within the room. The message will not be visible to other room occupants.
+
+    {
+        "action":"sendDirectMsg",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "msg": "[message payload]",
+                    "to": recipient
+                    "user": user,
+                    "sesskey": sessionkey
+        }
+    }
+
+The message payload is encrypted and base64 encoded by the client, so the server only sees a base64 string. 
+
+However, to keep compatability with the supplied client, your message payload (prior to encryption) should be a JSON encapsulated string of the format
+
+        msg = {
+            'user': user,
+            'text': msg,
+            "verb": verb
+            }
+
+This should then be PGP encrypted using the room passphrase, and then base64 encoded for embedding into the API payload.
+            
+Where `verb` is one of `do` or `say` (other values will be treated as `say` by the supplied client).
+
+
+*Response*
+
+If message sending is successful, the response will contain `status:"ok"`
+
+----
+
+### sendMsg
+
+Used to send a message to the entire room
+
+    {
+        "action":"sendMsg",
+        "payload": {    
+                    "roomName": "[name of room]",
+                    "msg": "[message payload]",
+                    "user": user,
+                    "sesskey": sessionkey
+        }
+    }
+
+The message payload is encrypted and base64 encoded by the client, so the server only sees a base64 string. 
+
+However, to keep compatability with the supplied client, your message payload (prior to encryption) should be a JSON encapsulated string of the format
+
+        msg = {
+            'user': user,
+            'text': msg,
+            "verb": verb
+            }
+
+This should then be PGP encrypted using the room passphrase, and then base64 encoded for embedding into the API payload.
+            
+Where `verb` is one of `do` or `say` (other values will be treated as `say` by the supplied client).
+
+
+*Response*
+
+If message sending is successful, the response will contain `status:"ok"`
+
+
+----
+
+
+
+
+    
+    

--- a/server/LocalChat.py
+++ b/server/LocalChat.py
@@ -73,7 +73,6 @@
             print "WARNING - Messages will be written to disk"
 
 
-
     def createDB(self):
         ''' Create the in-memory database ready for use 
         '''
@@ -139,7 +138,6 @@
         thread.start_new_thread(taskScheduler,(self.cronpass,self.bindpoint))
 
 
-
     def processSubmission(self,reqjson):
         ''' Process an incoming request and route it to
         the correct function
@@ -197,17 +195,12 @@
         elif reqjson['action'] == 'pollMsg':
             return self.fetchMsgs(reqjson)
          
-        
-        
-        
-        
 
     def decrypt(self,msg):
         ''' This is currently just a placeholder
         Will be updated later
         '''
         return msg
-
 
 
     def createRoom(self,reqjson):
@@ -267,7 +260,6 @@
             }
         
 
-
     def closeRoom(self,reqjson):
         ''' Close a room.
         
@@ -305,10 +297,6 @@
         self.conn.commit()
         
         return { "status" : "ok" }
-    
-        
-           
-        
 
 
     def inviteUser(self,reqjson):
@@ -345,8 +333,7 @@
         return {
                 "status":'ok'
             }
-        
-    
+
     
     def kickUser(self,reqjson,ban=False):
         ''' Kick a user out of room
@@ -369,8 +356,6 @@
         
         if not n:
             return self.returnFailure(403)
-        
-        
         
         self.cursor.execute("UPDATE users set active=0 where room=? and username=?",(room,reqjson["payload"]["kick"]))
         
@@ -388,9 +373,8 @@
             self.pushSystemMsg("User %s banned %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn')
             
         return { "status" : "ok" }
-    
-
-    
+
+
     def processjoinRoom(self,reqjson):
         ''' Process a request from a user to login to a room
         
@@ -403,12 +387,10 @@
             if i not in reqjson['payload']:
                 return self.returnFailure(400)
                 
-        
         room = self.getRoomID(reqjson['payload']["roomName"])
         
         if not room:
             return self.returnFailure(400)
-        
         
         if reqjson["payload"]["user"] == "SYSTEM":
             return self.returnFailure(403)
@@ -419,13 +401,11 @@
         
         if not r:
             return { "status": "NOK" }
-        
         
         # Now we need to verify they've supplied a correct password for that user
         stored = r[2].encode("utf-8")
         if stored != bcrypt.hashpw(reqjson['payload']['userpass'].encode('utf-8'),stored):
             return self.returnFailure(403)
-        
             
         # Tidy older messages away.
         #
@@ -435,15 +415,12 @@
         # to scroll up and down in their client anyway
         self.tidyMsgs(time.time()-10,room)
         
-        
         # Push a message to the room to note that the user joined
         msgid = self.pushSystemMsg("User %s joined the room" % (reqjson['payload']['user']),room)
-
 
         # If we're in testing mode, push a warning so the new user can see it
         if self.testingMode:
             msgid = self.pushSystemMsg("Server is in testing mode. Messages are being written to disk",room,'syswarn')
-
 
         # Check the latest message ID for that room
         self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
@@ -457,15 +434,14 @@
         # Mark the user as active in the users table
         self.cursor.execute("UPDATE users set active=1 where username=? and room=?", (reqjson['payload']['user'],room))
         
-        
         # Create a session for the user
         sesskey = "%s-%s" % (reqjson['payload']["roomName"],self.genSessionKey())
         self.cursor.execute("INSERT INTO sessions (username,sesskey) values (?,?)", (reqjson['payload']['user'],sesskey))
         self.conn.commit()
                 
         return {"status":"ok","last":last,"session":sesskey,"syskey":self.syskey}
-        
-        
+
+
     def processleaveRoom(self,reqjson):
         ''' Process a user's request to leave a room
         '''
@@ -491,8 +467,8 @@
         # Push a message to the room to note they left
         self.pushSystemMsg("User %s left the room" % (reqjson['payload']['user']),room)
         return {"status":"ok"}
-    
-    
+
+
     def sendMsg(self,reqjson):
         ''' Push a message into a room
         
@@ -502,7 +478,6 @@
         
         if not self.validateUser(reqjson['payload']):
             return self.returnFailure(403)
-        
         
         if "roomName" not in reqjson['payload'] or "msg" not in reqjson['payload']:
             return self.returnFailure(400)
@@ -511,8 +486,7 @@
         print room
         if not room:
             return self.returnFailure(400)
-
-            
+        
         self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user']))
         msgid = self.cursor.lastrowid
         self.conn.commit()
@@ -525,7 +499,6 @@
             last = 0
         else:
             last = r[0]
-        
         
         # Update the last activity field in the DB
         # See LOC-11
@@ -537,7 +510,6 @@
                 "msgid" : msgid,
                 "last" : last
             }
-        
 
 
     def sendDirectMsg(self,reqjson):
@@ -587,10 +559,8 @@
                 "msgid" : msgid,
                 "last" : last
             }
-        
-
-
-        
+
+
     def fetchMsgs(self,reqjson):
         ''' Check to see if there are any new messages in the room
         
@@ -605,7 +575,6 @@
         print room
         if not room:
             return self.returnFailure(400)
-
 
         if not self.validateUser(reqjson['payload']):
             return self.returnFailure(403,reqjson['payload'],room)
@@ -627,18 +596,13 @@
         return {"status":"updated",
                 "messages" : r
                 }
-    
-    
-        
-        
-        
-    
+
+
     def validateUser(self,payload):
         ''' Placeholder for now. Auth will be handled in more depth later
         '''
         if "user" not in payload or "roomName" not in payload:
             return False
-        
         
         # Validate the session information
         self.cursor.execute("SELECT username from sessions where username=? and sesskey=?",(payload['user'],payload['sesskey']))
@@ -647,13 +611,10 @@
         if not r:
             return False
         
-        
         room = self.getRoomID(payload["roomName"])
         if not room:
             return False        
         
-        
-        
         # Check whether the user has been marked as active
         self.cursor.execute("SELECT username, room from users where username=? and room=? and active=1",(payload['user'],room))
         r = self.cursor.fetchone()
@@ -664,7 +625,6 @@
         return True
 
 
-    
     def getRoomID(self,roomname):
         ''' Get a room's ID from its name
         '''
@@ -676,7 +636,6 @@
             return False
         
         return r[0]
-    
 
 
     def triggerClean(self,reqjson):
@@ -697,8 +656,7 @@
         self.autoCloseRooms()
         
         return {'status':'ok'}
-        
-        
+
 
     def tidyMsgs(self,thresholdtime,room=False):
         ''' Remove messages older than the threshold time
@@ -717,8 +675,6 @@
         self.cursor.execute("DELETE FROM failuremsgs  where expires < ?",(time.time(),))
 
 
-
-
     def autoCloseRooms(self):
         ''' Automatically close any rooms that have been sat idle for too long
         '''
@@ -734,10 +690,6 @@
             self.cursor.execute("DELETE FROM sessions where sesskey like ?", (r[1] + '-%',))
             self.cursor.execute("DELETE FROM rooms where id=?",(r[0],))
             self.conn.commit()
-            
-            
-        
-
 
 
     def pushSystemMsg(self,msg,room,verb="sysinfo"):
@@ -769,8 +721,6 @@
         '''
         self.cursor.execute("INSERT INTO failuremsgs (username,room,expires,msg) values (?,?,?,?)",(user,room,time.time() + 300,msg))
         self.conn.commit()
-        
-        
 
 
     def returnFailure(self,status,reqjson=False,room=False):
@@ -796,9 +746,7 @@
             self.conn.commit()
             return {"status":"errmessage","text":r[0]}
         
-        
         return status
-        
 
 
     def encryptSysMsg(self,msg):
@@ -809,17 +757,15 @@
             more info.
         
         '''
-        
-        crypted = self.gpg.encrypt(msg,None,passphrase=self.syskey,symmetric="AES256",armor=False)
+
+        crypted = self.gpg.encrypt(msg,None,passphrase=self.syskey,symmetric="AES256",armor=False,encrypt=False)
         return crypted.data.encode('base64')
-        
 
 
     def genSessionKey(self,N=48):
         return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits + '/=?&@#%^()+,.<>:!').encode('utf-8') for _ in range(N))
 
 
-
     def genpassw(self,N=16):
         ''' Generate a random string of chars to act as an encryption password
         '''
@@ -827,10 +773,8 @@
         return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(N))
 
 
-
 # Create the scheduler function
 def taskScheduler(passw,bindpoint):
-    
     
     # Ignore cert errors
     ctx = ssl.create_default_context()
@@ -853,37 +797,29 @@
             # Don't let the thread abort just because one request went wrong
             continue
 
-    
-
 
 if __name__ == '__main__':
+    passw = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(64))
+    bindpoint = "https://127.0.0.1:8090" 
+    purgeinterval = 600 # Wipe messages older than 10 mins
+    closethresh = 3600 * 6 # Auto-close rooms after 6 hours of inactivity
     
     # LOC-15
     testingmode = False
     if '--testing-mode-enable' in sys.argv:
         testingmode = True
-    
-    
-    # These will be handled properly later
-    passw = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(64))
-    bindpoint = "https://127.0.0.1:8090" 
-    purgeinterval = 600 # Wipe messages older than 10 mins
-    closethresh = 3600 * 6 # Auto-close rooms after 6 hours of inactivity
-
+        purgeinterval = 30
+        closethresh = 180
+    
     # Create a global instance of the wrapper so that state can be retained
     #
     # We pass in the password we generated for the scheduler thread to use
     # as well as the URL it should POST to
     msghandler = MsgHandler(passw,bindpoint,purgeinterval,closethresh,testingmode)
 
-
     # Bind to PORT if defined, otherwise default to 8090.
+    #
+    # This will likely become a CLI argument later
     port = int(os.environ.get('PORT', 8090))
-    app.run(host='0.0.0.0', port=port,debug=True,ssl_context='adhoc')
-
-
-
-
-
-
-
+    app.run(host='127.0.0.1', port=port,ssl_context='adhoc',threaded=False)
+

file:b/server/README.md (new)
--- /dev/null
+++ b/server/README.md
@@ -1,1 +1,50 @@
+# LocalChat Server
 
+
+## About
+
+Localchat is a simple and lightweight chat application. It's primary purpose is to provide a means to have a multi-user Off-The-Record transient chat, minimising the likelihood that anyone but the chat participants has even a record that the chat took place.
+
+It binds to the loopback adapter, and uses ad-hoc SSL to ensure that chat messages aren't available to anyone capable of sniffing loopback traffic. Where clients are remote, there are a number of possible deployment options, see [The main README](../README.md) for more information on these.
+
+The internal database is stored in memory only, to ensure that metadata isn't written to (and therefore recoverable from) disk.
+
+It's intended to be incredibly lightweight, so is provided as a single Python file rather than being broken out into multiple files. Although it can support a reasonable number of active clients, it *is* a single threaded application and isn't designed to support 1000's of active users.
+
+
+
+
+## Dependancies
+
+The following non-standard modules are required
+
+* flask
+* urllib2
+* sqlite3
+* bcrypt
+* gnupg
+
+
+
+## Usage Instructions
+
+To start the server, simply run it
+
+    ./LocalChat.py
+    
+
+## Commandline Arguments
+
+The server takes a limited number of commandline arguments. By default, none are needed.
+
+
+    ./LocalChat.py [--testing-mode-enable]
+    
+
+When `--testing-mode-enable` is present, the internal database is written to disk rather than being kept in memory. This is for testing purposes only, and is outright dangerous for use in production. When this mode is enabled, whenever a user joins a room, `SYSTEM` will push a message to warn the room that messages are being written to disk. Testing mode also changes purge thresholds to a lower number so that the automated cleans will complete in a time more conduicive to testing. *Do Not* use this argument without very, very good reason.
+
+
+
+
+
+

--- a/tests/run_tests.py
+++ b/tests/run_tests.py
@@ -11,6 +11,7 @@
 import sqlite3
 import traceback
 import json
+import datetime
 
 
 try:
@@ -143,7 +144,8 @@
     test_results = []
     tests = ['test_one','test_two','test_three','test_four',
              'test_five','test_six','test_seven','test_eight',
-             'test_nine']
+             'test_nine','test_ten','test_eleven','test_twelve',
+             'test_thirteen','test_fourteen','test_fifteen']
     x = 1
     for test in tests:
         print "Running %s " % (test,)
@@ -562,7 +564,464 @@
     return [result,isFatal]
 
 
-    
+def test_ten(msg):
+    ''' Send a direct message and ensure it's received
+    
+    '''
+    
+    result = {'Test' : 'Send a Direct Message','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    # Poll for messages to clear the queue
+    f = msg.pollForMessage()
+    f = STORAGE['testuser']['clientInstance'].pollForMessage()
+    
+    testpayload = 'Hi Hi'
+    
+    # Now send a direct message from test user to admin
+    n = STORAGE['testuser']['clientInstance'].sendDirectMsg(testpayload,'testadmin')
+    if not n:
+        result['Notes'] = 'Could not send direct message'
+        return [result,isFatal]
+    
+    # Poll as the admin user to ensure it was received
+    received = msg.pollForMessage()
+    
+    # Poll as testuser to clear the queue ready for future tests
+    f = STORAGE['testuser']['clientInstance'].pollForMessage()
+    
+    if len(received) < 1:
+        result['Notes'] = 'No messages received'
+        return [result,isFatal]
+    
+    if len(received[0]) < 2:
+        result['Notes'] = 'Malformed message returned'
+        return [result,isFatal]
+        
+    # Now check the payload
+    #
+    # We need to trim out the timestamp etc
+    r = received[0][1].split('>')
+    m = r[1].lstrip()
+    if testpayload != m:
+        result['Notes'] = 'Incorrect message received: (%s)v(%s)' % (testpayload,m)
+        return [result,isFatal]
+        
+    if "DM" not in r[0]:
+        result['Notes'] = 'Envelope not marked as DM: %s' % (r[0],)
+        return [result,isFatal]
+        
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+    
+
+
+def test_eleven(msg):
+    ''' Invite a new user and then have admin kick them to ensure they're 
+    actually kicked out of the room
+    
+    '''
+    result = {'Test' : 'Invite and kick a user','Result' : 'FAIL', 'Notes': '' }
+    isFatal = True
+    
+    n = msg.inviteUser('testuser2')
+    if not n:
+        result['Notes'] = 'Could not invite testuser2'
+        return [result,isFatal]
+    
+    if len(n) < 4:
+        result['Notes'] = 'Client returned too short response'
+        return [result,isFatal]
+        
+    # Otherwise, we've got details for a new user to be able to join
+    #
+    # Store them for a later test
+    
+    STORAGE['testuser2'] = {
+        'room':n[0],
+        'pass':"%s:%s" % (n[1],n[2]),
+        'User':n[3]
+        }
+    
+    # Create a new instance so we can join as testuser2
+    usermsg = getClientInstance();
+    n = usermsg.joinRoom(STORAGE['testuser2']['User'],STORAGE['testuser2']['room'],STORAGE['testuser2']['pass'])
+    
+    if not n:
+        result['Notes'] = 'User could not join'
+        return [result,isFatal]
+    
+    STORAGE['testuser2']['clientInstance'] = usermsg
+    
+    # Now have the admin kick (but not ban) them
+    msg.kickUser('testuser2',False)
+    
+    
+    # Check that a failure message was written
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'User not notified'
+        return [result,isFatal]  
+        
+    # Poll to receive the failure message and verify it's deleted
+    msgs = STORAGE['testuser2']['clientInstance'].pollForMessage()
+    
+    if len(msgs) < 1:
+        result['Notes'] = "User didn't receive notification"
+        return [result,isFatal]        
+    
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r:
+        result['Notes'] = 'Notification not purged on poll'
+        return [result,isFatal]  
+    
+    # Now check that their session was suspended
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT username FROM users where username=? and active=1",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r:
+        result['Notes'] = 'User still considered active in room'
+        return [result,isFatal]         
+    
+    # Check if their session still exists
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()    
+    if r:
+        result['Notes'] = 'User still has active session'
+        return [result,isFatal]        
+    
+    # Otherwise, looks good
+    result['Result'] = "Pass"
+    return [result,isFatal]
+
+
+def test_twelve(msg):
+    ''' Rejoin as the previously invited user, and then ban them
+    
+    
+    '''
+    result = {'Test' : 'Ban a user','Result' : 'FAIL', 'Notes': '' }
+    isFatal = True
+    
+    # Flush the failedmessage queue
+    f = STORAGE['testuser2']['clientInstance'].pollForMessage()
+    
+    # Re-join as testuser2
+    n = STORAGE['testuser2']['clientInstance'].joinRoom(STORAGE['testuser2']['User'],
+                                                        STORAGE['testuser2']['room'],STORAGE['testuser2']['pass'])
+    if not n:
+        result['Notes'] = 'TestUser2 could not join'
+        return [result,isFatal]
+    
+    # Now have the admin ban them
+    msg.kickUser('testuser2',True)
+    
+    # Check that a failure message was written
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'User not notified'
+        return [result,isFatal]  
+    
+    # Poll to receive the failure message and verify it's deleted
+    msgs = STORAGE['testuser2']['clientInstance'].pollForMessage()
+    
+    if len(msgs) < 1:
+        result['Notes'] = "User didn't receive notification"
+        return [result,isFatal]        
+    
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r:
+        result['Notes'] = 'Notification not purged on poll'
+        return [result,isFatal]  
+   
+    # Now check that their session was suspended
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT username FROM users where username=?",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r:
+        result['Notes'] = 'User still exists for room'
+        return [result,isFatal]         
+    
+    # Check if their session still exists
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()    
+    if r:
+        result['Notes'] = 'User still has active session'
+        return [result,isFatal]        
+    
+    # We don't need this any more
+    del STORAGE['testuser2']
+    
+    # Otherwise, looks good
+    result['Result'] = "Pass"
+    return [result,isFatal]
+
+
+def test_thirteen(msg):
+    ''' Check that /leave works
+    
+    Essentially, we send a message, wait 240 seconds and then check if it's gone
+    
+    In testing mode, closure is after 3 mins, but we wait an extra minute to make sure the scheduler has run
+    
+    
+    '''
+    result = {'Test' : 'Leave a room','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    newmsg = getClientInstance()
+
+    n = msg.inviteUser('testuser3')
+    if not n:
+        result['Notes'] = 'Could not invite testuser'
+        return [result,isFatal]
+    
+    if len(n) < 4:
+        result['Notes'] = 'Client returned too short response'
+        return [result,isFatal]
+        
+    # Otherwise, we've got details for a new user to be able to join
+    #
+    # Store them for a later test
+    
+    STORAGE['testuser3'] = {
+        'room':n[0],
+        'pass':"%s:%s" % (n[1],n[2]),
+        'User':n[3]
+        }
+
+    n = newmsg.joinRoom(STORAGE['testuser3']['User'],STORAGE['testuser3']['room'],STORAGE['testuser3']['pass'])
+    
+    if not n:
+        result['Notes'] = 'Could not join'
+        return [result,isFatal]
+    
+    STORAGE['testuser3']['clientInstance'] = newmsg
+    
+    CONN,CURSOR = opendb()
+    
+    # Check the DB to ensure They're now active
+    CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser3']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'Not Active in DB'
+        return [result,isFatal]
+    
+
+
+    # Now have them leave the room and check they drop out of the DB
+    if not newmsg.leaveRoom():
+        result['Notes'] = 'API reported failure'
+        return [result,isFatal]
+
+
+
+    # They should no longer be in the DB
+    CONN,CURSOR = opendb()
+    
+    # Check the DB to ensure They're now active
+    CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser3']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r:
+        result['Notes'] = 'We left but are still active in the DB'
+        return [result,isFatal]
+    
+    # Verify the client cleared it's stored info
+    if newmsg.room or newmsg.roompass:
+        result['Notes'] = 'Client still stores room info'
+        return [result,isFatal]
+        
+    
+    # Otherwise, we're good
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+def test_fourteen(msg):
+    ''' Check that message purging is actually happening
+    
+    Essentially, we send a message, wait 90 seconds and then check if it's gone
+    
+    '''
+    result = {'Test' : 'Automated message expiration','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    msg.sendMsg('This is a test message')
+    
+    # Open the DB and verify there are messages in the queue now
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages")
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r or r[0] < 1:
+        result['Notes'] = 'Messages not getting into queue'
+        return [result,isFatal]  
+     
+    # Now, we wait
+    time.sleep(90)
+    
+    # Re-open the database and the messages should be gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages")
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r and r[0] > 0:
+        result['Notes'] = 'Messages not purged'
+        return [result,isFatal]  
+         
+    # Otherwise, we're good
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+def test_fifteen(msg):
+    ''' Check that auto room closure happens
+    
+    Essentially, we send a message, wait 240 seconds and then check if it's gone
+    
+    In testing mode, closure is after 3 mins, but we wait an extra minute to make sure the scheduler has run
+    
+    
+    '''
+    result = {'Test' : 'Automated Room Closure','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    newmsg = getClientInstance()
+
+    n = newmsg.createRoom('TestRoom2','testadmin2')
+    if not n:
+        result['Notes'] = 'Empty Response'
+        return [result,isFatal]
+
+    # The client should have given us two passwords
+    if len(n) < 2:
+        result['Notes'] = 'Response too small'
+        return [result,isFatal]
+
+    # Seperate out the return value
+    roompass = n[0]
+    userpass = n[1] # user specific password
+
+    n = newmsg.joinRoom('testadmin2','TestRoom2',
+                    "%s:%s" % (n[0],n[1])
+                    )
+    if not n:
+        result['Notes'] = 'Could not join'
+        return [result,isFatal]
+    
+    # Get the room ID
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT id from rooms where name=?",('TestRoom2',))
+    r = CURSOR.fetchone()
+    CONN.close()    
+    
+    room = r[0]
+    
+    newmsg.sendMsg('This is a test message')
+    
+    # Open the DB and verify there are messages in the queue now
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r or r[0] < 1:
+        result['Notes'] = 'Messages not getting into queue'
+        return [result,isFatal]  
+     
+    # Now, we wait
+    time.sleep(240)
+    
+    # Re-open the database and the messages should be gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r and r[0] > 0:
+        result['Notes'] = 'Messages not purged'
+        return [result,isFatal]  
+
+    # The room should be gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from rooms where name=?",('TestRoom2',))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'Room record not deleted'
+        return [result,isFatal]          
+
+    # Check the user records have gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from users where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'User records not deleted'
+        return [result,isFatal]          
+    
+    # Check Failure messages have gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from failuremsgs where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'Failure Messages not deleted'
+        return [result,isFatal]          
+    
+    # Check Sessions have gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from sessions where sesskey like 'TestRoom2-%'")
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'User Sessions not deleted'
+        return [result,isFatal]          
+    
+    # Otherwise, we're good
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+
+
 
 
 if __name__ == '__main__':
@@ -579,7 +1038,13 @@
         # I don't like generic catchall exceptions
         # but, we want to make sure we kill the background
         # process if there is one.
+        cmd = "git show | head -n1"
+        ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
+        revision = ps.communicate()[0].split(" ")[1]
+        now = datetime.datetime.utcnow().strftime("%Y%m%d%H%M%S")
+        print """Test Run %s-%s""" % (now,revision)
         results = run_tests()
+        
     except Exception as e:
         print traceback.format_exc()
         print e