int _mutt_append_message (CONTEXT *dest, FILE *fpin, CONTEXT *src, HEADER *hdr, BODY *body, int flags, int chflags) { char buf[STRING]; MESSAGE *msg; int r; fseeko (fpin, hdr->offset, 0); if (fgets (buf, sizeof (buf), fpin) == NULL) return -1; if ((msg = mx_open_new_message (dest, hdr, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL) return -1; if (dest->magic == M_MBOX || dest->magic == M_MMDF) chflags |= CH_FROM | CH_FORCE_FROM; chflags |= (dest->magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE); r = _mutt_copy_message (msg->fp, fpin, hdr, body, flags, chflags); if (mx_commit_message (msg, dest) != 0) r = -1; #ifdef USE_NOTMUCH if (hdr && msg->commited_path && dest->magic == M_MAILDIR && src->magic == M_NOTMUCH) nm_update_filename(src, NULL, msg->commited_path, hdr); #endif mx_close_message (&msg); return r; }
/* returns 0 on success, -1 on error */ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr) { if (fp) { /* recv mode */ if(hdr && m->hdr && m->encoding != ENCBASE64 && m->encoding != ENCQUOTEDPRINTABLE && mutt_is_message_type(m->type, m->subtype)) { /* message type attachments are written to mail folders. */ char buf[HUGE_STRING]; HEADER *hn; CONTEXT ctx; MESSAGE *msg; int chflags = 0; int r = -1; hn = m->hdr; hn->msgno = hdr->msgno; /* required for MH/maildir */ hn->read = 1; fseeko (fp, m->offset, 0); if (fgets (buf, sizeof (buf), fp) == NULL) return -1; if (mx_open_mailbox(path, M_APPEND | M_QUIET, &ctx) == NULL) return -1; if ((msg = mx_open_new_message (&ctx, hn, is_from (buf, NULL, 0, NULL) ? 0 : M_ADD_FROM)) == NULL) { mx_close_mailbox(&ctx, NULL); return -1; } if (ctx.magic == M_MBOX || ctx.magic == M_MMDF) chflags = CH_FROM | CH_UPDATE_LEN; chflags |= (ctx.magic == M_MAILDIR ? CH_NOSTATUS : CH_UPDATE); if (_mutt_copy_message (msg->fp, fp, hn, hn->content, 0, chflags) == 0 && mx_commit_message (msg, &ctx) == 0) r = 0; else r = -1; mx_close_message (&msg); mx_close_mailbox (&ctx, NULL); return r; } else { /* In recv mode, extract from folder and decode */ STATE s; memset (&s, 0, sizeof (s)); s.flags |= M_CHARCONV; if ((s.fpout = mutt_save_attachment_open (path, flags)) == NULL) { mutt_perror ("fopen"); mutt_sleep (2); return (-1); } fseeko ((s.fpin = fp), m->offset, 0); mutt_decode_attachment (m, &s); if (fclose (s.fpout) != 0) { mutt_perror ("fclose"); mutt_sleep (2); return (-1); } } } else { /* In send mode, just copy file */ FILE *ofp, *nfp; if ((ofp = fopen (m->filename, "r")) == NULL) { mutt_perror ("fopen"); return (-1); } if ((nfp = mutt_save_attachment_open (path, flags)) == NULL) { mutt_perror ("fopen"); safe_fclose (&ofp); return (-1); } if (mutt_copy_stream (ofp, nfp) == -1) { mutt_error _("Write fault!"); safe_fclose (&ofp); safe_fclose (&nfp); return (-1); } safe_fclose (&ofp); safe_fclose (&nfp); } return 0; }
/* Fetch messages and save them in $spoolfile */ void pop_fetch_mail(void) { char buffer[LONG_STRING]; char msgbuf[SHORT_STRING]; char *url, *p; int i, delanswer, last = 0, msgs, bytes, rset = 0, ret; CONNECTION *conn; CONTEXT ctx; MESSAGE *msg = NULL; ACCOUNT acct; POP_DATA *pop_data; if (!PopHost) { mutt_error _("POP host is not defined."); return; } url = p = safe_calloc(strlen(PopHost) + 7, sizeof(char)); if (url_check_scheme(PopHost) == U_UNKNOWN) { strcpy(url, "pop://"); /* __STRCPY_CHECKED__ */ p = strchr(url, '\0'); } strcpy(p, PopHost); /* __STRCPY_CHECKED__ */ ret = pop_parse_path(url, &acct); safe_free(&url); if (ret) { mutt_error(_("%s is an invalid POP path"), PopHost); return; } conn = mutt_conn_find(NULL, &acct); if (!conn) return; pop_data = safe_calloc(1, sizeof(POP_DATA)); pop_data->conn = conn; if (pop_open_connection(pop_data) < 0) { mutt_socket_free(pop_data->conn); safe_free(&pop_data); return; } conn->data = pop_data; mutt_message _("Checking for new messages..."); /* find out how many messages are in the mailbox. */ strfcpy(buffer, "STAT\r\n", sizeof(buffer)); ret = pop_query(pop_data, buffer, sizeof(buffer)); if (ret == -1) goto fail; if (ret == -2) { mutt_error("%s", pop_data->err_msg); goto finish; } sscanf(buffer, "+OK %d %d", &msgs, &bytes); /* only get unread messages */ if ((msgs > 0) && option(OPTPOPLAST)) { strfcpy(buffer, "LAST\r\n", sizeof(buffer)); ret = pop_query(pop_data, buffer, sizeof(buffer)); if (ret == -1) goto fail; if (ret == 0) sscanf(buffer, "+OK %d", &last); } if (msgs <= last) { mutt_message _("No new mail in POP mailbox."); goto finish; } if (mx_open_mailbox(NONULL(Spoolfile), M_APPEND, &ctx) == NULL) goto finish; delanswer = query_quadoption(OPT_POPDELETE, _( "Delete messages from server?")); snprintf(msgbuf, sizeof(msgbuf), _( "Reading new messages (%d bytes)..."), bytes); mutt_message("%s", msgbuf); for (i = last + 1; i <= msgs; i++) { if ((msg = mx_open_new_message(&ctx, NULL, M_ADD_FROM)) == NULL) ret = -3; else { snprintf(buffer, sizeof(buffer), "RETR %d\r\n", i); ret = pop_fetch_data(pop_data, buffer, NULL, fetch_message, msg->fp); if (ret == -3) rset = 1; if ((ret == 0) && (mx_commit_message(msg, &ctx) != 0)) { rset = 1; ret = -3; } mx_close_message(&msg); } if ((ret == 0) && (delanswer == M_YES)) { /* delete the message on the server */ snprintf(buffer, sizeof(buffer), "DELE %d\r\n", i); ret = pop_query(pop_data, buffer, sizeof(buffer)); } if (ret == -1) { mx_close_mailbox(&ctx, NULL); goto fail; } if (ret == -2) { mutt_error("%s", pop_data->err_msg); break; } if (ret == -3) { mutt_error _("Error while writing mailbox!"); break; } mutt_message(_( "%s [%d of %d messages read]"), msgbuf, i - last, msgs - last); } mx_close_mailbox(&ctx, NULL); if (rset) { /* make sure no messages get deleted */ strfcpy(buffer, "RSET\r\n", sizeof(buffer)); if (pop_query(pop_data, buffer, sizeof(buffer)) == -1) goto fail; } finish: /* exit gracefully */ strfcpy(buffer, "QUIT\r\n", sizeof(buffer)); if (pop_query(pop_data, buffer, sizeof(buffer)) == -1) goto fail; mutt_socket_close(conn); safe_free(&pop_data); return; fail: mutt_error _("Server closed connection!"); mutt_socket_close(conn); safe_free(&pop_data); }
void mutt_fetchPopMail (void) { struct sockaddr_in sin; #if SIZEOF_LONG == 4 long n; #else int n; #endif struct hostent *he; char buffer[2048]; char msgbuf[SHORT_STRING]; int s, i, last = 0, msgs, bytes, err = 0; CONTEXT ctx; MESSAGE *msg = NULL; if (!PopHost) { mutt_error _("POP host is not defined."); return; } if (!PopUser) { mutt_error _("No POP username is defined."); return; } if (!getPass ()) return; s = socket (AF_INET, SOCK_STREAM, IPPROTO_IP); memset ((char *) &sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons (PopPort); if ((n = inet_addr (NONULL(PopHost))) == -1) { /* Must be a DNS name */ if ((he = gethostbyname (NONULL(PopHost))) == NULL) { mutt_error (_("Could not find address for host %s."), PopHost); return; } memcpy ((void *)&sin.sin_addr, *(he->h_addr_list), he->h_length); } else memcpy ((void *)&sin.sin_addr, (void *)&n, sizeof(n)); mutt_message (_("Connecting to %s"), inet_ntoa (sin.sin_addr)); if (connect (s, (struct sockaddr *) &sin, sizeof (struct sockaddr_in)) == -1) { mutt_perror ("connect"); return; } if (getLine (s, buffer, sizeof (buffer)) == -1) goto fail; if (mutt_strncmp (buffer, "+OK", 3) != 0) { mutt_remove_trailing_ws (buffer); mutt_error ("%s", buffer); goto finish; } snprintf (buffer, sizeof(buffer), "user %s\r\n", PopUser); write (s, buffer, mutt_strlen (buffer)); if (getLine (s, buffer, sizeof (buffer)) == -1) goto fail; if (mutt_strncmp (buffer, "+OK", 3) != 0) { mutt_remove_trailing_ws (buffer); mutt_error ("%s", buffer); goto finish; } snprintf (buffer, sizeof(buffer), "pass %s\r\n", NONULL(PopPass)); write (s, buffer, mutt_strlen (buffer)); if (getLine (s, buffer, sizeof (buffer)) == -1) goto fail; if (mutt_strncmp (buffer, "+OK", 3) != 0) { if(PopPass) memset(PopPass, 0, mutt_strlen(PopPass)); safe_free((void **) &PopPass); /* void the given password */ mutt_remove_trailing_ws (buffer); mutt_error ("%s", buffer[0] ? buffer : _("Server closed connection!")); goto finish; } /* find out how many messages are in the mailbox. */ write (s, "stat\r\n", 6); if (getLine (s, buffer, sizeof (buffer)) == -1) goto fail; if (mutt_strncmp (buffer, "+OK", 3) != 0) { mutt_remove_trailing_ws (buffer); mutt_error ("%s", buffer); goto finish; } sscanf (buffer, "+OK %d %d", &msgs, &bytes); if (msgs == 0) { mutt_message _("No new mail in POP mailbox."); goto finish; } if (mx_open_mailbox (NONULL(Spoolfile), M_APPEND, &ctx) == NULL) goto finish; /* only get unread messages */ if(option(OPTPOPLAST)) { write (s, "last\r\n", 6); if (getLine (s, buffer, sizeof (buffer)) == -1) goto fail; if (mutt_strncmp (buffer, "+OK", 3) == 0) sscanf (buffer, "+OK %d", &last); else /* ignore an error here and assume all messages are new */ last = 0; } snprintf (msgbuf, sizeof (msgbuf), msgs > 1 ? _("Reading new messages (%d bytes)...") : _("Reading new message (%d bytes)..."), bytes); mutt_message (msgbuf); for (i = last + 1 ; i <= msgs ; i++) { snprintf (buffer, sizeof(buffer), "retr %d\r\n", i); write (s, buffer, mutt_strlen (buffer)); if (getLine (s, buffer, sizeof (buffer)) == -1) { mx_fastclose_mailbox (&ctx); goto fail; } if (mutt_strncmp (buffer, "+OK", 3) != 0) { mutt_remove_trailing_ws (buffer); mutt_error ("%s", buffer); break; } if ((msg = mx_open_new_message (&ctx, NULL, M_ADD_FROM)) == NULL) { err = 1; break; } /* Now read the actual message. */ FOREVER { char *p; int chunk; if ((chunk = getLine (s, buffer, sizeof (buffer))) == -1) { mutt_error _("Error reading message!"); err = 1; break; } /* check to see if we got a full line */ if (buffer[chunk-2] == '\r' && buffer[chunk-1] == '\n') { if (mutt_strcmp(".\r\n", buffer) == 0) { /* end of message */ break; } /* change CRLF to just LF */ buffer[chunk-2] = '\n'; buffer[chunk-1] = 0; chunk--; /* see if the line was byte-stuffed */ if (buffer[0] == '.') { p = buffer + 1; chunk--; } else p = buffer; } else p = buffer; fwrite (p, 1, chunk, msg->fp); } if (mx_commit_message (msg, &ctx) != 0) { mutt_error _("Error while writing mailbox!"); err = 1; } mx_close_message (&msg); if (err) break; if (option (OPTPOPDELETE)) { /* delete the message on the server */ snprintf (buffer, sizeof(buffer), "dele %d\r\n", i); write (s, buffer, mutt_strlen (buffer)); /* eat the server response */ getLine (s, buffer, sizeof (buffer)); if (mutt_strncmp (buffer, "+OK", 3) != 0) { err = 1; mutt_remove_trailing_ws (buffer); mutt_error ("%s", buffer); break; } } if ( msgs > 1) mutt_message (_("%s [%d of %d messages read]"), msgbuf, i, msgs); else mutt_message (_("%s [%d message read]"), msgbuf, msgs); } if (msg) { if (mx_commit_message (msg, &ctx) != 0) err = 1; mx_close_message (&msg); } mx_close_mailbox (&ctx, NULL); if (err) { /* make sure no messages get deleted */ write (s, "rset\r\n", 6); getLine (s, buffer, sizeof (buffer)); /* snarf the response */ } finish: /* exit gracefully */ write (s, "quit\r\n", 6); getLine (s, buffer, sizeof (buffer)); /* snarf the response */ close (s); return; /* not reached */ fail: mutt_error _("Server closed connection!"); close (s); }
static int edit_one_message (CONTEXT *ctx, HEADER *cur) { char tmp[_POSIX_PATH_MAX]; char buff[STRING]; int omagic; int oerrno; int rc; unsigned short o_read; unsigned short o_old; int of, cf; CONTEXT tmpctx; MESSAGE *msg; FILE *fp = NULL; struct stat sb; time_t mtime = 0; mutt_mktemp (tmp, sizeof (tmp)); omagic = DefaultMagic; DefaultMagic = MUTT_MBOX; rc = (mx_open_mailbox (tmp, MUTT_NEWFOLDER, &tmpctx) == NULL) ? -1 : 0; DefaultMagic = omagic; if (rc == -1) { mutt_error (_("could not create temporary folder: %s"), strerror (errno)); return -1; } rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN | ((ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF) ? 0 : CH_NOSTATUS)); oerrno = errno; mx_close_mailbox (&tmpctx, NULL); if (rc == -1) { mutt_error (_("could not write temporary mail folder: %s"), strerror (oerrno)); goto bail; } if ((rc = stat (tmp, &sb)) == -1) { mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno)); goto bail; } /* * 2002-09-05 [email protected] * The file the user is going to edit is not a real mbox, so we need to * truncate the last newline in the temp file, which is logically part of * the message separator, and not the body of the message. If we fail to * remove it, the message will grow by one line each time the user edits * the message. */ if (sb.st_size != 0 && truncate (tmp, sb.st_size - 1) == -1) { mutt_error (_("could not truncate temporary mail folder: %s"), strerror (errno)); goto bail; } mtime = mutt_decrease_mtime (tmp, &sb); mutt_edit_file (NONULL(Editor), tmp); if ((rc = stat (tmp, &sb)) == -1) { mutt_error (_("Can't stat %s: %s"), tmp, strerror (errno)); goto bail; } if (sb.st_size == 0) { mutt_message (_("Message file is empty!")); rc = 1; goto bail; } if (sb.st_mtime == mtime) { mutt_message (_("Message not modified!")); rc = 1; goto bail; } if ((fp = fopen (tmp, "r")) == NULL) { rc = -1; mutt_error (_("Can't open message file: %s"), strerror (errno)); goto bail; } if (mx_open_mailbox (ctx->path, MUTT_APPEND, &tmpctx) == NULL) { rc = -1; /* L10N: %s is from strerror(errno) */ mutt_error (_("Can't append to folder: %s"), strerror (errno)); goto bail; } of = 0; cf = ((tmpctx.magic == MUTT_MBOX || tmpctx.magic == MUTT_MMDF) ? 0 : CH_NOSTATUS); if (fgets (buff, sizeof (buff), fp) && is_from (buff, NULL, 0, NULL)) { if (tmpctx.magic == MUTT_MBOX || tmpctx.magic == MUTT_MMDF) cf = CH_FROM | CH_FORCE_FROM; } else of = MUTT_ADD_FROM; /* * XXX - we have to play games with the message flags to avoid * problematic behavior with maildir folders. * */ o_read = cur->read; o_old = cur->old; cur->read = cur->old = 0; msg = mx_open_new_message (&tmpctx, cur, of); cur->read = o_read; cur->old = o_old; if (msg == NULL) { mutt_error (_("Can't append to folder: %s"), strerror (errno)); mx_close_mailbox (&tmpctx, NULL); goto bail; } if ((rc = mutt_copy_hdr (fp, msg->fp, 0, sb.st_size, CH_NOLEN | cf, NULL)) == 0) { fputc ('\n', msg->fp); rc = mutt_copy_stream (fp, msg->fp); } rc = mx_commit_message (msg, &tmpctx); mx_close_message (&tmpctx, &msg); mx_close_mailbox (&tmpctx, NULL); bail: if (fp) safe_fclose (&fp); if (rc >= 0) unlink (tmp); if (rc == 0) { mutt_set_flag (Context, cur, MUTT_DELETE, 1); mutt_set_flag (Context, cur, MUTT_PURGE, 1); mutt_set_flag (Context, cur, MUTT_READ, 1); if (option (OPTDELETEUNTAG)) mutt_set_flag (Context, cur, MUTT_TAG, 0); } else if (rc == -1) mutt_message (_("Error. Preserving temporary file: %s"), tmp); return rc; }