Commit d6cc4fc1 authored by Andrew Price's avatar Andrew Price
Browse files

webclient: Send sayto messages as sayto messages

parent 93dc2abd
Loading
Loading
Loading
Loading
+31 −21
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#include <strings.h>
#include <pwd.h>
#include <inttypes.h>
#include <ctype.h>

#include <talker_privs.h>
#include <special.h>
@@ -426,26 +427,35 @@ static int handle_command(CONNECTION *co)
		}
	}else
	if (co->authd && strncasecmp(buff, "sayto ", 6)==0) {
		char *tmp;
		char *tmpm;
		char duser[17];
		char msg[8192];
		char *tmpu;
                char line[8192];
		tmp = remove_first_word(buff);
		tmpm = remove_first_word(tmp);
		tmpu = strtok(tmp," ");
		snprintf(msg,sizeof msg, "%s", tmpm);
		snprintf(duser,17,"%s",tmpu);
		if (buff[strlen(buff)-1]=='?') {
	                snprintf(line, sizeof line, "%s asks: %s", user->record.name, msg);
		}else {
			snprintf(line, sizeof line, "%s says: %s", user->record.name, msg);
		}
		mwlog("SAYTO %s %s", duser, msg);
                ipc_send_to_username(duser, IPC_TEXT, line);
                snprintf(buff, sizeof buff, "{\"status\":\"sayto sent to %s (%s)\"}", duser, msg);
                send(co->fd, buff, strlen(buff), 0);
		char *to;
		char *text;
		char *status;
		const char *type = "says";

		if (buff[strlen(buff) - 1] == '?')
			type = "asks";

		to = buff + 6; // Skip 'sayto '
		while (isspace(*to)) to++; // Skip whitespace to username
		text = to;
		while (!isspace(*text)) text++; // Skip username
		*text = '\0'; // Hasta la vista, username
		text++;
		while (*text && isspace(*text)) text++;  // Trim space before message

		ipc_message_t * msg = ipcmsg_create(IPC_SAYTOUSER, user->posn);
		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);

		asprintf(&status, "{\"status\":\"sayto sent to %s (%s)\"}", to, text);
		send(co->fd, status, strlen(status), 0);
		user->record.idletime = lastcomm;
		return 1;
	}else