]> code.citadel.org Git - citadel.git/commitdiff
* download attachments too.
authorWilfried Göesgens <willi@citadel.org>
Thu, 13 Mar 2008 23:45:06 +0000 (23:45 +0000)
committerWilfried Göesgens <willi@citadel.org>
Thu, 13 Mar 2008 23:45:06 +0000 (23:45 +0000)
citadel/getmail.c

index ae2c02938d030c9366016494928f00a8300c1c29..d2bb7a3a8cc206c93d1445060e4dfe04ef43a883 100644 (file)
@@ -163,6 +163,36 @@ void sendcommand_die(void) {
 }
 
 
 }
 
 
+/*
+ * saves filelen bytes from file at pathname
+ */
+int save_buffer(void *file, size_t filelen, const char *pathname)
+{
+       size_t block = 0;
+       size_t bytes_written = 0;
+       FILE *fp;
+
+       fp = fopen(pathname, "w");
+       if (!fp) {
+               fprintf(stderr, "Cannot open '%s': %s\n", pathname, strerror(errno));
+               return 0;
+       }
+       do {
+               block = fwrite((char *)file + bytes_written, 1,
+                               filelen - bytes_written, fp);
+               bytes_written += block;
+       } while (errno == EINTR && bytes_written < filelen);
+       fclose(fp);
+
+       if (bytes_written < filelen) {
+               fprintf(stderr,"Trouble saving '%s': %s\n", pathname,
+                               strerror(errno));
+               return 0;
+       }
+       return 1;
+}
+
+
 /*
  * main
  */
 /*
  * main
  */
@@ -186,6 +216,7 @@ int main(int argc, char **argv)
        struct ctdlipcmessage *mret;
        char cret[SIZ];
        unsigned long *msgarr;
        struct ctdlipcmessage *mret;
        char cret[SIZ];
        unsigned long *msgarr;
+       struct parts *att;
 
        strcpy(ctdl_home_directory, DEFAULT_PORT);
 
 
        strcpy(ctdl_home_directory, DEFAULT_PORT);
 
@@ -255,6 +286,32 @@ int main(int argc, char **argv)
        fprintf(stderr, "%s: %s\n", "email", mret->email);
        fprintf(stderr, "%s: %s\n", "text", mret->text);
 
        fprintf(stderr, "%s: %s\n", "email", mret->email);
        fprintf(stderr, "%s: %s\n", "text", mret->text);
 
+       att = mret->attachments;
+
+       while (att != NULL){
+               void *attachment;
+               char tmp[PATH_MAX];
+               char buf[SIZ];
+
+               fprintf(stderr, "Attachment: [%s] %s\n", att->number, att->filename);
+               r = CtdlIPCAttachmentDownload(ipc, MessageToRetrieve, att->number, &attachment, NULL, buf);
+               printf("----\%s\n----\n", buf);
+               if (r / 100 != 2) {
+                       printf("%s\n", buf);
+               } else {
+                       size_t len;
+                       
+                       len = (size_t)extract_long(buf, 0);
+                       CtdlMakeTempFileName(tmp, sizeof tmp);
+                       strcat(tmp, att->filename);
+                       printf("Saving Attachment to %s", tmp);
+                       save_buffer(attachment, len, tmp);
+                       free(attachment);
+               }
+               att = att->next;
+
+       }
+
        ///if (
 
 
        ///if (