summaryrefslogtreecommitdiff
path: root/frm
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2004-01-13 21:31:15 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2004-01-13 21:31:15 +0000
commitcea51c0b459f2b6e0752cb87daa03efafcaaf91a (patch)
tree3bf3ad5cc8dd40efab6787075bf0aea0784374b0 /frm
parentb951d4cb74d4389e157872ab4087a3820ba125c8 (diff)
downloadmailutils-cea51c0b459f2b6e0752cb87daa03efafcaaf91a.tar.gz
mailutils-cea51c0b459f2b6e0752cb87daa03efafcaaf91a.tar.bz2
Lots of changes for compatibility with ELM.
Diffstat (limited to 'frm')
-rw-r--r--frm/frm.c302
1 files changed, 190 insertions, 112 deletions
diff --git a/frm/frm.c b/frm/frm.c
index b734afb23..6b3df3cd9 100644
--- a/frm/frm.c
+++ b/frm/frm.c
@@ -45,7 +45,7 @@
45#include <mailutils/mutil.h> 45#include <mailutils/mutil.h>
46#include <mailutils/mime.h> 46#include <mailutils/mime.h>
47 47
48static char* show_field; 48static char *show_field;
49static int show_to; 49static int show_to;
50static int show_from = 1; 50static int show_from = 1;
51static int show_subject = 1; 51static int show_subject = 1;
@@ -54,7 +54,6 @@ static int show_summary;
54static int be_quiet; 54static int be_quiet;
55static int align = 1; 55static int align = 1;
56static int show_query; 56static int show_query;
57static int have_new_mail;
58static int dbug; 57static int dbug;
59 58
60#define IS_READ 0x001 59#define IS_READ 0x001
@@ -327,12 +326,18 @@ get_personal (header_t hdr, const char *field, char *personal, size_t buflen)
327 return status; 326 return status;
328} 327}
329 328
329static struct {
330 size_t index;
331 size_t new;
332 size_t read;
333 size_t unread;
334} counter;
335
330/* Observable action is being called on discovery of each message. */ 336/* Observable action is being called on discovery of each message. */
331/* FIXME: The format of the display is poorly done, please correct. */ 337/* FIXME: The format of the display is poorly done, please correct. */
332static int 338static int
333action (observer_t o, size_t type) 339action (observer_t o, size_t type)
334{ 340{
335 static int counter;
336 int status; 341 int status;
337 342
338 switch (type) 343 switch (type)
@@ -344,26 +349,33 @@ action (observer_t o, size_t type)
344 header_t hdr = NULL; 349 header_t hdr = NULL;
345 attribute_t attr = NULL; 350 attribute_t attr = NULL;
346 351
347 counter++; 352 counter.index++;
348 353
349 mailbox_get_message (mbox, counter, &msg); 354 mailbox_get_message (mbox, counter.index, &msg);
350 355
351 message_get_attribute (msg, &attr); 356 message_get_attribute (msg, &attr);
352 message_get_header (msg, &hdr); 357 message_get_header (msg, &hdr);
353 358
359 if (attribute_is_read (attr))
360 counter.read++;
361 else if (attribute_is_seen (attr))
362 counter.unread++;
363 else if (attribute_is_recent (attr))
364 counter.new++;
365
354 if (((select_attribute & IS_READ) && (!attribute_is_read (attr))) 366 if (((select_attribute & IS_READ) && (!attribute_is_read (attr)))
355 || ((select_attribute & IS_NEW) && (!attribute_is_recent (attr))) 367 || ((select_attribute & IS_NEW) && (!attribute_is_recent (attr)))
356 || ((select_attribute & IS_OLD) && (!attribute_is_seen (attr)))) 368 || ((select_attribute & IS_OLD) && (!attribute_is_seen (attr))))
357 break; 369 break;
358 370
359 if (attribute_is_recent (attr))
360 have_new_mail = 1;
361
362 if (select_attribute) 371 if (select_attribute)
363 selected = 1; 372 selected = 1;
364 373
374 if (be_quiet)
375 break;
376
365 if (show_number) 377 if (show_number)
366 printf ("%d: ", counter); 378 printf ("%4lu: ", (u_long) counter.index);
367 379
368 if (show_field) 380 if (show_field)
369 { 381 {
@@ -419,6 +431,24 @@ action (observer_t o, size_t type)
419 return 0; 431 return 0;
420} 432}
421 433
434static void
435frm_abort (mailbox_t *mbox)
436{
437 int status;
438
439 if ((status = mailbox_close (*mbox)) != 0)
440 {
441 url_t url;
442
443 mu_error (_("Couldn't close <%s>: %s."),
444 url_to_string (url), mu_strerror (status));
445 exit (3);
446 }
447
448 mailbox_destroy (mbox);
449 exit (3);
450}
451
422/* This is a clone of the elm program call "frm". It is a good example on 452/* This is a clone of the elm program call "frm". It is a good example on
423 how to use the observable(callback) of libmailbox. "frm" has to 453 how to use the observable(callback) of libmailbox. "frm" has to
424 be very interactive, it is not possible to call mailbox_messages_count() 454 be very interactive, it is not possible to call mailbox_messages_count()
@@ -427,123 +457,136 @@ action (observer_t o, size_t type)
427 an observable type MU_MAILBOX_MSG_ADD. The rest is formatting code. */ 457 an observable type MU_MAILBOX_MSG_ADD. The rest is formatting code. */
428 458
429int 459int
430main (int argc, char **argv) 460frm (char *mailbox_name)
431{ 461{
432 char *mailbox_name = NULL; 462 int status;
463 mailbox_t mbox;
464 url_t url = NULL;
433 size_t total = 0; 465 size_t total = 0;
434 int c;
435 int status = 0;
436
437 /* Native Language Support */
438 mu_init_nls ();
439
440 prepare_attrs ();
441
442 mu_argp_init (program_version, NULL);
443#ifdef WITH_TLS
444 mu_tls_init_client_argp ();
445#endif
446 mu_argp_parse (&argp, &argc, &argv, 0, frm_argp_capa, &c, NULL);
447
448 /* have an argument */
449 argc -= c;
450 argv += c;
451 466
452 if (argc) 467 status = mailbox_create_default (&mbox, mailbox_name);
453 mailbox_name = argv[0]; 468 if (status != 0)
454 469 {
455 /* register the formats. */ 470 mu_error (_("Couldn't create mailbox <%s>: %s."),
456 mu_register_all_mbox_formats (); 471 mailbox_name ? mailbox_name : _("default"),
457 472 mu_strerror (status));
458 /* Construct the mailbox_t, attach a notification and destroy */ 473 exit (3);
459 { 474 }
460 mailbox_t mbox;
461 observer_t observer;
462 observable_t observable;
463 url_t url = NULL;
464
465 status = mailbox_create_default (&mbox, mailbox_name);
466 if (status != 0)
467 {
468 mu_error (_("Couldn't create mailbox <%s>: %s."),
469 mailbox_name ? mailbox_name : _("default"),
470 mu_strerror (status));
471 exit (3);
472 }
473
474 if (dbug)
475 {
476 mu_debug_t debug;
477 mailbox_get_debug (mbox, &debug);
478 mu_debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT);
479 }
480
481 mailbox_get_url (mbox, &url);
482
483 status = mailbox_open (mbox, MU_STREAM_READ);
484 if (status != 0)
485 {
486 if (status == ENOENT)
487 goto cleanup1;
488 else
489 {
490 mu_error (_("Couldn't open mailbox <%s>: %s."),
491 url_to_string (url), mu_strerror (status));
492 goto cleanup;
493 }
494 }
495
496 if (! be_quiet)
497 {
498 observer_create (&observer, mbox);
499 observer_set_action (observer, action, mbox);
500 mailbox_get_observable (mbox, &observable);
501 observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
502 }
503
504 status = mailbox_scan (mbox, 1, &total);
505 if (status != 0)
506 {
507 mu_error (_("Couldn't scan mailbox <%s>: %s."),
508 url_to_string (url), mu_strerror (status));
509 goto cleanup;
510 }
511
512 if (! be_quiet)
513 {
514 observable_detach (observable, observer);
515 observer_destroy (&observer, mbox);
516 }
517 475
518cleanup: 476 if (dbug)
519 if (mailbox_close (mbox) != 0) 477 {
520 { 478 mu_debug_t debug;
521 mu_error (_("Couldn't close <%s>: %s."), 479 mailbox_get_debug (mbox, &debug);
522 url_to_string (url), mu_strerror (status)); 480 mu_debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT);
523 return -1; 481 }
524 }
525 482
526 mailbox_destroy (&mbox); 483 mailbox_get_url (mbox, &url);
527 484
528 if (status != 0) 485 status = mailbox_open (mbox, MU_STREAM_READ);
529 return 3; 486 if (status == ENOENT)
530 } 487 /* nothing to do */;
488 else if (status != 0)
489 {
490 mu_error (_("Couldn't open mailbox <%s>: %s."),
491 url_to_string (url), mu_strerror (status));
492 frm_abort (&mbox);
493 }
494 else
495 {
496 observer_t observer;
497 observable_t observable;
498
499 observer_create (&observer, mbox);
500 observer_set_action (observer, action, mbox);
501 mailbox_get_observable (mbox, &observable);
502 observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
531 503
532 cleanup1: 504 memset (&counter, 0, sizeof counter);
505
506 status = mailbox_scan (mbox, 1, &total);
507 if (status != 0)
508 {
509 mu_error (_("Couldn't scan mailbox <%s>: %s."),
510 url_to_string (url), mu_strerror (status));
511 frm_abort (&mbox);
512 }
513
514 observable_detach (observable, observer);
515 observer_destroy (&observer, mbox);
516
517 if ((status = mailbox_close (mbox)) != 0)
518 {
519 mu_error (_("Couldn't close <%s>: %s."),
520 url_to_string (url), mu_strerror (status));
521 exit (3);
522 }
523 }
524 mailbox_destroy (&mbox);
525
533 if (show_summary) 526 if (show_summary)
534 printf (ngettext ("You have %d message.\n", 527 {
535 "You have %d messages.\n", 528 if (total == 0)
536 total), 529 printf (_("Folder contains no messages."));
537 total); 530 else
538 if (show_query && have_new_mail) 531 {
539 printf (_("You have new mail.\n")); 532 char *delim = "";
533
534 printf (_("Folder contains "));
535
536 if (counter.new)
537 {
538 printf (ngettext ("%lu new message",
539 "%lu new messages",
540 counter.new),
541 (u_long) counter.new);
542 delim = ", ";
543 }
544
545 if (counter.unread)
546 {
547 printf ("%s", delim);
548
549 printf (ngettext ("%lu unread message",
550 "%lu unread messages",
551 counter.unread),
552 (u_long) counter.unread);
553 delim = ", ";
554 }
555
556 if (counter.read)
557 {
558 printf ("%s", delim);
559
560 printf (ngettext ("%lu read message",
561 "%lu read messages",
562 counter.read),
563 (u_long) counter.read);
564 }
565 /* TRANSLATORS: This dot finishes the sentence
566
567 "Folder contains XXX messages."
540 568
569 Leave it as it is unless your language requires to reorder
570 the parts of speach in the message
571 */
572 printf (_("."));
573 }
574 printf ("\n");
575 }
576 else if (show_query)
577 {
578 if (total > 0)
579 printf (_("There are messages in that folder.\n"));
580 else
581 printf (_("No messages in that folder!\n"));
582 }
583
541 /* EXIT STATUS 584 /* EXIT STATUS
542 Frm returns a zero status ("true") if messages matching `status' are 585 Frm returns a zero status ("true") if messages matching `status' are
543 present. Frm returns 1 if no messages matching `status' are present, 586 present. Frm returns 1 if no messages matching `status' are present,
544 but there are some messages, returns 2 if there are no messages at 587 but there are some messages, returns 2 if there are no messages at
545 all, or returns 3 if an error occurred. */ 588 all, or returns 3 if an error occurred. */
546 589
547 if (selected) 590 if (selected)
548 status = 0; 591 status = 0;
549 else if (total > 0) 592 else if (total > 0)
@@ -553,3 +596,38 @@ cleanup:
553 596
554 return status; 597 return status;
555} 598}
599
600int
601main (int argc, char **argv)
602{
603 int c;
604 int status = 0;
605
606 /* Native Language Support */
607 mu_init_nls ();
608
609 prepare_attrs ();
610
611 /* register the formats. */
612 mu_register_all_mbox_formats ();
613
614 mu_argp_init (program_version, NULL);
615#ifdef WITH_TLS
616 mu_tls_init_client_argp ();
617#endif
618 mu_argp_parse (&argp, &argc, &argv, 0, frm_argp_capa, &c, NULL);
619
620 /* have an argument */
621 if (c == argc)
622 status = frm (NULL);
623 else if (c + 1 == argc)
624 status = frm (argv[c]);
625 else
626 for (; c < argc; c++)
627 {
628 printf ("%s:\n", argv[c]);
629 status = frm (argv[c]);
630 }
631
632 return status;
633}

Return to:

Send suggestions and report system problems to the System administrator.