1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

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
This commit is contained in:
Will Song 2015-11-23 20:09:51 -06:00
parent a2f5e921f2
commit fa6a26c6fd
No known key found for this signature in database
GPG Key ID: F2B1A34F21A171E2
3 changed files with 69 additions and 0 deletions

View File

@ -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;

View File

@ -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)
{

View File

@ -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);