Commit 01e20578 authored by Andrew Price's avatar Andrew Price
Browse files

Move ownership of gags to the server

The client now just caches a simple list of gag names that the server
sends it via a new IPC_GAGLIST message.
parent ed7e3bd7
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
#include "talker_privs.h"
#include "alias.h"
#include "who.h"
#include "gaglist.h"

/*
  modes:	0 - commands
@@ -48,7 +49,7 @@ CompletionList tctable[]={
{"emote's"	,1	,1	,-1	,part_who_talk},
{"event"	,1	,1	,1	,list_script},
{"freeze"	,1	,1	,1	,part_who_talk},
{"gag"		,1	,2	,2	,part_gag_filter},
{"gag"		,1	,2	,2	,gaglist_tc_filter},
{"gag"		,1	,1	,2	,part_who_talk},
{"kick"		,1	,1	,1	,part_who_talk},
{"load"		,1	,1	,1	,list_mwsfile},

src/client/gaglist.c

0 → 100644
+51 −0
Original line number Diff line number Diff line
#include <stdlib.h>
#include <string.h>

#include "gaglist.h"

char **gaglist;
size_t gaglist_len;

int gaglist_append(const char *gagname)
{
	char **tmp;

	if (gaglist_len == GAGLIST_MAX_ENTRIES)
		return 1;
	tmp = realloc(gaglist, sizeof(*gaglist) * (gaglist_len + 1));
	if (tmp == NULL) {
		free(gaglist);
		gaglist_len = 0;
		return 1;
	}
	gaglist = tmp;
	gaglist[gaglist_len++] = strdup(gagname);
	return 0;
}

void gaglist_destroy(void)
{
	for (size_t i = 0; i < gaglist_len; i++)
		free(gaglist[i]);
	free(gaglist);
}

char *gaglist_tc_filter(const char *text, int state)
{
	static size_t len = 0;
	static size_t i = 0;

	if (state == 0) {
		i = 0;
		len = strlen(text);
	}

	for (; i < gaglist_len; i++) {
		if (*gaglist[i] == '\0')
			continue;
		if (len == 0 || !strncasecmp(gaglist[i], text, len))
			return strdup(gaglist[i++]);
	}
	return NULL;
}

src/client/gaglist.h

0 → 100644
+10 −0
Original line number Diff line number Diff line
#ifndef GAGLIST_H
#define GAGLIST_H

#define GAGLIST_MAX_ENTRIES (128)

extern int gaglist_append(const char *gagname);
extern void gaglist_destroy(void);
extern char *gaglist_tc_filter(const char *text, int state);

#endif /* GAGLIST_H */
+18 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include "user.h"
#include "util.h"
#include "incoming.h"
#include "gaglist.h"
#include <jansson.h>
#include <str_util.h>

@@ -667,6 +668,20 @@ static void display_content(ipc_message_t *msg)
				force_vtext(msg, whom, "%s gives the reason \"%s\".", whom, reason);
			}
		}
	} else
	if (msg->head.type == IPC_GAGLIST) {
		json_t *jgag;
		size_t i = 0;

		if (j== NULL || !json_is_array(j)) {
			printf("Invalid GAGLIST message from %s.\n", whom);
			json_decref(j);
			return;
		}
		gaglist_destroy();
		json_array_foreach(j, i, jgag) {
			gaglist_append(json_string_value(jgag));
		}
	} else {
		printf("Unknown message type %4.4s", (char *)&msg->head.type);
	}
@@ -716,6 +731,9 @@ static void accept_pipe_cmd(ipc_message_t *msg, struct user *mesg_user)
		case IPC_GAG:
			force_gag(newbuff, mesg_user->record.chatprivs, mesg_user->record.name);
			break;
		case IPC_GAGLIST:
			display_content(msg);
			break;
		case IPC_PROTLEVEL:
			force_protlevel(newbuff, mesg_user->record.chatprivs, mesg_user->record.name);
			break;
+2 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include "user.h"
#include "userio.h"
#include "who.h"
#include "gaglist.h"
#include <util.h>

#define MAX_ARGC 128
@@ -138,7 +139,7 @@ FuncNames scrtc[]={
{"talkname"	,part_who_talk},
{"whoname"	,part_who},
{"username"	,part_user},
{"gag"		,part_gag_filter},
{"gag"		,gaglist_tc_filter},
{"bind"		,list_bind},
{"boardexec"	,list_commands},
{"exec"		,list_chat_commands},
Loading