int mta_stdin (int argc, char **argv) { int c; char *tempfile; mu_mailbox_t mbox; mu_message_t msg = NULL; for (c = 0; c < argc; c++) { if (add_recipient (argv[c])) { mu_error ("%s: bad address %s", progname, argv[c]); return 1; } } make_tmp (stdin, from_person, &tempfile); if ((c = mu_mailbox_create_default (&mbox, tempfile)) != 0) { mu_error ("%s: can't create mailbox %s: %s", progname, tempfile, mu_strerror (c)); unlink (tempfile); return 1; } if ((c = mu_mailbox_open (mbox, MU_STREAM_RDWR)) != 0) { mu_error ("%s: can't open mailbox %s: %s", progname, tempfile, mu_strerror (c)); unlink (tempfile); return 1; } mu_mailbox_get_message (mbox, 1, &msg); if (message_finalize (msg, 1)) return 1; if (!recipients) { mu_error ("%s: Recipient names must be specified", progname); return 1; } mta_send (msg); unlink (tempfile); free (tempfile); return 0; }
int mail_file (int argc, char **argv) { if (argc == 1) { mail_summary (0, NULL); } else if (argc == 2) { /* switch folders */ char *pname; mu_url_t url; mu_mailbox_t newbox = NULL; char *name = mail_expand_name (argv[1]); int status; if (!name) return 1; if ((status = mu_mailbox_create_default (&newbox, name)) != 0 || (status = mu_mailbox_open (newbox, MU_STREAM_RDWR)) != 0) { mu_mailbox_destroy (&newbox); util_error(_("Cannot open mailbox %s: %s"), name, mu_strerror (status)); free (name); return 1; } free (name); /* won't need it any more */ page_invalidate (1); /* Invalidate current page map */ mu_mailbox_get_url (mbox, &url); pname = strdup (mu_url_to_string (url)); if (mail_mbox_close ()) { if (pname) free (pname); mu_mailbox_close (newbox); mu_mailbox_destroy (&newbox); return 1; } if (prev_name) free (prev_name); prev_name = pname; mbox = newbox; mu_mailbox_messages_count (mbox, &total); set_cursor (1); if (mailvar_get (NULL, "header", mailvar_type_boolean, 0) == 0) { util_do_command ("summary"); util_do_command ("headers"); } return 0; } else { util_error(_("%s takes only one argument"), argv[0]); } return 1; }
int main (int argc, char **argv) { mu_mailbox_t mbox = NULL; size_t i; size_t count = 0; char *mailbox_name; int debug = 0; for (i = 1; i < argc; i++) { if (strcmp (argv[i], "-d") == 0) debug = 1; else if (strcmp (argv[i], "-p") == 0) print_attachments = 1; else if (strcmp (argv[i], "-i") == 0) { if (++i == argc) { mu_error ("-i requires argument"); exit (1); } indent_level = strtoul (argv[i], NULL, 0); } else if (strcmp (argv[i], "-c") == 0) { if (++i == argc) { mu_error ("-c requires argument"); exit (1); } charset = argv[i]; } else break; } mailbox_name = argv[i]; /* Registration. */ mu_registrar_record (mu_imap_record); mu_registrar_record (mu_pop_record); mu_registrar_record (mu_mbox_record); mu_registrar_set_default_record (mu_mbox_record); MU_ASSERT (mu_mailbox_create_default (&mbox, mailbox_name)); /* Debugging trace. */ if (debug) { mu_debug_set_category_level (MU_DEBCAT_MAILBOX, MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT)); } /* Open the mailbox for reading only. */ MU_ASSERT (mu_mailbox_open (mbox, MU_STREAM_READ)); /* Iterate through the entire message set. */ MU_ASSERT (mu_mailbox_messages_count (mbox, &count)); for (i = 1; i <= count; ++i) { mu_message_t msg; mu_header_t hdr; size_t nparts; size_t msize, nlines; MU_ASSERT (mu_mailbox_get_message (mbox, i, &msg)); MU_ASSERT (mu_message_size (msg, &msize)); MU_ASSERT (mu_message_lines (msg, &nlines)); MU_ASSERT (mu_message_get_header (msg, &hdr)); if (mu_header_sget_value (hdr, MU_HEADER_FROM, &from)) from = ""; if (mu_header_sget_value (hdr, MU_HEADER_SUBJECT, &subject)) subject = ""; printf ("Message: %lu\n", (unsigned long) i); printf ("From: %s\n", from); printf ("Subject: %s\n", subject); MU_ASSERT (mu_message_get_num_parts (msg, &nparts)); printf ("Number of parts in message - %lu\n", (unsigned long) nparts); printf ("Total message size - %lu/%lu\n", (unsigned long) msize, (unsigned long) nlines); message_display_parts (msg, 0); } mu_mailbox_close (mbox); mu_mailbox_destroy (&mbox); return 0; }
void smtp (int fd) { int state, c; char *buf = NULL; size_t size = 0; mu_mailbox_t mbox; mu_message_t msg; char *tempfile; char *rcpt_addr; in = fdopen (fd, "r"); out = fdopen (fd, "w"); SETVBUF (in, NULL, _IOLBF, 0); SETVBUF (out, NULL, _IOLBF, 0); smtp_reply (220, "Ready"); for (state = STATE_INIT; state != STATE_QUIT; ) { int argc; char **argv; int kw, len; if (getline (&buf, &size, in) == -1) exit (1); len = strlen (buf); while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r')) len --; buf[len] = 0; if (mu_argcv_get (buf, "", NULL, &argc, &argv)) exit (1); kw = smtp_kw (argv[0]); if (kw == KW_QUIT) { smtp_reply (221, "Done"); state = STATE_QUIT; mu_argcv_free (argc, argv); continue; } switch (state) { case STATE_INIT: switch (kw) { case KW_EHLO: case KW_HELO: if (argc == 2) { smtp_reply (250, "pleased to meet you"); state = STATE_EHLO; } else smtp_reply (501, "%s requires domain address", argv[0]); break; default: smtp_reply (503, "Polite people say HELO first"); break; } break; case STATE_EHLO: switch (kw) { case KW_MAIL: if (argc == 2) from_person = check_prefix (argv[1], "from:"); else if (argc == 3 && mu_c_strcasecmp (argv[1], "from:") == 0) from_person = argv[2]; else from_person = NULL; if (from_person) { from_person = strdup (from_person); smtp_reply (250, "Sender OK"); state = STATE_MAIL; } else smtp_reply (501, "Syntax error"); break; default: smtp_reply (503, "Need MAIL command"); } break; case STATE_MAIL: switch (kw) { case KW_RCPT: if (argc == 2) rcpt_addr = check_prefix (argv[1], "to:"); else if (argc == 3 && mu_c_strcasecmp (argv[1], "to:") == 0) rcpt_addr = argv[2]; else rcpt_addr = NULL; if (rcpt_addr) { if (add_recipient (rcpt_addr)) smtp_reply (451, "Recipient not accepted"); else { smtp_reply (250, "Recipient OK"); state = STATE_RCPT; } } else smtp_reply (501, "Syntax error"); break; default: smtp_reply (503, "Need RCPT command"); } break; case STATE_RCPT: switch (kw) { case KW_RCPT: if (argc == 2) rcpt_addr = check_prefix (argv[1], "to:"); else if (argc == 3 && mu_c_strcasecmp (argv[1], "to:") == 0) rcpt_addr = argv[2]; else rcpt_addr = NULL; if (rcpt_addr) { if (add_recipient (rcpt_addr)) smtp_reply (451, "Recipient not accepted"); else { smtp_reply (250, "Recipient OK"); state = STATE_RCPT; } } else smtp_reply (501, "Syntax error"); break; case KW_DATA: smtp_reply (354, "Enter mail, end with \".\" on a line by itself"); make_tmp (in, from_person, &tempfile); if ((c = mu_mailbox_create_default (&mbox, tempfile)) != 0) { mu_error ("%s: can't create mailbox %s: %s", progname, tempfile, mu_strerror (c)); unlink (tempfile); exit (1); } if ((c = mu_mailbox_open (mbox, MU_STREAM_RDWR)) != 0) { mu_error ("%s: can't open mailbox %s: %s", progname, tempfile, mu_strerror (c)); unlink (tempfile); exit (1); } mu_mailbox_get_message (mbox, 1, &msg); if (message_finalize (msg, 0) == 0) mta_send (msg); else smtp_reply (501, "can't send message"); /*FIXME: code?*/ unlink (tempfile); mu_address_destroy (&recipients); from_person = NULL; smtp_reply (250, "Message accepted for delivery"); state = STATE_EHLO; break; default: smtp_reply (503, "Invalid command"); break; } break; } mu_argcv_free (argc, argv); } close (fd); }
int main (int argc, const char **argv) { char *from; char *subject; mu_mailbox_t mbox; size_t msgno, total = 0; int status; /* Register the formats. */ mu_register_all_mbox_formats (); status = mu_mailbox_create_default (&mbox, argv[1]); if (status != 0) { mu_error ("mu_mailbox_create: %s", mu_strerror (status)); exit (EXIT_FAILURE); } status = mu_mailbox_open (mbox, MU_STREAM_READ); if (status != 0) { mu_error ("mu_mailbox_open: %s", mu_strerror (status)); exit (EXIT_FAILURE); } mu_mailbox_messages_count (mbox, &total); for (msgno = 1; msgno <= total; msgno++) { mu_message_t msg; mu_header_t hdr; if ((status = mu_mailbox_get_message (mbox, msgno, &msg)) != 0 || (status = mu_message_get_header (msg, &hdr)) != 0) { mu_error ("Error message: %s", mu_strerror (status)); exit (EXIT_FAILURE); } if (mu_header_aget_value (hdr, MU_HEADER_FROM, &from)) from = strdup ("(NO FROM)"); if (mu_header_aget_value (hdr, MU_HEADER_SUBJECT, &subject)) subject = strdup ("(NO SUBJECT)"); printf ("%s\t%s\n", from, subject); free (from); free (subject); } status = mu_mailbox_close (mbox); if (status != 0) { mu_error ("mu_mailbox_close: %s", mu_strerror (status)); exit (EXIT_FAILURE); } mu_mailbox_destroy (&mbox); return 0; }
/* FIXME: How do we do this ??????: IF a new mailbox is created with the same name as a mailbox which was deleted, its unique identifiers MUST be greater than any unique identifiers used in the previous incarnation of the mailbox. */ int imap4d_create (struct imap4d_session *session, struct imap4d_command *command, imap4d_tokbuf_t tok) { char *name; int isdir = 0; int ns; int rc = RESP_OK; const char *msg = "Completed"; if (imap4d_tokbuf_argc (tok) != 3) return io_completion_response (command, RESP_BAD, "Invalid arguments"); name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1); if (*name == '\0') return io_completion_response (command, RESP_BAD, "Too few arguments"); /* Creating, "Inbox" should always fail. */ if (mu_c_strcasecmp (name, "INBOX") == 0) return io_completion_response (command, RESP_BAD, "Already exist"); /* RFC 3501: If the mailbox name is suffixed with the server's hierarchy separator character, this is a declaration that the client intends to create mailbox names under this name in the hierarchy. The trailing delimiter will be removed by namespace normalizer, so test for it now. */ if (name[strlen (name) - 1] == MU_HIERARCHY_DELIMITER) isdir = 1; /* Allocates memory. */ name = namespace_getfullpath (name, &ns); if (!name) return io_completion_response (command, RESP_NO, "Cannot create mailbox"); /* It will fail if the mailbox already exists. */ if (access (name, F_OK) != 0) { if (make_interdir (name, MU_HIERARCHY_DELIMITER, MKDIR_PERMISSIONS)) { rc = RESP_NO; msg = "Cannot create mailbox"; } if (rc == RESP_OK && !isdir) { mu_mailbox_t mbox; rc = mu_mailbox_create_default (&mbox, name); if (rc) { mu_diag_output (MU_DIAG_ERR, _("Cannot create mailbox %s: %s"), name, mu_strerror (rc)); rc = RESP_NO; msg = "Cannot create mailbox"; } else if ((rc = mu_mailbox_open (mbox, MU_STREAM_RDWR | MU_STREAM_CREAT | mailbox_mode[ns]))) { mu_diag_output (MU_DIAG_ERR, _("Cannot open mailbox %s: %s"), name, mu_strerror (rc)); rc = RESP_NO; msg = "Cannot create mailbox"; } else { mu_mailbox_close (mbox); mu_mailbox_destroy (&mbox); rc = RESP_OK; } } } else { rc = RESP_NO; msg = "already exists"; } return io_completion_response (command, rc, "%s", msg); }
/* This code is shared with EXAMINE. */ int imap4d_select0 (struct imap4d_command *command, const char *mboxname, int flags) { int status; char *mailbox_name; /* FIXME: Check state. */ /* Even if a mailbox is selected, a SELECT EXAMINE or LOGOUT command MAY be issued without previously issuing a CLOSE command. The SELECT, EXAMINE, and LOGUT commands implictly close the currently selected mailbox without doing an expunge. */ if (mbox) { imap4d_enter_critical (); mu_mailbox_sync (mbox); mu_mailbox_close (mbox); manlock_unlock (mbox); imap4d_leave_critical (); mu_mailbox_destroy (&mbox); /* Destroy the old uid table. */ imap4d_sync (); } if (strcmp (mboxname, "INBOX") == 0) flags |= MU_STREAM_CREAT; mailbox_name = namespace_getfullpath (mboxname, NULL); if (!mailbox_name) return io_completion_response (command, RESP_NO, "Couldn't open mailbox"); if (flags & MU_STREAM_RDWR) { status = manlock_open_mailbox (&mbox, mailbox_name, 1, flags); } else { status = mu_mailbox_create_default (&mbox, mailbox_name); if (status) mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_create_default", mailbox_name, status); else { status = mu_mailbox_open (mbox, flags); if (status) { mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_open", mailbox_name, status); mu_mailbox_destroy (&mbox); } } } if (status == 0) { select_flags = flags; state = STATE_SEL; imap4d_set_observer (mbox); if ((status = imap4d_select_status ()) == 0) { free (mailbox_name); /* Need to set the state explicitely for select. */ return io_sendf ("%s OK [%s] %s Completed\n", command->tag, ((flags & MU_STREAM_RDWR) == MU_STREAM_RDWR) ? "READ-WRITE" : "READ-ONLY", command->name); } } mu_mailbox_destroy (&mbox); status = io_completion_response (command, RESP_NO, "Could not open %s: %s", mboxname, mu_strerror (status)); free (mailbox_name); return status; }