Browse Source

Add basic headers handling and body encoding

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

+ 38
- 1
src/fmail.c View File

@@ -24,6 +24,7 @@
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
#include <time.h>

/* Help and informations about the program. */
@@ -96,7 +97,7 @@ qp_encode_stream(FILE *in, FILE *out)
fprintf(out, "=%02X", c);
n += 3;
}
else if ( isprintc(c) ) {
else if ( isprint(c) ) {
fputc(c, out);
n += 1;
}
@@ -111,6 +112,37 @@ qp_encode_stream(FILE *in, FILE *out)
}
}

static void
print_date_header(FILE *out)
{
time_t timestamp;
struct tm *timestruct;
char buffer[32];
const char *rfc2822_format = "%a, %d %b %Y %H:%M:%S %z";

timestamp = time(NULL);
timestruct = localtime(&timestamp);
strftime(buffer, sizeof(buffer), rfc2822_format, timestruct);

fprintf(out, "Date: %s\r\n", buffer);
}

static void
process_given_headers(FILE *in, FILE *out)
{
char *buffer = NULL;
size_t len;
ssize_t n;

while ( (n = getline(&buffer, &len, in)) > 1 ) {
fwrite(buffer, 1, n - 1, out);
fprintf(out, "\r\n");
}

if ( buffer )
free(buffer);
}


/* Main function. */
@@ -145,5 +177,10 @@ main(int argc, char *argv[])
}
}

print_date_header(stdout);
process_given_headers(stdin, stdout);
fprintf(stdout, "\r\n");
qp_encode_stream(stdin, stdout);

return EXIT_SUCCESS;
}

Loading…
Cancel
Save