Implement ability to leave room
Implement ability to leave room

Room will get a message to note the user left

Command is /leave

--- a/client/LocalChatClient.py
+++ b/client/LocalChatClient.py
@@ -158,11 +158,31 @@
 
 
     def leaveRoom(self):
-        ''' Placeholder '''
+        ''' Leave the current room
+        '''
         if not self.room:
             return False
         
-        
+        payload = {"roomName": self.room, 
+                   "user": self.user
+                   }
+        
+        request = {"action":"leaveRoom",
+                   "payload": json.dumps(payload)
+                   }        
+
+
+        resp = self.sendRequest(request)
+
+        if resp == "BROKENLINK" or resp['status'] != "ok":
+            return False
+        
+        self.room = False
+        self.user = False
+        self.last = 0
+        
+        return True
+                
 
 
     def sendRequest(self,data):
@@ -197,9 +217,9 @@
         Exception.__init__(self,'Message not sent')
 
 
-class UnableToJoin(Exception):
-    def __init__(self,cmd):
-        Exception.__init__(self,'Could not Join: %s' % (cmd,))
+class UnableTo(Exception):
+    def __init__(self,action,cmd):
+        Exception.__init__(self,'Could not %s: %s' % (action,cmd))
 
 
 class InvalidCommand(Exception):
@@ -233,9 +253,17 @@
                     raise InvalidCommand(line)
                 
                 if not msg.joinRoom(args[0],args[1],args[2]):
-                    raise UnableToJoin(line)
+                    raise UnableTo('join',line)
                 return
             
+            if cmd == "leave":
+                # /leave
+                if not msg.leaveRoom():
+                    raise UnableTo('leave',line)
+                
+                global c
+                c.output('Left the room','magenta')
+                return
         
 
         if cmd in self._quit_cmd:

--- a/server/LocalChat.py
+++ b/server/LocalChat.py
@@ -120,6 +120,9 @@
         elif reqjson['action'] == "joinRoom":
             return self.processjoinRoom(reqjson)
         
+        elif reqjson['action'] == "leaveRoom":
+            return self.processleaveRoom(reqjson)
+        
         elif reqjson['action'] == "inviteUser":
             return self.inviteUser(reqjson)
         
@@ -274,7 +277,36 @@
             return {"status":"ok","last":last}
         
         
-        
+    def processleaveRoom(self,reqjson):
+        ''' Process a user's request to leave a room
+        '''
+        if "roomName" not in reqjson['payload'] or "user" not in reqjson['payload']:
+            return 400
+        
+        room = self.getRoomID(reqjson['payload']["roomName"])
+        
+        if not room:
+            return 400
+        
+        # Check the user is actually in the room and authorised
+        if not self.validateUser(reqjson['payload']):
+            return 400
+        
+        # Mark them as not in the room
+        self.cursor.execute("UPDATE users set active=0 where username=? and room=?", (reqjson['payload']['user'],room))
+        self.conn.commit()
+        
+        # Push a message to the room to note they left
+        m = {
+                "user":"SYSTEM",
+                "text":"User %s left the room" % (reqjson['payload']['user'])
+            }
+        
+        self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m)))
+        msgid = self.cursor.lastrowid
+        self.conn.commit()        
+        
+        return {"status":"ok"}
     
     
     def sendMsg(self,reqjson):