1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Started work on command parser

This commit is contained in:
James Booth 2012-11-15 02:31:31 +00:00
parent e3d282eb6b
commit b5d1a8edcb

View File

@ -82,6 +82,7 @@ static void _parameter_autocomplete(char *input, int *size, char *command,
autocomplete_func func);
static int _strtoi(char *str, int *saveptr, int min, int max);
gchar** _cmd_parse_args(const char * const inp, int min, int max, int *num);
// command prototypes
static gboolean _cmd_quit(const char * const inp, struct cmd_help_t help);
@ -833,18 +834,21 @@ static gboolean
_cmd_connect(const char * const inp, struct cmd_help_t help)
{
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 1, &num_args);
if (args == NULL) {
cons_show("Usage: %s", help.usage);
result = TRUE;
} else {
jabber_conn_status_t conn_status = jabber_get_connection_status();
if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) {
cons_show("You are either connected already, or a login is in process.");
result = TRUE;
} else if (strlen(inp) < 10) {
cons_show("Usage: %s", help.usage);
result = TRUE;
} else {
char *user, *lower;
user = strndup(inp+9, strlen(inp)-9);
lower = g_utf8_strdown(user, -1);
char *user = args[0];
char *lower = g_utf8_strdown(user, -1);
status_bar_get_password();
status_bar_refresh();
@ -867,6 +871,8 @@ _cmd_connect(const char * const inp, struct cmd_help_t help)
result = TRUE;
}
}
g_strfreev(args);
return result;
}
@ -875,6 +881,13 @@ static gboolean
_cmd_sub(const char * const inp, struct cmd_help_t help)
{
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 2, &num_args);
if (args == NULL) {
cons_show("Usage: %s", help.usage);
result = TRUE;
} else {
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) {
@ -884,13 +897,9 @@ _cmd_sub(const char * const inp, struct cmd_help_t help)
cons_show("Usage: %s", help.usage);
result = TRUE;
} else {
char *inp_cpy, *subcmd;
char *jid, *bare_jid;
inp_cpy = strndup(inp+5, strlen(inp)-5);
/* using strtok is bad idea */
subcmd = strtok(inp_cpy, " ");
jid = strtok(NULL, " ");
char *subcmd, *jid, *bare_jid;
subcmd = args[0];
jid = args[1];
if (jid != NULL) {
jid = strdup(jid);
@ -917,9 +926,10 @@ _cmd_sub(const char * const inp, struct cmd_help_t help)
}
free(jid);
free(inp_cpy);
result = TRUE;
}
}
g_strfreev(args);
return result;
}
@ -1208,20 +1218,21 @@ _cmd_msg(const char * const inp, struct cmd_help_t help)
static gboolean
_cmd_info(const char * const inp, struct cmd_help_t help)
{
char *usr = NULL;
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 1, &num_args);
if (args == NULL) {
cons_show("Usage: %s", help.usage);
result = TRUE;
} else {
char *usr = args[0];
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected.");
} else {
// copy input
char inp_cpy[strlen(inp) + 1];
strcpy(inp_cpy, inp);
// get user
strtok(inp_cpy, " ");
usr = strtok(NULL, " ");
if (usr != NULL) {
win_show_status(usr);
} else {
@ -1229,34 +1240,38 @@ _cmd_info(const char * const inp, struct cmd_help_t help)
}
}
return TRUE;
result = TRUE;
}
g_strfreev(args);
return result;
}
static gboolean
_cmd_join(const char * const inp, struct cmd_help_t help)
{
char *room = NULL;
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 2, &num_args);
if (args == NULL) {
cons_show("Usage: %s", help.usage);
result = TRUE;
} else {
char *room = args[0];
char *nick = NULL;
if (num_args == 2) {
nick = args[1];
}
jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected.");
} else {
// copy input
char inp_cpy[strlen(inp) + 1];
strcpy(inp_cpy, inp);
// get room jid
strtok(inp_cpy, " ");
room = strtok(NULL, " ");
if (room == NULL) {
cons_show("Usage: %s", help.usage);
} else {
if ((strlen(inp) > (6 + strlen(room) + 1))) {
nick = strndup(inp+6+strlen(room)+1, strlen(inp)-(6+strlen(room)+1));
}
// if no nick, set to first part of jid
if (nick == NULL) {
const char *jid = jabber_get_jid();
@ -1269,18 +1284,30 @@ _cmd_join(const char * const inp, struct cmd_help_t help)
}
}
return TRUE;
result = TRUE;
}
g_strfreev(args);
return result;
}
static gboolean
_cmd_tiny(const char * const inp, struct cmd_help_t help)
{
if (strlen(inp) > 6) {
char *url = strndup(inp+6, strlen(inp)-6);
if (url == NULL) {
log_error("Not enough memory.");
return FALSE;
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 1, &num_args);
if (args == NULL) {
cons_show("Usage: %s", help.usage);
if (win_in_chat()) {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
win_show(usage);
}
result = TRUE;
} else {
char *url = args[0];
if (!tinyurl_valid(url)) {
GString *error = g_string_new("/tiny, badly formed URL: ");
@ -1311,18 +1338,12 @@ _cmd_tiny(const char * const inp, struct cmd_help_t help)
} else {
cons_bad_command(inp);
}
free(url);
} else {
cons_show("Usage: %s", help.usage);
if (win_in_chat()) {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
win_show(usage);
}
result = TRUE;
}
g_strfreev(args);
return TRUE;
return result;
}
static gboolean
@ -1378,35 +1399,28 @@ _cmd_set_outtype(const char * const inp, struct cmd_help_t help)
static gboolean
_cmd_set_notify(const char * const inp, struct cmd_help_t help)
{
char *kind = NULL;
char *value = NULL;
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 2, 2, &num_args);
// copy input
char inp_cpy[strlen(inp) + 1];
strcpy(inp_cpy, inp);
if (args == NULL) {
cons_show("Usage: %s", help.usage);
result = TRUE;
} else {
char *kind = args[0];
char *value = args[1];
// get kind
strtok(inp_cpy, " ");
kind = strtok(NULL, " ");
if ((kind != NULL) && (strlen(inp) > (8 + strlen(kind) + 1))) {
if ((strcmp(kind, "message") != 0) &&
(strcmp(kind, "typing") != 0) &&
// bad kind
if ((strcmp(kind, "message") != 0) && (strcmp(kind, "typing") != 0) &&
(strcmp(kind, "remind") != 0)) {
cons_show("Usage: %s", help.usage);
return TRUE;
} else {
// get value
value = strndup(inp+8+strlen(kind)+1, strlen(inp)-(8+strlen(kind)+1));
if (value != NULL) {
// set message setting
if (strcmp(kind, "message") == 0) {
if (strcmp(inp, "/notify message on") == 0) {
} else if (strcmp(kind, "message") == 0) {
if (strcmp(value, "on") == 0) {
cons_show("Message notifications enabled.");
prefs_set_notify_message(TRUE);
} else if (strcmp(inp, "/notify message off") == 0) {
} else if (strcmp(value, "off") == 0) {
cons_show("Message notifications disabled.");
prefs_set_notify_message(FALSE);
} else {
@ -1415,10 +1429,10 @@ _cmd_set_notify(const char * const inp, struct cmd_help_t help)
// set typing setting
} else if (strcmp(kind, "typing") == 0) {
if (strcmp(inp, "/notify typing on") == 0) {
if (strcmp(value, "on") == 0) {
cons_show("Typing notifications enabled.");
prefs_set_notify_typing(TRUE);
} else if (strcmp(inp, "/notify typing off") == 0) {
} else if (strcmp(value, "off") == 0) {
cons_show("Typing notifications disabled.");
prefs_set_notify_typing(FALSE);
} else {
@ -1427,7 +1441,6 @@ _cmd_set_notify(const char * const inp, struct cmd_help_t help)
} else { // remind
gint period = atoi(value);
prefs_set_notify_remind(period);
if (period == 0) {
cons_show("Message reminders disabled.");
@ -1436,61 +1449,61 @@ _cmd_set_notify(const char * const inp, struct cmd_help_t help)
} else {
cons_show("Message reminder period set to %d seconds.", period);
}
}
result = TRUE;
}
return TRUE;
} else {
cons_show("Usage: %s", help.usage);
return TRUE;
}
}
} else {
cons_show("Usage: %s", help.usage);
return TRUE;
}
g_strfreev(args);
return result;
}
static gboolean
_cmd_set_log(const char * const inp, struct cmd_help_t help)
{
char *subcmd, *value;
char inp_cpy[strlen(inp) + 1];
int intval;
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 2, 2, &num_args);
strcpy(inp_cpy, inp);
strtok(inp_cpy, " ");
subcmd = strtok(NULL, " ");
value = strtok(NULL, " ");
if (subcmd == NULL || value == NULL) {
if (args == NULL) {
cons_show("Usage: %s", help.usage);
return TRUE;
}
result = TRUE;
} else {
char *subcmd = args[0];
char *value = args[1];
int intval;
if (strcmp(subcmd, "maxsize") == 0) {
if (_strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX) == 0) {
prefs_set_max_log_size(intval);
cons_show("Log maxinum size set to %d bytes", intval);
}
} else {
cons_show("Usage: %s", help.usage);
}
/* TODO: make 'level' subcommand for debug level */
return TRUE;
result = TRUE;
}
g_strfreev(args);
return result;
}
static gboolean
_cmd_set_priority(const char * const inp, struct cmd_help_t help)
{
char *value;
char inp_cpy[strlen(inp) + 1];
int intval;
gboolean result = FALSE;
int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 1, &num_args);
strcpy(inp_cpy, inp);
strtok(inp_cpy, " ");
value = strtok(NULL, " ");
if (value == NULL) {
if (args == NULL) {
cons_show("Usage: %s", help.usage);
return TRUE;
}
result = TRUE;
} else {
char *value = args[0];
int intval;
if (_strtoi(value, &intval, -128, 127) == 0) {
char *status = jabber_get_status();
@ -1501,7 +1514,12 @@ _cmd_set_priority(const char * const inp, struct cmd_help_t help)
free(status);
cons_show("Priority set to %d.", intval);
}
return TRUE;
result = TRUE;
}
g_strfreev(args);
return result;
}
static gboolean
@ -1787,3 +1805,30 @@ _strtoi(char *str, int *saveptr, int min, int max)
return 0;
}
gchar **
_cmd_parse_args(const char * const inp, int min, int max, int *num)
{
char *copy = strdup(inp);
gchar **args = NULL;
g_strstrip(copy);
gchar **split = g_strsplit(copy, " ", 0);
*num = g_strv_length(split) - 1;
if ((*num < min) || (*num > max)) {
g_strfreev(split);
free(copy);
return NULL;
} else {
args = malloc((*num + 1) * sizeof(*args));
int i;
for (i = 0; i < *num; i++) {
args[i] = strdup(split[i+1]);
}
args[i] = NULL;
g_strfreev(split);
free(copy);
return args;
}
}