From 0d9c300bc47aa8a250cf63e233aa465ae584840c Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 28 Jul 2012 00:42:22 +0100 Subject: Added tinyurl module --- src/tinyurl.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/tinyurl.h (limited to 'src/tinyurl.h') diff --git a/src/tinyurl.h b/src/tinyurl.h new file mode 100644 index 00000000..8c0f198a --- /dev/null +++ b/src/tinyurl.h @@ -0,0 +1,24 @@ +/* + * tinyurl.h + * + * Copyright (C) 2012 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +void tinyurl_init(void); +char * tinyurl_get(char *url); -- cgit 1.4.1-2-gfad0 From a16a7171f2fe758ae40affe8a483532a036d9992 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 29 Jul 2012 01:12:39 +0100 Subject: Validate tinyurl --- src/command.c | 43 ++++++++++++++++--------------------------- src/tinyurl.c | 7 +++++++ src/tinyurl.h | 1 + 3 files changed, 24 insertions(+), 27 deletions(-) (limited to 'src/tinyurl.h') 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); -- cgit 1.4.1-2-gfad0