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