File: linestack-cmd.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (270 lines) | stat: -rw-r--r-- 5,613 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "line.h"
#include "linestack.h"
#include "settings.h"
#include "state.h"
#include <readline/readline.h>

static struct linestack_context *ctx = NULL;
static struct irc_network_state *state = NULL;
static struct ctrlproxy_config *cfg = NULL;

static void handle_help(int, char **);
static void handle_mark(int, char **);
static void handle_replay(int, char **);
static void handle_state(int, char **);
static void handle_exit(int, char **);
static void handle_insert(int, char **);
static GHashTable *markers = NULL;

/* there are no hup signals here */
void register_hup_handler(void *fn, void *data) {}

/**
 * Shell command.
 */
struct cmd {
	const char *name;
	void (*handler) (int argc, char **);
} cmds[] = {
	{ "help", handle_help },
	{ "mark", handle_mark },
	{ "replay", handle_replay },
	{ "state", handle_state },
	{ "exit", handle_exit },
	{ "insert", handle_insert },
};


static gboolean line_printer (struct irc_line *l, time_t time, void *f) 
{
	fprintf((FILE *)f, "[%lu] %s\n", time, irc_line_string(l));
	return TRUE;
}

static void handle_state(int argc, char **argv)
{
	struct linestack_marker *pos;
	struct irc_network_state *state;
	GList *gl;

	if (argc > 2) {
		fprintf(stderr, "Usage: state [<pos>]\n");
		return;
	}

	if (argv[1] != NULL) {
		pos = g_hash_table_lookup(markers, argv[1]);

		if (pos == NULL) {
			fprintf(stderr, "Unable to find marker '%s'\n", argv[1]);
			return;
		}
	} else {
		pos = NULL;
	}

	state = linestack_get_state(ctx, pos);
	if (state == NULL) {
		fprintf(stderr, "Unable to retrieve network state for marker '%s'\n", argv[1]);
		return;
	}

	printf("Channels:\n");

	for (gl = state->channels; gl; gl = gl->next) {
		struct irc_channel_state *cs = gl->data;
		printf("\t%s\n", cs->name);
	}
}

static void handle_replay(int argc, char **argv)
{
	struct linestack_marker *from, *to = NULL;

	if (argc < 2) {
		fprintf(stderr, "Usage: replay <from> [<to>]\n");
		return;
	}

	from = g_hash_table_lookup(markers, argv[1]);

	if (from == NULL) {
		fprintf(stderr, "Unable to find marker '%s'\n", argv[1]);
		return;
	}

	if (argv[2] != NULL)
		to = g_hash_table_lookup(markers, argv[2]);

	linestack_traverse(ctx, from, to, line_printer, stdout);
}

static void handle_help(int argc, char **argv)
{
	fprintf(stderr, "Available commands:\n"
					"help\n"
					"insert\n"
					"mark\n"
					"state\n"
					"replay\n"
					"exit\n");
}

static void handle_mark(int argc, char **argv)
{
	struct linestack_marker *marker;

	if (argc < 2) {
		fprintf(stderr, "Usage: mark <name>\n");
		return;
	}

	marker = linestack_get_marker(ctx);

	g_assert(marker != NULL);

	fprintf(stderr, "Marker '%s' added\n", argv[1]);

	g_hash_table_insert(markers, g_strdup(argv[1]), marker);
}

static void handle_insert(int argc, char **argv)
{
	struct irc_line *l;
	enum data_direction direction;

	if (argc < 2) {
		fprintf(stderr, "Usage: insert <client|server> <line>\n");
		return;
	}

	if (!g_strcasecmp(argv[1], "client")) 
		direction = TO_SERVER;
	else if (!g_strcasecmp(argv[1], "server"))
		direction = FROM_SERVER;
	else {
		fprintf(stderr, "Unable to parse direction `%s'\n", argv[1]);
		return;
	}
	
	l = irc_parse_line(argv[2]);
	if (l == NULL) {
		fprintf(stderr, "Error parsing line\n");
		return;
	}

	linestack_insert_line(ctx, l, direction, state);
}

static void handle_exit(int argc, char **argv)
{
	exit(0);
}

static void freels(void)
{
	free_linestack_context(ctx);
}

int main(int argc, char **argv)
{
	GOptionContext *pc;
	char *line;
	const struct linestack_ops *ops;
	struct irc_network_info *info;
	char *config_dir = NULL;
	char *backend = NULL;
	GOptionEntry options[] = {
		{"backend", 'b', 0, G_OPTION_ARG_STRING, &backend, "Backend to use [default: file]" },
		{"config-dir", 'c', 0, G_OPTION_ARG_STRING, &config_dir, ("Override configuration directory"), ("DIR")},
		{ NULL }
	};

	pc = g_option_context_new("");
	g_option_context_add_main_entries(pc, options, NULL);
	if(!g_option_context_parse(pc, &argc, &argv, NULL))
		return 1;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <name>\n", argv[0]);
		return 1;
	}

	info = g_new0(struct irc_network_info, 1);

	state = network_state_init("nick", "username", "hostname");

	g_assert(state != NULL);

	if (backend != NULL) {
		ops = linestack_find_ops(backend);
		if (ops == NULL) {
			fprintf(stderr, "No such backend `%s'\n", backend);
			return 1;
		}
	} else {
		ops = &linestack_file;
	}

	if (config_dir == NULL) 
		config_dir = g_build_filename(g_get_home_dir(), ".ctrlproxy", NULL);

	cfg = load_configuration(config_dir);
	if (cfg == NULL) {
		fprintf(stderr, "Unable to load configuration from `%s'", config_dir);
		return 1;
	}

	markers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, 
									(GDestroyNotify)linestack_free_marker);

	ctx = create_linestack(ops, argv[1], cfg, state);

	atexit(freels);

	if (ctx == NULL) {
		fprintf(stderr, "Unable to open linestack context\n");
		return 1;
	}

	while (1) {
		gint cargc;
		GError *error = NULL;
		gchar **cargv = NULL;
		int i;

		line = readline("linestack> ");	

		if (line == NULL)
			return 0;

		if (!g_shell_parse_argv(line, &cargc, &cargv, &error)) {
			fprintf(stderr, "Error parsing: %s\n", error->message);
			goto next;
		}

		if (cargc == 0) {
			goto next;
		}

		for (i = 0; cmds[i].name; i++) {
			if (!strcmp(cmds[i].name, cargv[0])) {
				cmds[i].handler(cargc, cargv);
				goto next;
			}
		}

		fprintf(stderr, "Unknown command `%s'\n", cargv[0]);

next:
		g_strfreev(cargv);

		free(line);
	}
	g_option_context_free(pc);

	return 0;
}