[mw-devel] MW3 r1174 - trunk/src/webclient

arthur at sucs.org arthur at sucs.org
Wed Oct 6 12:31:44 BST 2010


Author: arthur
Date: 2010-10-06 12:31:43 +0100 (Wed, 06 Oct 2010)
New Revision: 1174

Modified:
   trunk/src/webclient/comms.c
   trunk/src/webclient/mwpoll.c
Log:
working initial version


Modified: trunk/src/webclient/comms.c
===================================================================
--- trunk/src/webclient/comms.c	2010-10-05 16:31:36 UTC (rev 1173)
+++ trunk/src/webclient/comms.c	2010-10-06 11:31:43 UTC (rev 1174)
@@ -60,6 +60,8 @@
 
 struct list_head connlist;
 
+char * json_escape(char *in);
+
 /* unix socket to accept control commands from */
 void open_command_socket()
 {
@@ -97,6 +99,7 @@
 // Everyone in a room except people with global turned on
         struct filter_info *    f_info = (struct filter_info *) info;
 
+	printf("filter: user '%s' pid=%d\n", usr->name, who->pid);
         if ((f_info->noloopback) && (getpid() == who->pid)) return 0;
         if (! cm_flags(usr->chatmode, CM_ONCHAT, CM_MODE_ALL)) return 0;
         if (cm_flags(usr->chatmode, CM_GLOBAL, CM_MODE_ALL)) return 0;
@@ -112,7 +115,7 @@
 	msg->state = state;
 	msg->pid = mesg_pid;
 	msg->user = *mesg_user;
-	msg->text = strdup(newbuff);
+	msg->text = json_escape(newbuff);
 
 	list_add_tail(&msg->list, &msglist);
 
@@ -274,6 +277,37 @@
 	return 1;
 }
 
+
+char * json_escape(char *in)
+{
+	int maxlen = (strlen(in)*2) + 1;
+	char *line = malloc(maxlen);
+	line[0] = 0;
+	int i;
+	char *out = line;
+
+	for (i=0;i<strlen(in);i++) {
+		switch (in[i]) {
+			case '\b': snprintf(out, maxlen, "\\b"); out+=2; maxlen-=2; break;
+			case '\n': snprintf(out, maxlen, "\\n"); out+=2; maxlen-=2; break;
+			case '\r': snprintf(out, maxlen, "\\r"); out+=2; maxlen-=2; break;
+			case '\t': snprintf(out, maxlen, "\\t"); out+=2; maxlen-=2; break;
+			case '"': snprintf(out, maxlen, "\\\""); out+=2; maxlen-=2; break;
+			case '\\': snprintf(out, maxlen, "\\\\"); out+=2; maxlen-=2; break;
+			case '/': snprintf(out, maxlen, "\\/"); out+=2; maxlen-=2; break;
+			default:
+				  if (in[i] < ' ') {
+					snprintf(out, maxlen, "\\u%04x", in[i]); out+=6; maxlen-=6; break;
+				  }else
+					snprintf(out, maxlen, "%c", in[i]); out+=1; maxlen-=1; break;
+		}
+		if (maxlen < 2) break;
+	}
+	*out = 0;
+
+	return line;
+}
+
 static int handle_command(CONNECTION *co)
 {
 	/* read and answer the command socket */
@@ -318,9 +352,11 @@
 	}else
 	if (co->authd && strncasecmp(buff, "say ", 4)==0) {
 		struct filter_info f_info;
+		char line[8192];
 		memset(&f_info, 0, sizeof(f_info));
 		f_info.channel = user->room;
-		ipc_send_to_all(IPC_TEXT, &buff[4], send_filter_oneroom, &f_info);
+		snprintf(line, sizeof line, "%s: %s", user->name, &buff[4]);
+		ipc_send_to_all(IPC_TEXT, line, send_filter_oneroom, &f_info);
 		snprintf(buff, sizeof buff, "{\"status\":\"Message Sent to channel %d\"}", user->room);
 		send(co->fd, buff, strlen(buff), 0);
 		return 1;

Modified: trunk/src/webclient/mwpoll.c
===================================================================
--- trunk/src/webclient/mwpoll.c	2010-10-05 16:31:36 UTC (rev 1173)
+++ trunk/src/webclient/mwpoll.c	2010-10-06 11:31:43 UTC (rev 1174)
@@ -64,9 +64,10 @@
 	int opt;
 	int32_t userposn;
 	int channel = 0;
+	int debug = 0;
 
 	username = strdup("Arthur2");
-	while ((opt=getopt(argc,argv,"u:c:"))!=-1) {
+	while ((opt=getopt(argc,argv,"u:c:d"))!=-1) {
 		switch (opt) {
 			case 'u':
 				if (username) free(username);
@@ -75,6 +76,9 @@
 			case 'c':
 				channel = atoi(optarg);
 				break;
+			case 'd':
+				debug++;
+				break;
 			default:
 				usage(argv[0]);
 				return 1;
@@ -122,10 +126,17 @@
 		for (i=0;i<8;i++) authtext[i] = 'A' + (rand() % 26);
 	}
 
-	int cpid = mydaemon();
-	if (cpid != 0) {
-		printf("%d\n%s\n", cpid, authtext);
-		return 0;
+	if (!debug) {
+		int cpid = mydaemon();
+		if (cpid != 0) {
+			printf("%d\n%s\n", cpid, authtext);
+			return 0;
+		}
+	} else {
+		printf("Debugging foreground mode.\n");
+		printf("PID: %d\n", getpid());
+		printf("AUTH: %s\n", authtext);
+		printf("http://sucs.org/~arthur/mw/index.php?mwsess=a:2:{s:3:\"pid\";i:%d;s:4:\"auth\";s:%zd:\"%s\";}\n", getpid(), strlen(authtext), authtext);
 	}
 
 	/* load us up */




More information about the mw-devel mailing list