[mw-devel] MW3 r1394 - in trunk/src: . client server

arthur at sucs.org arthur at sucs.org
Fri Sep 25 17:00:41 BST 2015


Author: arthur
Date: 2015-09-25 17:00:41 +0100 (Fri, 25 Sep 2015)
New Revision: 1394

Modified:
   trunk/src/client/incoming.c
   trunk/src/ipc.h
   trunk/src/server/PROTOCOL
   trunk/src/server/servsock.c
   trunk/src/server/servsock.h
Log:
lay groundwork for moving the old Broadcast() and IPC_TEXT commands
over to the new protocol. Should be tranformed into a request to the
server to perform an action, followed by a response and an announcment
of wether the command succeeded.


Modified: trunk/src/client/incoming.c
===================================================================
--- trunk/src/client/incoming.c	2015-09-24 09:30:00 UTC (rev 1393)
+++ trunk/src/client/incoming.c	2015-09-25 16:00:41 UTC (rev 1394)
@@ -516,7 +516,8 @@
 		const char * type = json_getstring(j, "type");
 		const char * text = json_getstring(j, "text");
 
-		if (type == NULL || text == NULL) {
+		if (type == NULL) type="unknown";
+	       	if (text == NULL) {
 			printf("Invalid SAYTOROOM message from %s.\n", whom);
 			json_decref(j);
 			return;

Modified: trunk/src/ipc.h
===================================================================
--- trunk/src/ipc.h	2015-09-24 09:30:00 UTC (rev 1393)
+++ trunk/src/ipc.h	2015-09-25 16:00:41 UTC (rev 1394)
@@ -49,7 +49,9 @@
 	IPC_SAYTOUSER	= FCC('SAYU'),
 	IPC_SAYTOALL	= FCC('WALL'),
 	IPC_TALKERROR	= FCC('TERR'),
-	IPC_REPLAY      = FCC('PLAY')
+	IPC_REPLAY      = FCC('PLAY'),
+	IPC_EVENT	= FCC('EVNT'),
+	IPC_ACTION	= FCC('ACTN')
 };
 
 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))

Modified: trunk/src/server/PROTOCOL
===================================================================
--- trunk/src/server/PROTOCOL	2015-09-24 09:30:00 UTC (rev 1393)
+++ trunk/src/server/PROTOCOL	2015-09-25 16:00:41 UTC (rev 1394)
@@ -85,5 +85,23 @@
 	Response:
 		you will get all of the relevant messages sent again
 
+IPC_EVENT - an event occured
+	Header:
+		room number to be told, -1 for all
+	Body: (json)
+		type - type of event that occured
+		target - who did the event happen to
+		text - prefered text for announcement
+
+IPC_ACTION - user requests an action
+	Body: (json)
+		type - type of event that occured
+		target - who did the event happen to
+	Response:
+		type - command request
+		target - victim
+		success=0|1
+		text - prefered acknowledgement text
+
 all other codes are old unformatted messages and are to be phased out,
 most commonly used is IPC_TEXT which is raw text to be displayed

Modified: trunk/src/server/servsock.c
===================================================================
--- trunk/src/server/servsock.c	2015-09-24 09:30:00 UTC (rev 1393)
+++ trunk/src/server/servsock.c	2015-09-25 16:00:41 UTC (rev 1394)
@@ -21,6 +21,7 @@
 
 #include "servsock.h"
 #include "replay.h"
+#include "actions.h"
 
 struct list_head connection_list;
 
@@ -315,6 +316,12 @@
 		return;
 	}
 
+	if (msg->head.type == IPC_ACTION) {
+		accept_action(conn, msg);
+		ipcmsg_destroy(msg);
+		return;
+	}
+
 	/**** end of messages to the server
 	 *    all below this point are intended to be retransmitted
 	 */
@@ -325,42 +332,10 @@
 
 	/* send message to everyone in the room */
 	if (msg->head.type == IPC_SAYTOROOM) {
-		/* eventually this should be a server maintained list */
-		int who_fd = who_open(O_RDONLY);
-		int users_fd = userdb_open(O_RDONLY);
-		struct who who;
-		struct person user;
 		json_t * j = json_init(msg);
-
 		const char * exclude = json_getstring(j, "exclude");
-
-		struct room room;
-		RoomInit(&room);
-		LoadRoom(&room, msg->head.dst);
-
-		while (read(who_fd, &who, sizeof(who)) > 0) {
-			if (who.posn < 0) continue;
-			if (who.pid <= 0) continue;
-
-			lseek(users_fd, who.posn, SEEK_SET);
-			if (read(users_fd, &user, sizeof(user)) <= 0) continue;
-
-			/* have we been told to exclude someone in this room */
-			if (exclude!=NULL && strcasecmp(exclude, user.name)==0) continue;
-
-			/* room matches, send them a copy */
-			if (user.room == msg->head.dst) {
-				msg_attach_to_pid(msg, who.pid);
-			} else
-			/* room not soundproof, and user has global on */
-			if (room.sproof < 1 && (user.chatmode & CM_GLOBAL)) {
-				msg_attach_to_pid(msg, who.pid);
-			}
-		}
-		RoomDestroy(&room);
+		msg_attach_to_channel(msg, msg->head.dst, exclude);
 		json_decref(j);
-		close(who_fd);
-		close(users_fd);
 		ipcmsg_destroy(msg);
 		return;
 	}
@@ -368,50 +343,26 @@
 	/* message is for a specific username */
 	if (msg->head.type == IPC_SAYTOUSER) {
 		/* eventually this should be a server maintained list */
-		int who_fd = who_open(O_RDONLY);
-		int users_fd = userdb_open(O_RDONLY);
-		struct who who;
-		struct person user;
 		json_t * j = json_init(msg);
-		int found = 0;
-
 		const char * target = json_getstring(j, "target");
-
-		while (read(who_fd, &who, sizeof(who)) > 0) {
-			if (who.posn < 0) continue;
-			if (who.pid <= 0) continue;
-
-			lseek(users_fd, who.posn, SEEK_SET);
-			if (read(users_fd, &user, sizeof(user)) <= 0) continue;
-			/* is this the username we are looking for */
-			if (strcasecmp(target, user.name)==0) {
-				msg_attach_to_pid(msg, who.pid);
-				found++;
-			}
-		}
-		if (found < 1) {
+		if (msg_attach_to_username(msg, target) < 1) {
 			send_error(conn, msg, "User '%s' not found", target); 
 		}
 		json_decref(j);
-		close(who_fd);
-		close(users_fd);
 		ipcmsg_destroy(msg);
+		return;
 
-		return;
 	}
 
 	/* send message to everyone (unless this room is soundproof) */
 	if (msg->head.type == IPC_SAYTOALL ) {
-		/* eventually this should be a server maintained list */
-		int who_fd = who_open(O_RDONLY);
+		/* the sender */
 		int users_fd = userdb_open(O_RDONLY);
-		struct who who;
-		struct person user;
 		struct person from;
 
-		/* the sender */
 		lseek(users_fd, conn->user, SEEK_SET);
 		read(users_fd, &from, sizeof(from));
+		close(users_fd);
 
 		/* load the senders room to see if its soundproof */
 		struct room room;
@@ -420,66 +371,120 @@
 
 		printf("Shout from '%s' in %d (%s)\n", from.name, room.num, room.name);
 
-		while (read(who_fd, &who, sizeof(who)) > 0) {
-			if (who.posn < 0) continue;
-			if (who.pid <= 0) continue;
-
-			lseek(users_fd, who.posn, SEEK_SET);
-			if (read(users_fd, &user, sizeof(user)) <= 0) continue;
-
-			/* senders room is soundproof, act as if it was say in this room*/
-			if (room.sproof > 0) {
-				/* room matches, send them a copy */
-				if (user.room == from.room) {
-					msg_attach_to_pid(msg, who.pid);
-					printf("Soundproof %s in %d\n", user.name, user.room);
-				} else
-					printf("Soundproof %s not in %d (is in %d)\n", user.name, from.room, user.room);
-			} else
-			/* room not soundproof, everyone gets it */
-			{
-				msg_attach_to_pid(msg, who.pid);
-				printf("Shout to %s in %d\n", user.name, user.room);
-			}
-		}
-
-		if (room.sproof) {
+		if (room.sproof > 0) {
 			/* nobble the stored message to act correctly */
 			/* shout in sproof room is just a say to that room */
 			msg->head.type = IPC_SAYTOROOM;
 			msg->head.dst = from.room;
+			msg_attach_to_channel(msg, room.num, NULL);
+			printf("Shout in soundproof room (%d) change to SAYTORROM %d\n", room.sproof, from.room);
+		} else {
+			msg_attach_to_all(msg);
 		}
+
 		RoomDestroy(&room);
-		close(who_fd);
-		close(users_fd);
 		ipcmsg_destroy(msg);
 		return;
 	}
 
 	/* it was a broadcast message */
-        if (msg->head.dst == SYSTEM_USER) {
-		int who_fd = who_open(O_RDONLY);
-		int users_fd = userdb_open(O_RDONLY);
-		struct who who;
-		struct person user;
+	if (msg->head.dst == SYSTEM_USER) {
+		msg_attach_to_all(msg);
+	}
 
-		while (read(who_fd, &who, sizeof(who)) > 0) {
-			if (who.posn < 0) continue;
-			if (who.pid <= 0) continue;
+	/* otherwise redistribute this message to intended target */
+	msg_attach_to_userid(msg, msg->head.dst);
+	ipcmsg_destroy(msg);
+}
 
-			lseek(users_fd, who.posn, SEEK_SET);
-			if (read(users_fd, &user, sizeof(user)) <= 0) continue;
 
+/* send copy to every user in given channel,
+ * except username==exclude (notsayto command) */
+void msg_attach_to_channel(ipc_message_t *msg, int channel, const char * exclude)
+{
+	int who_fd = who_open(O_RDONLY);
+	int users_fd = userdb_open(O_RDONLY);
+	struct who who;
+	struct person user;
+
+	struct room room;
+	RoomInit(&room);
+	LoadRoom(&room, channel);
+
+	while (read(who_fd, &who, sizeof(who)) > 0) {
+		if (who.posn < 0) continue;
+		if (who.pid <= 0) continue;
+
+		lseek(users_fd, who.posn, SEEK_SET);
+		if (read(users_fd, &user, sizeof(user)) <= 0) continue;
+
+		/* have we been told to exclude someone in this room */
+		if (exclude!=NULL && strcasecmp(exclude, user.name)==0) continue;
+
+		/* room matches, send them a copy */
+		if (user.room == msg->head.dst) {
 			msg_attach_to_pid(msg, who.pid);
-			printf("Broadcast to %s in %d\n", user.name, user.room);
+		} else
+		/* room not soundproof, and user has global on */
+		if (room.sproof < 1 && (user.chatmode & CM_GLOBAL)) {
+			msg_attach_to_pid(msg, who.pid);
 		}
 	}
+	RoomDestroy(&room);
+	close(who_fd);
+	close(users_fd);
+}
 
-	/* otherwise redistribute this message to intended target */
-	msg_attach_to_user(msg, msg->head.dst);
-	ipcmsg_destroy(msg);
+/* send copy to all sessions belonging to username */
+int msg_attach_to_username(ipc_message_t *msg, const char * username)
+{
+	int who_fd = who_open(O_RDONLY);
+	int users_fd = userdb_open(O_RDONLY);
+	struct who who;
+	struct person user;
+	int found = 0;
+
+	while (read(who_fd, &who, sizeof(who)) > 0) {
+		if (who.posn < 0) continue;
+		if (who.pid <= 0) continue;
+
+		lseek(users_fd, who.posn, SEEK_SET);
+		if (read(users_fd, &user, sizeof(user)) <= 0) continue;
+		/* is this the username we are looking for */
+		if (strcasecmp(username, user.name)==0) {
+			msg_attach_to_pid(msg, who.pid);
+			found++;
+		}
+	}
+
+	close(who_fd);
+	close(users_fd);
+	return found;
 }
 
+/* send copy of message to every session */
+void msg_attach_to_all(ipc_message_t *msg)
+{
+	int who_fd = who_open(O_RDONLY);
+	int users_fd = userdb_open(O_RDONLY);
+	struct who who;
+	struct person user;
+
+	while (read(who_fd, &who, sizeof(who)) > 0) {
+		if (who.posn < 0) continue;
+		if (who.pid <= 0) continue;
+
+		lseek(users_fd, who.posn, SEEK_SET);
+		if (read(users_fd, &user, sizeof(user)) <= 0) continue;
+
+		msg_attach_to_pid(msg, who.pid);
+		printf("Broadcast to %s in %d\n", user.name, user.room);
+	}
+	close(who_fd);
+	close(users_fd);
+}
+
+/* send copy to specific session/connection by PID */
 void msg_attach_to_pid(ipc_message_t *msg, uint32_t pid)
 {
 	struct list_head *pos;
@@ -498,8 +503,10 @@
 	}
 }
 
-void msg_attach_to_user(ipc_message_t *msg, uint32_t userposn)
+/* send msg to all sessions of user idetified by userid/posn */
+int msg_attach_to_userid(ipc_message_t *msg, uint32_t userposn)
 {
+	int found = 0;
 	struct list_head *pos;
 	list_for_each(pos, &connection_list) {
 		ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
@@ -507,15 +514,19 @@
 			if (userposn == SYSTEM_USER) {
 				/* broadcast */
 				msg_attach(msg, c);
+				found++;
 			} else
 			if (userposn == c->user) {
 				/* unicast */
 				msg_attach(msg, c);
+				found++;
 			}
 		}
 	}
+	return found;
 }
 
+/* attach messgae to outgoing queue */
 void msg_attach(ipc_message_t *msg, ipc_connection_t *conn)
 {
 	if (conn == NULL) return;

Modified: trunk/src/server/servsock.h
===================================================================
--- trunk/src/server/servsock.h	2015-09-24 09:30:00 UTC (rev 1393)
+++ trunk/src/server/servsock.h	2015-09-25 16:00:41 UTC (rev 1394)
@@ -6,8 +6,11 @@
 void watch_mainsock(int mainsock);
 void write_socket(ipc_connection_t *conn);
 void process_msg(ipc_connection_t *conn, ipc_message_t *msg);
+void msg_attach_to_all(ipc_message_t *msg);
 void msg_attach_to_pid(ipc_message_t *msg, uint32_t pid);
-void msg_attach_to_user(ipc_message_t *msg, uint32_t userposn);
+int msg_attach_to_userid(ipc_message_t *msg, uint32_t userposn);
+int msg_attach_to_username(ipc_message_t *msg, const char *username);
+void msg_attach_to_channel(ipc_message_t *msg, int channel, const char * exclude);
 void msg_attach(ipc_message_t *msg, ipc_connection_t *conn);
 void init_server(void);
 void send_error(ipc_connection_t *conn, ipc_message_t *orig, const char *format, ...);




More information about the mw-devel mailing list