/* GNU Mailutils -- a suite of utilities for electronic mail Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. GNU Mailutils is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Mailutils is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Mailutils; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static char* show_field; static int show_to; static int show_from = 1; static int show_subject = 1; static int show_number; static int show_summary; static int be_quiet; static int align = 1; static int show_query; static int have_new_mail; static int dbug; #define IS_READ 0x001 #define IS_OLD 0x010 #define IS_NEW 0x100 static int select_attribute; static int selected; static int action (observer_t, size_t); const char *program_version = "frm (" PACKAGE_STRING ")"; static char doc[] = N_("GNU frm -- display From: lines"); static struct attr_tab { char *name; /* Attribute name */ int code; /* Corresponding IS_.* flag */ size_t len; /* Minimum abbreviation length */ } attr_tab[] = { /* TRANSLATORS: See comment marked [frm status] below */ { N_("new"), IS_NEW, 0 }, /* TRANSLATORS: See comment marked [frm status] below */ { N_("old"), IS_OLD, 0 }, /* TRANSLATORS: See comment marked [frm status] below */ { N_("unread"), IS_OLD, 0 }, /* TRANSLATORS: See comment marked [frm status] below */ { N_("read"), IS_READ, 0 }, { NULL } }; static char attr_help[] = /* TRANSLATORS: [frm status] 1) Please make sure the words "new", "unread", "old" and "read" are translated exactly as in four preceeding messages. 2) If possible, select such translations for these words, that differ by the first letter. */ N_("Select messages with the specific attribute. STATUS is one \ of the following: new, unread, old (same as unread) or read. Any unambiguous \ abbreviation of those is also accepted."); /* Attribute table handling */ /* Prepares the table for use. Computes minimum abbreviation lengths. */ static void prepare_attrs (void) { struct attr_tab *p, *q; for (p = attr_tab; p->name; p++) { const char *name = gettext (p->name); size_t len = strlen (name); size_t n = 1; for (q = attr_tab; q->name; q++) { if (p != q) { const char *str = gettext (q->name); size_t slen = strlen (str); if (memcmp (name, str, n) == 0) { for (n++; memcmp (name, str, n) == 0 && n < len && n < slen; n++) ; q->len = n < slen ? n : slen; } } } p->len = n < len ? n : len; } } /* Translates the textual status representation to the corresponding IS_.* flag */ static int decode_attr (char *arg) { struct attr_tab *p; int len = strlen (arg); int pretendents = 0; for (p = attr_tab; p->name; p++) { const char *str = gettext (p->name); if (str[0] == arg[0]) { if (len < p->len) pretendents++; else if (len > strlen (str)) continue; if (memcmp (str, arg, p->len) == 0) return p->code; } } if (pretendents) mu_error (_("%s: ambiguous abbreviation"), arg); else mu_error (_("%s: unknown attribute"), arg); return 0; } static struct argp_option options[] = { {"debug", 'd', NULL, 0, N_("Enable debugging output"), 0}, {"field", 'f', N_("NAME"), 0, N_("Header field to display"), 0}, {"to", 'l', NULL, 0, N_("Include the To: information"), 0}, {"number", 'n', NULL, 0, N_("Display message numbers"), 0}, {"Quiet", 'Q', NULL, 0, N_("Very quiet"), 0}, {"query", 'q', NULL, 0, N_("Print a message if unread mail"), 0}, {"summary",'S', NULL, 0, N_("Print a summary of messages"), 0}, {"status", 's', N_("STATUS"), 0, attr_help, 0}, {"align", 't', NULL, 0, N_("Try to align"), 0}, {0, 0, 0, 0} }; static error_t parse_opt (int key, char *arg, struct argp_state *state) { switch (key) { case 'd': dbug++; break; case 'f': show_field = arg; show_from = 0; show_subject = 0; align = 0; break; case 'l': show_to = 1; break; case 'n': show_number = 1; break; case 'Q': /* Very silent. */ be_quiet += 2; if (freopen ("/dev/null", "w", stdout) == NULL) { perror (_("Can not be very quiet")); exit (3); } break; case 'q': be_quiet++; show_query = 1; break; case 'S': show_summary = 1; break; case 's': select_attribute = decode_attr (optarg); break; case 't': align = 1; break; default: return ARGP_ERR_UNKNOWN; } return 0; } static struct argp argp = { options, parse_opt, N_("[URL]"), doc, NULL, NULL, NULL }; static const char *frm_argp_capa[] = { "common", "license", "mailbox", #ifdef WITH_TLS "tls", #endif NULL }; static void frm_rfc2047_decode (char *personal, size_t buflen) { char *charset = NULL; char *tmp; int rc; /* Try to deduce the charset from LC_ALL or LANG variables */ tmp = getenv ("LC_ALL"); if (!tmp) tmp = getenv ("LANG"); if (tmp) { char *sp; char *lang; char *terr; lang = strtok_r (tmp, "_", &sp); terr = strtok_r (NULL, ".", &sp); charset = strtok_r (NULL, "@", &sp); if (!charset) charset = mu_charset_lookup (lang, terr); } if (!charset) return; rc = rfc2047_decode (charset, personal, &tmp); if (rc) { if (dbug) mu_error (_("Can't decode line `%s': %s"), personal, mu_strerror (rc)); } else { strncpy (personal, tmp, buflen - 1); free (tmp); } } /* Retrieve the Personal Name from the header To: or From: */ static int get_personal (header_t hdr, const char *field, char *personal, size_t buflen) { char hfield[512]; int status; /* Empty string. */ *hfield = '\0'; status = header_get_value_unfold (hdr, field, hfield, sizeof (hfield), NULL); if (status == 0) { address_t address = NULL; size_t len = 0; frm_rfc2047_decode (hfield, sizeof (hfield)); address_create (&address, hfield); address_get_personal (address, 1, personal, buflen, &len); address_destroy (&address); if (len == 0) strncpy (personal, hfield, buflen)[buflen - 1] = '\0'; } return status; } /* Observable action is being called on discovery of each message. */ /* FIXME: The format of the display is poorly done, please correct. */ static int action (observer_t o, size_t type) { static int counter; int status; switch (type) { case MU_EVT_MESSAGE_ADD: { mailbox_t mbox = observer_get_owner (o); message_t msg = NULL; header_t hdr = NULL; attribute_t attr = NULL; counter++; mailbox_get_message (mbox, counter, &msg); message_get_attribute (msg, &attr); message_get_header (msg, &hdr); if (((select_attribute & IS_READ) && (!attribute_is_read (attr))) || ((select_attribute & IS_NEW) && (!attribute_is_recent (attr))) || ((select_attribute & IS_OLD) && (!attribute_is_seen (attr)))) break; if (attribute_is_recent (attr)) have_new_mail = 1; if (select_attribute) selected = 1; if (show_number) printf ("%d: ", counter); if (show_field) { char hfield[256]; status = header_get_value_unfold (hdr, show_field, hfield, sizeof (hfield), NULL); if (status == 0) printf ("%s", hfield); } if (show_to) { char hto[16]; status = get_personal (hdr, MU_HEADER_TO, hto, sizeof (hto)); if (status == 0) printf ("(%s) ", hto); else printf ("(------)"); } if (show_from) { char hfrom[32]; status = get_personal (hdr, MU_HEADER_FROM, hfrom, sizeof (hfrom)); if (status == 0) printf ("%s\t", hfrom); else printf ("-----\t"); } if (show_subject) { char hsubject[64]; status = header_get_value_unfold (hdr, MU_HEADER_SUBJECT, hsubject, sizeof (hsubject), NULL); if (status == 0) { frm_rfc2047_decode (hsubject, sizeof (hsubject)); printf ("%s", hsubject); } } printf ("\n"); break; } case MU_EVT_MAILBOX_PROGRESS: /* Noop. */ break; } return 0; } /* This is a clone of the elm program call "frm". It is a good example on how to use the observable(callback) of libmailbox. "frm" has to be very interactive, it is not possible to call mailbox_messages_count() and wait for the scanning to finish before displaying. As soon as the scan finds a new message we want to know about it. This is done by registering an observable type MU_MAILBOX_MSG_ADD. The rest is formatting code. */ int main (int argc, char **argv) { char *mailbox_name = NULL; size_t total = 0; int c; int status = 0; /* Native Language Support */ mu_init_nls (); prepare_attrs (); mu_argp_init (program_version, NULL); #ifdef WITH_TLS mu_tls_init_client_argp (); #endif mu_argp_parse (&argp, &argc, &argv, 0, frm_argp_capa, &c, NULL); /* have an argument */ argc -= c; argv += c; if (argc) mailbox_name = argv[0]; /* register the formats. */ mu_register_all_mbox_formats (); /* Construct the mailbox_t, attach a notification and destroy */ { mailbox_t mbox; observer_t observer; observable_t observable; url_t url = NULL; status = mailbox_create_default (&mbox, mailbox_name); if (status != 0) { mu_error (_("Couldn't create mailbox <%s>: %s."), mailbox_name ? mailbox_name : _("default"), mu_strerror (status)); exit (3); } if (dbug) { mu_debug_t debug; mailbox_get_debug (mbox, &debug); mu_debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT); } mailbox_get_url (mbox, &url); status = mailbox_open (mbox, MU_STREAM_READ); if (status != 0) { if (status == ENOENT) goto cleanup1; else { mu_error (_("Couldn't open mailbox <%s>: %s."), url_to_string (url), mu_strerror (status)); goto cleanup; } } if (! be_quiet) { observer_create (&observer, mbox); observer_set_action (observer, action, mbox); mailbox_get_observable (mbox, &observable); observable_attach (observable, MU_EVT_MESSAGE_ADD, observer); } status = mailbox_scan (mbox, 1, &total); if (status != 0) { mu_error (_("Couldn't scan mailbox <%s>: %s."), url_to_string (url), mu_strerror (status)); goto cleanup; } if (! be_quiet) { observable_detach (observable, observer); observer_destroy (&observer, mbox); } cleanup: if (mailbox_close (mbox) != 0) { mu_error (_("Couldn't close <%s>: %s."), url_to_string (url), mu_strerror (status)); return -1; } mailbox_destroy (&mbox); if (status != 0) return 3; } cleanup1: if (show_summary) printf (ngettext ("You have %d message.\n", "You have %d messages.\n", total), total); if (show_query && have_new_mail) printf (_("You have new mail.\n")); /* EXIT STATUS Frm returns a zero status ("true") if messages matching `status' are present. Frm returns 1 if no messages matching `status' are present, but there are some messages, returns 2 if there are no messages at all, or returns 3 if an error occurred. */ if (selected) status = 0; else if (total > 0) status = 1; else status = 2; return status; }