about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-11-25 00:43:26 +0000
committerJames Booth <boothj5@gmail.com>2015-11-25 00:43:26 +0000
commit01682a759480a057ed8223ccc9961831fa700208 (patch)
tree8febd577566bb16bb0f1d5133119bcf8d3fffe4d
parent9c8b137a515e90f55606177c75847493fa02f556 (diff)
parentc14d5b77e0da4cfbf43b6cb87e24383795a824b5 (diff)
downloadprofani-tty-01682a759480a057ed8223ccc9961831fa700208.tar.gz
Merge branch 'master' into notifications
-rw-r--r--CHANGELOG1
-rw-r--r--src/command/command.c14
-rw-r--r--src/command/commands.c95
-rw-r--r--src/command/commands.h1
4 files changed, 111 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 7a113907..cf0f3030 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -9,6 +9,7 @@
 - Last Activity (xep-0012)
 - Run command sequence with /script
 - Account startscript property
+- Add /export command
 
 0.4.7
 =====
diff --git a/src/command/command.c b/src/command/command.c
index 225d8d01..547b5275 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1782,6 +1782,20 @@ static struct cmd_t command_defs[] =
             "/script run myscript",
             "/script show somescript")
     },
+
+    { "/export",
+        cmd_export, parse_args, 1, 1, NULL,
+        CMD_NOTAGS
+        CMD_SYN(
+            "/export <filepath>")
+        CMD_DESC(
+            "Exports contacts to a csv file.")
+        CMD_ARGS(
+            { "<filepath>", "Path to the output file." })
+        CMD_EXAMPLES(
+            "/export /path/to/output.csv",
+            "/export ~/contacts.csv")
+    },
 };
 
 static Autocomplete commands_ac;
diff --git a/src/command/commands.c b/src/command/commands.c
index 3f8a0145..9bc7bdad 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -39,6 +39,10 @@
 #include <errno.h>
 #include <assert.h>
 #include <glib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "chat_session.h"
 #include "command/commands.h"
@@ -793,6 +797,97 @@ cmd_script(ProfWin *window, const char *const command, gchar **args)
     return TRUE;
 }
 
+/* escape a string into csv and write it to the file descriptor */
+static int
+_writecsv(int fd, const char *const str)
+{
+    if (!str) return 0;
+    size_t len = strlen(str);
+    char *s = malloc(2 * len * sizeof(char));
+    char *c = s;
+    int i = 0;
+    for (; i < strlen(str); i++) {
+        if (str[i] != '"') *c++ = str[i];
+        else { *c++ = '"'; *c++ = '"'; len++; }
+    }
+    if (-1 == write(fd, s, len)) {
+        cons_show("error: failed to write '%s' to the requested file: %s", s, strerror(errno));
+        return -1;
+    }
+    free(s);
+    return 0;
+}
+
+gboolean
+cmd_export(ProfWin *window, const char *const command, gchar **args)
+{
+    jabber_conn_status_t conn_status = jabber_get_connection_status();
+
+    if (conn_status != JABBER_CONNECTED) {
+        cons_show("You are not currently connected.");
+        cons_show("");
+        return TRUE;
+    } else {
+        GString *fname = g_string_new("");
+        GSList *list = NULL;
+        int fd;
+
+        /* deal with the ~ convention for $HOME */
+        if (args[0][0] == '~') {
+            fname = g_string_append(fname, getenv("HOME"));
+            fname = g_string_append(fname, args[0] + 1);
+        } else {
+            fname = g_string_append(fname, args[0]);
+        }
+
+        fd = open(fname->str, O_WRONLY | O_CREAT, 00600);
+        g_string_free(fname, TRUE);
+
+        if (-1 == fd) {
+            cons_show("error: cannot open %s: %s", args[0], strerror(errno));
+            cons_show("");
+            return TRUE;
+        }
+
+        if (-1 == write(fd, "jid,name\n", strlen("jid,name\n"))) goto write_error;
+
+        list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
+        if (list) {
+            GSList *curr = list;
+            while (curr){
+                PContact contact = curr->data;
+                const char *jid = p_contact_barejid(contact);
+                const char  *name = p_contact_name(contact);
+
+                /* write the data to the file */
+                if (-1 == write(fd, "\"", 1)) goto write_error;
+                if (-1 == _writecsv(fd, jid)) goto write_error;
+                if (-1 == write(fd, "\",\"", 3)) goto write_error;
+                if (-1 == _writecsv(fd, name)) goto write_error;
+                if (-1 == write(fd, "\"\n", 2)) goto write_error;
+
+                /* loop */
+                curr = g_slist_next(curr);
+            }
+            cons_show("Contacts exported successfully");
+            cons_show("");
+        } else {
+            cons_show("No contacts in roster.");
+            cons_show("");
+        }
+
+        g_slist_free(list);
+        close(fd);
+        return TRUE;
+write_error:
+        cons_show("error: write failed: %s", strerror(errno));
+        cons_show("");
+        g_slist_free(list);
+        close(fd);
+        return TRUE;
+    }
+}
+
 gboolean
 cmd_sub(ProfWin *window, const char *const command, gchar **args)
 {
diff --git a/src/command/commands.h b/src/command/commands.h
index 4256387e..501046d4 100644
--- a/src/command/commands.h
+++ b/src/command/commands.h
@@ -150,6 +150,7 @@ gboolean cmd_resource(ProfWin *window, const char *const command, gchar **args);
 gboolean cmd_inpblock(ProfWin *window, const char *const command, gchar **args);
 gboolean cmd_encwarn(ProfWin *window, const char *const command, gchar **args);
 gboolean cmd_script(ProfWin *window, const char *const command, gchar **args);
+gboolean cmd_export(ProfWin *window, const char *const command, gchar **args);
 
 gboolean cmd_form_field(ProfWin *window, char *tag, gchar **args);