LOC-29 Fix NO_RECP exception when calling gpg.encrypt on Debian9
LOC-29 Fix NO_RECP exception when calling gpg.encrypt on Debian9

file:a/.gitignore -> file:b/.gitignore
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 *~
 *.pyc
+localchat-testing.db
 
 

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.
+
+
+
+
+
+

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

--- /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.yml
@@ -1,1 +1,7 @@
+---
 
+-   hosts: localchat-servers
+    roles:
+        - localchat-server
+
+

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

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

--- /dev/null
+++ b/ansible/roles/localchat-server/tasks/main.yml
@@ -1,1 +1,26 @@
+---
 
+- name: Install Dependancies
+  apt: name={{item}} state=installed
+  with_items:
+    - python-flask
+    - python-urllib3
+    - python-sqlite
+    - python-bcrypt
+    - python-gnupg
+  become: true
+  tags: deps
+    
+
+    
+    
+    
+    
+    
+-   name: Create Unit File
+    template: src=localchat.service dest=/etc/systemd/system/localchat.service
+    notify: reload systemd
+    become: true
+
+    
+

--- /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):
     
@@ -39,6 +36,9 @@
         self.server = SERVER
         self.room = False
         self.roompass = False
+        self.sesskey = False
+        self.syskey = False
+        self.verifycert = False
         self.gpg = gnupg.GPG()
     
     
@@ -51,7 +51,8 @@
         
         payload = {"roomName": self.room, 
                    "mylast":self.last,
-                   "user": self.user
+                   "user": self.user,
+                   "sesskey": self.sesskey
                    }
         
         request = {"action":"pollMsg",
@@ -66,14 +67,23 @@
         
         if resp['status'] == "unchanged":
             return False
+        
+        
+        if resp['status'] == "errmessage":
+            # Got an error, we need to stop the poll and then return the text
+            self.room = False
+            self.roompass = False
+            self.sesskey = False
+            return [['reversed',resp['text']]]
         
         to_print = []
         # Otherwise, process the messages
         for i in resp["messages"]:
             self.last = i[0]
+            upstruser = i[3]
             
             try:
-                msgbody = json.loads(self.decrypt(i[1]))
+                msgbody = json.loads(self.decrypt(i[1],upstruser))
             except:
                 # Means an invalid message was received - LOC-8
                 to_print.append(['error','Received message which could not be decrypted'])
@@ -83,30 +93,44 @@
             # but not currently including that info in my curl tests. Also means test that part of the next block
             
             color = "green"
-            upstruser = msgbody['user'] # Temporary placeholder
-            
+
             if upstruser == self.user:
                 # One of our own, change the color
                 color = "blue"
             
             elif upstruser == "SYSTEM":
                 color = "magenta"
+                if msgbody['verb'] == "sysalert":
+                    color = 'reversed'
+                elif msgbody['verb'] == 'syswarn':
+                    color = 'cyan'
                 
             
             ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]")
             
-            line = [
-                ts, # timestamp
-                "%s>" % (upstruser,), # To be replaced later
-                msgbody['text']
-                ]
+            if msgbody["verb"] == "do":
+                color = 'yellow'
+                line = [
+                    "        ** %s %s **" % (upstruser,msgbody['text'])
+                    ]
+            else:
+                
+                if i[4] != "0":
+                    color = 'brown'
+                    upstruser = 'DM %s' % (upstruser,)
+                
+                line = [
+                    ts, # timestamp
+                    "%s>" % (upstruser,), # To be replaced later
+                    msgbody['text']
+                    ]
             
             to_print.append([color,' '.join(line)])
         
         return to_print
         
         
-    def sendMsg(self,line):
+    def sendMsg(self,line,verb='say'):
         ''' Send a message 
         '''
         
@@ -119,12 +143,14 @@
         
         msg = {
             'user': self.user,
-            'text': line
+            'text': line,
+            "verb": verb
             }
         
         payload = {"roomName": self.room, 
                    "msg":self.encrypt(json.dumps(msg)),
-                   "user": self.user
+                   "user": self.user,
+                   "sesskey": self.sesskey
                    }
         
         request = {"action":"sendMsg",
@@ -133,11 +159,48 @@
 
         resp = self.sendRequest(request)        
         
-        if resp['status'] == "ok":
+        if "status" in resp and resp['status'] == "ok":
             return True
         
         return False
         
+
+
+    def sendDirectMsg(self,line,user,verb='say'):
+        ''' Send a direct message
+        '''
+        
+        if not self.room:
+            # We're not in a room. Abort
+            return False
+        
+        # Otherwise, build a payload
+        
+        msg = {
+            'user': self.user,
+            'text': line,
+            "verb": verb
+            }
+        
+        payload = {"roomName": self.room, 
+                   "msg":self.encrypt(json.dumps(msg)), # TODO this should use the user's key
+                   "to": user,
+                   "user": self.user,
+                   "sesskey": self.sesskey
+                   }
+        
+        request = {"action":"sendDirectMsg",
+                   "payload": json.dumps(payload)
+                   }
+
+        resp = self.sendRequest(request)        
+        
+        if "status" in resp and resp['status'] == "ok":
+            return True
+        
+        return False
+
+
 
 
     def joinRoom(self,user,room,passw):
@@ -149,7 +212,7 @@
         userpass = p[1]
         
         payload = {"roomName": room, 
-                   "userpass": userpass,
+                   "userpass": userpass.encode('utf-8'),
                    "user": user
                    }
         
@@ -168,6 +231,8 @@
         self.user = user
         self.last = resp['last']
         self.roompass = p[0] # The room password is the first section of the password
+        self.sesskey = resp['session']
+        self.syskey = resp['syskey']
         return True
 
 
@@ -180,7 +245,8 @@
             return False
         
         payload = {"roomName": self.room, 
-                   "user": self.user
+                   "user": self.user,
+                   "sesskey": self.sesskey
                    }
         
         request = {"action":"leaveRoom",
@@ -197,6 +263,7 @@
         self.user = False
         self.last = 0
         self.roompass = False
+        self.sesskey = False
         
         return True
                 
@@ -239,6 +306,7 @@
 
         payload = {"roomName": self.room, 
                    "user": self.user,
+                   "sesskey": self.sesskey
                    }
         
         request = {"action":"closeRoom",
@@ -259,9 +327,15 @@
         #TODO - Authentication
         '''
         
+        
+        # Generate a password for the new user
+        passw = self.genpassw()
+        
         payload = {"roomName": self.room, 
                    "user": self.user,
-                   "invite": user
+                   "invite": user,
+                   "pass": passw,
+                   "sesskey": self.sesskey
                    }
         
         request = {"action":"inviteUser",
@@ -273,7 +347,7 @@
         if resp == "BROKENLINK" or resp['status'] != "ok":
             return False
         
-        return True
+        return [self.room,self.roompass,passw,user]
 
 
 
@@ -288,7 +362,8 @@
         
         payload = {"roomName": self.room, 
                    "user": self.user,
-                   "kick": user
+                   "kick": user,
+                   "sesskey": self.sesskey
                    }
         
         request = {"action":action,
@@ -309,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)
@@ -326,17 +401,19 @@
 
 
 
-    def decrypt(self,msg):
+    def decrypt(self,msg,sender):
         ''' Placeholder
         '''
-        
-        try:
-            # Check if it's json as it may be a system message
-            json.loads(msg)
-            return msg
+                
+        try:       
+            key = self.roompass
+            if sender == "SYSTEM":
+                key = self.syskey
+                
+            return str(self.gpg.decrypt(msg.decode("base64"),passphrase=key))
         
         except:
-            return str(self.gpg.decrypt(msg.decode("base64"),passphrase=self.roompass))
+            return False
         
     
 
@@ -382,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
         
@@ -397,6 +474,14 @@
             cmd = cmd[1:]
             
             
+            if cmd == "me":
+                #/me [string]
+                r = msg.sendMsg(' '.join(args),'do')
+                if not r:
+                    raise NotInRoom(line)
+                return
+
+            
             if cmd == "ban":
                 # /kick [user]
                 
@@ -440,6 +525,25 @@
                 global c
                 c.output('Left the room','magenta')
                 return
+
+
+            if cmd == 'msg':
+                # /msg ben Hello ben this is a DM
+                line = ' '.join(args[1:])
+                r = msg.sendDirectMsg(line,args[0])
+                if not r:
+                    raise NotInRoom(line)
+                
+                # Otherwise push a copy of the message to display
+                # cos we won't get this one back from pollMsg
+                global c
+                
+                m = "%s DM %s>%s" % (msg.user,args[0],line)
+                
+                c.output(m,'blue')
+                
+                return
+
         
             if cmd == "room":
                 
@@ -487,13 +591,18 @@
                     
                     global c
                     c.output('User %s may now join room' %(args[1],))
+                    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:
@@ -509,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
@@ -516,7 +628,8 @@
             
             
             /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:
             
@@ -614,7 +727,12 @@
               ('error', urwid.LIGHT_RED, urwid.BLACK),
               ('green', urwid.DARK_GREEN, urwid.BLACK),
               ('blue', urwid.LIGHT_BLUE, urwid.BLACK),
-              ('magenta', urwid.DARK_MAGENTA, urwid.BLACK), ]
+              ('magenta', urwid.DARK_MAGENTA, urwid.BLACK), 
+              ('yellow', urwid.YELLOW, urwid.BLACK), 
+              ('cyan', urwid.LIGHT_CYAN, urwid.BLACK), 
+              ('brown', urwid.BROWN, urwid.BLACK), 
+              
+              ]
     
     
     def __init__(self, title, command_caption='Message:  (Tab to switch focus to upper frame, where you can scroll text)', cmd_cb=None, max_size=1000):
@@ -684,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
@@ -13,13 +13,19 @@
 from flask import Flask
 from flask import request, make_response
 
-
-
+import thread
+import urllib2
+import ssl
 import sqlite3
 import time
 import os
 import json
 import bcrypt
+import random
+import string
+import gnupg
+import sys
+
 
 app = Flask(__name__)
 
@@ -39,9 +45,9 @@
         return make_response("",400)
     
     a = msghandler.processSubmission(reqjson)
-    
+
     # Check the status
-    if a in [400,403]:
+    if a in [400,403,500]:
         response = make_response("",a)
         return response
         
@@ -51,33 +57,55 @@
 class MsgHandler(object):
 
 
-    def __init__(self):
+    def __init__(self,cronpass,bindpoint,purgeinterval,closethresh,testingmode):
         self.conn = False
         self.cursor = False
-
+        # Generate a key for encryption of SYSTEM messages (LOC-13)
+        self.syskey = self.genpassw(16)
+        self.gpg = gnupg.GPG()
+        self.cronpass = cronpass
+        self.bindpoint = bindpoint
+        self.purgeInterval = purgeinterval
+        self.roomCloseThresh = closethresh
+        self.testingMode = testingmode
+        
+        if self.testingMode:
+            print "WARNING - Messages will be written to disk"
 
 
     def createDB(self):
         ''' Create the in-memory database ready for use 
         '''
-        self.conn = sqlite3.connect(':memory:')
+        
+        dbpath=':memory:'
+        if self.testingMode:
+            # LOC-15 - allow DB to written to disk in test mode
+            dbpath = "%s/localchat-testing.db" % (os.getcwd(),)
+        
+        self.conn = sqlite3.connect(dbpath)
         self.cursor = self.conn.cursor()
         
-        sql = """ CREATE TABLE rooms (
+        sql = """
+        
+        DROP TABLE IF EXISTS rooms;
+        CREATE TABLE rooms (
             id INTEGER PRIMARY KEY,
             name TEXT NOT NULL UNIQUE,
-            owner TEXT NOT NULL
+            owner TEXT NOT NULL,
+            lastactivity INTEGER DEFAULT 0
         );
         
-        
+        DROP TABLE IF EXISTS messages;
         CREATE TABLE messages (
             id INTEGER PRIMARY KEY AUTOINCREMENT,
             ts INTEGER NOT NULL,
             room INTEGER NOT NULL,
+            user NOT NULL,
+            touser TEXT DEFAULT '0',
             msg TEXT NOT NULL
         );
         
-        
+        DROP TABLE IF EXISTS users;
         CREATE TABLE users (
             username TEXT NOT NULL,
             room INTEGER NOT NULL,
@@ -86,10 +114,28 @@
             PRIMARY KEY (username,room)
         );
         
+        DROP TABLE IF EXISTS sessions;
+        CREATE TABLE sessions (
+            username TEXT NOT NULL,
+            sesskey TEXT NOT NULL,
+            PRIMARY KEY(sesskey)
+        );
+        
+        DROP TABLE IF EXISTS failuremsgs;
+        CREATE TABLE failuremsgs (
+            username TEXT NOT NULL,
+            room INTEGER NOT NULL,
+            expires INTEGER NOT NULL,
+            msg TEXT NOT NULL,
+            PRIMARY KEY (username,room)
+        );
+        
         """
         
         self.conn.executescript(sql)
-
+        
+        # We also need to start the scheduler thread (LOC-6)
+        thread.start_new_thread(taskScheduler,(self.cronpass,self.bindpoint))
 
 
     def processSubmission(self,reqjson):
@@ -103,8 +149,12 @@
         
         
         print reqjson
+        
+        if "action" in reqjson and reqjson['action'] == 'schedulerTrigger':
+            return self.triggerClean(reqjson)
+        
         if "action" not in reqjson or "payload" not in reqjson:
-            return 400
+            return self.returnFailure(400)
         
         
         # Decrypt the payload
@@ -113,7 +163,7 @@
         try:
             reqjson['payload'] = json.loads(reqjson['payload'])
         except:
-            return 400
+            return self.returnFailure(400)
         
         if reqjson['action'] == "createRoom":
             return self.createRoom(reqjson)
@@ -135,6 +185,9 @@
         
         elif reqjson['action'] == "kickUser":
             return self.kickUser(reqjson,False)
+        
+        elif reqjson['action'] == 'sendDirectMsg':
+            return self.sendDirectMsg(reqjson)
         
         elif reqjson['action'] == 'sendMsg':
             return self.sendMsg(reqjson)
@@ -142,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):
@@ -182,7 +230,7 @@
         required = ['roomName','owner','pass']
         for i in required:
             if i not in reqjson['payload']:
-                return 400
+                return self.returnFailure(400)
         
         print "Creating room %s" % (reqjson['payload'])
         
@@ -196,7 +244,7 @@
             roomid = self.cursor.lastrowid
         except:
             # Probably a duplicate name, but we don't want to give the other end a reason anyway
-            return 500
+            return self.returnFailure(500)
         
         
         # Generate a password hash for the owners password
@@ -211,7 +259,6 @@
                 'name' : reqjson['payload']['roomName']
             }
         
-    
 
     def closeRoom(self,reqjson):
         ''' Close a room.
@@ -224,12 +271,12 @@
         '''
         
         if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
-            return 400
+            return self.returnFailure(400)
         
         room = self.getRoomID(reqjson['payload']["roomName"])
         
         if not room:
-            return 400
+            return self.returnFailure(400)
         
         
         # Check the requesting user is the admin
@@ -237,25 +284,19 @@
         n = self.cursor.fetchone()
         
         if not n:
-            return 403
-        
-        m = {
-            "user":"SYSTEM",
-            "text":"Room has been closed. Buh-Bye"
-        }
-        self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m))) 
-        self.conn.commit()
-        
+            return self.returnFailure(403)
+        
+
+        self.pushSystemMsg("Room has been closed. Buh-Bye",room,'syswarn')
+                
         self.cursor.execute("DELETE FROM users where room=?",(room,))
         self.cursor.execute("DELETE FROM rooms where id=?",(room,))
         self.cursor.execute("DELETE FROM messages where room=?",(room,))
+        self.cursor.execute("DELETE FROM sessions where sesskey like ?", (reqjson['payload']["roomName"] + '-%',))
+        
         self.conn.commit()
         
         return { "status" : "ok" }
-    
-        
-           
-        
 
 
     def inviteUser(self,reqjson):
@@ -263,48 +304,36 @@
         '''
         
         if "roomName" not in reqjson['payload'] or "pass" not in reqjson['payload'] or "invite" not in reqjson['payload']:
-            return 400
+            return self.returnFailure(400)
         
         room = self.getRoomID(reqjson['payload']["roomName"])
         
         if not room:
-            return 400
+            return self.returnFailure(400)
         
         if not self.validateUser(reqjson['payload']):
-            return 403
+            return self.returnFailure(403,reqjson['payload'],room)
         
         
         if reqjson['payload']['invite'] == "SYSTEM":
             # Push a notification into the group
-            m = {
-                    "user":"SYSTEM",
-                    "text":"ALERT: User %s tried to invite SYSTEM" % (reqjson['payload']['user'])
-                }
-            self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m))) 
-            self.conn.commit()
-            return 403
+            self.pushSystemMsg("ALERT: User %s tried to invite SYSTEM" % (reqjson['payload']['user']),room,'sysalert')
+            return self.returnFailure(403)
        
         
         # Generate a hash of the submitted password
         passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt())
         
         # Otherwise, link the user in
-        self.cursor.execute("INSERT INTO users (username,room) values (?,?,?)",(reqjson['payload']['invite'],room,passhash))
+        self.cursor.execute("INSERT INTO users (username,room,passhash) values (?,?,?)",(reqjson['payload']['invite'],room,passhash))
         
         # Push a notification into the group
-        m = {
-                "user":"SYSTEM",
-                "text":"User %s invited %s to the room" % (reqjson['payload']['user'],reqjson['payload']['invite'])
-            }
-        
-        self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m)))        
-        self.conn.commit()        
+        self.pushSystemMsg("User %s invited %s to the room" % (reqjson['payload']['user'],reqjson['payload']['invite']),room)
         
         return {
                 "status":'ok'
             }
-        
-    
+
     
     def kickUser(self,reqjson,ban=False):
         ''' Kick a user out of room
@@ -313,12 +342,12 @@
         '''
         
         if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload'] or "kick" not in reqjson['payload']:
-            return 400
+            return self.returnFailure(400)
         
         room = self.getRoomID(reqjson['payload']["roomName"])
         
         if not room:
-            return 400
+            return self.returnFailure(400)
         
         
         # Check the requesting user is the admin
@@ -326,155 +355,73 @@
         n = self.cursor.fetchone()
         
         if not n:
-            return 403
-        
-        
+            return self.returnFailure(403)
         
         self.cursor.execute("UPDATE users set active=0 where room=? and username=?",(room,reqjson["payload"]["kick"]))
-        m = {
-                "user":"SYSTEM",
-                "text":"User %s kicked %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick'])
-            }
-        
-        self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m)))
-        msgid = self.cursor.lastrowid
-        self.conn.commit()
-                    
+        
+        # Delete their session
+        self.cursor.execute("DELETE FROM sessions where username=? and sesskey like ?", (reqjson['payload']['kick'],reqjson['payload']["roomName"] + '-%'))
+        
+        self.pushSystemMsg("User %s kicked %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn')
+        
+        self.pushFailureMessage(reqjson['payload']['kick'],room,'You have been kicked from the room')
+        
+        
         if ban:
             # If we're banning them, also need to disinvite them
             self.cursor.execute("DELETE from users where room=? and username=?",(room,reqjson["payload"]["kick"]))
-            m = {
-                    "user":"SYSTEM",
-                    "text":"User %s banned %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick'])
-                }
+            self.pushSystemMsg("User %s banned %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn')
             
-            self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m)))
-            self.conn.commit()
-        
         return { "status" : "ok" }
-    
-    
-    
-    
+
+
     def processjoinRoom(self,reqjson):
         ''' Process a request from a user to login to a room
         
         Not yet defined the authentication mechanism to use, so that's a TODO
         '''
-        if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
-            return 400
-        
+
+        # Check the required information is present
+        required = ['roomName','user','userpass']
+        for i in required:
+            if i not in reqjson['payload']:
+                return self.returnFailure(400)
+                
         room = self.getRoomID(reqjson['payload']["roomName"])
         
         if not room:
-            return 400
-        
+            return self.returnFailure(400)
         
         if reqjson["payload"]["user"] == "SYSTEM":
-            return 403
+            return self.returnFailure(403)
         
         # Check whether that user is authorised to connect to that room
-        self.cursor.execute("SELECT username, room from users where username=? and room=?",(reqjson['payload']['user'],room))
+        self.cursor.execute("SELECT username, room,passhash from users where username=? and room=?",(reqjson['payload']['user'],room))
         r = self.cursor.fetchone()
         
         if not r:
             return { "status": "NOK" }
-        else:
+        
+        # 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.
-            #
-            # We do this so that a user who joins can't then send a poll with last:0 to retrieve the full history
-            #
-            # Basically, anything older than 10 seconds should go. Users who were already present will be able
-            # 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
-            
-            m = {
-                    "user":"SYSTEM",
-                    "text":"User %s joined the room" % (reqjson['payload']['user'])
-                }
-            
-            self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m)))
-            msgid = self.cursor.lastrowid
-            self.conn.commit()
-            
-            # 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))
-            r = self.cursor.fetchone()
-            
-            if not r:
-                last = 0
-            else:
-                last = r[0]
-                   
-            # 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))
-            self.conn.commit()
-            
-            
-            return {"status":"ok","last":last}
-        
-        
-    def processleaveRoom(self,reqjson):
-        ''' Process a user's request to leave a room
-        '''
-        if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
-            return 400
-        
-        room = self.getRoomID(reqjson['payload']["roomName"])
-        
-        if not room:
-            return 400
-        
-        # Check the user is actually in the room and authorised
-        if not self.validateUser(reqjson['payload']):
-            return 400
-        
-        # Mark them as not in the room
-        self.cursor.execute("UPDATE users set active=0 where username=? and room=?", (reqjson['payload']['user'],room))
-        self.conn.commit()
-        
-        # Push a message to the room to note they left
-        m = {
-                "user":"SYSTEM",
-                "text":"User %s left the room" % (reqjson['payload']['user'])
-            }
-        
-        self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m)))
-        msgid = self.cursor.lastrowid
-        self.conn.commit()        
-        
-        return {"status":"ok"}
-    
-    
-    def sendMsg(self,reqjson):
-        ''' Push a message into a room
-        
-        curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA\",\"user\":\"ben2\"}"}'
-        
-        '''
-        
-        if not self.validateUser(reqjson['payload']):
-            return 403
-        
-        
-        if "roomName" not in reqjson['payload'] or "msg" not in reqjson['payload']:
-            return 400
-        
-        room = self.getRoomID(reqjson['payload']["roomName"])
-        print room
-        if not room:
-            return 400
-
-            
-        self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,reqjson['payload']['msg']))
-        msgid = self.cursor.lastrowid
-        self.conn.commit()
-        
+        # Tidy older messages away.
+        #
+        # We do this so that a user who joins can't then send a poll with last:0 to retrieve the full history
+        #
+        # Basically, anything older than 10 seconds should go. Users who were already present will be able
+        # 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))
         r = self.cursor.fetchone()
@@ -483,37 +430,161 @@
             last = 0
         else:
             last = r[0]
+                
+        # 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
+        '''
+        if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
+            return self.returnFailure(400)
+        
+        room = self.getRoomID(reqjson['payload']["roomName"])
+        
+        if not room:
+            return self.returnFailure(400)
+        
+        # Check the user is actually in the room and authorised
+        if not self.validateUser(reqjson['payload']):
+            return self.returnFailure(400)
+        
+        # Mark them as not in the room
+        self.cursor.execute("UPDATE users set active=0 where username=? and room=?", (reqjson['payload']['user'],room))
+        self.conn.commit()
+        
+        # Delete their session
+        self.cursor.execute("DELETE FROM sessions where username=? and sesskey = ?", (reqjson['payload']['user'],reqjson['payload']["sesskey"]))
+        
+        # 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
+        
+        curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA\",\"user\":\"ben2\"}"}'
+        
+        '''
+        
+        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)
+        
+        room = self.getRoomID(reqjson['payload']["roomName"])
+        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()
+        
+        # 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))
+        r = self.cursor.fetchone()
+        
+        if not r:
+            last = 0
+        else:
+            last = r[0]
+        
+        # Update the last activity field in the DB
+        # See LOC-11
+        self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
+        self.conn.commit()
         
         return {
                 "status" : "ok",
                 "msgid" : msgid,
                 "last" : last
             }
-        
-        
-    def fetchMsgs(self,reqjson):
-        ''' Check to see if there are any new messages in the room
-        
-        curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"pollMsg","payload":"{\"roomName\":\"BenTest\", \"mylast\":1,\"user\":\"ben2\"}"}'
-        
+
+
+    def sendDirectMsg(self,reqjson):
+        ''' Push a message to a user in the same room
         '''
         
         if not self.validateUser(reqjson['payload']):
-            return 403
-
-        if "mylast" not in reqjson['payload']:
-            return 400
+            return self.returnFailure(403)
+                
+        required = ['roomName','msg','to']
+        for i in required:
+            if i not in reqjson['payload']:
+                return self.returnFailure(400)
         
         room = self.getRoomID(reqjson['payload']["roomName"])
         print room
         if not room:
-            return 400        
-        
-        self.cursor.execute("""SELECT id,msg,ts FROM messages
+            return self.returnFailure(400)
+
+        # Check the other user is in the room and active
+        self.cursor.execute("SELECT username from users where username=? and room=? and active=1",(reqjson['payload']['to'],room))
+        r = self.cursor.fetchone()
+        
+        if not r:
+            return self.returnFailure(400)
+            
+        self.cursor.execute("INSERT INTO messages (ts,room,msg,user,touser) VALUES (?,?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user'],reqjson['payload']['to']))
+        msgid = self.cursor.lastrowid
+        self.conn.commit()
+        
+        # Update the last activity field in the DB
+        # See LOC-11
+        self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
+        self.conn.commit()        
+        
+        # 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))
+        r = self.cursor.fetchone()
+        
+        if not r:
+            last = 0
+        else:
+            last = r[0]
+        
+        return {
+                "status" : "ok",
+                "msgid" : msgid,
+                "last" : last
+            }
+
+
+    def fetchMsgs(self,reqjson):
+        ''' Check to see if there are any new messages in the room
+        
+        curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"pollMsg","payload":"{\"roomName\":\"BenTest\", \"mylast\":1,\"user\":\"ben2\"}"}'
+        
+        '''
+
+        if "mylast" not in reqjson['payload']:
+            return self.returnFailure(400)
+        
+        room = self.getRoomID(reqjson['payload']["roomName"])
+        print room
+        if not room:
+            return self.returnFailure(400)
+
+        if not self.validateUser(reqjson['payload']):
+            return self.returnFailure(403,reqjson['payload'],room)
+        
+        self.cursor.execute("""SELECT id,msg,ts,user,touser FROM messages
             WHERE room=? AND
+            (touser = 0 OR touser = ?) AND 
             id > ?
             ORDER BY ts ASC           
-            """,(room,reqjson['payload']['mylast']))
+            """,(room,reqjson['payload']['user'],reqjson['payload']['mylast']))
         
         r = self.cursor.fetchall()
         
@@ -525,24 +596,24 @@
         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']))
+        r = self.cursor.fetchone();
+        
+        if not r:
+            return False
         
         room = self.getRoomID(payload["roomName"])
         if not room:
-            return 400        
-        
-        
+            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))
@@ -554,7 +625,6 @@
         return True
 
 
-    
     def getRoomID(self,roomname):
         ''' Get a room's ID from its name
         '''
@@ -566,12 +636,32 @@
             return False
         
         return r[0]
-    
+
+
+    def triggerClean(self,reqjson):
+        ''' Trigger a clean of old messages etc
+        '''
+        
+        if 'pass' not in reqjson:
+            # No need for failure messages here
+            return 403
+        
+        if reqjson['pass'] != self.cronpass:
+            return 403
+        
+        # Tidy messages older than 10 minutes
+        self.tidyMsgs(time.time() - self.purgeInterval);
+        
+        # Auto-close any rooms beyond the threshold
+        self.autoCloseRooms()
+        
+        return {'status':'ok'}
+
 
     def tidyMsgs(self,thresholdtime,room=False):
         ''' Remove messages older than the threshold time
         '''
-        
+
         if room:
             # Tidy from a specific room
             self.cursor.execute("DELETE FROM messages where ts < ? and room = ?",(thresholdtime,room))
@@ -581,20 +671,155 @@
             self.cursor.execute("DELETE FROM messages where ts < ?",(thresholdtime,))
             self.conn.commit()
 
-
-
-    def test(self):
-        return ['foo']
-
-
-
-
-# Create a global instance of the wrapper so that state can be retained
-msghandler = MsgHandler()
+        # Tidy away any failure messages
+        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
+        '''
+        
+        self.cursor.execute("SELECT id,name from rooms where lastactivity < ? and lastactivity > 0",(time.time() - self.roomCloseThresh,))
+        rooms = self.cursor.fetchall()
+        
+        # Messages probably have been auto-purged, but make sure
+        for r in rooms:
+            self.cursor.execute("DELETE FROM messages where room=?",(r[0],))
+            self.cursor.execute("DELETE FROM failuremsgs where room=?",(r[0],))
+            self.cursor.execute("DELETE FROM users where room=?",(r[0],))
+            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"):
+        ''' Push a message from the SYSTEM user into the queue
+        '''
+        m = {
+            "text":msg,
+            "verb":verb
+        }
+        
+        m = self.encryptSysMsg(json.dumps(m))
+        
+        self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,'SYSTEM')",(time.time(),room,m))
+        msgid = self.cursor.lastrowid
+        
+        # Update the last activity field in the DB
+        # See LOC-11
+        self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
+        
+        self.conn.commit()
+        return msgid
+
+
+    def pushFailureMessage(self,user,room,msg):
+        ''' Record a failure message against a user
+        
+        See LOC-14
+        
+        '''
+        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):
+        ''' For whatever reason, a request isn't being actioned. We need to return a status code
+        
+        However, in some instances, we may allow a HTTP 200 just once in order to send the user
+        information on why their next request will fail 
+        '''
+        
+        # TODO - implement the failure handling stuff
+        
+        if reqjson and room:
+            # Check whether there's a failure message or not 
+            self.cursor.execute("SELECT msg from failuremsgs where username=? and room=?",(reqjson['user'],room))
+            r = self.cursor.fetchone()
+            
+            if not r:
+                # No message to return
+                return status
+            
+            # Otherwise, return the message and remove it
+            self.cursor.execute("DELETE from failuremsgs where username=? and room=?",(reqjson['user'],room))
+            self.conn.commit()
+            return {"status":"errmessage","text":r[0]}
+        
+        return status
+
+
+    def encryptSysMsg(self,msg):
+        ''' Encrypt a message from system - LOC-13
+        
+            This isn't so much for protection of the data in memory (as the key is also in memory) as it
+            is to protect against a couple of things you could otherwise do in the client. See LOC-13 for
+            more info.
+        
+        '''
+
+        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
+        '''
+        
+        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()
+    ctx.check_hostname = False
+    ctx.verify_mode = ssl.CERT_NONE
+    
+    data = json.dumps({"action":'schedulerTrigger',
+            "pass": passw
+            })
+    
+    while True:
+        time.sleep(60)
+        
+        try:
+            req = urllib2.Request(bindpoint, data, {'Content-Type': 'application/json'})
+            f = urllib2.urlopen(req,context=ctx)
+            response = f.read()
+            f.close()  
+        except:
+            # 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
+        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,debug=True,ssl_context='adhoc')
+

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.
+
+
+
+
+
+

--- /dev/null
+++ b/tests/run_tests.py
@@ -1,1 +1,1058 @@
-
+#!/usr/bin/env python
+#
+# apt-get install:
+#   python-psutil
+#
+import subprocess
+import psutil
+import os
+import sys
+import time
+import sqlite3
+import traceback
+import json
+import datetime
+
+
+try:
+    from subprocess import DEVNULL # py3k
+except ImportError:
+    import os
+    DEVNULL = open(os.devnull, 'wb')
+
+
+DB_FILE = "%s/localchat-testing.db" % (os.getcwd(),)
+PARENT_DIR = "%s/.." % (os.path.dirname(os.path.abspath(__file__)),)
+sys.path.append('%s/client/' % (PARENT_DIR,))
+
+import LocalChatClient
+STORAGE = {}
+
+
+def restartServer(proc1):
+    ''' Start the server component
+    
+    If already running (i.e. proc1 != False) kill the existing instance
+    '''
+    
+    # If we've already got a process, kill it
+    if proc1:
+        kill(proc1.pid)
+    
+    try:
+        serverloc = "%s/server/LocalChat.py" % (PARENT_DIR,)
+        proc1 = subprocess.Popen([serverloc,'--testing-mode-enable'],stderr=subprocess.STDOUT,stdout=DEVNULL)
+    except Exception as e:
+        print "Failed to start server"
+        print e
+        return False
+    
+    # Give it time to start up
+    time.sleep(5)
+    return proc1
+
+
+def getClientInstance():
+    ''' Get an instance of the client class
+    '''
+    return LocalChatClient.msgHandler()
+
+
+def exit(proc1,code=0):
+    ''' Tidy up and exit
+    '''
+    
+    if proc1:
+        kill(proc1.pid)
+        
+    sys.exit(code)
+
+
+
+def kill(proc_pid):
+    ''' Kill the process and it's children
+    
+    From https://stackoverflow.com/a/25134985
+    
+    We use this because proc1.kill() doesn't seem to kill of the child thread of the server, even if shell=True
+    '''
+    process = psutil.Process(proc_pid)
+    for proc in process.children(recursive=True):
+        proc.kill()
+    process.kill()
+
+
+
+def make_table(columns, data):
+    """Create an ASCII table and return it as a string.
+
+    Pass a list of strings to use as columns in the table and a list of
+    dicts. The strings in 'columns' will be used as the keys to the dicts in
+    'data.'
+
+
+    https://snippets.bentasker.co.uk/page-1705192300-Make-ASCII-Table-Python.html
+    """
+    # Calculate how wide each cell needs to be
+    cell_widths = {}
+    for c in columns:
+        lens = []
+        values = [lens.append(len(str(d.get(c, "")))) for d in data]
+        lens.append(len(c))
+        lens.sort()
+        cell_widths[c] = max(lens)
+
+    # Used for formatting rows of data
+    row_template = "|" + " {} |" * len(columns)
+
+    # CONSTRUCT THE TABLE
+
+    # The top row with the column titles
+    justified_column_heads = [c.ljust(cell_widths[c]) for c in columns]
+    header = row_template.format(*justified_column_heads)
+    # The second row contains separators
+    sep = "|" + "-" * (len(header) - 2) + "|"
+    end = "-" * len(header)
+    # Rows of data
+    rows = []
+
+    for d in data:
+        fields = [str(d.get(c, "")).ljust(cell_widths[c]) for c in columns]
+        row = row_template.format(*fields)
+        rows.append(row)
+    rows.append(end)
+    return "\n".join([header, sep] + rows)
+
+
+
+def opendb():
+    ''' We'll do this and then close after every query to make sure we don't
+    inadvertantly lock the DB and force tests to fail.
+    
+    '''
+    CONN = sqlite3.connect(DB_FILE)
+    CURSOR = CONN.cursor()
+
+    return [CONN,CURSOR]
+    
+
+def run_tests():
+    
+    # Get an instance of the client
+    msg = getClientInstance();
+    
+    test_results = []
+    tests = ['test_one','test_two','test_three','test_four',
+             'test_five','test_six','test_seven','test_eight',
+             'test_nine','test_ten','test_eleven','test_twelve',
+             'test_thirteen','test_fourteen','test_fifteen']
+    x = 1
+    for test in tests:
+        print "Running %s " % (test,)
+        stat,isFatal = globals()[test](msg)
+        stat['No'] = x
+        test_results.append(stat)
+        if isFatal and stat['Result'] == 'FAIL':
+            break
+
+        x = x + 1
+
+    return test_results
+
+
+
+
+def test_one(msg):
+    ''' Create a room and verify that it gets created
+    '''
+    
+    result = {'Test' : 'Create a Room','Result' : 'FAIL', 'Notes': '' }
+    isFatal = True
+    # Test 1 - create a room and check it's recorded in the DB
+    n = msg.createRoom('TestRoom1','testadmin')
+    
+    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    
+
+    STORAGE['room'] = {"name":"TestRoom1",
+                       "RoomPass":roompass,
+                       "UserPass":userpass,
+                       'User':'testadmin'
+                       }
+    
+    
+    CONN,CURSOR = opendb()
+    
+    # Check the DB to ensure the room was actually created
+    CURSOR.execute("SELECT * from rooms where name=?",('TestRoom1',))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'Room not in DB'
+        return [result,isFatal]
+    
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+
+def test_two(msg):
+    ''' Try joining the previously created room with invalid credentials
+    '''
+    
+    result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'],
+                     "%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong'))
+    
+    if n:
+        result['Notes'] = 'Allowed to join with invalid pass'
+        return [result,isFatal]
+    
+    
+    # Now try with an invalid username
+    result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' }
+    n = msg.joinRoom('AlsoWrong',STORAGE['room']['name'],"%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong'))
+    
+    if n:
+        result['Notes'] = 'Invalid user Allowed to join'
+        return [result,isFatal]
+        
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+
+def test_three(msg):
+    ''' Join the previously created room
+    '''
+    
+    result = {'Test' : 'Join the room','Result' : 'FAIL', 'Notes': '' }
+    isFatal = True
+    n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'],
+                     "%s:%s" % (STORAGE['room']['RoomPass'],STORAGE['room']['UserPass'])
+                     )
+    
+    if not n:
+        result['Notes'] = 'Could not join'
+        return [result,isFatal]
+    
+    CONN,CURSOR = opendb()
+    
+    # Check the DB to ensure we're now active
+    CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['room']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'Not Active in DB'
+        return [result,isFatal]
+    
+    # Check we've got a session token
+    if not msg.sesskey:
+        result['Notes'] = 'No Session Key'
+        return [result,isFatal]
+        
+    # Check the client has recorded what it needs to
+    if not msg.room:
+        result['Notes'] = 'Client forgot room'
+        return [result,isFatal]
+
+    if not msg.user:
+        result['Notes'] = 'Client forgot user'
+        return [result,isFatal]
+
+    if not msg.roompass:
+        result['Notes'] = 'Client forgot roompass'
+        return [result,isFatal]
+
+    if not msg.syskey:
+        result['Notes'] = 'No SYSTEM key'
+        return [result,isFatal]
+    
+    
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+def test_four(msg):
+    ''' When we joined, SYSTEM will have pushed a message. Ensure it's encrypted
+    '''
+    
+    result = {'Test' : 'SYSTEM uses E2E','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+
+
+    CONN,CURSOR = opendb()
+
+    CURSOR.execute("SELECT msg FROM messages where user='SYSTEM' ORDER BY ts DESC")
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'No System Message'
+        return [result,isFatal]
+    
+    
+    try:
+        json.loads(r[0])
+        result['Notes'] = 'System Message not E2E encrypted'
+        return [result,isFatal]       
+    except:
+        # This is a good thing in this case!
+        
+        # Now try and decrypt the message
+        m = msg.decrypt(r[0],'SYSTEM')
+        if not m:
+            result['Notes'] = 'Could not decrypt'
+            return [result,isFatal]     
+    
+        # Now check we got valid json
+        try:
+            j = json.loads(m)
+            # Finally
+            if "text" not in j or "verb" not in j:
+                result['Notes'] = 'Not valid msg payload'
+                return [result,isFatal]              
+            
+            # Otherwise, we're good
+            result['Result'] = 'Pass'
+            return [result,isFatal]
+            
+        except:
+            result['Notes'] = 'Not valid JSON'
+            return [result,isFatal]              
+    
+
+def test_five(msg):
+    ''' Send a message and ensure it's encrypted in the DB
+    '''
+    
+    result = {'Test' : 'Ensure payloads are encrypted','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    msg.sendMsg('Hello World')
+    
+    CONN,CURSOR = opendb()
+    
+    CURSOR.execute("SELECT msg FROM messages where user=? ORDER BY ts DESC",(STORAGE['room']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'Message not recorded'
+        return [result,isFatal]
+    
+    
+    try:
+        json.loads(r[0])
+        result['Notes'] = 'Message not E2E encrypted'
+        return [result,isFatal]       
+    except:
+        # This is a good thing in this case!
+        
+        # Now try and decrypt the message
+        m = msg.decrypt(r[0],STORAGE['room']['User'])
+        if not m:
+            result['Notes'] = 'Could not decrypt'
+            return [result,isFatal]     
+    
+        # Now check we got valid json
+        try:
+            j = json.loads(m)
+            # Finally
+            if "text" not in j or "verb" not in j:
+                result['Notes'] = 'Not valid msg payload'
+                return [result,isFatal]              
+            
+            # Otherwise, we're good
+            result['Result'] = 'Pass'
+            return [result,isFatal]
+            
+        except:
+            result['Notes'] = 'Not valid JSON'
+            return [result,isFatal]              
+    
+
+
+def test_six(msg):
+    ''' Invite a user
+    '''
+    result = {'Test' : 'Invite a user','Result' : 'FAIL', 'Notes': '' }
+    isFatal = True
+    
+    n = msg.inviteUser('testuser')
+    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['testuser'] = {
+        'room':n[0],
+        'pass':"%s:%s" % (n[1],n[2]),
+        'User':n[3]
+        }
+    
+    result['Result'] = "Pass"
+    return [result,isFatal]
+
+
+
+def test_seven(msg):
+    ''' Have the previously invited user join from a new client instance
+    '''
+    result = {'Test' : 'Join as Invited User','Result' : 'FAIL', 'Notes': '' }
+    isFatal = True
+    
+    usermsg = getClientInstance();
+    n = usermsg.joinRoom(STORAGE['testuser']['User'],STORAGE['testuser']['room'],STORAGE['testuser']['pass'])
+    
+    if not n:
+        result['Notes'] = 'Could not join'
+        return [result,isFatal]
+    
+    STORAGE['testuser']['clientInstance'] = usermsg
+    
+    CONN,CURSOR = opendb()
+    
+    # Check the DB to ensure we're now active
+    CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'Not Active in DB'
+        return [result,isFatal]
+    
+    # Check we've got a session token
+    if not msg.sesskey:
+        result['Notes'] = 'No Session Key'
+        return [result,isFatal]
+        
+    # Check the client has recorded what it needs to
+    if not msg.room:
+        result['Notes'] = 'Client forgot room'
+        return [result,isFatal]
+
+    if not msg.user:
+        result['Notes'] = 'Client forgot user'
+        return [result,isFatal]
+
+    if not msg.roompass:
+        result['Notes'] = 'Client forgot roompass'
+        return [result,isFatal]
+
+    if not msg.syskey:
+        result['Notes'] = 'No SYSTEM key'
+        return [result,isFatal]
+    
+    
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+def test_eight(msg):
+    ''' Have the testuser send a message and ensure that the adminuser receives it
+    
+    We first call pollMsg to flush the message queue
+    '''
+    
+    result = {'Test' : 'Send a Message','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+        
+    # Poll for messages to clear the queue
+    f = msg.pollForMessage()
+    
+    testpayload = 'The admin is a ....'
+    # Now send a message as test user
+    STORAGE['testuser']['clientInstance'].sendMsg(testpayload)
+    
+    # Now call pollMsg as admin to ensure we receive the message
+    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]
+        
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+def test_nine(msg):
+    ''' Try to maliciously invite SYSTEM (as a precursor to logging in as them)
+    
+    Essentially a regression test for LOC-5
+    
+    '''
+    
+    result = {'Test' : 'LOC-5 Try to invite SYSTEM','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+        
+    # Poll for messages to clear the queue
+    f = msg.pollForMessage()
+    
+    # Now have the test user try to invite SYSTEM
+    n = STORAGE['testuser']['clientInstance'].inviteUser('SYSTEM')
+    if n:
+        result['Notes'] = 'Allowed to invite SYSTEM'
+        return [result,isFatal]
+    
+    # Now call pollMsg as admin to ensure we received the warning message
+    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 warning message 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()
+    
+    expected = "ALERT: User %s tried to invite SYSTEM" % (STORAGE['testuser']['User'],)
+
+    if expected != m:
+        result['Notes'] = 'Unexpected message received: %s' % (m)
+        return [result,isFatal]
+        
+    result['Result'] = 'Pass'
+    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__':
+    # Start the server
+    proc1 = restartServer(False)
+
+    if not proc1:
+        # Server start failed.
+        # abort, abort, abort
+        exit(proc1,1)
+
+    
+    try:
+        # 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
+        exit(proc1,1)
+    
+    cols = ['No','Test','Result','Notes']
+    print make_table(cols,results)
+    
+    exit(proc1,0)
+    
+