about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorWill Song <incertia9474@gmail.com>2015-11-23 20:09:51 -0600
committerWill Song <incertia9474@gmail.com>2015-11-23 20:09:51 -0600
commitfa6a26c6fdb10831328d63efd35606ede3819b03 (patch)
treeae319f2bbfc3b6235a3d6db905a25e6d72995ed9
parenta2f5e921f2e5fb4158aad14928882e694e5d8a85 (diff)
downloadprofani-tty-fa6a26c6fdb10831328d63efd35606ede3819b03.tar.gz
add feature in issue #585
this should only be temporary due the silly amounts of syscalls involved

ideally we would create a new escaped string and write that directly via
fputs
-rw-r--r--src/command/command.c14
-rw-r--r--src/command/commands.c54
-rw-r--r--src/command/commands.h1
3 files changed, 69 insertions, 0 deletions
diff --git a/src/command/command.c b/src/command/command.c
index bc2ee121..6a66d730 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1766,6 +1766,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.cxv",
+            "/export ~/contacts.csv")
+    },
 };
 
 static Autocomplete commands_ac;
diff --git a/src/command/commands.c b/src/command/commands.c
index c17dca43..c6d7986b 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,56 @@ cmd_script(ProfWin *window, const char *const command, gchar **args)
     return TRUE;
 }
 
+static void
+writecsv(int fd, const char *const str){
+    if(str){
+        for(int i = 0; i < strlen(str); i++){
+            if(str[i] != '"') write(fd, str + i, 1);
+            /* two quotes ("") escapes a single quote (") */
+            else write(fd, "\"\"", 2);
+        }
+    }
+}
+
+gboolean
+cmd_export(ProfWin *window, const char *const command, gchar **args)
+{
+    if(args[0]){
+        /* temporary, we SHOULD pass everything to an escape function (to escape out quotes)
+         * and then use fputs */
+        int fd = open(args[0], O_WRONLY | O_CREAT, 00600);
+        GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
+
+        write(fd, "jid,name\n", strlen("jid,name\n"));
+        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 */
+                write(fd, "\"", 1);
+                writecsv(fd, jid);
+                write(fd, "\",\"", 3);
+                writecsv(fd, name);
+                write(fd, "\"\n", 2);
+
+                /* loop */
+                curr = g_slist_next(curr);
+            }
+        } else {
+            cons_show("No contacts in roster.");
+        }
+
+        g_slist_free(list);
+        close(fd);
+        return TRUE;
+    } else {
+        return FALSE;
+    }
+}
+
 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);