Skip to content

Commit

Permalink
add attachment infos to csv export
Browse files Browse the repository at this point in the history
resolves tbvdm#17
  • Loading branch information
R-Zwi committed Nov 26, 2024
1 parent 156119a commit e484b6c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
29 changes: 17 additions & 12 deletions cmd-export-attachments.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@

#include "sigbak.h"

#define FLAG_EXPORT_ALL 0x1
#define FLAG_FILENAME_ID 0x2
#define FLAG_MTIME_SENT 0x4
#define FLAG_MTIME_RECV 0x8

#define FLAG_MTIME_MASK (FLAG_MTIME_SENT | FLAG_MTIME_RECV)

static enum cmd_status cmd_export_attachments(int, char **);

const struct cmd_entry cmd_export_attachments_entry = {
Expand Down Expand Up @@ -119,10 +112,13 @@ create_unique_file(int dfd, const char *name)
return fp;
}

static FILE *
get_file(int dfd, struct sbk_attachment *att, int flags)
// get the name of an attachment file
// attention:
// this does not obey possible renames by create_unique_file()
// so it is better to use it with FLAG_FILENAME_ID which ensures a unique filename
char *
get_file_name(struct sbk_attachment *att, int flags)
{
FILE *fp;
struct tm *tm;
char *name, *tmp;
const char *ext;
Expand Down Expand Up @@ -176,9 +172,18 @@ get_file(int dfd, struct sbk_attachment *att, int flags)
free(name);
name = tmp;
}
return name;
}// get_file_name()

fp = create_unique_file(dfd, name);
free(name);
static FILE *
get_file(int dfd, struct sbk_attachment *att, int flags)
{
FILE *fp= 0;
char *name= get_file_name(att, flags);
if (name)
{ fp = create_unique_file(dfd, name);
free(name);
}
return fp;
}

Expand Down
59 changes: 49 additions & 10 deletions cmd-export-messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "sigbak.h"

static enum cmd_status cmd_export_messages(int, char **);
static char *get_attachment_field(struct sbk_attachment *, int);

const struct cmd_entry cmd_export_messages_entry = {
.name = "export-messages",
Expand Down Expand Up @@ -116,6 +117,7 @@ csv_write_message(FILE *fp, struct sbk_message *msg)
struct sbk_reaction *rct;
const char *addr;
int nattachments;
char *att_text;

addr = (msg->recipient->type == SBK_CONTACT) ?
msg->recipient->contact->phone : "group";
Expand Down Expand Up @@ -147,6 +149,25 @@ csv_write_message(FILE *fp, struct sbk_message *msg)
sbk_get_recipient_display_name(rct->recipient),
rct->emoji);

if (msg->attachments != NULL)
TAILQ_FOREACH(att, msg->attachments, entries)
{
att_text= get_attachment_field(att, FLAG_FILENAME_ID); // always add ID to ensure a unique name
if (att_text==NULL)
att_text= strdup("error: failed to get attachment field");

csv_write_record(fp,
msg->time_sent,
msg->time_recv,
msg->thread,
3,
0,
addr,
sbk_get_recipient_display_name(msg->recipient),
att_text);
free(att_text);
} // TAILQ_FOREACH

return 0;
}

Expand Down Expand Up @@ -242,21 +263,39 @@ text_write_time_field(FILE *fp, const char *field, int64_t msec)
#endif
}

static char
*get_attachment_field(struct sbk_attachment *att, int flags)
{
char *att_text= NULL, *att_fname;

att_fname= get_file_name(att, flags);
if (att_fname==NULL)
att_fname= strdup("no filename");

asprintf(&att_text, "%s (%s, %" PRIu64 " bytes, id %s)", att_fname,
(att->content_type != NULL) ?
att->content_type : "",
att->size,
sbk_attachment_id_to_string(att));
free(att_fname);

return att_text;
}

static void
text_write_attachment_field(FILE *fp, struct sbk_attachment *att)
{
fputs("Attachment: ", fp);
char *att_text;

if (att->filename == NULL || *att->filename == '\0')
fputs("no filename", fp);
fputs("Attachment: ", fp);

att_text= get_attachment_field(att, 0);
if (att_text==NULL)
fputs("error: failed to get attachment field", fp);
else
fprintf(fp, "%s", att->filename);

fprintf(fp, " (%s, %" PRIu64 " bytes, id %s)\n",
(att->content_type != NULL) ?
att->content_type : "",
att->size,
sbk_attachment_id_to_string(att));
{ fprintf(fp, "%s\n", att_text);
free(att_text);
}
}

static void
Expand Down
10 changes: 9 additions & 1 deletion sigbak.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
#define nitems(a) (sizeof (a) / sizeof (a)[0])
#endif

#define FLAG_EXPORT_ALL 0x1
#define FLAG_FILENAME_ID 0x2
#define FLAG_MTIME_SENT 0x4
#define FLAG_MTIME_RECV 0x8

#define FLAG_MTIME_MASK (FLAG_MTIME_SENT | FLAG_MTIME_RECV)


enum cmd_status {
CMD_OK,
CMD_ERROR,
Expand All @@ -42,5 +50,5 @@ int get_passphrase(const char *, char *, size_t);
int unveil_dirname(const char *, const char *);
void sanitise_filename(char *);
char *get_recipient_filename(struct sbk_recipient *, const char *);

char *get_file_name(struct sbk_attachment *, int);
#endif

0 comments on commit e484b6c

Please sign in to comment.