Commit a9c50f14 authored by Justin Mitchell's avatar Justin Mitchell
Browse files

Only use the variadic json_addstring when its needed

parent 151cdc31
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
		json_addstring(ej, "type", "mrod");

		if (success) {
			json_addstring(ej, "text", "%s has just dropped the Magic Roundabout of Death on %s",
			json_vaddstring(ej, "text", "%s has just dropped the Magic Roundabout of Death on %s",
			               attacker_name, victim_name);
			json_addstring(ej, "verbose", "You hear a boing in the distance, closely followed by the distant wail of a hurdy-gurdy, which is suddenly terminated by a blood curdling scream, a C major chord explosion and a few curious springing noises...");

@@ -109,7 +109,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
			ipcmsg_destroy(update);

		} else {
			json_addstring(ej, "text", "%s just tried to MROD %s and failed",
			json_vaddstring(ej, "text", "%s just tried to MROD %s and failed",
			               attacker_name, victim_name);
		}
		ipcmsg_json_encode(event, ej);
@@ -131,7 +131,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
		json_addstring(ej, "type", "zod");

		if (success) {
			json_addstring(ej, "text", "%s has just sent the Zebedee of Death to %s",
			json_vaddstring(ej, "text", "%s has just sent the Zebedee of Death to %s",
			               attacker_name, victim_name);
			json_addstring(ej, "verbose", "You hear a boing in the distance");

@@ -150,7 +150,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
			msg_attach_to_username(update, victim_name);
			ipcmsg_destroy(update);
		} else {
			json_addstring(ej, "text", "%s just tried to ZoD %s and failed",
			json_vaddstring(ej, "text", "%s just tried to ZoD %s and failed",
			               attacker_name, victim_name);
		}
		ipcmsg_json_encode(event, ej);
@@ -184,7 +184,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
		json_addstring(ej, "type", "gag");

		if (success) {
			json_addstring(ej, "text", "%s has just gagged %s with %s",
			json_vaddstring(ej, "text", "%s has just gagged %s with %s",
			               attacker_name, victim_name, gag_type(gtnum));

			// change users mode
@@ -196,7 +196,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
			msg_attach_to_username(update, victim_name);
			ipcmsg_destroy(update);
		} else {
			json_addstring(ej, "text", "%s just tried to gag %s with %s and failed",
			json_vaddstring(ej, "text", "%s just tried to gag %s with %s and failed",
			               attacker_name, victim_name, gag_type(gtnum));
		}
		ipcmsg_json_encode(event, ej);
@@ -216,7 +216,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
		json_addstring(ej, "type", "ungag");

		if (success) {
			json_addstring(ej, "text", "%s has just ungagged %s", attacker_name, victim_name);
			json_vaddstring(ej, "text", "%s has just ungagged %s", attacker_name, victim_name);

			// change users mode
			_autofree char *buff=NULL;
@@ -227,7 +227,7 @@ void accept_action(ipc_connection_t *conn, ipc_message_t *msg)
			msg_attach_to_username(update, victim_name);
			ipcmsg_destroy(update);
		} else {
			json_addstring(ej, "text", "%s just tried to ungag %s and failed",
			json_vaddstring(ej, "text", "%s just tried to ungag %s and failed",
			               attacker_name, victim_name);
		}
		ipcmsg_json_encode(event, ej);
+11 −5
Original line number Diff line number Diff line
@@ -300,25 +300,31 @@ json_t * json_init(ipc_message_t * msg)

/** add/replace a string value in the base object */

int json_addstring(json_t * js, const char * key, const char * value, ...) __attribute__((format (printf, 3, 4)));
int json_vaddstring(json_t * js, const char * key, const char * value, ...) __attribute__((format (printf, 3, 4)));

int json_addstring(json_t * js, const char * key, const char * value, ...)
int json_vaddstring(json_t * js, const char * key, const char * value, ...)
{
	json_t * tmp;
	va_list va;
	va_start(va, value);
	_autofree char *text = NULL;
	vasprintf(&text, value, va);
	va_end(va);

	return json_addstring(js, key, text);
}

int json_addstring(json_t * js, const char * key, const char * value)
{
	json_t * tmp;

	tmp = json_object_get(js, key);
	if (tmp == NULL) {
		tmp = json_string(text);
		tmp = json_string(value);
		if (tmp == NULL) return -1;
		json_object_set_new(js, key, tmp);
	} else {
		if (!json_is_string(tmp)) return -1;
		json_string_set(tmp, text);
		json_string_set(tmp, value);
	}
	return 0;
}
+2 −1
Original line number Diff line number Diff line
@@ -78,7 +78,8 @@ ipc_message_t *read_socket(ipc_connection_t *conn, int doread);
json_t *ipcmsg_json_decode(ipc_message_t *msg);
int ipcmsg_json_encode(ipc_message_t *msg, json_t *js);
json_t *json_init(ipc_message_t *msg);
int json_addstring(json_t *js, const char *key, const char *value, ...);
int json_vaddstring(json_t *js, const char *key, const char *value, ...);
int json_addstring(json_t *js, const char *key, const char *value);
int json_addint(json_t *js, const char *key, int value);
int json_addobject(json_t *js, const char *key, json_t * obj);
const char *json_getstring(json_t *js, const char *key);