From 0d9c300bc47aa8a250cf63e233aa465ae584840c Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 28 Jul 2012 00:42:22 +0100 Subject: [PATCH 1/8] Added tinyurl module --- Makefile.am | 7 +++-- src/tinyurl.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/tinyurl.h | 24 +++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/tinyurl.c create mode 100644 src/tinyurl.h diff --git a/Makefile.am b/Makefile.am index 02582865..b7262ad5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,11 +6,12 @@ profanity_SOURCES = src/command.c src/contact.c src/history.c src/jabber.h \ src/contact_list.c src/input_win.c src/log.h src/profanity.c \ src/prof_history.c src/ui.h src/common.h src/ contact_list.h src/jabber.c \ src/main.c src/profanity.h src/prof_history.h src/util.c src/chat_log.c \ - src/chat_log.h + src/chat_log.h src/tinyurl.c src/tinyurl.h profanity_CFLAGS = -O3 -Werror -Wall -Wextra -Wno-unused-parameter \ -Wno-unused-but-set-variable -Wno-unused-result \ - -Wno-missing-field-initializers - -lstrophe -lxml2 -lexpat -lncurses $(DEPS_CFLAGS) $(DEPS_LIBS) -lresolv \ + -Wno-missing-field-initializers \ + -lstrophe -lxml2 -lexpat -lncurses -lcurl \ + $(DEPS_CFLAGS) $(DEPS_LIBS) -lresolv \ $(NOTIFY_CFLAGS) $(NOTIFY_LIBS) TESTS = tests/testsuite diff --git a/src/tinyurl.c b/src/tinyurl.c new file mode 100644 index 00000000..03e4b8f1 --- /dev/null +++ b/src/tinyurl.c @@ -0,0 +1,84 @@ +/* + * tinyurl.c + * + * 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 . + * + */ + +#include +#include + +#include +#include +#include + +struct curl_data_t +{ + char *buffer; + size_t size; +}; + +static size_t _data_callback(void *ptr, size_t size, size_t nmemb, void *data); + +void +tinyurl_init(void) +{ + curl_global_init(CURL_GLOBAL_ALL); +} + +char * +tinyurl_get(char *url) +{ + GString *full_url = g_string_new("http://tinyurl.com/api-create.php?url="); + g_string_append(full_url, url); + + CURL *handle = curl_easy_init(); + CURLcode result; + struct curl_data_t output; + output.buffer = NULL; + output.size = 0; + + curl_easy_setopt(handle, CURLOPT_URL, full_url->str); + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, _data_callback); + curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&output); + + result = curl_easy_perform(handle); + curl_easy_cleanup(handle); + + output.buffer[output.size++] = '\0'; + g_string_free(full_url, TRUE); + + return output.buffer; +} + +static size_t +_data_callback(void *ptr, size_t size, size_t nmemb, void *data) +{ + size_t realsize = size * nmemb; + struct curl_data_t *mem = (struct curl_data_t *) data; + mem->buffer = realloc(mem->buffer, mem->size + realsize + 1); + + if ( mem->buffer ) + { + memcpy( &( mem->buffer[ mem->size ] ), ptr, realsize ); + mem->size += realsize; + mem->buffer[ mem->size ] = 0; + } + + return realsize; +} 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); From ee6ac9be264db31736bde40af0320fa9542e885f Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 28 Jul 2012 00:49:53 +0100 Subject: [PATCH 2/8] Simple tinyurl test --- src/windows.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/windows.c b/src/windows.c index a28bdbc6..1d52fa64 100644 --- a/src/windows.c +++ b/src/windows.c @@ -34,6 +34,7 @@ #include "util.h" #include "contact.h" #include "preferences.h" +#include "tinyurl.h" #define CONS_WIN_TITLE "_cons" #define PAD_SIZE 200 @@ -517,6 +518,10 @@ _create_windows(void) wprintw(_cons_win, "\n"); _win_show_time(_cons_win); wprintw(_cons_win, "Type '/help' to get started.\n"); + + tinyurl_init(); + char *url = tinyurl_get("http://www.london2012.com/schedule-and-results/"); + cons_show(url); } prefresh(_cons_win, 0, 0, 1, 0, rows-3, cols-1); From ea5c1f0fa452a8ffc1009921f19d0b93d9fc891e Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 28 Jul 2012 01:36:08 +0100 Subject: [PATCH 3/8] Added tiny command --- src/command.c | 39 +++++++++++++++++++++++++++++++++++++++ src/profanity.c | 2 ++ src/windows.c | 5 +---- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/command.c b/src/command.c index 9ae60488..26fb68c8 100644 --- a/src/command.c +++ b/src/command.c @@ -33,6 +33,7 @@ #include "util.h" #include "preferences.h" #include "prof_autocomplete.h" +#include "tinyurl.h" static gboolean _handle_command(const char * const command, const char * const inp); @@ -43,6 +44,7 @@ static gboolean _cmd_who(const char * const inp); static gboolean _cmd_ros(const char * const inp); static gboolean _cmd_connect(const char * const inp); static gboolean _cmd_msg(const char * const inp); +static gboolean _cmd_tiny(const char * const inp); static gboolean _cmd_close(const char * const inp); static gboolean _cmd_set_beep(const char * const inp); static gboolean _cmd_set_notify(const char * const inp); @@ -77,6 +79,7 @@ static struct cmd_t commands[] = { { "/help", _cmd_help }, { "/prefs", _cmd_prefs }, { "/msg", _cmd_msg }, + { "/tiny", _cmd_tiny }, { "/online", _cmd_online }, { "/quit", _cmd_quit }, { "/ros", _cmd_ros }, @@ -277,6 +280,42 @@ _cmd_msg(const char * const inp) return TRUE; } +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."); + } 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"); + } + } + + return TRUE; +} + static gboolean _cmd_close(const char * const inp) { diff --git a/src/profanity.c b/src/profanity.c index 76663354..440f3b33 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -34,6 +34,7 @@ #include "command.h" #include "preferences.h" #include "contact_list.h" +#include "tinyurl.h" static void _profanity_shutdown(void); @@ -78,6 +79,7 @@ profanity_init(const int disable_tls) jabber_init(disable_tls); command_init(); contact_list_init(); + tinyurl_init(); atexit(_profanity_shutdown); } diff --git a/src/windows.c b/src/windows.c index 1d52fa64..c2d2294b 100644 --- a/src/windows.c +++ b/src/windows.c @@ -362,6 +362,7 @@ cons_help(void) cons_show("/prefs : Show current UI preferences."); cons_show("/connect user@host : Login to jabber."); cons_show("/msg user@host mesg : Send mesg to user."); + cons_show("/tiny user@host url : Send url as tinyurl"); cons_show("/close : Close a chat window."); cons_show("/who : Find out who is online."); cons_show("/ros : List all contacts."); @@ -518,10 +519,6 @@ _create_windows(void) wprintw(_cons_win, "\n"); _win_show_time(_cons_win); wprintw(_cons_win, "Type '/help' to get started.\n"); - - tinyurl_init(); - char *url = tinyurl_get("http://www.london2012.com/schedule-and-results/"); - cons_show(url); } prefresh(_cons_win, 0, 0, 1, 0, rows-3, cols-1); From 5b769a3ff0c8cdf31d576adb5c3e49278c5bc933 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 29 Jul 2012 00:29:37 +0100 Subject: [PATCH 4/8] Updated configure.ac for libcurl --- configure.ac | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index eeac58bb..cff84ff4 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([profanity], [0.1.2], [boothj5web@gmail.com]) +AC_INIT([profanity], [0.1.3], [boothj5web@gmail.com]) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([src/config.h]) AC_CONFIG_AUX_DIR([build-aux]) @@ -26,6 +26,8 @@ AC_CHECK_LIB([strophe], [main], [], [AC_MSG_ERROR([libstrophe is required for profanity])]) AC_CHECK_LIB([glib-2.0], [main], [], [AC_MSG_ERROR([glib-2.0 is required for profanity])]) +AC_CHECK_LIB([curl], [main], [], + [AC_MSG_ERROR([libcurl is required for profanity])]) AC_CHECK_LIB([notify], [main], [], [AC_MSG_NOTICE([libnotify not found])]) AC_CHECK_LIB([headunit], [main], [], @@ -34,7 +36,7 @@ AC_CHECK_LIB([headunit], [main], [], # Checks for header files. AC_CHECK_HEADERS([stdlib.h string.h]) -PKG_CHECK_MODULES([DEPS], [openssl glib-2.0]) +PKG_CHECK_MODULES([DEPS], [openssl glib-2.0 libcurl]) PKG_CHECK_MODULES([NOTIFY], [libnotify], [], [AC_MSG_NOTICE([libnotify module not found])]) From 45e2415b715756aa0f0df14ae310a5ee1d89862d Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 29 Jul 2012 00:33:10 +0100 Subject: [PATCH 5/8] Fixed num commands --- src/command.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/command.c b/src/command.c index 26fb68c8..fd9d4e9a 100644 --- a/src/command.c +++ b/src/command.c @@ -76,7 +76,6 @@ static struct cmd_t commands[] = { { "/connect", _cmd_connect }, { "/dnd", _cmd_dnd }, { "/flash", _cmd_set_flash }, - { "/help", _cmd_help }, { "/prefs", _cmd_prefs }, { "/msg", _cmd_msg }, { "/tiny", _cmd_tiny }, @@ -87,9 +86,10 @@ static struct cmd_t commands[] = { { "/chlog", _cmd_set_chlog }, { "/who", _cmd_who }, { "/xa", _cmd_xa }, + { "/help", _cmd_help } }; -static const int num_cmds = 17; +static const int num_cmds = 19; gboolean process_input(char *inp) From a16a7171f2fe758ae40affe8a483532a036d9992 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 29 Jul 2012 01:12:39 +0100 Subject: [PATCH 6/8] 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); From 85a564a43799832776263966c3ca2caf468dc7a5 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 29 Jul 2012 01:42:40 +0100 Subject: [PATCH 7/8] Added libcurl3-dev to ubuntu dependencies --- install-all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install-all.sh b/install-all.sh index b5568fb1..df57a062 100755 --- a/install-all.sh +++ b/install-all.sh @@ -10,7 +10,7 @@ ubuntu_deps() echo echo Profanity installer... installing dependencies echo - sudo apt-get -y install g++ autoconf libssl-dev libexpat1-dev libncurses5-dev libxml2-dev libglib2.0-dev libnotify-dev + sudo apt-get -y install g++ autoconf libssl-dev libexpat1-dev libncurses5-dev libxml2-dev libglib2.0-dev libnotify-dev libcurl3-dev } From f58d8a2d22f3e5bdc5e67f1c4a3c4abdf5d0ad8c Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 29 Jul 2012 02:09:20 +0100 Subject: [PATCH 8/8] Added libcurl-devel to Fedora dependencies --- install-all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install-all.sh b/install-all.sh index df57a062..16a9d2ee 100755 --- a/install-all.sh +++ b/install-all.sh @@ -22,7 +22,7 @@ fedora_deps() ARCH=`arch` - sudo yum -y install gcc gcc-c++ autoconf automake openssl-devel.$ARCH expat-devel.$ARCH ncurses-devel.$ARCH libxml2-devel.$ARCH glib2-devel.$ARCH libnotify-devel.$ARCH + sudo yum -y install gcc gcc-c++ autoconf automake openssl-devel.$ARCH expat-devel.$ARCH ncurses-devel.$ARCH libxml2-devel.$ARCH glib2-devel.$ARCH libnotify-devel.$ARCH libcurl-devel.$ARCH } install_head_unit()