From a16a7171f2fe758ae40affe8a483532a036d9992 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 29 Jul 2012 01:12:39 +0100 Subject: [PATCH] Validate tinyurl --- src/command.c | 41 +++++++++++++++-------------------------- src/tinyurl.c | 7 +++++++ src/tinyurl.h | 1 + 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/command.c b/src/command.c index fd9d4e9a..3bddc895 100644 --- a/src/command.c +++ b/src/command.c @@ -283,34 +283,23 @@ _cmd_msg(const char * const inp) static gboolean _cmd_tiny(const char * const inp) { - char *usr = NULL; - char *msg = NULL; + char *url = strndup(inp+6, strlen(inp)-6); - jabber_conn_status_t conn_status = jabber_connection_status(); - - if (conn_status != JABBER_CONNECTED) { - cons_show("You are not currently connected."); + if (!tinyurl_valid(url)) { + GString *error = g_string_new("/tiny, badly formed URL: "); + g_string_append(error, url); + cons_bad_show(error->str); + g_string_free(error, TRUE); + } else if (win_in_chat()) { + char *tiny = tinyurl_get(url); + char *recipient = win_get_recipient(); + jabber_send(tiny, recipient); + win_show_outgoing_msg("me", recipient, tiny); + free(recipient); + free(tiny); + free(url); } else { - // copy input - char inp_cpy[strlen(inp) + 1]; - strcpy(inp_cpy, inp); - - // get user - strtok(inp_cpy, " "); - usr = strtok(NULL, " "); - if ((usr != NULL) && (strlen(inp) > (6 + strlen(usr) + 1))) { - // get message - msg = strndup(inp+6+strlen(usr)+1, strlen(inp)-(6+strlen(usr)+1)); - if (msg != NULL) { - char *tinyurl = tinyurl_get(msg); - jabber_send(tinyurl, usr); - win_show_outgoing_msg("me", usr, tinyurl); - } else { - cons_show("Usage: /tiny user@host url"); - } - } else { - cons_show("Usage: /tiny user@host url"); - } + cons_bad_command(inp); } return TRUE; diff --git a/src/tinyurl.c b/src/tinyurl.c index 03e4b8f1..c492ff1d 100644 --- a/src/tinyurl.c +++ b/src/tinyurl.c @@ -41,6 +41,13 @@ tinyurl_init(void) curl_global_init(CURL_GLOBAL_ALL); } +gboolean +tinyurl_valid(char *url) +{ + return (g_str_has_prefix(url, "http://") || + g_str_has_prefix(url, "https://")); +} + char * tinyurl_get(char *url) { diff --git a/src/tinyurl.h b/src/tinyurl.h index 8c0f198a..7c958d7a 100644 --- a/src/tinyurl.h +++ b/src/tinyurl.h @@ -21,4 +21,5 @@ */ void tinyurl_init(void); +gboolean tinyurl_valid(char *url); char * tinyurl_get(char *url);