summaryrefslogtreecommitdiff
path: root/src/commands.c
blob: 9ca2d8a600df8c7922bdc64be29f62a4e687f362 (plain)
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
#include "../config.h"
#include "commands.h"
#include "cmumble.h"

#include <glib.h>
#include <string.h>

#include <readline/readline.h>
#include <readline/history.h>

static void
list_users(struct cmumlbe *cm,
	   int argc, char **argv)
{
	struct cmumble_user *user = NULL;
	GList *l;

	for (l = cm->users; l; l = l->next) {
		user = l->data;

		g_print("%4d: %s\n", user->session, user->name);
	}
}

static void
list_channels(struct cmumlbe *cm,
	      int argc, char **argv)
{
	struct cmumble_channel *channel = NULL;
	GList *l;

	for (l = cm->channels; l; l = l->next) {
		channel = l->data;

		g_print("%4d: %s\n", channel->id, channel->name);
	}
}

static void
quit(struct cmumlbe *cm,
     int argc, char **argv)
{
	rl_already_prompted = 1;
	g_main_loop_quit(cm->loop);
}

static void
clear(struct cmumlbe *cm,
      int argc, char **argv)
{
	rl_clear_screen(0,0);
	rl_reset_line_state();
	g_print("\n");
}

static void
help(struct cmumlbe *cm,
     int argc, char **argv)
{
	int i;

	for (i = 0; cm->commands[i].name; ++i)
		g_print("%s\t%s\n",
			cm->commands[i].name, cm->commands[i].description);
}

static void
msg(struct cmumlbe *cm,
    int argc, char **argv)
{
	MumbleProto__TextMessage message;

	mumble_proto__text_message__init(&message);
	message.actor = cm->session;

	if (argc < 2) {
		g_print("usage: msg message\n");
		return;
	}

	message.message = argv[argc-1];

	/* FIXME: cache this in general somehow? */
	if (!cm->user || !cm->user->channel) {
		g_printerr("No information about current CHannel available\n");
		return;
	}

	message.n_channel_id = 1;
	message.channel_id = (uint32_t[]) { cm->user->channel->id };
	message.n_session = 0;
	message.n_tree_id = 0;

	cmumble_send_msg(cm, &message.base);
}

static const struct cmumble_command commands[] = {
	{ "lu", list_users, "list users" },
	{ "lc", list_channels, "list channels" },
	{ "clear", clear, "clear screen" },
	{ "help", help, "show this help" },
	{ "msg", msg, "send a text message" },
	{ "quit", quit, "quit " PACKAGE },
	{ NULL, NULL , NULL}
};

const char *
cmumble_command_expand_shortcut(const char *text)
{
	int i = 0;
	int found_index = -1;

	do {
		if (strncmp(commands[i].name, text, strlen(text)) == 0) {
			/* Found at least two matches, so do not complete. */
			if (found_index >= 0)
				return text;
			found_index = i;
		}
	} while (commands[++i].name);

	return found_index >= 0 ? commands[found_index].name : text;
}

static char *
complete(const char *in, int n)
{
	static int index = 0, len;
	const char *name;

	if (n == 0) {
		index = 0;
		len = strlen(in);
	}

	while (commands[index].name) {
		name = commands[index++].name;
		if (strncmp(name, in, len) == 0)
			return g_strdup(name);
	}
	
	return NULL;
}

void
cmumble_commands_init(struct cmumlbe *cm)
{
	cm->commands = commands;

	rl_completion_entry_function = complete;
}