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,40 +834,45 @@ 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;
jabber_conn_status_t conn_status = jabber_get_connection_status(); int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 1, &num_args);
if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { if (args == NULL) {
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); cons_show("Usage: %s", help.usage);
result = TRUE; result = TRUE;
} else { } else {
char *user, *lower; jabber_conn_status_t conn_status = jabber_get_connection_status();
user = strndup(inp+9, strlen(inp)-9);
lower = g_utf8_strdown(user, -1);
status_bar_get_password(); if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) {
status_bar_refresh(); cons_show("You are either connected already, or a login is in process.");
char passwd[21]; result = TRUE;
inp_block(); } else {
inp_get_password(passwd); char *user = args[0];
inp_non_block(); char *lower = g_utf8_strdown(user, -1);
log_debug("Connecting as %s", lower); status_bar_get_password();
status_bar_refresh();
char passwd[21];
inp_block();
inp_get_password(passwd);
inp_non_block();
conn_status = jabber_connect(lower, passwd); log_debug("Connecting as %s", lower);
if (conn_status == JABBER_CONNECTING) {
cons_show("Connecting..."); conn_status = jabber_connect(lower, passwd);
log_debug("Connecting..."); if (conn_status == JABBER_CONNECTING) {
cons_show("Connecting...");
log_debug("Connecting...");
}
if (conn_status == JABBER_DISCONNECTED) {
cons_bad_show("Connection to server failed.");
log_debug("Connection using %s failed", lower);
}
result = TRUE;
} }
if (conn_status == JABBER_DISCONNECTED) {
cons_bad_show("Connection to server failed.");
log_debug("Connection using %s failed", lower);
}
result = TRUE;
} }
g_strfreev(args);
return result; return result;
} }
@ -875,51 +881,55 @@ 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;
jabber_conn_status_t conn_status = jabber_get_connection_status(); int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 2, &num_args);
if (conn_status != JABBER_CONNECTED) { if (args == NULL) {
cons_show("You are currently not connected.");
result = TRUE;
} else if (strlen(inp) < 6) {
cons_show("Usage: %s", help.usage); cons_show("Usage: %s", help.usage);
result = TRUE; result = TRUE;
} else { } else {
char *inp_cpy, *subcmd; jabber_conn_status_t conn_status = jabber_get_connection_status();
char *jid, *bare_jid;
inp_cpy = strndup(inp+5, strlen(inp)-5); if (conn_status != JABBER_CONNECTED) {
/* using strtok is bad idea */ cons_show("You are currently not connected.");
subcmd = strtok(inp_cpy, " "); result = TRUE;
jid = strtok(NULL, " "); } else if (strlen(inp) < 6) {
cons_show("Usage: %s", help.usage);
if (jid != NULL) { result = TRUE;
jid = strdup(jid);
} else { } else {
jid = win_get_recipient(); char *subcmd, *jid, *bare_jid;
subcmd = args[0];
jid = args[1];
if (jid != NULL) {
jid = strdup(jid);
} else {
jid = win_get_recipient();
}
bare_jid = strtok(jid, "/");
if (strcmp(subcmd, "add") == 0) {
jabber_subscription(bare_jid, PRESENCE_SUBSCRIBED);
cons_show("Accepted subscription for %s", bare_jid);
log_info("Accepted subscription for %s", bare_jid);
} else if (strcmp(subcmd, "del") == 0) {
jabber_subscription(bare_jid, PRESENCE_UNSUBSCRIBED);
cons_show("Deleted subscription for %s", bare_jid);
log_info("Deleted subscription for %s", bare_jid);
} else if (strcmp(subcmd, "req") == 0) {
jabber_subscription(bare_jid, PRESENCE_SUBSCRIBE);
cons_show("Sent subscription request to %s.", bare_jid);
log_info("Sent subscription request to %s.", bare_jid);
} else if (strcmp(subcmd, "show") == 0) {
/* TODO: not implemented yet */
}
free(jid);
result = TRUE;
} }
bare_jid = strtok(jid, "/");
if (strcmp(subcmd, "add") == 0) {
jabber_subscription(bare_jid, PRESENCE_SUBSCRIBED);
cons_show("Accepted subscription for %s", bare_jid);
log_info("Accepted subscription for %s", bare_jid);
} else if (strcmp(subcmd, "del") == 0) {
jabber_subscription(bare_jid, PRESENCE_UNSUBSCRIBED);
cons_show("Deleted subscription for %s", bare_jid);
log_info("Deleted subscription for %s", bare_jid);
} else if (strcmp(subcmd, "req") == 0) {
jabber_subscription(bare_jid, PRESENCE_SUBSCRIBE);
cons_show("Sent subscription request to %s.", bare_jid);
log_info("Sent subscription request to %s.", bare_jid);
} else if (strcmp(subcmd, "show") == 0) {
/* TODO: not implemented yet */
}
free(jid);
free(inp_cpy);
result = TRUE;
} }
g_strfreev(args);
return result; return result;
} }
@ -1208,79 +1218,96 @@ _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);
jabber_conn_status_t conn_status = jabber_get_connection_status(); if (args == NULL) {
cons_show("Usage: %s", help.usage);
if (conn_status != JABBER_CONNECTED) { result = TRUE;
cons_show("You are not currently connected.");
} else { } else {
// copy input char *usr = args[0];
char inp_cpy[strlen(inp) + 1];
strcpy(inp_cpy, inp);
// get user jabber_conn_status_t conn_status = jabber_get_connection_status();
strtok(inp_cpy, " ");
usr = strtok(NULL, " "); if (conn_status != JABBER_CONNECTED) {
if (usr != NULL) { cons_show("You are not currently connected.");
win_show_status(usr);
} else { } else {
cons_show("Usage: %s", help.usage); if (usr != NULL) {
win_show_status(usr);
} else {
cons_show("Usage: %s", help.usage);
}
} }
}
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;
char *nick = NULL; int num_args = 0;
gchar **args = _cmd_parse_args(inp, 1, 2, &num_args);
jabber_conn_status_t conn_status = jabber_get_connection_status(); if (args == NULL) {
cons_show("Usage: %s", help.usage);
if (conn_status != JABBER_CONNECTED) { result = TRUE;
cons_show("You are not currently connected.");
} else { } else {
// copy input char *room = args[0];
char inp_cpy[strlen(inp) + 1]; char *nick = NULL;
strcpy(inp_cpy, inp); if (num_args == 2) {
nick = args[1];
// 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();
char jid_cpy[strlen(jid) + 1];
strcpy(jid_cpy, jid);
nick = strdup(strtok(jid_cpy, "@"));
}
jabber_join(room, nick);
win_join_chat(room, nick);
} }
}
return TRUE; jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected.");
} else {
if (room == NULL) {
cons_show("Usage: %s", help.usage);
} else {
// if no nick, set to first part of jid
if (nick == NULL) {
const char *jid = jabber_get_jid();
char jid_cpy[strlen(jid) + 1];
strcpy(jid_cpy, jid);
nick = strdup(strtok(jid_cpy, "@"));
}
jabber_join(room, nick);
win_join_chat(room, nick);
}
}
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,130 +1399,127 @@ _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; // set message setting
} else { } else if (strcmp(kind, "message") == 0) {
// get value if (strcmp(value, "on") == 0) {
value = strndup(inp+8+strlen(kind)+1, strlen(inp)-(8+strlen(kind)+1)); cons_show("Message notifications enabled.");
prefs_set_notify_message(TRUE);
if (value != NULL) { } else if (strcmp(value, "off") == 0) {
cons_show("Message notifications disabled.");
// set message setting prefs_set_notify_message(FALSE);
if (strcmp(kind, "message") == 0) {
if (strcmp(inp, "/notify message on") == 0) {
cons_show("Message notifications enabled.");
prefs_set_notify_message(TRUE);
} else if (strcmp(inp, "/notify message off") == 0) {
cons_show("Message notifications disabled.");
prefs_set_notify_message(FALSE);
} else {
cons_show("Usage: /notify message on|off");
}
// set typing setting
} else if (strcmp(kind, "typing") == 0) {
if (strcmp(inp, "/notify typing on") == 0) {
cons_show("Typing notifications enabled.");
prefs_set_notify_typing(TRUE);
} else if (strcmp(inp, "/notify typing off") == 0) {
cons_show("Typing notifications disabled.");
prefs_set_notify_typing(FALSE);
} else {
cons_show("Usage: /notify typing on|off");
}
} else { // remind
gint period = atoi(value);
prefs_set_notify_remind(period);
if (period == 0) {
cons_show("Message reminders disabled.");
} else if (period == 1) {
cons_show("Message reminder period set to 1 second.");
} else {
cons_show("Message reminder period set to %d seconds.", period);
}
}
return TRUE;
} else { } else {
cons_show("Usage: %s", help.usage); cons_show("Usage: /notify message on|off");
return TRUE; }
// set typing setting
} else if (strcmp(kind, "typing") == 0) {
if (strcmp(value, "on") == 0) {
cons_show("Typing notifications enabled.");
prefs_set_notify_typing(TRUE);
} else if (strcmp(value, "off") == 0) {
cons_show("Typing notifications disabled.");
prefs_set_notify_typing(FALSE);
} else {
cons_show("Usage: /notify typing on|off");
}
} else { // remind
gint period = atoi(value);
prefs_set_notify_remind(period);
if (period == 0) {
cons_show("Message reminders disabled.");
} else if (period == 1) {
cons_show("Message reminder period set to 1 second.");
} else {
cons_show("Message reminder period set to %d seconds.", period);
} }
} }
} else {
cons_show("Usage: %s", help.usage); result = TRUE;
return TRUE;
} }
g_strfreev(args);
return result;
} }
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 */
return TRUE; /* TODO: make 'level' subcommand for debug level */
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();
prefs_set_priority((int)intval); prefs_set_priority((int)intval);
// update presence with new priority // update presence with new priority
jabber_update_presence(jabber_get_presence(), status); jabber_update_presence(jabber_get_presence(), status);
if (status != NULL) if (status != NULL)
free(status); free(status);
cons_show("Priority set to %d.", intval); cons_show("Priority set to %d.", intval);
}
result = TRUE;
} }
return 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;
}
}