From b01f6191b0354b605fb7703ae7e753a8c372a269 Mon Sep 17 00:00:00 2001
From: Tom Smeding <tom.smeding@gmail.com>
Date: Tue, 29 Sep 2020 22:51:34 +0200
Subject: webclient: Update to protocol version 3

---
 webclient/client.html | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/webclient/client.html b/webclient/client.html
index f88ee07..5d8ca6f 100644
--- a/webclient/client.html
+++ b/webclient/client.html
@@ -4,7 +4,7 @@
 <meta charset="utf-8">
 <title>tomsg webclient</title>
 <script>
-var PROTOCOL_VERSION=2;
+var PROTOCOL_VERSION=3;
 
 // Note: message id's are considered strings in this code. This is to not have to deal with full 64-bit integers.
 
@@ -20,6 +20,7 @@ var commandlist=[
 	{cmd:"login",usage:"/login <username> <password>"},
 	{cmd:"createroom",usage:"/createroom"},
 	{cmd:"invite",usage:"/invite <username>  (adds to current room)"},
+	{cmd:"leave",usage:"/leave  (leaves the current room)"},
 	{cmd:"shrug",usage:"/shrug  (sends '¯\\_(ツ)_/¯')"},
 	{cmd:"help",usage:"/help  (show list of commands)"}
 ];
@@ -130,12 +131,17 @@ function reconnect(){
 				var r=spl.word[2],inviter=spl.word[3];
 				roomlist.push(r);
 				roomlogs.set(r,[]);
-				addRoomEntry(r,"notice",[now(),"User '"+inviter+"' invited you to this room"]);
+				if(inviter==username)addRoomEntry(r,"notice",[now(),"You created this room in another session"]);
+				else addRoomEntry(r,"notice",[now(),"User '"+inviter+"' invited you to this room"]);
 				fetchRoomHistory(r);
 				updateRoomList();
 			} else if(type=="join"){
 				var r=spl.word[2],u=spl.word[3];
 				addRoomEntry(r,"notice",[now(),"User '"+u+"' joined this room"]);
+			} else if(type=="leave"){
+				var r=spl.word[2],u=spl.word[3];
+				if(u==username)uiLeaveRoom(r);
+				else addRoomEntry(r,"notice",[now(),"User '"+u+"' left the room"]);
 			} else if(type=="ping"){
 				// Do nothing
 			} else if(type=="online"){
@@ -271,6 +277,15 @@ function updateRoomList(){
 	}
 }
 
+function uiLeaveRoom(roomid){
+	roomlist.splice(roomlist.indexOf(roomid),1);
+	roomlogs.delete(roomid);
+	var isCurrent=currentroom==roomid;
+	if(isCurrent)currentroom=":console";
+	updateRoomList();
+	if(isCurrent)drawRoom(currentroom);
+}
+
 function drawRoom(roomid){
 	var tbody=document.getElementById("roomlog");
 	var ch=tbody.children,len=ch.length;
@@ -465,6 +480,25 @@ function executeCommand(roomid,text){
 			});
 			break;
 
+		case "leave":
+			if(!username){
+				addRoomEntry(roomid,"error",[now(),"invite: not logged in"]);
+				break;
+			}
+			var roomtoleave=currentroom;
+			if(!roomtoleave||roomtoleave[0]==":"){
+				addRoomEntry(roomid,"error",[now(),"invite: cannot leave room '"+roomtoleave+"'"]);
+				break;
+			}
+			net_send("leave_room "+roomtoleave,function(ok,err){
+				if(err){
+					addRoomEntry(roomid,"error",[now(),"Unable to leave this room: "+err]);
+				} else {
+					uiLeaveRoom(roomtoleave);
+				}
+			});
+			break;
+
 		case "shrug":
 			sendMessage(roomid,"¯\\_(ツ)_/¯");
 			break;
-- 
cgit v1.2.3-70-g09d2