commit 408e8287b667c34daae6513a0815b649118ff65a
parent c718f6e6df0f7f9785dc5cab39906cf450b98452
Author: pancake <nopcode.org>
Date: Sat, 8 May 2010 22:24:42 +0200
* Implement dmcmailpath (but not yet used)
* Fix error message in dmc -m
* Handle FROM MAIL in dmc -m
* Fix feof handling in dmc -a
* Use #define SZ in pack.c
Diffstat:
TODO | | | 10 | +++++++--- |
dmc.c | | | 38 | +++++++++++++++++++++++++++++++++++--- |
pack.c | | | 46 | +++++++++++++++++++++++----------------------- |
3 files changed, 65 insertions(+), 29 deletions(-)
diff --git a/TODO b/TODO
@@ -2,15 +2,16 @@ TODO
----
* Make it more fail-fast (use exit, instead of free)
* how to reply a mail?
- - dmc -r [mail]
+ - dmc -r [mail] [addr]
-> runs dmc-filter -b < mail | sed -e 's,^,> ,'
* how to forward a mail?
- - dmc -f [mail]
+ - dmc -f [mail] [addr]
* Create a mail with attachments (dmc -m addr subject file1 file2 file3 ..)
* Attach file to given mail (not last one)
-* Define a list of 'subscribed' folders for IMAP
+* Define a list of 'subscribed' folders for IMAP ~/.dmc/box/.folders ?
* Pull specific folder (dmc pull [folder])
* getword() is implemented so MANY times.. MERGE!
+* implement dmc-dmc to walk thru local dirs
* pull methods...
@@ -29,6 +30,9 @@ TODO
- delete messages from server?
- limit number of messages to fetch? (we must remove old messages) dmclimit()?
+status of messages:
+ - read/unread/forwarded
+
Sync algorithm should look like this:
fetchdir ("~/.dmc/box/foo/in/")
diff --git a/dmc.c b/dmc.c
@@ -22,6 +22,36 @@ static void dmcstop();
static int dmcsend(const char *file);
static int fexist(const char *path);
+static char *dmcnamegen(const char *file) {
+ // return filename from mail
+ // Name is YYYYMMDDhhmm## -- name sort by date
+ // if no Date..use file timestamp
+ // well. we just need at the end.. a unique incremental ID
+ // interesting mails are the last ones in mail server
+ return NULL;
+}
+
+static const char *dmcmailpath(const char *name) {
+ static char path[256];
+ /* check ./%s */
+ if (fexist (name))
+ return name;
+ /* check DMCDIR/box/$acc/in/%s */
+ snprintf (path, sizeof (path), DMCDIR"/box/%s/in/%s", defacc, name);
+ if (fexist (path))
+ return path;
+ /* check DMCDIR/box/$acc/%s */
+ snprintf (path, sizeof (path), DMCDIR"/box/%s/%s", defacc, name);
+ if (fexist (path))
+ return path;
+ /* check DMCDIR/box/%s */
+ snprintf (path, sizeof (path), DMCDIR"/box/%s", name);
+ if (fexist (path))
+ return path;
+ /* not found */
+ return NULL;
+}
+
static void dmcinit() {
if (!fexist (DMCDIR))
if (mkdir (DMCDIR, DIRPERM) == -1) {
@@ -363,7 +393,7 @@ static int usage(const char *argv0, int lon) {
}
static int dmcmail(const char *addr, const char *subj) {
- const char *from = "todo@from.com";
+ const char *from = cfgget("MAIL=");
char file[128], line[128];
int fd;
snprintf (file, sizeof (file), DMCDIR"/box/%s/out/mail.XXXXXX", acc[0]);
@@ -382,7 +412,7 @@ static int dmcmail(const char *addr, const char *subj) {
fprintf (stderr, "Aborted\n");
unlink (file);
} else symlink (file, DMCDIR"/mail.last");
- } else fprintf (stderr, "Cannot create '%s'\n", line);
+ } else fprintf (stderr, "Cannot create '%s'\n", file);
return 0;
}
@@ -512,8 +542,10 @@ int main(int argc, char **argv) {
} else {
FILE *fd = fopen (DMCDIR"/addrbook", "r");
if (fd) {
- while (!feof (fd)) {
+ for (;;) {
fgets (line, sizeof (line), fd);
+ if (feof (fd))
+ break;
for (i=2; i<argc; i++) {
if (strstr (line, argv[i]))
printf ("%s", line);
diff --git a/pack.c b/pack.c
@@ -7,6 +7,7 @@
#include <string.h>
#include <unistd.h>
+#define SZ 1024
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
@@ -34,12 +35,12 @@ static int b64_decode(unsigned char in[4], unsigned char out[3]) {
static void mime_pack(char **files, int nfiles) {
FILE *fd = NULL;
- char b[1024], cmd[1024], *ptr = NULL, *ptr2 = NULL;
- unsigned char bd[1024];
+ char b[SZ], cmd[SZ], *ptr = NULL, *ptr2 = NULL;
+ unsigned char bd[SZ];
int header = 1, len, in, out, i;
- memset (b, '\0', 1024);
- while(fgets (b, 1023, stdin)) {
+ memset (b, '\0', SZ);
+ while(fgets (b, SZ-1, stdin)) {
if (header && b[0] == '\n') {
printf ("Content-Type: multipart/mixed; boundary=\"dmc-multipart\"\n\n"
"--dmc-multipart\n"
@@ -49,10 +50,10 @@ static void mime_pack(char **files, int nfiles) {
fputs (b, stdout);
}
for(i = 0; i < nfiles; i++) {
- snprintf (cmd, 1023, "file -iL \"%s\"", files[i]);
+ snprintf (cmd, SZ-1, "file -iL \"%s\"", files[i]);
if (!(fd=popen (cmd, "r")))
continue;
- fgets (b, 1023, fd);
+ fgets (b, SZ-1, fd);
pclose (fd);
if (!(ptr = strchr(b, ' ')))
continue;
@@ -66,14 +67,14 @@ static void mime_pack(char **files, int nfiles) {
printf ("Content-Disposition: attachment; filename=\"%s\"\n", ptr2);
if (strstr (ptr, "text")) {
printf("Content-Transfer-Encoding: quoted-printable\n\n");
- while (fgets(b, 1023, fd))
+ while (fgets(b, SZ-1, fd))
printf("%s", b);
} else {
puts ("Content-Transfer-Encoding: base64\n");
- while ((len=fread(b, 1, 57, fd))) {
- memset(bd,'\0',1024);
- for(in=out=0;in<len;in+=3,out+=4)
- b64_encode((unsigned char*)b+in,bd+out,len-in>3?3:len-in);
+ while ((len=fread (b, 1, 57, fd))) {
+ memset (bd, '\0', SZ);
+ for (in=out=0;in<len;in+=3,out+=4)
+ b64_encode ((unsigned char*)b+in,bd+out,len-in>3?3:len-in);
puts ((char *)bd);
}
}
@@ -84,22 +85,22 @@ static void mime_pack(char **files, int nfiles) {
static void mime_unpack (int xtr) {
FILE *fd = NULL;
- char b[1024], boundary[1024], encoding[1024], filename[1024], *ptr, *ptr2;
- unsigned char bd[1024];
+ char b[SZ], boundary[SZ], encoding[SZ], filename[SZ], *ptr, *ptr2;
+ unsigned char bd[SZ];
int entity = 0, dump = 0, empty = 0, len, in, out, i, n = 0;
boundary[0] = encoding[0] = filename[0] = '\0';
- memset (b, '\0', 1024);
- while (fgets(b, 1023, stdin)) {
+ memset (b, '\0', SZ);
+ while (fgets(b, SZ-1, stdin)) {
if (!memcmp(b, "--", 2)) {
if (boundary[0] && strstr(b, boundary) &&
!memcmp(b+strlen(b)-3, "--", 2)) {
entity = dump = empty = 0;
} else {
- strncpy(boundary, b+2, 1023);
+ strncpy(boundary, b+2, SZ-1);
if ((len = strlen(boundary)) > 0)
boundary[len-1] = '\0';
- if (fgets(b, 1023, stdin) && strstr(b, "Content-Type:")) {
+ if (fgets(b, SZ-1, stdin) && strstr(b, "Content-Type:")) {
entity = 1;
dump = empty = 0;
}
@@ -107,10 +108,10 @@ static void mime_unpack (int xtr) {
}
if (entity) {
if ((ptr = strstr(b, "Content-Transfer-Encoding:"))) {
- strncpy(encoding, ptr+26, 1023);
+ strncpy(encoding, ptr+26, SZ-1);
} else if ((ptr = strstr(b, "filename=")) &&
((ptr2 = strchr(ptr, '"')) || (ptr2 = strchr(ptr, '=')))) {
- strncpy(filename, ptr2+1, 1023);
+ strncpy(filename, ptr2+1, SZ-1);
if ((ptr = strchr(filename, '"')) ||
(ptr = strchr(filename, ' ')) ||
(ptr = strchr(filename, '\n')))
@@ -120,7 +121,7 @@ static void mime_unpack (int xtr) {
empty = 1;
} else if (b[0] == '\n') {
if (!empty && !filename[0])
- snprintf(filename, 1023, "mimepart-%i", n++);
+ snprintf(filename, SZ-1, "mimepart-%i", n++);
if (xtr && !dump && filename[0] && (fd = fopen(filename, "w"))) {
dump = 1;
continue;
@@ -130,7 +131,7 @@ static void mime_unpack (int xtr) {
} else boundary[0] = '\0';
if (dump) {
if (strstr(encoding, "base64")) {
- memset(bd,'\0',1024);
+ memset(bd,'\0',SZ);
if ((len = strlen(b)) > 0)
b[len-1] = '\0';
for(in=out=0;in<len-1;in+=4)
@@ -149,8 +150,7 @@ static void mime_unpack (int xtr) {
int main(int argc, char **argv) {
if (argc < 2) {
mime_pack (argv+1, argc-1);
- } else
- if (!strcmp(argv[1], "-h")) {
+ } else if (!strcmp(argv[1], "-h")) {
fprintf (stderr, "Usage: %s [-hlu | attachment1 attachment2...] < mail\n", argv[0]);
return 1;
} else if (!strcmp(argv[1], "-l"))