2012-03-13 01:22:22 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gio/gio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-03-13 16:24:03 +00:00
|
|
|
#include "interpreter.h"
|
2012-03-13 01:22:22 +00:00
|
|
|
#include "networking.h"
|
2012-03-14 19:19:05 +00:00
|
|
|
#include "main.h"
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-13 16:24:03 +00:00
|
|
|
#define IS_SPACE(c) (g_ascii_isspace((c)) || (!(c)))
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
WMUD_COMMAND(gcmd_quit);
|
|
|
|
|
2012-03-13 16:24:03 +00:00
|
|
|
static wmudCommand command_list[] = {
|
|
|
|
{ "quit", gcmd_quit },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2012-03-13 01:22:22 +00:00
|
|
|
static void
|
2012-03-14 19:06:02 +00:00
|
|
|
destroy_string(GString *string)
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
g_string_free(string, TRUE);
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmud_interpret_game_command(wmudClient *client)
|
|
|
|
{
|
|
|
|
GSList *command_parts = NULL;
|
|
|
|
gchar *a,
|
2012-03-14 19:06:02 +00:00
|
|
|
*start,
|
|
|
|
*end;
|
|
|
|
gchar str_delim = 0;
|
|
|
|
wmudCommand *cmd;
|
|
|
|
int command_parts_count = 0,
|
|
|
|
match_count = 0;
|
|
|
|
GSList *matches = NULL;
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
if (strchr(client->buffer->str, '\r') || strchr(client->buffer->str, '\n'))
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
/* TODO: We should NEVER reach this point! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = client->buffer->str;
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
GString *token;
|
|
|
|
|
|
|
|
while (*a)
|
|
|
|
{
|
|
|
|
for (start = a; *start; start++)
|
2012-03-13 16:24:03 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
if (!str_delim)
|
|
|
|
{
|
|
|
|
if ((*start == '"') || (*start == '\''))
|
|
|
|
{
|
|
|
|
str_delim = *start;
|
|
|
|
start++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!IS_SPACE(*start))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
for (end = start; *end; end++)
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
if (!str_delim && strchr("'\" \t", *end))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (str_delim && (*end == str_delim))
|
|
|
|
{
|
|
|
|
str_delim = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!*end)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
if (*start)
|
2012-03-13 16:24:03 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
token = g_string_new_len(start, end - start);
|
|
|
|
command_parts = g_slist_prepend(command_parts, token);
|
|
|
|
command_parts_count++;
|
2012-03-13 16:24:03 +00:00
|
|
|
}
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
a = end;
|
|
|
|
if (((*a == '"') || (*a == '\'')) && str_delim)
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str_delim)
|
|
|
|
{
|
|
|
|
wmud_client_send(client, "You should close quotes of any kind, like %c, shouldn't you?\r\n", str_delim);
|
|
|
|
#if GLIB_CHECK_VERSION(2, 28, 0)
|
2012-03-14 19:19:05 +00:00
|
|
|
g_slist_free_full(command_parts, (GDestroyNotify)destroy_string);
|
2012-03-14 19:06:02 +00:00
|
|
|
#else
|
|
|
|
g_slist_foreach(command_parts, (GFunc)destroy_string, NULL);
|
|
|
|
g_slist_free(command_parts);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command_parts_count == 0)
|
|
|
|
{
|
|
|
|
/* TODO: handle empty command */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_parts = g_slist_reverse(command_parts);
|
|
|
|
|
|
|
|
for (cmd = command_list; cmd->command; cmd++)
|
|
|
|
{
|
|
|
|
GString *input = (GString *)(command_parts->data);
|
|
|
|
gint cmp;
|
|
|
|
|
|
|
|
if (((cmp = g_ascii_strncasecmp(input->str, cmd->command, input->len)) == 0) && !cmd->command[input->len])
|
|
|
|
{
|
|
|
|
g_slist_free(matches);
|
|
|
|
match_count = 1;
|
|
|
|
matches = NULL;
|
|
|
|
matches = g_slist_prepend(matches, cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (cmp == 0)
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
matches = g_slist_prepend(matches, cmd);
|
|
|
|
match_count++;
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:19:05 +00:00
|
|
|
switch (match_count)
|
2012-03-14 19:06:02 +00:00
|
|
|
{
|
2012-03-14 19:19:05 +00:00
|
|
|
case 0:
|
|
|
|
switch (random_number(1, 3))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
wmud_client_send(client, "Huh?\r\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
wmud_client_send(client, "What?\r\n");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
wmud_client_send(client, "I can hardly understand you...\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
((wmudCommand *)(matches->data))->commandFunc(client, ((GString *)(command_parts->data))->str, command_parts->next);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
wmud_client_send(client, "This command could mean several things, please try a more exact form!\r\n");
|
2012-03-13 16:24:03 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
g_slist_free(matches);
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
WMUD_COMMAND(gcmd_quit)
|
|
|
|
{
|
|
|
|
wmud_client_send(client, "Are you sure you want to get back to that freaky other reality? [y/N] ");
|
2012-03-21 15:07:02 +00:00
|
|
|
client->state = WMUD_CLIENT_STATE_QUITWAIT;
|
2012-03-14 19:06:02 +00:00
|
|
|
}
|
|
|
|
|