]> code.citadel.org Git - citadel.git/blobdiff - citadel/getmail.c
Removed the global variable 'home_specified'
[citadel.git] / citadel / getmail.c
index c526f7c05068478c7afd9a4d4eaf083a68677798..104921162b5e2c17fc65ecb181d7042ec4efd9a6 100644 (file)
@@ -126,6 +126,9 @@ void logoff(int e)
        cleanup(e);
 }
 
+static char *args[] =
+{"getmail", NULL};
+
 /*
  * Connect sendcommand to the Citadel server running on this computer.
  */
@@ -134,8 +137,6 @@ void np_attach_to_server(char *host, char *port)
        char buf[SIZ];
        char hostbuf[256] = "";
        char portbuf[256] = "";
-       char *args[] =
-       {"getmail", NULL};
        int r;
 
        fprintf(stderr, "Attaching to server...\n");
@@ -162,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
  */
@@ -170,7 +201,8 @@ int main(int argc, char **argv)
        int a, r, i;
        char cmd[5][SIZ];
        char buf[SIZ];
-
+       int MessageToRetrieve;
+       int MessageFound = 0;
        int relh=0;
        int home=0;
        int n=0;
@@ -184,6 +216,7 @@ int main(int argc, char **argv)
        struct ctdlipcmessage *mret;
        char cret[SIZ];
        unsigned long *msgarr;
+       struct parts *att;
 
        strcpy(ctdl_home_directory, DEFAULT_PORT);
 
@@ -198,7 +231,6 @@ int main(int argc, char **argv)
                        else
                                safestrncpy(relhome, &argv[a][2],
                                                        sizeof relhome);
-                       home_specified = 1;
                        home=1;
                } else {
 
@@ -220,8 +252,8 @@ int main(int argc, char **argv)
                        ctdl_home_directory);
        fflush(stderr);
 
-       alarm(5);
-       signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
+//     alarm(5);
+//     signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
        
        np_attach_to_server(UDS, ctdl_run_dir);
        fflush(stderr);
@@ -231,20 +263,65 @@ int main(int argc, char **argv)
        CtdlIPCGotoRoom(ipc, cmd[0], "", &Room, cret);
        fprintf(stderr, "%s\n", cret);
 
+       MessageToRetrieve = atol(cmd[1]);
+
        r = CtdlIPCGetMessages(ipc, 0, 0, NULL, &msgarr, buf);
-       for (i = 0; i < r ; i ++)
+       printf("Messages: ");
+       for (i = 0; msgarr[i] > 0 ; i ++)
        {
-               printf("Message: %ld\n", msgarr[i]);
+//             printf(" %ld ", msgarr[i]);
+               if (msgarr[i] == MessageToRetrieve)
+                       MessageFound = 1;
        }
+       if (!MessageFound)
+               printf("Message %d not found in the above list.", MessageToRetrieve);
+       printf("\n");
 
-
-       CtdlIPCGetSingleMessage(ipc, atol(cmd[1]) ,atol(cmd[1]),4, &mret, cret);
+       CtdlIPCGetSingleMessage(ipc,  MessageToRetrieve,0,4, &mret, cret);
        fprintf(stderr, "%s\n", cret);
        fprintf(stderr, "%s: %s\n", "path", mret->path);
+       fprintf(stderr, "%s: %s\n", "author", mret->author);
        fprintf(stderr, "%s: %s\n", "subject", mret->subject);
        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 (
+
+
        CtdlIPCQuit(ipc);
        exit (1);
+
+
+
+
+
+
        CtdlIPC_chat_send(ipc, cmd[4]);
        CtdlIPC_chat_recv(ipc, buf);
        fprintf(stderr, "%s\n", buf);