about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-07-29 01:12:39 +0100
committerJames Booth <boothj5@gmail.com>2012-07-29 01:12:39 +0100
commita16a7171f2fe758ae40affe8a483532a036d9992 (patch)
tree0f2ec5976f996b3bcb21151012a17d128e8c3840 /src
parent45e2415b715756aa0f0df14ae310a5ee1d89862d (diff)
downloadprofani-tty-a16a7171f2fe758ae40affe8a483532a036d9992.tar.gz
Validate tinyurl
Diffstat (limited to 'src')
-rw-r--r--src/command.c43
-rw-r--r--src/tinyurl.c7
-rw-r--r--src/tinyurl.h1
3 files changed, 24 insertions, 27 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;
-
-    jabber_conn_status_t conn_status = jabber_connection_status();
-
-    if (conn_status != JABBER_CONNECTED) {
-        cons_show("You are not currently connected.");
+    char *url = strndup(inp+6, strlen(inp)-6);
+
+    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);