about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-07-29 02:20:05 +0100
committerJames Booth <boothj5@gmail.com>2012-07-29 02:20:05 +0100
commita0cb58bb1cbc043c4c97c4409e491cc653503e0f (patch)
tree1926bc0e209e1d34163ae47351f43a2cf1483b56 /src
parentc738966d74899dc4aa81799690effb204db6675e (diff)
parentf58d8a2d22f3e5bdc5e67f1c4a3c4abdf5d0ad8c (diff)
downloadprofani-tty-a0cb58bb1cbc043c4c97c4409e491cc653503e0f.tar.gz
Merge branch 'tinyurl'
Conflicts:
	src/command.c
Diffstat (limited to 'src')
-rw-r--r--src/command.c30
-rw-r--r--src/profanity.c2
-rw-r--r--src/tinyurl.c91
-rw-r--r--src/tinyurl.h25
-rw-r--r--src/windows.c2
5 files changed, 149 insertions, 1 deletions
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)
@@ -278,6 +281,31 @@ _cmd_msg(const char * const inp)
 }
 
 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)
 {
     if (!win_close_win())
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 <boothj5@gmail.com>
+ * 
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <curl/curl.h>
+#include <curl/easy.h>
+#include <glib.h>
+
+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 <boothj5@gmail.com>
+ * 
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+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.");