From fa6a26c6fdb10831328d63efd35606ede3819b03 Mon Sep 17 00:00:00 2001 From: Will Song Date: Mon, 23 Nov 2015 20:09:51 -0600 Subject: [PATCH] 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 --- src/command/command.c | 14 +++++++++++ src/command/commands.c | 54 ++++++++++++++++++++++++++++++++++++++++++ src/command/commands.h | 1 + 3 files changed, 69 insertions(+) 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 ") + CMD_DESC( + "Exports contacts to a csv file.") + CMD_ARGS( + { "", "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 #include #include +#include +#include +#include +#include #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);