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

arthur at sucs.org arthur at sucs.org
Sun Dec 30 15:56:36 GMT 2012


Author: arthur
Date: 2012-12-30 15:56:36 +0000 (Sun, 30 Dec 2012)
New Revision: 1318

Modified:
   trunk/src/incoming.c
   trunk/src/ipc.h
   trunk/src/script_inst.c
   trunk/src/server/servsock.c
   trunk/src/server/servsock.h
   trunk/src/talker.c
   trunk/src/talker.h
Log:
Migrate whisper and sayto commands to new message types


Modified: trunk/src/incoming.c
===================================================================
--- trunk/src/incoming.c	2012-12-12 22:40:18 UTC (rev 1317)
+++ trunk/src/incoming.c	2012-12-30 15:56:36 UTC (rev 1318)
@@ -473,25 +473,47 @@
 	json_decref(j);
 }
 
+static void display_error(ipc_message_t *msg)
+{
+	json_t * j = json_init(msg);
+	const char * type = json_getstring(j, "type");
+	const char * text = json_getstring(j, "text");
+
+	char buff[MAXTEXTLENGTH];
+	char * tb = buff;
+	int len = MAXTEXTLENGTH;
+
+	if (msg->head.type == IPC_TALKERROR) {
+		snprintf(tb, len, "Talker Error: %s", text);
+	} else {
+		snprintf(tb, len, "Unknown %s Error: %s", type, text);
+	}
+
+	force_text(buff, "System");
+	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;
+	int32_t msg_posn = -1;
+	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;
-		}
+	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);
+	json_t * j = json_init(msg);
+
+	if (msg->head.type == IPC_SAYTOROOM) {
 		const char * type = json_getstring(j, "type");
 		const char * text = json_getstring(j, "text");
 
@@ -551,11 +573,47 @@
 			snprintf(tb, len, "%s: %s", whom, text);
 		}
 		force_text(buff, whom);
-		json_decref(j);
-		return;
+	} else
+	if (msg->head.type == IPC_SAYTOUSER) {
+		const char * type = json_getstring(j, "type");
+		const char * text = json_getstring(j, "text");
+
+		if (type == NULL || text == NULL) {
+			printf("Invalid SAYTOUSER 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)) {
+			if (msg_posn == -1)
+				snprintf(tb, len, "*:");
+			else
+				snprintf(tb, len, "%d:", msg_user.room);
+			int s = strlen(tb);
+			len -= s;
+			tb += s;
+		}
+	
+		if (strcmp(type, "whispers")==0) {
+			snprintf(tb, len, "%s whispers: %s", whom, text);
+		} else
+		if (strcmp(type, "asks")==0) {
+			snprintf(tb, len, "%s asks: %s", whom, text);
+		} else  {
+			snprintf(tb, len, "%s says: %s", whom, text);
+		}
+		force_text(buff, whom);
+	} else {
+		printf("Unknown message type %4.4s", (char *)&msg->head.type);
 	}
 
-	printf("Unknown message type %4.4s", (char *)&msg->head.type);
+	json_decref(j);
+
 }
 
 static void accept_pipe_cmd(ipc_message_t *msg, struct person *mesg_user)
@@ -644,8 +702,12 @@
 			display_uptime(msg);
 			break;
 		case IPC_SAYTOROOM:
+		case IPC_SAYTOUSER:
 			display_content(msg);
 			break;
+		case IPC_TALKERROR:
+			display_error(msg);
+			break;
 		default:
 			devel_msg("incoming_mesg", "unknown message type %d.\007", state);
 	}

Modified: trunk/src/ipc.h
===================================================================
--- trunk/src/ipc.h	2012-12-12 22:40:18 UTC (rev 1317)
+++ trunk/src/ipc.h	2012-12-30 15:56:36 UTC (rev 1318)
@@ -38,7 +38,9 @@
 	IPC_PROTPOWER	= 25,
 	IPC_DOING	= 26,
 	IPC_UPTIME	= MKFOURCC('U','P','T','M'),
-	IPC_SAYTOROOM	= MKFOURCC('S','A','Y','R')
+	IPC_SAYTOROOM	= MKFOURCC('S','A','Y','R'),
+	IPC_SAYTOUSER	= MKFOURCC('S','A','Y','U'),
+	IPC_TALKERROR	= MKFOURCC('T','E','R','R')
 };
 
 typedef int (send_filter)(const struct person * usr, const struct who * who, const void * info);

Modified: trunk/src/script_inst.c
===================================================================
--- trunk/src/script_inst.c	2012-12-12 22:40:18 UTC (rev 1317)
+++ trunk/src/script_inst.c	2012-12-30 15:56:36 UTC (rev 1318)
@@ -2323,7 +2323,7 @@
 void scr_sayto( struct code *pc, int fargc, char **fargv )
 {
 	int i, size, count;
-	char *tmp, *p, *text;
+	char *tmp, *p, *target;
 
 	if (pc->argc < 2) return;
 	if (!cm_flags(user->chatmode, CM_ONCHAT, CM_MODE_ANY)) return;
@@ -2355,9 +2355,11 @@
 		return;
 	}
 
-	text = eval_arg(pc->argv[0], fargc, fargv);
-	chat_sayto(text,tmp);
-	free(text);
+	target = eval_arg(pc->argv[0], fargc, fargv);
+	const char *type = "says";
+	if (tmp[strlen(tmp)-1] == '?') type = "asks";
+	talk_sayto(tmp, target, type);
+	free(target);
 	free(tmp);
 }
 

Modified: trunk/src/server/servsock.c
===================================================================
--- trunk/src/server/servsock.c	2012-12-12 22:40:18 UTC (rev 1317)
+++ trunk/src/server/servsock.c	2012-12-30 15:56:36 UTC (rev 1318)
@@ -223,7 +223,32 @@
 }
 
 extern time_t uptime;
+			
+void send_error(ipc_connection_t *conn, ipc_message_t *orig, const char *format, ...) __attribute__((format (printf, 3, 4)));
 
+void send_error(ipc_connection_t *conn, ipc_message_t *orig, const char *format, ...)
+{
+	va_list va;
+	va_start(va, format);
+	AUTOFREE_BUFFER text = NULL;
+	vasprintf(&text, format, va);
+	va_end(va);
+
+	ipc_message_t * msg = ipcmsg_create(FOURCC("TERR"), 0);
+	ipcmsg_destination(msg, conn->addr);
+
+	char type[5];
+	snprintf(type, 5, "%4.4s", (char *)&orig->head.type);
+	json_t * j = json_init(NULL);
+	json_addstring(j, "text", text);
+	json_addstring(j, "type", type);
+	ipcmsg_json_encode(msg, j);
+	msg_attach(msg, conn);
+	ipcmsg_destroy(msg);
+	json_decref(j);
+}
+
+
 void process_msg(ipc_connection_t *conn, ipc_message_t *msg)
 {
 	ipcmsg_summary("PROCESS", msg);
@@ -304,6 +329,42 @@
 		return;
 	}
 
+	/* message is for a specific username */
+	if (msg->head.type == FOURCC("SAYU")) {
+		/* 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;
+		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(msg, who.pid);
+				found++;
+			}
+		}
+		if (found < 1) {
+			send_error(conn, msg, "User '%s' not found", target); 
+		}
+		json_decref(j);
+		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);

Modified: trunk/src/server/servsock.h
===================================================================
--- trunk/src/server/servsock.h	2012-12-12 22:40:18 UTC (rev 1317)
+++ trunk/src/server/servsock.h	2012-12-30 15:56:36 UTC (rev 1318)
@@ -9,3 +9,4 @@
 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);
+void send_error(ipc_connection_t *conn, ipc_message_t *orig, const char *format, ...);

Modified: trunk/src/talker.c
===================================================================
--- trunk/src/talker.c	2012-12-12 22:40:18 UTC (rev 1317)
+++ trunk/src/talker.c	2012-12-30 15:56:36 UTC (rev 1318)
@@ -164,7 +164,20 @@
 	talk_send_to_room(text, channel, "emote", plural);
 }
 
+void talk_sayto(char *text, const char *to, const char *type) 
+{
+	ipc_message_t * msg = ipcmsg_create(IPC_SAYTOUSER, getpid());
+	json_t * j = json_init(NULL);
+	json_addstring(j, "target", to);
+	json_addstring(j, "type", type);
+	json_addstring(j, "text", text);
+	ipcmsg_json_encode(msg, j);
+	json_decref(j);
+	ipcmsg_transmit(msg);
 
+	mwlog("SAYTO %s %s",to,text);
+}
+
 int ison(const char * uname)
 {
     /*
@@ -461,44 +474,7 @@
 
 void t_whisper(CommandList *cm, int argc, const char **argv, char *args)
 {
-	char text[MAXTEXTLENGTH];
-	char buff[MAXTEXTLENGTH];
-	struct person u;
-	struct who w;
-	int ufile,wfile;
-	int count=0;
-
-	strncpy(text, args, MAXTEXTLENGTH - 1);
-	text[MAXTEXTLENGTH - 1] = '\0';
-	apply_gag(text);
-	wfile=openwhofile(O_RDONLY);
-	ufile=openuserfile(O_RDONLY);
-
-	snprintf(buff, MAXTEXTLENGTH, "%s whispers: %s", user->name, text);	
-
-	while (read(wfile,&w,sizeof(w)))
-	{
-		/* Skip invalid entries */
-		if (w.posn < 0)
-			continue;
-
-		lseek(ufile,w.posn,0);
-		read(ufile,&u,sizeof(u));
-
-		if (cm_flags(u.chatmode,CM_ONCHAT,CM_MODE_ALL) && !strcasecmp(argv[1], u.name) )
-			if (! ipc_send_to_pid(w.pid, IPC_TEXT, buff)) count++;
-	}
-
-	if (count==0)
-	{
-		printf("User '%s' is not logged on.\n", argv[1]);
-	}else
-	{
-		mwlog("WHISPER %s %s", argv[1], text);
-	}
-
-	close(ufile);
-	close(wfile);
+	talk_sayto(args, argv[1], "whispers");
 }
 
 
@@ -508,7 +484,12 @@
 
 	text[0]=0;
 	strncat(text, args, MAXTEXTLENGTH - strlen(text) - 1);
-	chat_sayto(argv[1],text);
+
+	const char *type = "says";
+
+	if (text[strlen(text)-1] == '?') type = "asks";
+	apply_gag(text);
+	talk_sayto(text, argv[1], type);
 }
 
 void t_sayto_warn(CommandList *cm, int argc, const char **argv, char *args)
@@ -1513,74 +1494,6 @@
 	current_rights = RIGHTS_TALK;
 }	
 
-static void talk_sayto(char *text, const char *to, int channel) 
-{
-	char buff[MAXTEXTLENGTH];
-	char gbuff[MAXTEXTLENGTH];
-	struct person u;
-	struct who w;
-	int ufile,wfile;
-	int count=0;
-	int send2user;
-	int global;
-	char * sendbuff;
-
-	snprintf(buff, MAXTEXTLENGTH,"%s %s", user->name, text);
-	snprintf(gbuff, MAXTEXTLENGTH,"%d:%s %s", channel, user->name, text);
-
-	wfile=openwhofile(O_RDONLY);
-	ufile=openuserfile(O_RDONLY);
-	
-	while (read(wfile,&w,sizeof(w)))
-	{
-		/* Skip invalid entries */
-		if (w.posn < 0)
-			continue;
-
-		lseek(ufile,w.posn,0);
-		read(ufile,&u,sizeof(u));
-
-		global = cm_flags(u.chatmode, CM_GLOBAL, CM_MODE_ALL);
-		if (global) sendbuff = gbuff;
-		else sendbuff = buff;
-
-		send2user = !strcasecmp(to, u.name);
-
-		if (cm_flags(u.chatmode,CM_ONCHAT,CM_MODE_ALL) && send2user)
-			if (! ipc_send_to_pid(w.pid, IPC_TEXT, sendbuff)) count++;
-	}
-
-	if (count==0)
-	{
-		printf("User '%s' is not logged on.\n",to);
-	}else
-	{
-		mwlog("SAYTO %s %s",to,text);
-	}
-
-	close(ufile);
-	close(wfile);
-}
-
-void chat_sayto(const char *to, char *text)
-{
-	char buff[MAXTEXTLENGTH];
-
-	buff[0]=0;
-	if (text[strlen(text)-1] == '?')
-	{
-		snprintf(buff,MAXTEXTLENGTH-1,"asks: ");
-	}
-	else
-	{
-		snprintf(buff,MAXTEXTLENGTH-1,"says: ");
-	}
-	apply_gag(text);
-	strncat(buff, text, MAXTEXTLENGTH - strlen(buff) - 1);
-
-	talk_sayto(buff, to, user->room);
-}
-
 struct IgnoreList *is_ignored(const char *name, struct IgnoreList **prev)
 {
 	struct IgnoreList *tail;

Modified: trunk/src/talker.h
===================================================================
--- trunk/src/talker.h	2012-12-12 22:40:18 UTC (rev 1317)
+++ trunk/src/talker.h	2012-12-30 15:56:36 UTC (rev 1318)
@@ -64,7 +64,7 @@
 void t_chaton(void);
 
 void chat_say(char *text);
-void chat_sayto(const char *to,char *text);
+void talk_sayto(char *text, const char *to, const char *type);
 void set_talk_rights(void);
 void talk_send_emote(char * text, int channel, int plural);
 void talk_send_rawbcast(char * text);




More information about the mw-devel mailing list