Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add attachment infos to csv export #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions cmd-export-attachments.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@

#include "sigbak.h"

#define FLAG_EXPORT_ALL 0x1
#define FLAG_FILENAME_ID 0x2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FLAG_FILENAME_ID is also needed in cmd-export-messages.c now. I moved all of those FLAG_* to keep them together.

#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 @@ -118,10 +111,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 @@ -175,9 +171,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 @@ -31,6 +31,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 @@ -115,6 +116,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 @@ -146,6 +148,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 @@ -229,21 +250,39 @@ text_write_time_field(FILE *fp, const char *field, int64_t msec)
labs(tm->tm_gmtoff) % 3600 / 60);
}

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));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if FLAG_FILENAME_ID is given, we get the ID twice (one in the filename and one here).
I think I should change this to not repeat it here when it is already in the filename.
Do you think so, too?

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