LOC-17 Update main README to remove dependancies (to avoid duplication of info)
LOC-17 Update main README to remove dependancies (to avoid duplication of info)

file:b/LICENSE (new)
  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)
  # 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.
 
 
 
 
 
 
  ## 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.
 
 
 
 
 
 
#!/usr/bin/env python #!/usr/bin/env python
# #
# #
# Interface based upon https://github.com/izderadicka/xmpp-tester/blob/master/commander.py # Interface based upon https://github.com/izderadicka/xmpp-tester/blob/master/commander.py
# #
# apt-get install: # apt-get install:
# python-urwid # python-urwid
# python-gnupg # python-gnupg
   
   
import urwid import urwid
from collections import deque from collections import deque
from threading import Thread from threading import Thread
import threading import threading
   
import json import json
import urllib2 import urllib2
import ssl import ssl
import string import string
import random import random
  import sys
import datetime as dt import datetime as dt
   
import gnupg import gnupg
   
   
   
# We'll get these from the commandline later # This can be changed from the commandline
USER='ben2'  
SERVER='https://127.0.0.1:8090' SERVER='https://127.0.0.1:8090'
ROOMNAME='BenTest'  
   
   
class msgHandler(object): class msgHandler(object):
def __init__(self): def __init__(self):
self.last = 0 self.last = 0
self.user = False self.user = False
self.server = SERVER self.server = SERVER
self.room = False self.room = False
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
self.syskey = False self.syskey = False
  self.verifycert = False
self.gpg = gnupg.GPG() self.gpg = gnupg.GPG()
def pollForMessage(self): def pollForMessage(self):
if not self.room: if not self.room:
# We're not connected to a room # We're not connected to a room
return False return False
payload = {"roomName": self.room, payload = {"roomName": self.room,
"mylast":self.last, "mylast":self.last,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"pollMsg", request = {"action":"pollMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp == "BROKENLINK": if resp == "BROKENLINK":
return resp return resp
if resp['status'] == "unchanged": if resp['status'] == "unchanged":
return False return False
if resp['status'] == "errmessage": if resp['status'] == "errmessage":
# Got an error, we need to stop the poll and then return the text # Got an error, we need to stop the poll and then return the text
self.room = False self.room = False
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
return [['reversed',resp['text']]] return [['reversed',resp['text']]]
to_print = [] to_print = []
# Otherwise, process the messages # Otherwise, process the messages
for i in resp["messages"]: for i in resp["messages"]:
self.last = i[0] self.last = i[0]
upstruser = i[3] upstruser = i[3]
try: try:
msgbody = json.loads(self.decrypt(i[1],upstruser)) msgbody = json.loads(self.decrypt(i[1],upstruser))
except: except:
# Means an invalid message was received - LOC-8 # Means an invalid message was received - LOC-8
to_print.append(['error','Received message which could not be decrypted']) to_print.append(['error','Received message which could not be decrypted'])
continue continue
# TODO - We'll need to json decode and extract the sending user's name # TODO - We'll need to json decode and extract the sending user's name
# but not currently including that info in my curl tests. Also means test that part of the next block # but not currently including that info in my curl tests. Also means test that part of the next block
color = "green" color = "green"
   
if upstruser == self.user: if upstruser == self.user:
# One of our own, change the color # One of our own, change the color
color = "blue" color = "blue"
elif upstruser == "SYSTEM": elif upstruser == "SYSTEM":
color = "magenta" color = "magenta"
if msgbody['verb'] == "sysalert": if msgbody['verb'] == "sysalert":
color = 'reversed' color = 'reversed'
elif msgbody['verb'] == 'syswarn': elif msgbody['verb'] == 'syswarn':
color = 'cyan' color = 'cyan'
ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]") ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]")
if msgbody["verb"] == "do": if msgbody["verb"] == "do":
color = 'yellow' color = 'yellow'
line = [ line = [
" ** %s %s **" % (upstruser,msgbody['text']) " ** %s %s **" % (upstruser,msgbody['text'])
] ]
else: else:
if i[4] != "0": if i[4] != "0":
color = 'brown' color = 'brown'
upstruser = 'DM %s' % (upstruser,) upstruser = 'DM %s' % (upstruser,)
line = [ line = [
ts, # timestamp ts, # timestamp
"%s>" % (upstruser,), # To be replaced later "%s>" % (upstruser,), # To be replaced later
msgbody['text'] msgbody['text']
] ]
to_print.append([color,' '.join(line)]) to_print.append([color,' '.join(line)])
return to_print return to_print
def sendMsg(self,line,verb='say'): def sendMsg(self,line,verb='say'):
''' Send a message ''' Send a message
''' '''
if not self.room: if not self.room:
# We're not in a room. Abort # We're not in a room. Abort
return False return False
# Otherwise, build a payload # Otherwise, build a payload
msg = { msg = {
'user': self.user, 'user': self.user,
'text': line, 'text': line,
"verb": verb "verb": verb
} }
payload = {"roomName": self.room, payload = {"roomName": self.room,
"msg":self.encrypt(json.dumps(msg)), "msg":self.encrypt(json.dumps(msg)),
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"sendMsg", request = {"action":"sendMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if "status" in resp and resp['status'] == "ok": if "status" in resp and resp['status'] == "ok":
return True return True
return False return False
   
   
def sendDirectMsg(self,line,user,verb='say'): def sendDirectMsg(self,line,user,verb='say'):
''' Send a direct message ''' Send a direct message
''' '''
if not self.room: if not self.room:
# We're not in a room. Abort # We're not in a room. Abort
return False return False
# Otherwise, build a payload # Otherwise, build a payload
msg = { msg = {
'user': self.user, 'user': self.user,
'text': line, 'text': line,
"verb": verb "verb": verb
} }
payload = {"roomName": self.room, payload = {"roomName": self.room,
"msg":self.encrypt(json.dumps(msg)), # TODO this should use the user's key "msg":self.encrypt(json.dumps(msg)), # TODO this should use the user's key
"to": user, "to": user,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"sendDirectMsg", request = {"action":"sendDirectMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if "status" in resp and resp['status'] == "ok": if "status" in resp and resp['status'] == "ok":
return True return True
return False return False
   
   
   
   
def joinRoom(self,user,room,passw): def joinRoom(self,user,room,passw):
''' Join a room ''' Join a room
''' '''
# We only want to send the user password section of the password # We only want to send the user password section of the password
p = passw.split(":") p = passw.split(":")
userpass = p[1] userpass = p[1]
payload = {"roomName": room, payload = {"roomName": room,
"userpass": userpass.encode('utf-8'), "userpass": userpass.encode('utf-8'),
"user": user "user": user
} }
request = {"action":"joinRoom", request = {"action":"joinRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
self.room = room self.room = room
self.user = user self.user = user
self.last = resp['last'] self.last = resp['last']
self.roompass = p[0] # The room password is the first section of the password self.roompass = p[0] # The room password is the first section of the password
self.sesskey = resp['session'] self.sesskey = resp['session']
self.syskey = resp['syskey'] self.syskey = resp['syskey']
return True return True
   
   
   
   
def leaveRoom(self): def leaveRoom(self):
''' Leave the current room ''' Leave the current room
''' '''
if not self.room: if not self.room:
return False return False
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"leaveRoom", request = {"action":"leaveRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
self.room = False self.room = False
self.user = False self.user = False
self.last = 0 self.last = 0
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
return True return True
   
   
def createRoom(self,room,user=False): def createRoom(self,room,user=False):
''' Create a new room ''' Create a new room
''' '''
if not user and not self.user: if not user and not self.user:
return False return False
if not user: if not user:
user = self.user user = self.user
   
   
# Generate a password for the admin # Generate a password for the admin
passw = self.genpassw() passw = self.genpassw()
payload = {"roomName": room, payload = {"roomName": room,
"owner": user, "owner": user,
"pass": passw "pass": passw
} }
request = {"action":"createRoom", request = {"action":"createRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return [resp['name'],passw] return [resp['name'],passw]
   
def closeRoom(self): def closeRoom(self):
if not self.room: if not self.room:
return False return False
   
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"closeRoom", request = {"action":"closeRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return True return True
   
def inviteUser(self,user): def inviteUser(self,user):
''' Invite a user into a room ''' Invite a user into a room
#TODO - Authentication #TODO - Authentication
''' '''
# Generate a password for the new user # Generate a password for the new user
passw = self.genpassw() passw = self.genpassw()
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"invite": user, "invite": user,
"pass": passw, "pass": passw,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"inviteUser", request = {"action":"inviteUser",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return [self.room,self.roompass,passw,user] return [self.room,self.roompass,passw,user]
   
   
   
def kickUser(self,user,ban=False): def kickUser(self,user,ban=False):
''' Kick a user out of the room ''' Kick a user out of the room
''' '''
action = 'banUser' action = 'banUser'
if not ban: if not ban:
action = 'kickUser' action = 'kickUser'
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"kick": user, "kick": user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":action, request = {"action":action,
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return True return True
   
   
def sendRequest(self,data): def sendRequest(self,data):
data = json.dumps(data) data = json.dumps(data)
try: try:
# The cert the other end will be considered invalid  
# ctx = ssl.create_default_context()
# Ignore it  
ctx = ssl.create_default_context() if not self.verifycert:
ctx.check_hostname = False ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE ctx.verify_mode = ssl.CERT_NONE
   
req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'}) req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req,context=ctx) f = urllib2.urlopen(req,context=ctx)
response = f.read() response = f.read()
f.close() f.close()
return json.loads(response) return json.loads(response)
except: except:
return "BROKENLINK" return "BROKENLINK"
   
   
   
def decrypt(self,msg,sender): def decrypt(self,msg,sender):
''' Placeholder ''' Placeholder
''' '''
try: try:
key = self.roompass key = self.roompass
if sender == "SYSTEM": if sender == "SYSTEM":
key = self.syskey key = self.syskey
return str(self.gpg.decrypt(msg.decode("base64"),passphrase=key)) return str(self.gpg.decrypt(msg.decode("base64"),passphrase=key))
except: except:
return False return False
   
def encrypt(self,msg): def encrypt(self,msg):
''' Placeholder ''' Placeholder
''' '''
crypted = self.gpg.encrypt(msg,None,passphrase=self.roompass,symmetric="AES256",armor=False) crypted = self.gpg.encrypt(msg,None,passphrase=self.roompass,symmetric="AES256",armor=False)
return crypted.data.encode('base64') return crypted.data.encode('base64')
   
   
def hashpw(self,passw): def hashpw(self,passw):
''' Placeholder ''' Placeholder
''' '''
return passw return passw
   
   
def genpassw(self,N=16): def genpassw(self,N=16):
''' Generate a random string of chars to act as a password ''' Generate a random string of chars to act as a password
''' '''
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits).encode('utf-8') for _ in range(N)) return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits).encode('utf-8') for _ in range(N))
   
   
   
class NotInRoom(Exception): class NotInRoom(Exception):
def __init__(self,cmd): def __init__(self,cmd):
Exception.__init__(self,'Message not sent') Exception.__init__(self,'Message not sent')
   
   
class UnableTo(Exception): class UnableTo(Exception):
def __init__(self,action,cmd): def __init__(self,action,cmd):
Exception.__init__(self,'Could not %s: %s' % (action,cmd)) Exception.__init__(self,'Could not %s: %s' % (action,cmd))
   
   
class InvalidCommand(Exception): class InvalidCommand(Exception):
def __init__(self,cmd): def __init__(self,cmd):
Exception.__init__(self,'Command is invalid: %s' % (cmd,)) Exception.__init__(self,'Command is invalid: %s' % (cmd,))
   
   
class Command(object): class Command(object):
""" Base class to manage commands in commander """ Base class to manage commands in commander
similar to cmd.Cmd in standard library similar to cmd.Cmd in standard library
just extend with do_something method to handle your commands""" 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._quit_cmd=quit_commands
self._help_cmd=help_commands self._help_cmd=help_commands
def __call__(self,line): def __call__(self,line):
global msg global msg
tokens=line.split() tokens=line.split()
cmd=tokens[0].lower() cmd=tokens[0].lower()
args=tokens[1:] args=tokens[1:]
if cmd[0] == "/": if cmd[0] == "/":
# It's a command # It's a command
cmd = cmd[1:] cmd = cmd[1:]
if cmd == "me": if cmd == "me":
#/me [string] #/me [string]
r = msg.sendMsg(' '.join(args),'do') r = msg.sendMsg(' '.join(args),'do')
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
return return
   
if cmd == "ban": if cmd == "ban":
# /kick [user] # /kick [user]
if len(args) < 1: if len(args) < 1:
raise InvalidCommand(line) raise InvalidCommand(line)
m = msg.kickUser(args[0],True) m = msg.kickUser(args[0],True)
return return
if cmd == "join": if cmd == "join":
# /join [room] [password] [username] # /join [room] [password] [username]
if len(args) < 3: if len(args) < 3:
raise InvalidCommand(line) raise InvalidCommand(line)
return return
if not msg.joinRoom(args[2],args[0],args[1]): if not msg.joinRoom(args[2],args[0],args[1]):
raise UnableTo('join',line) raise UnableTo('join',line)
return return
if cmd == "kick": if cmd == "kick":
# /kick [user] # /kick [user]
if len(args) < 1: if len(args) < 1:
raise InvalidCommand(line) raise InvalidCommand(line)
m = msg.kickUser(args[0]) m = msg.kickUser(args[0])
return return
if cmd == "leave": if cmd == "leave":
# /leave # /leave
if not msg.leaveRoom(): if not msg.leaveRoom():
raise UnableTo('leave',line) raise UnableTo('leave',line)
return return
global c global c
c.output('Left the room','magenta') c.output('Left the room','magenta')
return return
   
   
if cmd == 'msg': if cmd == 'msg':
# /msg ben Hello ben this is a DM # /msg ben Hello ben this is a DM
line = ' '.join(args[1:]) line = ' '.join(args[1:])
r = msg.sendDirectMsg(line,args[0]) r = msg.sendDirectMsg(line,args[0])
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
# Otherwise push a copy of the message to display # Otherwise push a copy of the message to display
# cos we won't get this one back from pollMsg # cos we won't get this one back from pollMsg
global c global c
m = "%s DM %s>%s" % (msg.user,args[0],line) m = "%s DM %s>%s" % (msg.user,args[0],line)
c.output(m,'blue') c.output(m,'blue')
return return
   
if cmd == "room": if cmd == "room":
# /room close [roompass] # /room close [roompass]
if args[0] == "close": if args[0] == "close":
if len(args) < 2: if len(args) < 2:
raise InvalidCommand('/room close [pass]') raise InvalidCommand('/room close [pass]')
if not msg.closeRoom(): if not msg.closeRoom():
raise UnableTo('Close Room','') raise UnableTo('Close Room','')
return return
# /room create [roomname] [[user]] # /room create [roomname] [[user]]
if args[0] == "create": if args[0] == "create":
if len(args) < 3: if len(args) < 3:
args[2] = False args[2] = False
n = msg.createRoom(args[1],args[2]) n = msg.createRoom(args[1],args[2])
if not n: if not n:
raise UnableTo('create room',line) raise UnableTo('create room',line)
return return
# Seperate out the return value # Seperate out the return value
rm = n[0] rm = n[0]
up = n[1] # user specific password up = n[1] # user specific password
global c global c
# Generate a room password # Generate a room password
p = msg.genpassw() p = msg.genpassw()
c.output('Created Room %s' %(rm)) c.output('Created Room %s' %(rm))
c.output('To join the room, do /join %s %s:%s %s' %(args[1],p,up,args[2])) c.output('To join the room, do /join %s %s:%s %s' %(args[1],p,up,args[2]))
return return
elif args[0] == "invite": elif args[0] == "invite":
if len(args) < 2: if len(args) < 2:
raise InvalidCommand(line) raise InvalidCommand(line)
n = msg.inviteUser(args[1]) n = msg.inviteUser(args[1])
if not n: if not n:
raise UnableTo('invite user',line) raise UnableTo('invite user',line)
return return
global c global c
c.output('User %s may now join room' %(args[1],)) 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])) c.output('To join the room, they should do /join %s %s:%s %s' %(n[0],n[1],n[2],n[3]))
return return
   
if cmd in self._quit_cmd: if cmd in self._quit_cmd:
return Commander.Exit return Commander.Exit
elif cmd in self._help_cmd: elif cmd in self._help_cmd:
return self.help(args[0] if args else None) return self.help(args[0] if args else None)
elif hasattr(self, 'do_'+cmd): elif hasattr(self, 'do_'+cmd):
return getattr(self, 'do_'+cmd)(*args) return getattr(self, 'do_'+cmd)(*args)
else: else:
# If it's not a command, then we're trying to send a message # If it's not a command, then we're trying to send a message
r = msg.sendMsg(line) r = msg.sendMsg(line)
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
def help(self,cmd=None): def help(self,cmd=None):
def std_help(): def std_help():
qc='|'.join(self._quit_cmd) qc='|'.join(self._quit_cmd)
hc ='|'.join(self._help_cmd) hc ='|'.join(self._help_cmd)
res='Type [%s] to quit program\n' % qc res='Type [%s] to quit program\n' % qc
res += """Available commands: res += """Available commands:
/join [room] [password] [username] Join a room /join [room] [password] [username] Join a room
/leave Leave current room /leave Leave current room
/room create [roomname] [roompass] [admin user] New room management /room create [roomname] [roompass] [admin user] New room management
/room invite [user] Invite a user into the current room /room invite [user] Invite a user into the current room
/me [string] Send an 'action' instead of a message /me [string] Send an 'action' instead of a message
Room Admin commands: Room Admin commands:
/kick [user] Kick a user out of the room (they can return) /kick [user] Kick a user out of the room (they can return)
/ban [user] Kick a user out and disinvite them (they cannot return) /ban [user] Kick a user out and disinvite them (they cannot return)
/room close [roompass] Kick all users out and close the room /room close [roompass] Kick all users out and close the room
Once in a room, to send a message, just type it. Once in a room, to send a message, just type it.
""" """
return res return res
if not cmd: if not cmd:
return std_help() return std_help()
else: else:
try: try:
fn=getattr(self,'do_'+cmd) fn=getattr(self,'do_'+cmd)
doc=fn.__doc__ doc=fn.__doc__
return doc or 'No documentation available for %s'%cmd return doc or 'No documentation available for %s'%cmd
except AttributeError: except AttributeError:
return std_help() return std_help()
class FocusMixin(object): class FocusMixin(object):
def mouse_event(self, size, event, button, x, y, focus): def mouse_event(self, size, event, button, x, y, focus):
if focus and hasattr(self, '_got_focus') and self._got_focus: if focus and hasattr(self, '_got_focus') and self._got_focus:
self._got_focus() self._got_focus()
return super(FocusMixin,self).mouse_event(size, event, button, x, y, focus) return super(FocusMixin,self).mouse_event(size, event, button, x, y, focus)
class ListView(FocusMixin, urwid.ListBox): class ListView(FocusMixin, urwid.ListBox):
def __init__(self, model, got_focus, max_size=None): def __init__(self, model, got_focus, max_size=None):
urwid.ListBox.__init__(self,model) urwid.ListBox.__init__(self,model)
self._got_focus=got_focus self._got_focus=got_focus
self.max_size=max_size self.max_size=max_size
self._lock=threading.Lock() self._lock=threading.Lock()
def add(self,line): def add(self,line):
with self._lock: with self._lock:
was_on_end=self.get_focus()[1] == len(self.body)-1 was_on_end=self.get_focus()[1] == len(self.body)-1
if self.max_size and len(self.body)>self.max_size: if self.max_size and len(self.body)>self.max_size:
del self.body[0] del self.body[0]
self.body.append(urwid.Text(line)) self.body.append(urwid.Text(line))
last=len(self.body)-1 last=len(self.body)-1
if was_on_end: if was_on_end:
self.set_focus(last,'above') self.set_focus(last,'above')
   
class Input(FocusMixin, urwid.Edit): class Input(FocusMixin, urwid.Edit):
signals=['line_entered'] signals=['line_entered']
def __init__(self, got_focus=None): def __init__(self, got_focus=None):
urwid.Edit.__init__(self) urwid.Edit.__init__(self)
self.history=deque(maxlen=1000) self.history=deque(maxlen=1000)
self._history_index=-1 self._history_index=-1
self._got_focus=got_focus self._got_focus=got_focus
def keypress(self, size, key): def keypress(self, size, key):
if key=='enter': if key=='enter':
line=self.edit_text.strip() line=self.edit_text.strip()
if line: if line:
urwid.emit_signal(self,'line_entered', line) urwid.emit_signal(self,'line_entered', line)
self.history.append(line) self.history.append(line)
self._history_index=len(self.history) self._history_index=len(self.history)
self.edit_text=u'' self.edit_text=u''
if key=='up': if key=='up':
self._history_index-=1 self._history_index-=1
if self._history_index< 0: if self._history_index< 0:
self._history_index= 0 self._history_index= 0
else: else:
self.edit_text=self.history[self._history_index] self.edit_text=self.history[self._history_index]
if key=='down': if key=='down':
self._history_index+=1 self._history_index+=1
if self._history_index>=len(self.history): if self._history_index>=len(self.history):
self._history_index=len(self.history) self._history_index=len(self.history)
self.edit_text=u'' self.edit_text=u''
else: else:
self.edit_text=self.history[self._history_index] self.edit_text=self.history[self._history_index]
else: else:
urwid.Edit.keypress(self, size, key) urwid.Edit.keypress(self, size, key)
   
   
class Commander(urwid.Frame): class Commander(urwid.Frame):
""" Simple terminal UI with command input on bottom line and display frame above """ Simple terminal UI with command input on bottom line and display frame above
similar to chat client etc. similar to chat client etc.
Initialize with your Command instance to execute commands Initialize with your Command instance to execute commands
and the start main loop Commander.loop(). and the start main loop Commander.loop().
You can also asynchronously output messages with Commander.output('message') """ You can also asynchronously output messages with Commander.output('message') """
   
class Exit(object): class Exit(object):
pass pass
PALLETE=[('reversed', urwid.BLACK, urwid.LIGHT_GRAY), PALLETE=[('reversed', urwid.BLACK, urwid.LIGHT_GRAY),
('normal', urwid.LIGHT_GRAY, urwid.BLACK), ('normal', urwid.LIGHT_GRAY, urwid.BLACK),
('error', urwid.LIGHT_RED, urwid.BLACK), ('error', urwid.LIGHT_RED, urwid.BLACK),
('green', urwid.DARK_GREEN, urwid.BLACK), ('green', urwid.DARK_GREEN, urwid.BLACK),
('blue', urwid.LIGHT_BLUE, 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), ('yellow', urwid.YELLOW, urwid.BLACK),
('cyan', urwid.LIGHT_CYAN, urwid.BLACK), ('cyan', urwid.LIGHT_CYAN, urwid.BLACK),
('brown', urwid.BROWN, 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): 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):
self.header=urwid.Text(title) self.header=urwid.Text(title)
self.model=urwid.SimpleListWalker([]) self.model=urwid.SimpleListWalker([])
self.body=ListView(self.model, lambda: self._update_focus(False), max_size=max_size ) self.body=ListView(self.model, lambda: self._update_focus(False), max_size=max_size )
self.input=Input(lambda: self._update_focus(True)) self.input=Input(lambda: self._update_focus(True))
foot=urwid.Pile([urwid.AttrMap(urwid.Text(command_caption), 'reversed'), foot=urwid.Pile([urwid.AttrMap(urwid.Text(command_caption), 'reversed'),
urwid.AttrMap(self.input,'normal')]) urwid.AttrMap(self.input,'normal')])
urwid.Frame.__init__(self, urwid.Frame.__init__(self,
urwid.AttrWrap(self.body, 'normal'), urwid.AttrWrap(self.body, 'normal'),
urwid.AttrWrap(self.header, 'reversed'), urwid.AttrWrap(self.header, 'reversed'),
foot) foot)
self.set_focus_path(['footer',1]) self.set_focus_path(['footer',1])
self._focus=True self._focus=True
urwid.connect_signal(self.input,'line_entered',self.on_line_entered) urwid.connect_signal(self.input,'line_entered',self.on_line_entered)
self._cmd=cmd_cb self._cmd=cmd_cb
self._output_styles=[s[0] for s in self.PALLETE] self._output_styles=[s[0] for s in self.PALLETE]
self.eloop=None self.eloop=None
def loop(self, handle_mouse=False): def loop(self, handle_mouse=False):
self.eloop=urwid.MainLoop(self, self.PALLETE, handle_mouse=handle_mouse) self.eloop=urwid.MainLoop(self, self.PALLETE, handle_mouse=handle_mouse)
self._eloop_thread=threading.current_thread() self._eloop_thread=threading.current_thread()
self.eloop.run() self.eloop.run()
def on_line_entered(self,line): def on_line_entered(self,line):
if self._cmd: if self._cmd:
try: try:
res = self._cmd(line) res = self._cmd(line)
except Exception,e: except Exception,e:
self.output('Error: %s'%e, 'error') self.output('Error: %s'%e, 'error')
return return
if res==Commander.Exit: if res==Commander.Exit:
raise urwid.ExitMainLoop() raise urwid.ExitMainLoop()
elif res: elif res:
self.output(str(res)) self.output(str(res))
else: else:
if line in ('q','quit','exit'): if line in ('q','quit','exit'):
raise urwid.ExitMainLoop() raise urwid.ExitMainLoop()
else: else:
self.output(line) self.output(line)
def output(self, line, style=None): def output(self, line, style=None):
if style and style in self._output_styles: if style and style in self._output_styles:
line=(style,line) line=(style,line)
self.body.add(line) self.body.add(line)
#since output could be called asynchronously form other threads we need to refresh screen in these cases #since output could be called asynchronously form other threads we need to refresh screen in these cases
if self.eloop and self._eloop_thread != threading.current_thread(): if self.eloop and self._eloop_thread != threading.current_thread():
self.eloop.draw_screen() self.eloop.draw_screen()
def _update_focus(self, focus): def _update_focus(self, focus):
self._focus=focus self._focus=focus
def switch_focus(self): def switch_focus(self):
if self._focus: if self._focus:
self.set_focus('body') self.set_focus('body')
self._focus=False self._focus=False
else: else:
self.set_focus_path(['footer',1]) self.set_focus_path(['footer',1])
self._focus=True self._focus=True
def keypress(self, size, key): def keypress(self, size, key):
if key=='tab': if key=='tab':
self.switch_focus() self.switch_focus()
return urwid.Frame.keypress(self, size, key) return urwid.Frame.keypress(self, size, key)
   
   
  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__': if __name__=='__main__':
   
   
msg = msgHandler() msg = msgHandler()
   
  if len(sys.argv) > 1:
  msg = handle_cmdline_opts(sys.argv,msg)
class TestCmd(Command): class TestCmd(Command):
def do_echo(self, *args): def do_echo(self, *args):
'''echo - Just echos all arguments''' '''echo - Just echos all arguments'''
return ' '.join(args) return ' '.join(args)
def do_raise(self, *args): def do_raise(self, *args):
raise Exception('Some Error') raise Exception('Some Error')
c=Commander('LocalChat', cmd_cb=TestCmd()) c=Commander('LocalChat', cmd_cb=TestCmd())
#Test asynch output - e.g. comming from different thread #Test asynch output - e.g. comming from different thread
import time import time
def run(): def run():
state=1 state=1
while True: while True:
time.sleep(1) time.sleep(1)
m = msg.pollForMessage() m = msg.pollForMessage()
if m == "BROKENLINK": if m == "BROKENLINK":
if state == 1: if state == 1:
c.output("Server went away", 'Red') c.output("Server went away", 'Red')
state = 0 state = 0
continue continue
if m: if m:
state = 1 state = 1
for i in m: for i in m:
c.output(i[1], i[0]) c.output(i[1], i[0])
t=Thread(target=run) t=Thread(target=run)
t.daemon=True t.daemon=True
t.start() t.start()
#start main loop #start main loop
c.loop() c.loop()
   
file:b/client/README.md (new)
  # 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
 
 
  `TODO`
 
 
 
  ## Usage Instructions
 
  `TODO`
 
 
 
  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"`
 
 
  ----
 
 
 
 
 
 
#!/usr/bin/env python #!/usr/bin/env python
# #
# #
# LocalChat Server Script # LocalChat Server Script
# #
# #
# apt-get install: # apt-get install:
# python-flask # python-flask
# python-openssl # python-openssl
# python-bcrypt # python-bcrypt
# #
   
from flask import Flask from flask import Flask
from flask import request, make_response from flask import request, make_response
   
import thread import thread
import urllib2 import urllib2
import ssl import ssl
import sqlite3 import sqlite3
import time import time
import os import os
import json import json
import bcrypt import bcrypt
import random import random
import string import string
import gnupg import gnupg
import sys import sys
   
   
app = Flask(__name__) app = Flask(__name__)
   
   
   
@app.route('/', defaults={'path': ''},methods=["POST"]) @app.route('/', defaults={'path': ''},methods=["POST"])
@app.route('/<path:path>',methods=["POST"]) @app.route('/<path:path>',methods=["POST"])
def index(path): def index(path):
print "%.6f Request start" % (time.time()) # DEBUGONLY print "%.6f Request start" % (time.time()) # DEBUGONLY
reqdata = request.get_data() reqdata = request.get_data()
try: try:
reqjson = json.loads(reqdata) reqjson = json.loads(reqdata)
except: except:
return make_response("",400) return make_response("",400)
a = msghandler.processSubmission(reqjson) a = msghandler.processSubmission(reqjson)
   
# Check the status # Check the status
if a in [400,403,500]: if a in [400,403,500]:
response = make_response("",a) response = make_response("",a)
return response return response
return json.dumps(a) return json.dumps(a)
   
   
class MsgHandler(object): class MsgHandler(object):
   
   
def __init__(self,cronpass,bindpoint,purgeinterval,closethresh,testingmode): def __init__(self,cronpass,bindpoint,purgeinterval,closethresh,testingmode):
self.conn = False self.conn = False
self.cursor = False self.cursor = False
# Generate a key for encryption of SYSTEM messages (LOC-13) # Generate a key for encryption of SYSTEM messages (LOC-13)
self.syskey = self.genpassw(16) self.syskey = self.genpassw(16)
self.gpg = gnupg.GPG() self.gpg = gnupg.GPG()
self.cronpass = cronpass self.cronpass = cronpass
self.bindpoint = bindpoint self.bindpoint = bindpoint
self.purgeInterval = purgeinterval self.purgeInterval = purgeinterval
self.roomCloseThresh = closethresh self.roomCloseThresh = closethresh
self.testingMode = testingmode self.testingMode = testingmode
if self.testingMode: if self.testingMode:
print "WARNING - Messages will be written to disk" print "WARNING - Messages will be written to disk"
   
   
   
def createDB(self): def createDB(self):
''' Create the in-memory database ready for use ''' Create the in-memory database ready for use
''' '''
dbpath=':memory:' dbpath=':memory:'
if self.testingMode: if self.testingMode:
# LOC-15 - allow DB to written to disk in test mode # LOC-15 - allow DB to written to disk in test mode
dbpath = "%s/localchat-testing.db" % (os.getcwd(),) dbpath = "%s/localchat-testing.db" % (os.getcwd(),)
self.conn = sqlite3.connect(dbpath) self.conn = sqlite3.connect(dbpath)
self.cursor = self.conn.cursor() self.cursor = self.conn.cursor()
sql = """ sql = """
DROP TABLE IF EXISTS rooms; DROP TABLE IF EXISTS rooms;
CREATE TABLE rooms ( CREATE TABLE rooms (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE,
owner TEXT NOT NULL, owner TEXT NOT NULL,
lastactivity INTEGER DEFAULT 0 lastactivity INTEGER DEFAULT 0
); );
DROP TABLE IF EXISTS messages; DROP TABLE IF EXISTS messages;
CREATE TABLE messages ( CREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER NOT NULL, ts INTEGER NOT NULL,
room INTEGER NOT NULL, room INTEGER NOT NULL,
user NOT NULL, user NOT NULL,
touser TEXT DEFAULT '0', touser TEXT DEFAULT '0',
msg TEXT NOT NULL msg TEXT NOT NULL
); );
DROP TABLE IF EXISTS users; DROP TABLE IF EXISTS users;
CREATE TABLE users ( CREATE TABLE users (
username TEXT NOT NULL, username TEXT NOT NULL,
room INTEGER NOT NULL, room INTEGER NOT NULL,
active INTEGER DEFAULT 0, active INTEGER DEFAULT 0,
passhash TEXT NOT NULL, passhash TEXT NOT NULL,
PRIMARY KEY (username,room) PRIMARY KEY (username,room)
); );
DROP TABLE IF EXISTS sessions; DROP TABLE IF EXISTS sessions;
CREATE TABLE sessions ( CREATE TABLE sessions (
username TEXT NOT NULL, username TEXT NOT NULL,
sesskey TEXT NOT NULL, sesskey TEXT NOT NULL,
PRIMARY KEY(sesskey) PRIMARY KEY(sesskey)
); );
DROP TABLE IF EXISTS failuremsgs; DROP TABLE IF EXISTS failuremsgs;
CREATE TABLE failuremsgs ( CREATE TABLE failuremsgs (
username TEXT NOT NULL, username TEXT NOT NULL,
room INTEGER NOT NULL, room INTEGER NOT NULL,
expires INTEGER NOT NULL, expires INTEGER NOT NULL,
msg TEXT NOT NULL, msg TEXT NOT NULL,
PRIMARY KEY (username,room) PRIMARY KEY (username,room)
); );
""" """
self.conn.executescript(sql) self.conn.executescript(sql)
# We also need to start the scheduler thread (LOC-6) # We also need to start the scheduler thread (LOC-6)
thread.start_new_thread(taskScheduler,(self.cronpass,self.bindpoint)) thread.start_new_thread(taskScheduler,(self.cronpass,self.bindpoint))
   
   
   
def processSubmission(self,reqjson): def processSubmission(self,reqjson):
''' Process an incoming request and route it to ''' Process an incoming request and route it to
the correct function the correct function
''' '''
if not self.conn or not self.cursor: if not self.conn or not self.cursor:
self.createDB() self.createDB()
print reqjson print reqjson
if "action" in reqjson and reqjson['action'] == 'schedulerTrigger': if "action" in reqjson and reqjson['action'] == 'schedulerTrigger':
return self.triggerClean(reqjson) return self.triggerClean(reqjson)
if "action" not in reqjson or "payload" not in reqjson: if "action" not in reqjson or "payload" not in reqjson:
return self.returnFailure(400) return self.returnFailure(400)
# Decrypt the payload # Decrypt the payload
reqjson['payload'] = self.decrypt(reqjson['payload']) reqjson['payload'] = self.decrypt(reqjson['payload'])
try: try:
reqjson['payload'] = json.loads(reqjson['payload']) reqjson['payload'] = json.loads(reqjson['payload'])
except: except:
return self.returnFailure(400) return self.returnFailure(400)
if reqjson['action'] == "createRoom": if reqjson['action'] == "createRoom":
return self.createRoom(reqjson) return self.createRoom(reqjson)
if reqjson['action'] == "closeRoom": if reqjson['action'] == "closeRoom":
return self.closeRoom(reqjson) return self.closeRoom(reqjson)
elif reqjson['action'] == "joinRoom": elif reqjson['action'] == "joinRoom":
return self.processjoinRoom(reqjson) return self.processjoinRoom(reqjson)
elif reqjson['action'] == "leaveRoom": elif reqjson['action'] == "leaveRoom":
return self.processleaveRoom(reqjson) return self.processleaveRoom(reqjson)
   
elif reqjson['action'] == "banUser": elif reqjson['action'] == "banUser":
return self.kickUser(reqjson,True) return self.kickUser(reqjson,True)
elif reqjson['action'] == "inviteUser": elif reqjson['action'] == "inviteUser":
return self.inviteUser(reqjson) return self.inviteUser(reqjson)
elif reqjson['action'] == "kickUser": elif reqjson['action'] == "kickUser":
return self.kickUser(reqjson,False) return self.kickUser(reqjson,False)
elif reqjson['action'] == 'sendDirectMsg': elif reqjson['action'] == 'sendDirectMsg':
return self.sendDirectMsg(reqjson) return self.sendDirectMsg(reqjson)
elif reqjson['action'] == 'sendMsg': elif reqjson['action'] == 'sendMsg':
return self.sendMsg(reqjson) return self.sendMsg(reqjson)
elif reqjson['action'] == 'pollMsg': elif reqjson['action'] == 'pollMsg':
return self.fetchMsgs(reqjson) return self.fetchMsgs(reqjson)
   
def decrypt(self,msg): def decrypt(self,msg):
''' This is currently just a placeholder ''' This is currently just a placeholder
Will be updated later Will be updated later
''' '''
return msg return msg
   
   
   
def createRoom(self,reqjson): def createRoom(self,reqjson):
''' '''
Payload should contain a JSON object consisting of Payload should contain a JSON object consisting of
roomName roomName
owner owner
passhash passhash
e.g. e.g.
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"createRoom","payload":"{ curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"createRoom","payload":"{
\"roomName\":\"BenTest\", \"roomName\":\"BenTest\",
\"owner\":\"ben\", \"owner\":\"ben\",
\"passhash\":\"abcdefg\" \"passhash\":\"abcdefg\"
}" }"
}' }'
''' '''
# Validate the request # Validate the request
# #
# All validation snippets will change to this format soon # All validation snippets will change to this format soon
required = ['roomName','owner','pass'] required = ['roomName','owner','pass']
for i in required: for i in required:
if i not in reqjson['payload']: if i not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
print "Creating room %s" % (reqjson['payload']) print "Creating room %s" % (reqjson['payload'])
# Create a tuple for sqlite3 # Create a tuple for sqlite3
t = (reqjson['payload']['roomName'], t = (reqjson['payload']['roomName'],
reqjson['payload']['owner'] reqjson['payload']['owner']
) )
try: try:
self.cursor.execute("INSERT INTO rooms (name,owner) VALUES (?,?)",t) self.cursor.execute("INSERT INTO rooms (name,owner) VALUES (?,?)",t)
roomid = self.cursor.lastrowid roomid = self.cursor.lastrowid
except: except:
# Probably a duplicate name, but we don't want to give the other end a reason anyway # Probably a duplicate name, but we don't want to give the other end a reason anyway
return self.returnFailure(500) return self.returnFailure(500)
# Generate a password hash for the owners password # Generate a password hash for the owners password
passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt()) passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt())
self.cursor.execute("INSERT INTO users (username,room,passhash) values (?,?,?)",(reqjson['payload']['owner'],roomid,passhash)) self.cursor.execute("INSERT INTO users (username,room,passhash) values (?,?,?)",(reqjson['payload']['owner'],roomid,passhash))
self.conn.commit() self.conn.commit()
return { return {
'status':'ok', 'status':'ok',
'roomid': roomid, 'roomid': roomid,
'name' : reqjson['payload']['roomName'] 'name' : reqjson['payload']['roomName']
} }
   
   
def closeRoom(self,reqjson): def closeRoom(self,reqjson):
''' Close a room. ''' Close a room.
Means we need to Means we need to
- Ban all the users - Ban all the users
- Scrub the message queue - Scrub the message queue
- Remove the room record - Remove the room record
''' '''
if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
# Check the requesting user is the admin # Check the requesting user is the admin
self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"])) self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"]))
n = self.cursor.fetchone() n = self.cursor.fetchone()
if not n: if not n:
return self.returnFailure(403) return self.returnFailure(403)
   
self.pushSystemMsg("Room has been closed. Buh-Bye",room,'syswarn') self.pushSystemMsg("Room has been closed. Buh-Bye",room,'syswarn')
self.cursor.execute("DELETE FROM users where room=?",(room,)) self.cursor.execute("DELETE FROM users where room=?",(room,))
self.cursor.execute("DELETE FROM rooms where id=?",(room,)) self.cursor.execute("DELETE FROM rooms where id=?",(room,))
self.cursor.execute("DELETE FROM messages where room=?",(room,)) self.cursor.execute("DELETE FROM messages where room=?",(room,))
self.cursor.execute("DELETE FROM sessions where sesskey like ?", (reqjson['payload']["roomName"] + '-%',)) self.cursor.execute("DELETE FROM sessions where sesskey like ?", (reqjson['payload']["roomName"] + '-%',))
self.conn.commit() self.conn.commit()
return { "status" : "ok" } return { "status" : "ok" }
   
   
def inviteUser(self,reqjson): def inviteUser(self,reqjson):
''' Link a username into a room ''' Link a username into a room
''' '''
if "roomName" not in reqjson['payload'] or "pass" not in reqjson['payload'] or "invite" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "pass" not in reqjson['payload'] or "invite" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403,reqjson['payload'],room) return self.returnFailure(403,reqjson['payload'],room)
if reqjson['payload']['invite'] == "SYSTEM": if reqjson['payload']['invite'] == "SYSTEM":
# Push a notification into the group # Push a notification into the group
self.pushSystemMsg("ALERT: User %s tried to invite SYSTEM" % (reqjson['payload']['user']),room,'sysalert') self.pushSystemMsg("ALERT: User %s tried to invite SYSTEM" % (reqjson['payload']['user']),room,'sysalert')
return self.returnFailure(403) return self.returnFailure(403)
# Generate a hash of the submitted password # Generate a hash of the submitted password
passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt()) passhash = bcrypt.hashpw(reqjson['payload']['pass'].encode('utf-8'),bcrypt.gensalt())
# Otherwise, link the user in # Otherwise, link the user in
self.cursor.execute("INSERT INTO users (username,room,passhash) 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 # Push a notification into the group
self.pushSystemMsg("User %s invited %s to the room" % (reqjson['payload']['user'],reqjson['payload']['invite']),room) self.pushSystemMsg("User %s invited %s to the room" % (reqjson['payload']['user'],reqjson['payload']['invite']),room)
return { return {
"status":'ok' "status":'ok'
} }
def kickUser(self,reqjson,ban=False): def kickUser(self,reqjson,ban=False):
''' Kick a user out of room ''' Kick a user out of room
Default is just to boot them out, but can also remove their authorisation to enter Default is just to boot them out, but can also remove their authorisation to enter
''' '''
if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload'] or "kick" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload'] or "kick" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
# Check the requesting user is the admin # Check the requesting user is the admin
self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"])) self.cursor.execute("SELECT * from rooms where id=? and owner=?",(room,reqjson["payload"]["user"]))
n = self.cursor.fetchone() n = self.cursor.fetchone()
if not n: if not n:
return self.returnFailure(403) return self.returnFailure(403)
self.cursor.execute("UPDATE users set active=0 where room=? and username=?",(room,reqjson["payload"]["kick"])) self.cursor.execute("UPDATE users set active=0 where room=? and username=?",(room,reqjson["payload"]["kick"]))
# Delete their session # Delete their session
self.cursor.execute("DELETE FROM sessions where username=? and sesskey like ?", (reqjson['payload']['kick'],reqjson['payload']["roomName"] + '-%')) 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.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') self.pushFailureMessage(reqjson['payload']['kick'],room,'You have been kicked from the room')
if ban: if ban:
# If we're banning them, also need to disinvite them # If we're banning them, also need to disinvite them
self.cursor.execute("DELETE from users where room=? and username=?",(room,reqjson["payload"]["kick"])) self.cursor.execute("DELETE from users where room=? and username=?",(room,reqjson["payload"]["kick"]))
self.pushSystemMsg("User %s banned %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn') self.pushSystemMsg("User %s banned %s from the room" % (reqjson['payload']['user'],reqjson['payload']['kick']),room,'syswarn')
return { "status" : "ok" } return { "status" : "ok" }
   
def processjoinRoom(self,reqjson): def processjoinRoom(self,reqjson):
''' Process a request from a user to login to a room ''' Process a request from a user to login to a room
Not yet defined the authentication mechanism to use, so that's a TODO Not yet defined the authentication mechanism to use, so that's a TODO
''' '''
   
# Check the required information is present # Check the required information is present
required = ['roomName','user','userpass'] required = ['roomName','user','userpass']
for i in required: for i in required:
if i not in reqjson['payload']: if i not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
if reqjson["payload"]["user"] == "SYSTEM": if reqjson["payload"]["user"] == "SYSTEM":
return self.returnFailure(403) return self.returnFailure(403)
# Check whether that user is authorised to connect to that room # Check whether that user is authorised to connect to that room
self.cursor.execute("SELECT username, room,passhash 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() r = self.cursor.fetchone()
if not r: if not r:
return { "status": "NOK" } return { "status": "NOK" }
# Now we need to verify they've supplied a correct password for that user # Now we need to verify they've supplied a correct password for that user
stored = r[2].encode("utf-8") stored = r[2].encode("utf-8")
if stored != bcrypt.hashpw(reqjson['payload']['userpass'].encode('utf-8'),stored): if stored != bcrypt.hashpw(reqjson['payload']['userpass'].encode('utf-8'),stored):
return self.returnFailure(403) return self.returnFailure(403)
# Tidy older messages away. # 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 # 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 # 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 # to scroll up and down in their client anyway
self.tidyMsgs(time.time()-10,room) self.tidyMsgs(time.time()-10,room)
# Push a message to the room to note that the user joined # Push a message to the room to note that the user joined
msgid = self.pushSystemMsg("User %s joined the room" % (reqjson['payload']['user']),room) 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 we're in testing mode, push a warning so the new user can see it
if self.testingMode: if self.testingMode:
msgid = self.pushSystemMsg("Server is in testing mode. Messages are being written to disk",room,'syswarn') msgid = self.pushSystemMsg("Server is in testing mode. Messages are being written to disk",room,'syswarn')
   
   
# Check the latest message ID for that room # 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)) self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
last = 0 last = 0
else: else:
last = r[0] last = r[0]
# Mark the user as active in the users table # 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.cursor.execute("UPDATE users set active=1 where username=? and room=?", (reqjson['payload']['user'],room))
# Create a session for the user # Create a session for the user
sesskey = "%s-%s" % (reqjson['payload']["roomName"],self.genSessionKey()) sesskey = "%s-%s" % (reqjson['payload']["roomName"],self.genSessionKey())
self.cursor.execute("INSERT INTO sessions (username,sesskey) values (?,?)", (reqjson['payload']['user'],sesskey)) self.cursor.execute("INSERT INTO sessions (username,sesskey) values (?,?)", (reqjson['payload']['user'],sesskey))
self.conn.commit() self.conn.commit()
return {"status":"ok","last":last,"session":sesskey,"syskey":self.syskey} return {"status":"ok","last":last,"session":sesskey,"syskey":self.syskey}
def processleaveRoom(self,reqjson): def processleaveRoom(self,reqjson):
''' Process a user's request to leave a room ''' Process a user's request to leave a room
''' '''
if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
# Check the user is actually in the room and authorised # Check the user is actually in the room and authorised
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(400) return self.returnFailure(400)
# Mark them as not in the room # Mark them as not in the room
self.cursor.execute("UPDATE users set active=0 where username=? and room=?", (reqjson['payload']['user'],room)) self.cursor.execute("UPDATE users set active=0 where username=? and room=?", (reqjson['payload']['user'],room))
self.conn.commit() self.conn.commit()
# Delete their session # Delete their session
self.cursor.execute("DELETE FROM sessions where username=? and sesskey = ?", (reqjson['payload']['user'],reqjson['payload']["sesskey"])) 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 # Push a message to the room to note they left
self.pushSystemMsg("User %s left the room" % (reqjson['payload']['user']),room) self.pushSystemMsg("User %s left the room" % (reqjson['payload']['user']),room)
return {"status":"ok"} return {"status":"ok"}
def sendMsg(self,reqjson): def sendMsg(self,reqjson):
''' Push a message into a room ''' 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\"}"}' 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']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403) return self.returnFailure(403)
if "roomName" not in reqjson['payload'] or "msg" not in reqjson['payload']: if "roomName" not in reqjson['payload'] or "msg" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
print room print room
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
   
self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user'])) self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,?)",(time.time(),room,reqjson['payload']['msg'],reqjson['payload']['user']))
msgid = self.cursor.lastrowid msgid = self.cursor.lastrowid
self.conn.commit() self.conn.commit()
# Check the latest message ID for that room # 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)) self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
last = 0 last = 0
else: else:
last = r[0] last = r[0]
# Update the last activity field in the DB # Update the last activity field in the DB
# See LOC-11 # See LOC-11
self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room)) self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
self.conn.commit() self.conn.commit()
return { return {
"status" : "ok", "status" : "ok",
"msgid" : msgid, "msgid" : msgid,
"last" : last "last" : last
} }
   
   
def sendDirectMsg(self,reqjson): def sendDirectMsg(self,reqjson):
''' Push a message to a user in the same room ''' Push a message to a user in the same room
''' '''
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403) return self.returnFailure(403)
required = ['roomName','msg','to'] required = ['roomName','msg','to']
for i in required: for i in required:
if i not in reqjson['payload']: if i not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
print room print room
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
   
# Check the other user is in the room and active # 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)) self.cursor.execute("SELECT username from users where username=? and room=? and active=1",(reqjson['payload']['to'],room))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
return self.returnFailure(400) 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'])) 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 msgid = self.cursor.lastrowid
self.conn.commit() self.conn.commit()
# Update the last activity field in the DB # Update the last activity field in the DB
# See LOC-11 # See LOC-11
self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room)) self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
self.conn.commit() self.conn.commit()
# Check the latest message ID for that room # 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)) self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
last = 0 last = 0
else: else:
last = r[0] last = r[0]
return { return {
"status" : "ok", "status" : "ok",
"msgid" : msgid, "msgid" : msgid,
"last" : last "last" : last
} }
   
   
def fetchMsgs(self,reqjson): def fetchMsgs(self,reqjson):
''' Check to see if there are any new messages in the room ''' 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\"}"}' 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']: if "mylast" not in reqjson['payload']:
return self.returnFailure(400) return self.returnFailure(400)
room = self.getRoomID(reqjson['payload']["roomName"]) room = self.getRoomID(reqjson['payload']["roomName"])
print room print room
if not room: if not room:
return self.returnFailure(400) return self.returnFailure(400)
   
   
if not self.validateUser(reqjson['payload']): if not self.validateUser(reqjson['payload']):
return self.returnFailure(403,reqjson['payload'],room) return self.returnFailure(403,reqjson['payload'],room)
self.cursor.execute("""SELECT id,msg,ts,user,touser FROM messages self.cursor.execute("""SELECT id,msg,ts,user,touser FROM messages
WHERE room=? AND WHERE room=? AND
(touser = 0 OR touser = ?) AND (touser = 0 OR touser = ?) AND
id > ? id > ?
ORDER BY ts ASC ORDER BY ts ASC
""",(room,reqjson['payload']['user'],reqjson['payload']['mylast'])) """,(room,reqjson['payload']['user'],reqjson['payload']['mylast']))
r = self.cursor.fetchall() r = self.cursor.fetchall()
if not r: if not r:
# No changes # No changes
return {"status":"unchanged","last":reqjson['payload']['mylast']} return {"status":"unchanged","last":reqjson['payload']['mylast']}
# Otherwise, return the messages # Otherwise, return the messages
return {"status":"updated", return {"status":"updated",
"messages" : r "messages" : r
} }
def validateUser(self,payload): def validateUser(self,payload):
''' Placeholder for now. Auth will be handled in more depth later ''' Placeholder for now. Auth will be handled in more depth later
''' '''
if "user" not in payload or "roomName" not in payload: if "user" not in payload or "roomName" not in payload:
return False return False
# Validate the session information # Validate the session information
self.cursor.execute("SELECT username from sessions where username=? and sesskey=?",(payload['user'],payload['sesskey'])) self.cursor.execute("SELECT username from sessions where username=? and sesskey=?",(payload['user'],payload['sesskey']))
r = self.cursor.fetchone(); r = self.cursor.fetchone();
if not r: if not r:
return False return False
room = self.getRoomID(payload["roomName"]) room = self.getRoomID(payload["roomName"])
if not room: if not room:
return False return False
# Check whether the user has been marked as active # 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)) self.cursor.execute("SELECT username, room from users where username=? and room=? and active=1",(payload['user'],room))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
return False return False
return True return True
   
   
def getRoomID(self,roomname): def getRoomID(self,roomname):
''' Get a room's ID from its name ''' Get a room's ID from its name
''' '''
t = (roomname,) t = (roomname,)
self.cursor.execute("SELECT id from rooms where name=?",t) self.cursor.execute("SELECT id from rooms where name=?",t)
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
return False return False
return r[0] return r[0]
   
   
def triggerClean(self,reqjson): def triggerClean(self,reqjson):
''' Trigger a clean of old messages etc ''' Trigger a clean of old messages etc
''' '''
if 'pass' not in reqjson: if 'pass' not in reqjson:
# No need for failure messages here # No need for failure messages here
return 403 return 403
if reqjson['pass'] != self.cronpass: if reqjson['pass'] != self.cronpass:
return 403 return 403
# Tidy messages older than 10 minutes # Tidy messages older than 10 minutes
self.tidyMsgs(time.time() - self.purgeInterval); self.tidyMsgs(time.time() - self.purgeInterval);
# Auto-close any rooms beyond the threshold # Auto-close any rooms beyond the threshold
self.autoCloseRooms() self.autoCloseRooms()
return {'status':'ok'} return {'status':'ok'}
   
def tidyMsgs(self,thresholdtime,room=False): def tidyMsgs(self,thresholdtime,room=False):
''' Remove messages older than the threshold time ''' Remove messages older than the threshold time
''' '''
   
if room: if room:
# Tidy from a specific room # Tidy from a specific room
self.cursor.execute("DELETE FROM messages where ts < ? and room = ?",(thresholdtime,room)) self.cursor.execute("DELETE FROM messages where ts < ? and room = ?",(thresholdtime,room))
self.conn.commit() self.conn.commit()
else: else:
self.cursor.execute("DELETE FROM messages where ts < ?",(thresholdtime,)) self.cursor.execute("DELETE FROM messages where ts < ?",(thresholdtime,))
self.conn.commit() self.conn.commit()
   
# Tidy away any failure messages # Tidy away any failure messages
self.cursor.execute("DELETE FROM failuremsgs where expires < ?",(time.time(),)) self.cursor.execute("DELETE FROM failuremsgs where expires < ?",(time.time(),))
   
   
   
   
def autoCloseRooms(self): def autoCloseRooms(self):
''' Automatically close any rooms that have been sat idle for too long ''' 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,)) self.cursor.execute("SELECT id,name from rooms where lastactivity < ? and lastactivity > 0",(time.time() - self.roomCloseThresh,))
rooms = self.cursor.fetchall() rooms = self.cursor.fetchall()
# Messages probably have been auto-purged, but make sure # Messages probably have been auto-purged, but make sure
for r in rooms: for r in rooms:
self.cursor.execute("DELETE FROM messages where room=?",(r[0],)) 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 failuremsgs where room=?",(r[0],))
self.cursor.execute("DELETE FROM users 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 sessions where sesskey like ?", (r[1] + '-%',))
self.cursor.execute("DELETE FROM rooms where id=?",(r[0],)) self.cursor.execute("DELETE FROM rooms where id=?",(r[0],))
self.conn.commit() self.conn.commit()
   
   
   
def pushSystemMsg(self,msg,room,verb="sysinfo"): def pushSystemMsg(self,msg,room,verb="sysinfo"):
''' Push a message from the SYSTEM user into the queue ''' Push a message from the SYSTEM user into the queue
''' '''
m = { m = {
"text":msg, "text":msg,
"verb":verb "verb":verb
} }
m = self.encryptSysMsg(json.dumps(m)) m = self.encryptSysMsg(json.dumps(m))
self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,'SYSTEM')",(time.time(),room,m)) self.cursor.execute("INSERT INTO messages (ts,room,msg,user) VALUES (?,?,?,'SYSTEM')",(time.time(),room,m))
msgid = self.cursor.lastrowid msgid = self.cursor.lastrowid
# Update the last activity field in the DB # Update the last activity field in the DB
# See LOC-11 # See LOC-11
self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room)) self.cursor.execute("UPDATE rooms set lastactivity=? where id=?",(time.time(),room))
self.conn.commit() self.conn.commit()
return msgid return msgid
   
   
def pushFailureMessage(self,user,room,msg): def pushFailureMessage(self,user,room,msg):
''' Record a failure message against a user ''' Record a failure message against a user
See LOC-14 See LOC-14
''' '''
self.cursor.execute("INSERT INTO failuremsgs (username,room,expires,msg) values (?,?,?,?)",(user,room,time.time() + 300,msg)) self.cursor.execute("INSERT INTO failuremsgs (username,room,expires,msg) values (?,?,?,?)",(user,room,time.time() + 300,msg))
self.conn.commit() self.conn.commit()
   
   
def returnFailure(self,status,reqjson=False,room=False): def returnFailure(self,status,reqjson=False,room=False):
''' For whatever reason, a request isn't being actioned. We need to return a status code ''' 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 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 information on why their next request will fail
''' '''
# TODO - implement the failure handling stuff # TODO - implement the failure handling stuff
if reqjson and room: if reqjson and room:
# Check whether there's a failure message or not # Check whether there's a failure message or not
self.cursor.execute("SELECT msg from failuremsgs where username=? and room=?",(reqjson['user'],room)) self.cursor.execute("SELECT msg from failuremsgs where username=? and room=?",(reqjson['user'],room))
r = self.cursor.fetchone() r = self.cursor.fetchone()
if not r: if not r:
# No message to return # No message to return
return status return status
# Otherwise, return the message and remove it # Otherwise, return the message and remove it
self.cursor.execute("DELETE from failuremsgs where username=? and room=?",(reqjson['user'],room)) self.cursor.execute("DELETE from failuremsgs where username=? and room=?",(reqjson['user'],room))
self.conn.commit() self.conn.commit()
return {"status":"errmessage","text":r[0]} return {"status":"errmessage","text":r[0]}
return status return status
   
   
def encryptSysMsg(self,msg): def encryptSysMsg(self,msg):
''' Encrypt a message from system - LOC-13 ''' 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 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 is to protect against a couple of things you could otherwise do in the client. See LOC-13 for
more info. more info.
''' '''
crypted = self.gpg.encrypt(msg,None,passphrase=self.syskey,symmetric="AES256",armor=False) crypted = self.gpg.encrypt(msg,None,passphrase=self.syskey,symmetric="AES256",armor=False)
return crypted.data.encode('base64') return crypted.data.encode('base64')
   
   
def genSessionKey(self,N=48): def genSessionKey(self,N=48):
return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits + '/=?&@#%^()+,.<>:!').encode('utf-8') for _ in range(N)) return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits + '/=?&@#%^()+,.<>:!').encode('utf-8') for _ in range(N))
   
   
   
def genpassw(self,N=16): def genpassw(self,N=16):
''' Generate a random string of chars to act as an encryption password ''' 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)) return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(N))
   
   
   
# Create the scheduler function # Create the scheduler function
def taskScheduler(passw,bindpoint): def taskScheduler(passw,bindpoint):
# Ignore cert errors # Ignore cert errors
ctx = ssl.create_default_context() ctx = ssl.create_default_context()
ctx.check_hostname = False ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE ctx.verify_mode = ssl.CERT_NONE
data = json.dumps({"action":'schedulerTrigger', data = json.dumps({"action":'schedulerTrigger',
"pass": passw "pass": passw
}) })
while True: while True:
time.sleep(60) time.sleep(60)
try: try:
req = urllib2.Request(bindpoint, data, {'Content-Type': 'application/json'}) req = urllib2.Request(bindpoint, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req,context=ctx) f = urllib2.urlopen(req,context=ctx)
response = f.read() response = f.read()
f.close() f.close()
except: except:
# Don't let the thread abort just because one request went wrong # Don't let the thread abort just because one request went wrong
continue continue
   
   
   
if __name__ == '__main__': if __name__ == '__main__':
# LOC-15  
testingmode = False  
if '--testing-mode-enable' in sys.argv:  
testingmode = True  
   
   
# These will be handled properly later # These will be handled properly later
passw = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(64)) passw = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(64))
bindpoint = "https://127.0.0.1:8090" bindpoint = "https://127.0.0.1:8090"
purgeinterval = 600 # Wipe messages older than 10 mins purgeinterval = 600 # Wipe messages older than 10 mins
closethresh = 3600 * 6 # Auto-close rooms after 6 hours of inactivity 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 # 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 # We pass in the password we generated for the scheduler thread to use
# as well as the URL it should POST to # as well as the URL it should POST to
msghandler = MsgHandler(passw,bindpoint,purgeinterval,closethresh,testingmode) msghandler = MsgHandler(passw,bindpoint,purgeinterval,closethresh,testingmode)
   
   
# Bind to PORT if defined, otherwise default to 8090. # Bind to PORT if defined, otherwise default to 8090.
port = int(os.environ.get('PORT', 8090)) port = int(os.environ.get('PORT', 8090))
app.run(host='0.0.0.0', port=port,debug=True,ssl_context='adhoc') app.run(host='0.0.0.0', port=port,debug=True,ssl_context='adhoc')
   
   
   
   
   
   
   
file:b/server/README.md (new)
  # 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.
 
  `TODO`
 
 
 
  ## Dependancies
 
 
  `TODO`
 
 
 
  ## Usage Instructions
 
  `TODO`
 
 
 
#!/usr/bin/env python #!/usr/bin/env python
# #
# apt-get install: # apt-get install:
# python-psutil # python-psutil
# #
import subprocess import subprocess
import psutil import psutil
import os import os
import sys import sys
import time import time
import sqlite3 import sqlite3
import traceback import traceback
import json import json
  import datetime
   
   
try: try:
from subprocess import DEVNULL # py3k from subprocess import DEVNULL # py3k
except ImportError: except ImportError:
import os import os
DEVNULL = open(os.devnull, 'wb') DEVNULL = open(os.devnull, 'wb')
   
   
DB_FILE = "%s/localchat-testing.db" % (os.getcwd(),) DB_FILE = "%s/localchat-testing.db" % (os.getcwd(),)
PARENT_DIR = "%s/.." % (os.path.dirname(os.path.abspath(__file__)),) PARENT_DIR = "%s/.." % (os.path.dirname(os.path.abspath(__file__)),)
sys.path.append('%s/client/' % (PARENT_DIR,)) sys.path.append('%s/client/' % (PARENT_DIR,))
   
import LocalChatClient import LocalChatClient
STORAGE = {} STORAGE = {}
   
   
def restartServer(proc1): def restartServer(proc1):
''' Start the server component ''' Start the server component
If already running (i.e. proc1 != False) kill the existing instance If already running (i.e. proc1 != False) kill the existing instance
''' '''
# If we've already got a process, kill it # If we've already got a process, kill it
if proc1: if proc1:
kill(proc1.pid) kill(proc1.pid)
try: try:
serverloc = "%s/server/LocalChat.py" % (PARENT_DIR,) serverloc = "%s/server/LocalChat.py" % (PARENT_DIR,)
proc1 = subprocess.Popen([serverloc,'--testing-mode-enable'],stderr=subprocess.STDOUT,stdout=DEVNULL) proc1 = subprocess.Popen([serverloc,'--testing-mode-enable'],stderr=subprocess.STDOUT,stdout=DEVNULL)
except Exception as e: except Exception as e:
print "Failed to start server" print "Failed to start server"
print e print e
return False return False
# Give it time to start up # Give it time to start up
time.sleep(5) time.sleep(5)
return proc1 return proc1
   
   
def getClientInstance(): def getClientInstance():
''' Get an instance of the client class ''' Get an instance of the client class
''' '''
return LocalChatClient.msgHandler() return LocalChatClient.msgHandler()
   
   
def exit(proc1,code=0): def exit(proc1,code=0):
''' Tidy up and exit ''' Tidy up and exit
''' '''
if proc1: if proc1:
kill(proc1.pid) kill(proc1.pid)
sys.exit(code) sys.exit(code)
   
   
   
def kill(proc_pid): def kill(proc_pid):
''' Kill the process and it's children ''' Kill the process and it's children
From https://stackoverflow.com/a/25134985 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 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) process = psutil.Process(proc_pid)
for proc in process.children(recursive=True): for proc in process.children(recursive=True):
proc.kill() proc.kill()
process.kill() process.kill()
   
   
   
def make_table(columns, data): def make_table(columns, data):
"""Create an ASCII table and return it as a string. """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 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 dicts. The strings in 'columns' will be used as the keys to the dicts in
'data.' 'data.'
   
   
https://snippets.bentasker.co.uk/page-1705192300-Make-ASCII-Table-Python.html https://snippets.bentasker.co.uk/page-1705192300-Make-ASCII-Table-Python.html
""" """
# Calculate how wide each cell needs to be # Calculate how wide each cell needs to be
cell_widths = {} cell_widths = {}
for c in columns: for c in columns:
lens = [] lens = []
values = [lens.append(len(str(d.get(c, "")))) for d in data] values = [lens.append(len(str(d.get(c, "")))) for d in data]
lens.append(len(c)) lens.append(len(c))
lens.sort() lens.sort()
cell_widths[c] = max(lens) cell_widths[c] = max(lens)
   
# Used for formatting rows of data # Used for formatting rows of data
row_template = "|" + " {} |" * len(columns) row_template = "|" + " {} |" * len(columns)
   
# CONSTRUCT THE TABLE # CONSTRUCT THE TABLE
   
# The top row with the column titles # The top row with the column titles
justified_column_heads = [c.ljust(cell_widths[c]) for c in columns] justified_column_heads = [c.ljust(cell_widths[c]) for c in columns]
header = row_template.format(*justified_column_heads) header = row_template.format(*justified_column_heads)
# The second row contains separators # The second row contains separators
sep = "|" + "-" * (len(header) - 2) + "|" sep = "|" + "-" * (len(header) - 2) + "|"
end = "-" * len(header) end = "-" * len(header)
# Rows of data # Rows of data
rows = [] rows = []
   
for d in data: for d in data:
fields = [str(d.get(c, "")).ljust(cell_widths[c]) for c in columns] fields = [str(d.get(c, "")).ljust(cell_widths[c]) for c in columns]
row = row_template.format(*fields) row = row_template.format(*fields)
rows.append(row) rows.append(row)
rows.append(end) rows.append(end)
return "\n".join([header, sep] + rows) return "\n".join([header, sep] + rows)
   
   
   
def opendb(): def opendb():
''' We'll do this and then close after every query to make sure we don't ''' 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. inadvertantly lock the DB and force tests to fail.
''' '''
CONN = sqlite3.connect(DB_FILE) CONN = sqlite3.connect(DB_FILE)
CURSOR = CONN.cursor() CURSOR = CONN.cursor()
   
return [CONN,CURSOR] return [CONN,CURSOR]
   
def run_tests(): def run_tests():
# Get an instance of the client # Get an instance of the client
msg = getClientInstance(); msg = getClientInstance();
test_results = [] test_results = []
tests = ['test_one','test_two','test_three','test_four', tests = ['test_one','test_two','test_three','test_four',
'test_five','test_six','test_seven','test_eight', 'test_five','test_six','test_seven','test_eight',
'test_nine','test_ten','test_eleven','test_twelve'] 'test_nine','test_ten','test_eleven','test_twelve',
  'test_thirteen','test_fourteen','test_fifteen']
x = 1 x = 1
for test in tests: for test in tests:
print "Running %s " % (test,) print "Running %s " % (test,)
stat,isFatal = globals()[test](msg) stat,isFatal = globals()[test](msg)
stat['No'] = x stat['No'] = x
test_results.append(stat) test_results.append(stat)
if isFatal and stat['Result'] == 'FAIL': if isFatal and stat['Result'] == 'FAIL':
break break
   
x = x + 1 x = x + 1
   
return test_results return test_results
   
   
   
   
def test_one(msg): def test_one(msg):
''' Create a room and verify that it gets created ''' Create a room and verify that it gets created
''' '''
result = {'Test' : 'Create a Room','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Create a Room','Result' : 'FAIL', 'Notes': '' }
isFatal = True isFatal = True
# Test 1 - create a room and check it's recorded in the DB # Test 1 - create a room and check it's recorded in the DB
n = msg.createRoom('TestRoom1','testadmin') n = msg.createRoom('TestRoom1','testadmin')
if not n: if not n:
result['Notes'] = 'Empty Response' result['Notes'] = 'Empty Response'
return [result,isFatal] return [result,isFatal]
# The client should have given us two passwords # The client should have given us two passwords
if len(n) < 2: if len(n) < 2:
result['Notes'] = 'Response too small' result['Notes'] = 'Response too small'
return [result,isFatal] return [result,isFatal]
# Seperate out the return value # Seperate out the return value
roompass = n[0] roompass = n[0]
userpass = n[1] # user specific password userpass = n[1] # user specific password
   
STORAGE['room'] = {"name":"TestRoom1", STORAGE['room'] = {"name":"TestRoom1",
"RoomPass":roompass, "RoomPass":roompass,
"UserPass":userpass, "UserPass":userpass,
'User':'testadmin' 'User':'testadmin'
} }
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
# Check the DB to ensure the room was actually created # Check the DB to ensure the room was actually created
CURSOR.execute("SELECT * from rooms where name=?",('TestRoom1',)) CURSOR.execute("SELECT * from rooms where name=?",('TestRoom1',))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if not r: if not r:
result['Notes'] = 'Room not in DB' result['Notes'] = 'Room not in DB'
return [result,isFatal] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
   
   
   
   
def test_two(msg): def test_two(msg):
''' Try joining the previously created room with invalid credentials ''' Try joining the previously created room with invalid credentials
''' '''
result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' }
isFatal = False isFatal = False
n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'], n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'],
"%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong')) "%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong'))
if n: if n:
result['Notes'] = 'Allowed to join with invalid pass' result['Notes'] = 'Allowed to join with invalid pass'
return [result,isFatal] return [result,isFatal]
# Now try with an invalid username # Now try with an invalid username
result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' }
n = msg.joinRoom('AlsoWrong',STORAGE['room']['name'],"%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong')) n = msg.joinRoom('AlsoWrong',STORAGE['room']['name'],"%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong'))
if n: if n:
result['Notes'] = 'Invalid user Allowed to join' result['Notes'] = 'Invalid user Allowed to join'
return [result,isFatal] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
   
   
   
   
def test_three(msg): def test_three(msg):
''' Join the previously created room ''' Join the previously created room
''' '''
result = {'Test' : 'Join the room','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Join the room','Result' : 'FAIL', 'Notes': '' }
isFatal = True isFatal = True
n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'], n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'],
"%s:%s" % (STORAGE['room']['RoomPass'],STORAGE['room']['UserPass']) "%s:%s" % (STORAGE['room']['RoomPass'],STORAGE['room']['UserPass'])
) )
if not n: if not n:
result['Notes'] = 'Could not join' result['Notes'] = 'Could not join'
return [result,isFatal] return [result,isFatal]
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
# Check the DB to ensure we're now active # Check the DB to ensure we're now active
CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['room']['User'],)) CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['room']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if not r: if not r:
result['Notes'] = 'Not Active in DB' result['Notes'] = 'Not Active in DB'
return [result,isFatal] return [result,isFatal]
# Check we've got a session token # Check we've got a session token
if not msg.sesskey: if not msg.sesskey:
result['Notes'] = 'No Session Key' result['Notes'] = 'No Session Key'
return [result,isFatal] return [result,isFatal]
# Check the client has recorded what it needs to # Check the client has recorded what it needs to
if not msg.room: if not msg.room:
result['Notes'] = 'Client forgot room' result['Notes'] = 'Client forgot room'
return [result,isFatal] return [result,isFatal]
   
if not msg.user: if not msg.user:
result['Notes'] = 'Client forgot user' result['Notes'] = 'Client forgot user'
return [result,isFatal] return [result,isFatal]
   
if not msg.roompass: if not msg.roompass:
result['Notes'] = 'Client forgot roompass' result['Notes'] = 'Client forgot roompass'
return [result,isFatal] return [result,isFatal]
   
if not msg.syskey: if not msg.syskey:
result['Notes'] = 'No SYSTEM key' result['Notes'] = 'No SYSTEM key'
return [result,isFatal] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
   
   
def test_four(msg): def test_four(msg):
''' When we joined, SYSTEM will have pushed a message. Ensure it's encrypted ''' When we joined, SYSTEM will have pushed a message. Ensure it's encrypted
''' '''
result = {'Test' : 'SYSTEM uses E2E','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'SYSTEM uses E2E','Result' : 'FAIL', 'Notes': '' }
isFatal = False isFatal = False
   
   
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
   
CURSOR.execute("SELECT msg FROM messages where user='SYSTEM' ORDER BY ts DESC") CURSOR.execute("SELECT msg FROM messages where user='SYSTEM' ORDER BY ts DESC")
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if not r: if not r:
result['Notes'] = 'No System Message' result['Notes'] = 'No System Message'
return [result,isFatal] return [result,isFatal]
try: try:
json.loads(r[0]) json.loads(r[0])
result['Notes'] = 'System Message not E2E encrypted' result['Notes'] = 'System Message not E2E encrypted'
return [result,isFatal] return [result,isFatal]
except: except:
# This is a good thing in this case! # This is a good thing in this case!
# Now try and decrypt the message # Now try and decrypt the message
m = msg.decrypt(r[0],'SYSTEM') m = msg.decrypt(r[0],'SYSTEM')
if not m: if not m:
result['Notes'] = 'Could not decrypt' result['Notes'] = 'Could not decrypt'
return [result,isFatal] return [result,isFatal]
# Now check we got valid json # Now check we got valid json
try: try:
j = json.loads(m) j = json.loads(m)
# Finally # Finally
if "text" not in j or "verb" not in j: if "text" not in j or "verb" not in j:
result['Notes'] = 'Not valid msg payload' result['Notes'] = 'Not valid msg payload'
return [result,isFatal] return [result,isFatal]
# Otherwise, we're good # Otherwise, we're good
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
except: except:
result['Notes'] = 'Not valid JSON' result['Notes'] = 'Not valid JSON'
return [result,isFatal] return [result,isFatal]
   
def test_five(msg): def test_five(msg):
''' Send a message and ensure it's encrypted in the DB ''' Send a message and ensure it's encrypted in the DB
''' '''
result = {'Test' : 'Ensure payloads are encrypted','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Ensure payloads are encrypted','Result' : 'FAIL', 'Notes': '' }
isFatal = False isFatal = False
msg.sendMsg('Hello World') msg.sendMsg('Hello World')
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT msg FROM messages where user=? ORDER BY ts DESC",(STORAGE['room']['User'],)) CURSOR.execute("SELECT msg FROM messages where user=? ORDER BY ts DESC",(STORAGE['room']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if not r: if not r:
result['Notes'] = 'Message not recorded' result['Notes'] = 'Message not recorded'
return [result,isFatal] return [result,isFatal]
try: try:
json.loads(r[0]) json.loads(r[0])
result['Notes'] = 'Message not E2E encrypted' result['Notes'] = 'Message not E2E encrypted'
return [result,isFatal] return [result,isFatal]
except: except:
# This is a good thing in this case! # This is a good thing in this case!
# Now try and decrypt the message # Now try and decrypt the message
m = msg.decrypt(r[0],STORAGE['room']['User']) m = msg.decrypt(r[0],STORAGE['room']['User'])
if not m: if not m:
result['Notes'] = 'Could not decrypt' result['Notes'] = 'Could not decrypt'
return [result,isFatal] return [result,isFatal]
# Now check we got valid json # Now check we got valid json
try: try:
j = json.loads(m) j = json.loads(m)
# Finally # Finally
if "text" not in j or "verb" not in j: if "text" not in j or "verb" not in j:
result['Notes'] = 'Not valid msg payload' result['Notes'] = 'Not valid msg payload'
return [result,isFatal] return [result,isFatal]
# Otherwise, we're good # Otherwise, we're good
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
except: except:
result['Notes'] = 'Not valid JSON' result['Notes'] = 'Not valid JSON'
return [result,isFatal] return [result,isFatal]
   
   
def test_six(msg): def test_six(msg):
''' Invite a user ''' Invite a user
''' '''
result = {'Test' : 'Invite a user','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Invite a user','Result' : 'FAIL', 'Notes': '' }
isFatal = True isFatal = True
n = msg.inviteUser('testuser') n = msg.inviteUser('testuser')
if not n: if not n:
result['Notes'] = 'Could not invite testuser' result['Notes'] = 'Could not invite testuser'
return [result,isFatal] return [result,isFatal]
if len(n) < 4: if len(n) < 4:
result['Notes'] = 'Client returned too short response' result['Notes'] = 'Client returned too short response'
return [result,isFatal] return [result,isFatal]
# Otherwise, we've got details for a new user to be able to join # Otherwise, we've got details for a new user to be able to join
# #
# Store them for a later test # Store them for a later test
STORAGE['testuser'] = { STORAGE['testuser'] = {
'room':n[0], 'room':n[0],
'pass':"%s:%s" % (n[1],n[2]), 'pass':"%s:%s" % (n[1],n[2]),
'User':n[3] 'User':n[3]
} }
result['Result'] = "Pass" result['Result'] = "Pass"
return [result,isFatal] return [result,isFatal]
   
   
   
def test_seven(msg): def test_seven(msg):
''' Have the previously invited user join from a new client instance ''' Have the previously invited user join from a new client instance
''' '''
result = {'Test' : 'Join as Invited User','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Join as Invited User','Result' : 'FAIL', 'Notes': '' }
isFatal = True isFatal = True
usermsg = getClientInstance(); usermsg = getClientInstance();
n = usermsg.joinRoom(STORAGE['testuser']['User'],STORAGE['testuser']['room'],STORAGE['testuser']['pass']) n = usermsg.joinRoom(STORAGE['testuser']['User'],STORAGE['testuser']['room'],STORAGE['testuser']['pass'])
if not n: if not n:
result['Notes'] = 'Could not join' result['Notes'] = 'Could not join'
return [result,isFatal] return [result,isFatal]
STORAGE['testuser']['clientInstance'] = usermsg STORAGE['testuser']['clientInstance'] = usermsg
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
# Check the DB to ensure we're now active # Check the DB to ensure we're now active
CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser']['User'],)) CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if not r: if not r:
result['Notes'] = 'Not Active in DB' result['Notes'] = 'Not Active in DB'
return [result,isFatal] return [result,isFatal]
# Check we've got a session token # Check we've got a session token
if not msg.sesskey: if not msg.sesskey:
result['Notes'] = 'No Session Key' result['Notes'] = 'No Session Key'
return [result,isFatal] return [result,isFatal]
# Check the client has recorded what it needs to # Check the client has recorded what it needs to
if not msg.room: if not msg.room:
result['Notes'] = 'Client forgot room' result['Notes'] = 'Client forgot room'
return [result,isFatal] return [result,isFatal]
   
if not msg.user: if not msg.user:
result['Notes'] = 'Client forgot user' result['Notes'] = 'Client forgot user'
return [result,isFatal] return [result,isFatal]
   
if not msg.roompass: if not msg.roompass:
result['Notes'] = 'Client forgot roompass' result['Notes'] = 'Client forgot roompass'
return [result,isFatal] return [result,isFatal]
   
if not msg.syskey: if not msg.syskey:
result['Notes'] = 'No SYSTEM key' result['Notes'] = 'No SYSTEM key'
return [result,isFatal] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
   
   
def test_eight(msg): def test_eight(msg):
''' Have the testuser send a message and ensure that the adminuser receives it ''' Have the testuser send a message and ensure that the adminuser receives it
We first call pollMsg to flush the message queue We first call pollMsg to flush the message queue
''' '''
result = {'Test' : 'Send a Message','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Send a Message','Result' : 'FAIL', 'Notes': '' }
isFatal = False isFatal = False
# Poll for messages to clear the queue # Poll for messages to clear the queue
f = msg.pollForMessage() f = msg.pollForMessage()
testpayload = 'The admin is a ....' testpayload = 'The admin is a ....'
# Now send a message as test user # Now send a message as test user
STORAGE['testuser']['clientInstance'].sendMsg(testpayload) STORAGE['testuser']['clientInstance'].sendMsg(testpayload)
# Now call pollMsg as admin to ensure we receive the message # Now call pollMsg as admin to ensure we receive the message
received = msg.pollForMessage() received = msg.pollForMessage()
   
# Poll as testuser to clear the queue ready for future tests # Poll as testuser to clear the queue ready for future tests
f = STORAGE['testuser']['clientInstance'].pollForMessage() f = STORAGE['testuser']['clientInstance'].pollForMessage()
if len(received) < 1: if len(received) < 1:
result['Notes'] = 'No messages received' result['Notes'] = 'No messages received'
return [result,isFatal] return [result,isFatal]
if len(received[0]) < 2: if len(received[0]) < 2:
result['Notes'] = 'Malformed message returned' result['Notes'] = 'Malformed message returned'
return [result,isFatal] return [result,isFatal]
# Now check the payload # Now check the payload
# #
# We need to trim out the timestamp etc # We need to trim out the timestamp etc
r = received[0][1].split('>') r = received[0][1].split('>')
m = r[1].lstrip() m = r[1].lstrip()
if testpayload != m: if testpayload != m:
result['Notes'] = 'Incorrect message received: (%s)v(%s)' % (testpayload,m) result['Notes'] = 'Incorrect message received: (%s)v(%s)' % (testpayload,m)
return [result,isFatal] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
   
   
   
def test_nine(msg): def test_nine(msg):
''' Try to maliciously invite SYSTEM (as a precursor to logging in as them) ''' Try to maliciously invite SYSTEM (as a precursor to logging in as them)
Essentially a regression test for LOC-5 Essentially a regression test for LOC-5
''' '''
result = {'Test' : 'LOC-5 Try to invite SYSTEM','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'LOC-5 Try to invite SYSTEM','Result' : 'FAIL', 'Notes': '' }
isFatal = False isFatal = False
# Poll for messages to clear the queue # Poll for messages to clear the queue
f = msg.pollForMessage() f = msg.pollForMessage()
# Now have the test user try to invite SYSTEM # Now have the test user try to invite SYSTEM
n = STORAGE['testuser']['clientInstance'].inviteUser('SYSTEM') n = STORAGE['testuser']['clientInstance'].inviteUser('SYSTEM')
if n: if n:
result['Notes'] = 'Allowed to invite SYSTEM' result['Notes'] = 'Allowed to invite SYSTEM'
return [result,isFatal] return [result,isFatal]
# Now call pollMsg as admin to ensure we received the warning message # Now call pollMsg as admin to ensure we received the warning message
received = msg.pollForMessage() received = msg.pollForMessage()
   
# Poll as testuser to clear the queue ready for future tests # Poll as testuser to clear the queue ready for future tests
f = STORAGE['testuser']['clientInstance'].pollForMessage() f = STORAGE['testuser']['clientInstance'].pollForMessage()
if len(received) < 1: if len(received) < 1:
result['Notes'] = 'No warning message received' result['Notes'] = 'No warning message received'
return [result,isFatal] return [result,isFatal]
if len(received[0]) < 2: if len(received[0]) < 2:
result['Notes'] = 'Malformed message returned' result['Notes'] = 'Malformed message returned'
return [result,isFatal] return [result,isFatal]
# Now check the payload # Now check the payload
# #
# We need to trim out the timestamp etc # We need to trim out the timestamp etc
r = received[0][1].split('>') r = received[0][1].split('>')
m = r[1].lstrip() m = r[1].lstrip()
expected = "ALERT: User %s tried to invite SYSTEM" % (STORAGE['testuser']['User'],) expected = "ALERT: User %s tried to invite SYSTEM" % (STORAGE['testuser']['User'],)
   
if expected != m: if expected != m:
result['Notes'] = 'Unexpected message received: %s' % (m) result['Notes'] = 'Unexpected message received: %s' % (m)
return [result,isFatal] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
   
   
def test_ten(msg): def test_ten(msg):
''' Send a direct message and ensure it's received ''' Send a direct message and ensure it's received
''' '''
result = {'Test' : 'Send a Direct Message','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Send a Direct Message','Result' : 'FAIL', 'Notes': '' }
isFatal = False isFatal = False
# Poll for messages to clear the queue # Poll for messages to clear the queue
f = msg.pollForMessage() f = msg.pollForMessage()
f = STORAGE['testuser']['clientInstance'].pollForMessage() f = STORAGE['testuser']['clientInstance'].pollForMessage()
testpayload = 'Hi Hi' testpayload = 'Hi Hi'
# Now send a direct message from test user to admin # Now send a direct message from test user to admin
n = STORAGE['testuser']['clientInstance'].sendDirectMsg(testpayload,'testadmin') n = STORAGE['testuser']['clientInstance'].sendDirectMsg(testpayload,'testadmin')
if not n: if not n:
result['Notes'] = 'Could not send direct message' result['Notes'] = 'Could not send direct message'
return [result,isFatal] return [result,isFatal]
# Poll as the admin user to ensure it was received # Poll as the admin user to ensure it was received
received = msg.pollForMessage() received = msg.pollForMessage()
# Poll as testuser to clear the queue ready for future tests # Poll as testuser to clear the queue ready for future tests
f = STORAGE['testuser']['clientInstance'].pollForMessage() f = STORAGE['testuser']['clientInstance'].pollForMessage()
if len(received) < 1: if len(received) < 1:
result['Notes'] = 'No messages received' result['Notes'] = 'No messages received'
return [result,isFatal] return [result,isFatal]
if len(received[0]) < 2: if len(received[0]) < 2:
result['Notes'] = 'Malformed message returned' result['Notes'] = 'Malformed message returned'
return [result,isFatal] return [result,isFatal]
# Now check the payload # Now check the payload
# #
# We need to trim out the timestamp etc # We need to trim out the timestamp etc
r = received[0][1].split('>') r = received[0][1].split('>')
m = r[1].lstrip() m = r[1].lstrip()
if testpayload != m: if testpayload != m:
result['Notes'] = 'Incorrect message received: (%s)v(%s)' % (testpayload,m) result['Notes'] = 'Incorrect message received: (%s)v(%s)' % (testpayload,m)
return [result,isFatal] return [result,isFatal]
if "DM" not in r[0]: if "DM" not in r[0]:
result['Notes'] = 'Envelope not marked as DM: %s' % (r[0],) result['Notes'] = 'Envelope not marked as DM: %s' % (r[0],)
return [result,isFatal] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,isFatal] return [result,isFatal]
   
   
def test_eleven(msg): def test_eleven(msg):
''' Invite a new user and then have admin kick them to ensure they're ''' Invite a new user and then have admin kick them to ensure they're
actually kicked out of the room actually kicked out of the room
''' '''
result = {'Test' : 'Invite and kick a user','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Invite and kick a user','Result' : 'FAIL', 'Notes': '' }
isFatal = True isFatal = True
n = msg.inviteUser('testuser2') n = msg.inviteUser('testuser2')
if not n: if not n:
result['Notes'] = 'Could not invite testuser2' result['Notes'] = 'Could not invite testuser2'
return [result,isFatal] return [result,isFatal]
if len(n) < 4: if len(n) < 4:
result['Notes'] = 'Client returned too short response' result['Notes'] = 'Client returned too short response'
return [result,isFatal] return [result,isFatal]
# Otherwise, we've got details for a new user to be able to join # Otherwise, we've got details for a new user to be able to join
# #
# Store them for a later test # Store them for a later test
STORAGE['testuser2'] = { STORAGE['testuser2'] = {
'room':n[0], 'room':n[0],
'pass':"%s:%s" % (n[1],n[2]), 'pass':"%s:%s" % (n[1],n[2]),
'User':n[3] 'User':n[3]
} }
# Create a new instance so we can join as testuser2 # Create a new instance so we can join as testuser2
usermsg = getClientInstance(); usermsg = getClientInstance();
n = usermsg.joinRoom(STORAGE['testuser2']['User'],STORAGE['testuser2']['room'],STORAGE['testuser2']['pass']) n = usermsg.joinRoom(STORAGE['testuser2']['User'],STORAGE['testuser2']['room'],STORAGE['testuser2']['pass'])
if not n: if not n:
result['Notes'] = 'User could not join' result['Notes'] = 'User could not join'
return [result,isFatal] return [result,isFatal]
STORAGE['testuser2']['clientInstance'] = usermsg STORAGE['testuser2']['clientInstance'] = usermsg
# Now have the admin kick (but not ban) them # Now have the admin kick (but not ban) them
msg.kickUser('testuser2',False) msg.kickUser('testuser2',False)
# Check that a failure message was written # Check that a failure message was written
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if not r: if not r:
result['Notes'] = 'User not notified' result['Notes'] = 'User not notified'
return [result,isFatal] return [result,isFatal]
# Poll to receive the failure message and verify it's deleted # Poll to receive the failure message and verify it's deleted
msgs = STORAGE['testuser2']['clientInstance'].pollForMessage() msgs = STORAGE['testuser2']['clientInstance'].pollForMessage()
if len(msgs) < 1: if len(msgs) < 1:
result['Notes'] = "User didn't receive notification" result['Notes'] = "User didn't receive notification"
return [result,isFatal] return [result,isFatal]
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if r: if r:
result['Notes'] = 'Notification not purged on poll' result['Notes'] = 'Notification not purged on poll'
return [result,isFatal] return [result,isFatal]
# Now check that their session was suspended # Now check that their session was suspended
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT username FROM users where username=? and active=1",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT username FROM users where username=? and active=1",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if r: if r:
result['Notes'] = 'User still considered active in room' result['Notes'] = 'User still considered active in room'
return [result,isFatal] return [result,isFatal]
# Check if their session still exists # Check if their session still exists
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if r: if r:
result['Notes'] = 'User still has active session' result['Notes'] = 'User still has active session'
return [result,isFatal] return [result,isFatal]
# Otherwise, looks good # Otherwise, looks good
result['Result'] = "Pass" result['Result'] = "Pass"
return [result,isFatal] return [result,isFatal]
   
   
def test_twelve(msg): def test_twelve(msg):
''' Rejoin as the previously invited user, and then ban them ''' Rejoin as the previously invited user, and then ban them
''' '''
result = {'Test' : 'Ban a user','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Ban a user','Result' : 'FAIL', 'Notes': '' }
isFatal = True isFatal = True
# Flush the failedmessage queue # Flush the failedmessage queue
f = STORAGE['testuser2']['clientInstance'].pollForMessage() f = STORAGE['testuser2']['clientInstance'].pollForMessage()
# Re-join as testuser2 # Re-join as testuser2
n = STORAGE['testuser2']['clientInstance'].joinRoom(STORAGE['testuser2']['User'], n = STORAGE['testuser2']['clientInstance'].joinRoom(STORAGE['testuser2']['User'],
STORAGE['testuser2']['room'],STORAGE['testuser2']['pass']) STORAGE['testuser2']['room'],STORAGE['testuser2']['pass'])
   
if not n: if not n:
result['Notes'] = 'TestUser2 could not join' result['Notes'] = 'TestUser2 could not join'
return [result,isFatal] return [result,isFatal]
# Now have the admin ban them # Now have the admin ban them
msg.kickUser('testuser2',True) msg.kickUser('testuser2',True)
   
# Check that a failure message was written # Check that a failure message was written
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if not r: if not r:
result['Notes'] = 'User not notified' result['Notes'] = 'User not notified'
return [result,isFatal] return [result,isFatal]
# Poll to receive the failure message and verify it's deleted # Poll to receive the failure message and verify it's deleted
msgs = STORAGE['testuser2']['clientInstance'].pollForMessage() msgs = STORAGE['testuser2']['clientInstance'].pollForMessage()
if len(msgs) < 1: if len(msgs) < 1:
result['Notes'] = "User didn't receive notification" result['Notes'] = "User didn't receive notification"
return [result,isFatal] return [result,isFatal]
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if r: if r:
result['Notes'] = 'Notification not purged on poll' result['Notes'] = 'Notification not purged on poll'
return [result,isFatal] return [result,isFatal]
# Now check that their session was suspended # Now check that their session was suspended
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT username FROM users where username=?",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT username FROM users where username=?",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if r: if r:
result['Notes'] = 'User still exists for room' result['Notes'] = 'User still exists for room'
return [result,isFatal] return [result,isFatal]
# Check if their session still exists # Check if their session still exists
CONN,CURSOR = opendb() CONN,CURSOR = opendb()
CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],)) CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
CONN.close() CONN.close()
if r: if r:
result['Notes'] = 'User still has active session' result['Notes'] = 'User still has active session'
return [result,isFatal] return [result,isFatal]
# We don't need this any more # We don't need this any more
del STORAGE['testuser2'] del STORAGE['testuser2']
# Otherwise, looks good # Otherwise, looks good
result['Result'] = "Pass" result['Result'] = "Pass"
return [result,isFatal] 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__': if __name__ == '__main__':
# Start the server # Start the server
proc1 = restartServer(False) proc1 = restartServer(False)
   
if not proc1: if not proc1:
# Server start failed. # Server start failed.
# abort, abort, abort # abort, abort, abort
exit(proc1,1) exit(proc1,1)
   
try: try:
# I don't like generic catchall exceptions # I don't like generic catchall exceptions
# but, we want to make sure we kill the background # but, we want to make sure we kill the background
# process if there is one. # 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() results = run_tests()
   
except Exception as e: except Exception as e:
print traceback.format_exc() print traceback.format_exc()
print e print e
exit(proc1,1) exit(proc1,1)
cols = ['No','Test','Result','Notes'] cols = ['No','Test','Result','Notes']
print make_table(cols,results) print make_table(cols,results)
exit(proc1,0) exit(proc1,0)