static int _mh_prop_read_stream (mu_header_t *phdr, mu_stream_t stream) { int rc; mu_stream_t flt; const char *argv[4]; mu_off_t size; size_t total; char *blurb; rc = mu_stream_size (stream, &size); if (rc) return rc; argv[0] = "INLINE-COMMENT"; argv[1] = "#"; argv[2] = "-r"; argv[3] = NULL; rc = mu_filter_create_args (&flt, stream, argv[0], 3, argv, MU_FILTER_DECODE, MU_STREAM_READ); if (rc) { mu_error (_("cannot open filter stream: %s"), mu_strerror (rc)); return rc; } blurb = malloc (size + 1); if (!blurb) { mu_stream_destroy (&flt); return ENOMEM; } total = 0; while (1) { size_t n; rc = mu_stream_read (flt, blurb + total, size - total, &n); if (rc) break; if (n == 0) break; total += n; } mu_stream_destroy (&flt); if (rc) { free (blurb); return rc; } rc = mu_header_create (phdr, blurb, total); free (blurb); return rc; }
static int pop_create_header (struct _pop3_message *mpm) { int status; mu_header_t header = NULL; status = mu_header_create (&header, NULL, 0); if (status) return status; mu_header_set_fill (header, pop_header_fill, mpm); mu_message_set_header (mpm->message, header, mpm); return 0; }
int mu_message_encapsulate (mu_message_t msg, mu_message_t *newmsg, mu_mime_io_buffer_t info) { mu_stream_t istream, ostream; int ret = 0; mu_message_t tmsg = NULL; if (newmsg == NULL) return MU_ERR_OUT_PTR_NULL; if (msg == NULL) { mu_header_t hdr; ret = mu_message_create (&tmsg, NULL); if (ret) return ret; msg = tmsg; #define MSG822_HEADER "Content-Type: message/rfc822\n" \ "Content-Transfer-Encoding: 7bit\n\n" if ((ret = mu_header_create (&hdr, MSG822_HEADER, sizeof (MSG822_HEADER) - 1)) == 0) ret = mu_message_set_header (msg, hdr, NULL); #undef MSG822_HEADER if (ret) { mu_message_destroy (&msg, NULL); return ret; } } if ((ret = _attachment_setup (&info, msg, &ostream)) != 0) { mu_message_destroy (&tmsg, NULL); return ret; } info->msg = msg; if (ret == 0 && (ret = mu_message_get_streamref (msg, &istream)) == 0) { mu_stream_seek (istream, 0, MU_SEEK_SET, NULL); ret = mu_stream_copy (ostream, istream, 0, NULL); mu_stream_destroy (&istream); } if (ret == 0) *newmsg = info->msg; mu_stream_destroy (&ostream); _attachment_free (info, ret && ret != EAGAIN); return ret; }
static int load_file (const char *name) { struct stat st; size_t nread; char *buf; FILE *fp; int status; if (stat (name, &st)) { mu_error ("cannot stat %s: %s", name, mu_strerror (errno)); return 1; } buf = malloc (st.st_size + 2); if (!buf) { mu_error ("not enough memory"); return 1; } fp = fopen (name, "r"); if (!fp) { mu_error ("cannot open file %s: %s", name, mu_strerror (errno)); free (buf); return 1; } nread = fread (buf, 1, st.st_size, fp); fclose (fp); if (nread != st.st_size) { mu_error ("short read on file %s", name); free (buf); return 1; } buf[st.st_size] = '\n'; buf[st.st_size+1] = 0; status = mu_header_create (&header, buf, st.st_size + 1, NULL); free (buf); if (status) { mu_error ("cannot create header: %s", mu_strerror (status)); return 1; } return 0; }
static int _mh_prop_setval (struct _mu_property *prop, const char *key, const char *val, int overwrite) { struct mu_mh_prop *mhprop = prop->_prop_init_data; mu_header_t header = prop->_prop_data; if (!header) { int rc; if ((rc = mu_header_create (&header, NULL, 0)) != 0) { mu_error (_("cannot create context %s: %s"), mhprop->filename, mu_strerror (rc)); return 1; } prop->_prop_data = header; } return mu_header_set_value (header, key, val, overwrite); }
static int _mh_prop_fill (struct _mu_property *prop) { struct mu_mh_prop *mhprop = prop->_prop_init_data; int rc; mu_stream_t stream; mu_header_t header; rc = mu_file_stream_create (&stream, mhprop->filename, MU_STREAM_READ); if (rc) { if ((rc = mu_header_create (&header, NULL, 0)) != 0) mu_error (_("cannot create context %s: %s"), mhprop->filename, mu_strerror (rc)); } else { rc = _mh_prop_read_stream (&header, stream); mu_stream_unref (stream); } if (rc == 0) prop->_prop_data = header; return rc; }
int main (int argc, char **argv) { int c; char buf[512]; char **prevv; int prevc = 0; interactive = isatty (0); while ((c = getopt (argc, argv, "f:h")) != EOF) { switch (c) { case 'f': file = optarg; break; case 'h': printf ("usage: header [-f file]\n"); exit (0); default: exit (1); } } if (file) { if (load_file (file)) exit (1); } else { int status = mu_header_create (&header, NULL, 0, NULL); if (status) { mu_error ("cannot create header: %s", mu_strerror (status)); exit (1); } } while (prompt(0), fgets(buf, sizeof buf, stdin)) { int c; char **v; int status; line_num++; status = mu_argcv_get (buf, NULL, "#", &c, &v); if (status) { mu_error ("%u: cannot parse: %s", line_num, mu_strerror (status)); continue; } if (c == 0) { if (prevc) docmd (prevc, prevv); else mu_argcv_free (c, v); } else { docmd (c, v); mu_argcv_free (prevc, prevv); prevc = c; prevv = v; } } exit (0); }
/* Build a mime response message from original message MSG. TEXT is the message text. */ static int build_mime (mu_sieve_machine_t mach, mu_list_t tags, mu_mime_t *pmime, mu_message_t msg, const char *text) { mu_mime_t mime = NULL; mu_message_t newmsg; mu_stream_t stream, input; mu_header_t hdr; mu_body_t body; const char *header = "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n" "Content-Transfer-Encoding: 8bit\n\n"; int rc; mu_mime_create (&mime, NULL, 0); mu_message_create (&newmsg, NULL); mu_message_get_body (newmsg, &body); if ((rc = mu_static_memory_stream_create (&input, text, strlen (text)))) { mu_sieve_error (mach, _("cannot create temporary stream: %s"), mu_strerror (rc)); mu_mime_destroy (&mime); mu_message_destroy (&newmsg, NULL); return 1; } if (mu_sieve_tag_lookup (tags, "mime", NULL)) { mu_stream_t fstr; rc = mu_filter_create (&fstr, input, "base64", MU_FILTER_ENCODE, MU_STREAM_READ); mu_stream_unref (input); if (rc == 0) { header = "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n" "Content-Transfer-Encoding: base64\n\n"; input = fstr; } } rc = mu_body_get_streamref (body, &stream); if (rc) { mu_sieve_error (mach, _("cannot get input body stream: %s"), mu_strerror (rc)); mu_mime_destroy (&mime); mu_message_destroy (&newmsg, NULL); mu_stream_destroy (&input); return 1; } rc = mu_stream_copy (stream, input, 0, NULL); if (rc) { mu_sieve_error (mach, _("stream copy failed: %s"), mu_strerror (rc)); mu_mime_destroy (&mime); mu_message_destroy (&newmsg, NULL); mu_stream_destroy (&input); mu_stream_destroy (&stream); return 1; } mu_stream_destroy (&input); mu_header_create (&hdr, header, strlen (header)); mu_message_set_header (newmsg, hdr, NULL); mu_mime_add_part (mime, newmsg); mu_message_unref (newmsg); *pmime = mime; return 0; }
int mu_message_create_attachment (const char *content_type, const char *encoding, const char *filename, mu_message_t *newmsg) { mu_header_t hdr; mu_body_t body; mu_stream_t fstream = NULL, tstream = NULL; char *header = NULL, *name = NULL, *fname = NULL; int ret; if (newmsg == NULL) return MU_ERR_OUT_PTR_NULL; if (filename == NULL) return EINVAL; if ((ret = mu_message_create (newmsg, NULL)) == 0) { if (content_type == NULL) content_type = "text/plain"; if (encoding == NULL) encoding = "7bit"; if ((fname = strdup (filename)) != NULL) { name = strrchr (fname, '/'); if (name) name++; else name = fname; ret = mu_asprintf (&header, "Content-Type: %s; name=%s\n" "Content-Transfer-Encoding: %s\n" "Content-Disposition: attachment; filename=%s\n\n", content_type, name, encoding, name); if (ret == 0) { if ((ret = mu_header_create (&hdr, header, strlen (header))) == 0) { mu_stream_t bstr; mu_message_get_body (*newmsg, &body); mu_body_get_streamref (body, &bstr); if ((ret = mu_file_stream_create (&fstream, filename, MU_STREAM_READ)) == 0) { if ((ret = mu_filter_create (&tstream, fstream, encoding, MU_FILTER_ENCODE, MU_STREAM_READ)) == 0) { mu_stream_copy (bstr, tstream, 0, NULL); mu_stream_unref (tstream); mu_message_set_header (*newmsg, hdr, NULL); } } mu_stream_unref (bstr); free (header); } } } } if (ret) { if (*newmsg) mu_message_destroy (newmsg, NULL); if (hdr) mu_header_destroy (&hdr); if (fstream) mu_stream_destroy (&fstream); if (fname) free (fname); } return ret; }
static int nntp_mailbox_get_message (mu_mailbox_t mbox, size_t msgno, mu_message_t *pmsg) { m_nntp_t m_nntp = mbox->data; msg_nntp_t msg_nntp; mu_message_t msg = NULL; int status; size_t i; /* Sanity. */ if (pmsg == NULL) return MU_ERR_OUT_PTR_NULL; msgno--; mu_monitor_rdlock (mbox->monitor); /* See if we have already this message. */ for (i = 0; i < m_nntp->messages_count; i++) { if (m_nntp->messages[i]) { if (m_nntp->messages[i]->msgno == msgno + m_nntp->low) { *pmsg = m_nntp->messages[i]->message; mu_monitor_unlock (mbox->monitor); return 0; } } } mu_monitor_unlock (mbox->monitor); msg_nntp = calloc (1, sizeof (*msg_nntp)); if (msg_nntp == NULL) return ENOMEM; /* Back pointer. */ msg_nntp->m_nntp = m_nntp; msg_nntp->msgno = msgno + m_nntp->low; /* Create the message. */ { mu_stream_t stream = NULL; if ((status = mu_message_create (&msg, msg_nntp)) != 0 || (status = mu_stream_create (&stream, mbox->flags, msg)) != 0) { mu_stream_destroy (&stream, msg); mu_message_destroy (&msg, msg_nntp); free (msg_nntp); return status; } /* Help for the readline()s */ mu_stream_set_read (stream, nntp_message_read, msg); mu_stream_set_get_transport2 (stream, nntp_message_get_transport2, msg); mu_message_set_stream (msg, stream, msg_nntp); mu_message_set_size (msg, nntp_message_size, msg_nntp); } /* Create the header. */ { mu_header_t header = NULL; if ((status = mu_header_create (&header, NULL, 0, msg)) != 0) { mu_message_destroy (&msg, msg_nntp); free (msg_nntp); return status; } mu_header_set_fill (header, nntp_header_fill, msg); mu_message_set_header (msg, header, msg_nntp); } /* Create the body and its stream. */ { mu_body_t body = NULL; mu_stream_t stream = NULL; if ((status = mu_body_create (&body, msg)) != 0 || (status = mu_stream_create (&stream, mbox->flags, body)) != 0) { mu_body_destroy (&body, msg); mu_stream_destroy (&stream, body); mu_message_destroy (&msg, msg_nntp); free (msg_nntp); return status; } /* Helps for the readline()s */ mu_stream_set_read (stream, nntp_body_read, body); mu_stream_set_get_transport2 (stream, nntp_body_get_transport2, body); mu_body_set_size (body, nntp_body_size, msg); mu_body_set_lines (body, nntp_body_lines, msg); mu_body_set_stream (body, stream, msg); mu_message_set_body (msg, body, msg_nntp); } /* Set the UID on the message. */ mu_message_set_uid (msg, nntp_message_uid, msg_nntp); /* Add it to the list. */ mu_monitor_wrlock (mbox->monitor); { msg_nntp_t *m ; m = realloc (m_nntp->messages, (m_nntp->messages_count + 1)*sizeof (*m)); if (m == NULL) { mu_message_destroy (&msg, msg_nntp); free (msg_nntp); mu_monitor_unlock (mbox->monitor); return ENOMEM; } m_nntp->messages = m; m_nntp->messages[m_nntp->messages_count] = msg_nntp; m_nntp->messages_count++; } mu_monitor_unlock (mbox->monitor); /* Save The message pointer. */ mu_message_set_mailbox (msg, mbox, msg_nntp); *pmsg = msg_nntp->message = msg; return 0; }
static int new_message (mu_mailbox_t mailbox, mbox_message_t mum, mu_message_t *pmsg) { int status; mu_message_t msg; /* Get an empty message struct. */ status = mu_message_create (&msg, mum); if (status != 0) return status; /* Set the header. */ { mu_header_t header = NULL; status = mu_header_create (&header, NULL, 0, msg); if (status != 0) { mu_message_destroy (&msg, mum); return status; } mu_header_set_fill (header, mbox_header_fill, msg); mu_message_set_header (msg, header, mum); } /* Set the attribute. */ { mu_attribute_t attribute; status = mu_attribute_create (&attribute, msg); if (status != 0) { mu_message_destroy (&msg, mum); return status; } mu_attribute_set_get_flags (attribute, mbox_get_attr_flags, msg); mu_attribute_set_set_flags (attribute, mbox_set_attr_flags, msg); mu_attribute_set_unset_flags (attribute, mbox_unset_attr_flags, msg); mu_message_set_attribute (msg, attribute, mum); } /* Prepare the body. */ { mu_body_t body = NULL; mu_stream_t stream = NULL; if ((status = mu_body_create (&body, msg)) != 0 || (status = mu_stream_create (&stream, mailbox->flags | MU_STREAM_SEEKABLE, body)) != 0) { mu_body_destroy (&body, msg); mu_stream_destroy (&stream, body); mu_message_destroy (&msg, mum); return status; } mu_stream_set_read (stream, mbox_body_read, body); mu_stream_set_readline (stream, mbox_body_readline, body); mu_stream_set_get_transport2 (stream, mbox_get_body_transport, body); mu_stream_set_size (stream, mbox_stream_size, body); mu_body_set_stream (body, stream, msg); mu_body_set_size (body, mbox_body_size, msg); mu_body_set_lines (body, mbox_body_lines, msg); mu_message_set_body (msg, body, mum); } /* Set the envelope. */ { mu_envelope_t envelope= NULL; status = mu_envelope_create (&envelope, msg); if (status != 0) { mu_message_destroy (&msg, mum); return status; } mu_envelope_set_sender (envelope, mbox_envelope_sender, msg); mu_envelope_set_date (envelope, mbox_envelope_date, msg); mu_message_set_envelope (msg, envelope, mum); } /* Set the UID. */ mu_message_set_uid (msg, mbox_message_uid, mum); mu_message_set_qid (msg, mbox_message_qid, mum); /* Attach the message to the mailbox mbox data. */ mum->message = msg; mu_message_set_mailbox (msg, mailbox, mum); *pmsg = msg; return 0; }