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/configure.ac b/configure.ac index a01c0fc5..cff84ff4 100644 --- a/configure.ac +++ b/configure.ac @@ -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])]) diff --git a/install-all.sh b/install-all.sh index b5568fb1..16a9d2ee 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 } @@ -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() diff --git a/src/command.c b/src/command.c index 774c4264..3bddc895 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); @@ -76,6 +78,7 @@ static struct cmd_t commands[] = { { "/flash", _cmd_set_flash }, { "/prefs", _cmd_prefs }, { "/msg", _cmd_msg }, + { "/tiny", _cmd_tiny }, { "/online", _cmd_online }, { "/quit", _cmd_quit }, { "/ros", _cmd_ros }, @@ -86,7 +89,7 @@ static struct cmd_t commands[] = { { "/help", _cmd_help } }; -static const int num_cmds = 18; +static const int num_cmds = 19; gboolean process_input(char *inp) @@ -277,6 +280,31 @@ _cmd_msg(const char * const inp) return TRUE; } +static gboolean +_cmd_tiny(const char * const inp) +{ + 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 { + cons_bad_command(inp); + } + + 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/tinyurl.c b/src/tinyurl.c new file mode 100644 index 00000000..c492ff1d --- /dev/null +++ b/src/tinyurl.c @@ -0,0 +1,91 @@ +/* + * 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); +} + +gboolean +tinyurl_valid(char *url) +{ + return (g_str_has_prefix(url, "http://") || + g_str_has_prefix(url, "https://")); +} + +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..7c958d7a --- /dev/null +++ b/src/tinyurl.h @@ -0,0 +1,25 @@ +/* + * 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); +gboolean tinyurl_valid(char *url); +char * tinyurl_get(char *url); diff --git a/src/windows.c b/src/windows.c index a28bdbc6..c2d2294b 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 @@ -361,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.");