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

arthur at sucs.org arthur at sucs.org
Tue Dec 11 21:47:37 GMT 2012


Author: arthur
Date: 2012-12-11 21:47:37 +0000 (Tue, 11 Dec 2012)
New Revision: 1313

Modified:
   trunk/src/incoming.c
   trunk/src/ipc.h
   trunk/src/server/servsock.c
   trunk/src/server/servsock.h
   trunk/src/talker.c
Log:
Move all the 'say to room' commands (say,raw,emote) to new style


Modified: trunk/src/incoming.c
===================================================================
--- trunk/src/incoming.c	2012-12-10 01:18:20 UTC (rev 1312)
+++ trunk/src/incoming.c	2012-12-11 21:47:37 UTC (rev 1313)
@@ -46,7 +46,7 @@
 static struct mstack *MesgStack=NULL;
 
 static void accept_pipe_cmd(ipc_message_t *msg, struct person *mesg_user);
-static void force_text(char *text, char *from);
+static void force_text(const char *text, const char *from);
 static void force_ipc(char *text, char *from);
 static void force_rpc(char *text, char *from);
 static void force_checkonoff(char *text, char *from);
@@ -470,8 +470,86 @@
 
 	printf("Server version: %s\n", version);
 	printf("Server uptime: %s\n", itime(uptime));
+	json_decref(j);
 }
 
+/* handle formatted message */
+static void display_content(ipc_message_t *msg)
+{
+	if (msg->head.type == IPC_SAYTOROOM) {
+		int32_t msg_posn;
+		struct person msg_user;
+		const char *whom = NULL;
+
+		if (msg->head.src == 0) {
+			whom = "System";
+		} else
+		if ((msg_posn = get_who_userposn(msg->head.src)) == -1) {
+			whom = "Unknown";
+		}else {
+			fetch_user(&msg_user, msg_posn);
+			whom = msg_user.name;
+		}
+
+		json_t * j = json_init(msg);
+		const char * type = json_getstring(j, "type");
+		const char * text = json_getstring(j, "text");
+
+		if (type == NULL || text == NULL) {
+			printf("Invalid SAYTOROOM message from %s.\n", whom);
+			json_decref(j);
+			return;
+		}
+
+		char buff[MAXTEXTLENGTH];
+		char * tb = buff;
+		int len = MAXTEXTLENGTH;
+
+		/* we have global on, prepend the channel number */
+		if (cm_flags(user->chatmode, CM_GLOBAL, CM_MODE_ALL)) {
+			snprintf(tb, len, "%d:", msg->head.dst);
+			int s = strlen(tb);
+			len -= s;
+			tb += s;
+		}
+
+		if (strcmp(type, "say")==0) {
+			snprintf(tb, len, "%s: %s", whom, text);
+		} else
+		if (strcmp(type, "raw")==0) {
+			snprintf(tb, len, "%s", text);
+		} else 
+		if (strcmp(type, "emote")==0) {
+			int plural = json_getint(j, "plural");
+			switch (plural) {
+				case 1:
+					snprintf(tb, len, "%s's %s", whom, text);
+					break;
+				case 2:
+					snprintf(tb, len, "%s' %s", whom, text);
+					break;
+				case 3:
+					snprintf(tb, len, "%s'd %s", whom, text);
+					break;
+				case 4:
+					snprintf(tb, len, "%s'll %s", whom, text);
+					break;
+				default:
+					snprintf(tb, len, "%s %s", whom, text);
+					break;
+			}
+		} else {
+			/* same as say for now */
+			snprintf(tb, len, "%s: %s", whom, text);
+		}
+		force_text(buff, whom);
+		json_decref(j);
+		return;
+	}
+
+	printf("Unknown message type %4.4s", (char *)&msg->head.type);
+}
+
 static void accept_pipe_cmd(ipc_message_t *msg, struct person *mesg_user)
 {
 	enum ipc_types state = msg->head.type;
@@ -557,6 +635,9 @@
 		case IPC_UPTIME:
 			display_uptime(msg);
 			break;
+		case IPC_SAYTOROOM:
+			display_content(msg);
+			break;
 		default:
 			devel_msg("incoming_mesg", "unknown message type %d.\007", state);
 	}
@@ -570,7 +651,7 @@
 	new_mail_waiting++;
 }
 
-static void force_text(char *text, char *from)
+static void force_text(const char *text, const char *from)
 {
 	char tb[MAXTEXTLENGTH];
 	struct mstack *mesg;

Modified: trunk/src/ipc.h
===================================================================
--- trunk/src/ipc.h	2012-12-10 01:18:20 UTC (rev 1312)
+++ trunk/src/ipc.h	2012-12-11 21:47:37 UTC (rev 1313)
@@ -37,7 +37,8 @@
 	IPC_PROTLEVEL	= 24,
 	IPC_PROTPOWER	= 25,
 	IPC_DOING	= 26,
-	IPC_UPTIME	= MKFOURCC('U','P','T','M')
+	IPC_UPTIME	= MKFOURCC('U','P','T','M'),
+	IPC_SAYTOROOM	= MKFOURCC('S','A','Y','R')
 };
 
 typedef int (send_filter)(const struct person * usr, const struct who * who, const void * info);

Modified: trunk/src/server/servsock.c
===================================================================
--- trunk/src/server/servsock.c	2012-12-10 01:18:20 UTC (rev 1312)
+++ trunk/src/server/servsock.c	2012-12-11 21:47:37 UTC (rev 1313)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <sys/epoll.h>
 #include <sys/types.h>
@@ -16,6 +17,7 @@
 #include "../files.h"
 #include "../nonce.h"
 #include "../util.h"
+#include "../who.h"
 
 struct list_head connection_list;
 
@@ -269,24 +271,54 @@
 		return;
 	}
 
+	/* send message to everyone in the room */
+	if (msg->head.type == FOURCC("SAYR")) {
+		/* eventually this should be a server maintained list */
+		int who_fd = openwhofile(O_RDONLY);
+		int users_fd = openuserfile(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;
+			if (user.room == msg->head.dst) {
+				msg_attach_to(msg, who.pid);
+			}
+		}
+		close(who_fd);
+		close(users_fd);
+		ipcmsg_destroy(msg);
+		return;
+	}
+
 	/* otherwise redistribute this message to intended target */
+	msg_attach_to(msg, msg->head.dst);
+	ipcmsg_destroy(msg);
+}
+
+void msg_attach_to(ipc_message_t *msg, uint32_t dest)
+{
 	struct list_head *pos;
 	list_for_each(pos, &connection_list) {
 		ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
 		if (c->state == IPCSTATE_VALID) {
-			if (msg->head.dst == 0) {
+			if (dest == 0) {
 				/* broadcast */
 				msg_attach(msg, c);
 			} else
-			if (msg->head.dst == c->addr) {
+			if (dest == c->addr) {
 				/* unicast */
 				msg_attach(msg, c);
 			}
 		}
 	}
-	ipcmsg_destroy(msg);
 }
 
+
 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	2012-12-10 01:18:20 UTC (rev 1312)
+++ trunk/src/server/servsock.h	2012-12-11 21:47:37 UTC (rev 1313)
@@ -6,5 +6,6 @@
 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(ipc_message_t *msg, uint32_t dest);
 void msg_attach(ipc_message_t *msg, ipc_connection_t *conn);
 void init_server(void);

Modified: trunk/src/talker.c
===================================================================
--- trunk/src/talker.c	2012-12-10 01:18:20 UTC (rev 1312)
+++ trunk/src/talker.c	2012-12-11 21:47:37 UTC (rev 1313)
@@ -106,22 +106,16 @@
 	return (! sproof);
 }
 
-static void talk_send_to_room(const char * text, int channel) {
-	char			buff[MAXTEXTLENGTH];
-	struct room		tmp_room;
-	struct filter_info	f_info;
-
-	memset(&f_info, 0, sizeof(f_info));
-	f_info.channel = channel;
-	ipc_send_to_all(IPC_TEXT, text, send_filter_oneroom, &f_info);
-	
-	RoomInit(&tmp_room);
-	LoadRoom(&tmp_room, channel);
-	RoomDestroy(&tmp_room);
-	
-	if (myroom.hidden > 0) snprintf(buff, MAXTEXTLENGTH, "?:%s", text);
-	else snprintf(buff, MAXTEXTLENGTH, "%d:%s", channel, text);
-	ipc_send_to_all(IPC_TEXT, buff, send_filter_global, &f_info);
+static void talk_send_to_room(const char * text, int channel, const char * type, int plural) {
+	ipc_message_t * msg = ipcmsg_create(IPC_SAYTOROOM, getpid());
+	ipcmsg_destination(msg, channel);
+	json_t * j = json_init(NULL);
+	json_addstring(j, "text", text);
+	json_addstring(j, "type", type);
+	if (plural > -1) json_addint(j, "plural", plural);
+	ipcmsg_json_encode(msg, j);
+	json_decref(j);
+	ipcmsg_transmit(msg);
 }
 
 void talk_send_shout(char * text){
@@ -139,21 +133,17 @@
 }
 
 static void talk_send_say(char * text, int channel){
-	char buff[MAXTEXTLENGTH];
-	
 	mwlog("SAY %s", text);
 	catchuri(text);
-	snprintf(buff, MAXTEXTLENGTH, "%s: %s", user->name, text);
-	talk_send_to_room(buff, channel);
+	//snprintf(buff, MAXTEXTLENGTH, "%s: %s", user->name, text);
+	talk_send_to_room(text, channel, "say", -1);
 }
 
 void talk_send_raw(char * text, int channel){
-	char buff[MAXTEXTLENGTH];
-	
 	mwlog("RAW %s", text);
 	catchuri(text);
-	snprintf(buff, MAXTEXTLENGTH, "%s", text);
-	talk_send_to_room(buff, channel);
+	//snprintf(buff, MAXTEXTLENGTH, "%s", text);
+	talk_send_to_room(text, channel, "raw", -1);
 }
 
 void talk_send_rawbcast(char * text){
@@ -171,9 +161,8 @@
 }
 
 void talk_send_emote(char * text, int channel, int plural){
-	char buff[MAXTEXTLENGTH];
 
-	catchuri(text);
+	/*
 	switch (plural) {
 		case 1:
 			snprintf(buff, MAXTEXTLENGTH, "%s's %s", user->name, text);
@@ -190,8 +179,10 @@
 		default:
 			snprintf(buff, MAXTEXTLENGTH, "%s %s", user->name, text);
 	}
-	mwlog("EMOTE %s", buff);
-	talk_send_to_room(buff, channel);
+	*/
+	catchuri(text);
+	mwlog("EMOTE %s", text);
+	talk_send_to_room(text, channel, "emote", plural);
 }
 
 




More information about the mw-devel mailing list