Browse Source

Create the fmail_ctx structure

tags/v0.1.0
Damien Goutte-Gattat 9 years ago
parent
commit
dc0ec32d20
1 changed files with 31 additions and 29 deletions
  1. +31
    -29
      src/fmail.c

+ 31
- 29
src/fmail.c View File

@@ -96,6 +96,14 @@ const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

typedef struct fmail_ctx
{
const char **attachments;
size_t att_count;
magic_t magic_ctx;
gpgme_ctx_t signing_ctx;
} fmail_ctx_t;

static char *
generate_boundary(char *buffer, size_t len)
{
@@ -229,16 +237,12 @@ read_headers(FILE *in, string_buffer_t *headers)
static void process_attachment(const char *, magic_t, FILE *);

static void
process_text_body(FILE *in,
char **attachments,
size_t att_count,
magic_t mgc_ctx,
FILE *out)
process_text_body(fmail_ctx_t *ctx, FILE *in, FILE *out)
{
char boundary[32];
int i;

if ( att_count > 0 ) {
if ( ctx->att_count > 0 ) {
generate_boundary(boundary, sizeof(boundary));
fprintf(out, "Content-Type: multipart/mixed;\r\n"
" boundary=\"%s\"\r\n"
@@ -253,11 +257,11 @@ process_text_body(FILE *in,
qp_encode_stream(in, out);
fprintf(out, "\r\n");

for ( i = 0; i < att_count; i++ ) {
for ( i = 0; i < ctx->att_count; i++ ) {
fprintf(out, "--%s\r\n", boundary);
process_attachment(attachments[i], mgc_ctx, out);
process_attachment(ctx->attachments[i], ctx->magic_ctx, out);

if ( i == att_count - 1 )
if ( i == ctx->att_count - 1 )
fprintf(out, "--%s--\r\n", boundary);
}
}
@@ -385,11 +389,9 @@ process_attachment(const char *filename, magic_t ctx, FILE *out)
int
main(int argc, char *argv[])
{
char c, **attachments;
char c;
string_buffer_t *headers;
gpgme_ctx_t sign_ctx;
magic_t mgc_ctx;
int att_count;
fmail_ctx_t ctx;

struct option options[] = {
{ "help", 0, NULL, 'h' },
@@ -409,10 +411,10 @@ main(int argc, char *argv[])
srand(time(NULL));

headers = sb_new(0);
sign_ctx = NULL;
mgc_ctx = initialize_magic();
attachments = NULL;
att_count = 0;
ctx.signing_ctx = NULL;
ctx.magic_ctx = initialize_magic();
ctx.attachments = NULL;
ctx.att_count = 0;

while ( (c = getopt_long(argc, argv, "hvsa:H:F:T:C:S:",
options, NULL)) != -1 ) {
@@ -430,13 +432,13 @@ main(int argc, char *argv[])
break;

case 's':
sign_ctx = initialize_gpgme();
ctx.signing_ctx = initialize_gpgme();
break;

case 'a':
if ( att_count % 10 == 0 )
attachments = xrealloc(attachments, att_count + 10);
attachments[att_count++] = optarg;
if ( ctx.att_count % 10 == 0 )
ctx.attachments = xrealloc(ctx.attachments, ctx.att_count + 10);
ctx.attachments[ctx.att_count++] = optarg;
break;

case 'H':
@@ -471,23 +473,23 @@ main(int argc, char *argv[])
/* Write all headers. */
fprintf(stdout, "%s", sb_get(headers));

if ( sign_ctx ) {
if ( ctx.signing_ctx ) {
FILE *tmp = tmpfile();

process_text_body(stdin, attachments, att_count, mgc_ctx, tmp);
process_text_body(&ctx, stdin, tmp);
fseek(tmp, 0, SEEK_SET);
sign_stream(sign_ctx, tmp, stdout);
sign_stream(ctx.signing_ctx, tmp, stdout);

fclose(tmp);
gpgme_release(sign_ctx);
gpgme_release(ctx.signing_ctx);
}
else
process_text_body(stdin, attachments, att_count, mgc_ctx, stdout);
process_text_body(&ctx, stdin, stdout);

if ( attachments )
free(attachments);
if ( ctx.attachments )
free(ctx.attachments);

magic_close(mgc_ctx);
magic_close(ctx.magic_ctx);

return EXIT_SUCCESS;
}

Loading…
Cancel
Save