Lots of changes for compatibility with ELM.
Showing
1 changed file
with
145 additions
and
67 deletions
... | @@ -45,7 +45,7 @@ | ... | @@ -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 | ||
48 | static char* show_field; | 48 | static char *show_field; |
49 | static int show_to; | 49 | static int show_to; |
50 | static int show_from = 1; | 50 | static int show_from = 1; |
51 | static int show_subject = 1; | 51 | static int show_subject = 1; |
... | @@ -54,7 +54,6 @@ static int show_summary; | ... | @@ -54,7 +54,6 @@ static int show_summary; |
54 | static int be_quiet; | 54 | static int be_quiet; |
55 | static int align = 1; | 55 | static int align = 1; |
56 | static int show_query; | 56 | static int show_query; |
57 | static int have_new_mail; | ||
58 | static int dbug; | 57 | static 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,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 | ||
329 | static 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. */ |
332 | static int | 338 | static int |
333 | action (observer_t o, size_t type) | 339 | action (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,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,6 +431,24 @@ action (observer_t o, size_t type) |
419 | return 0; | 431 | return 0; |
420 | } | 432 | } |
421 | 433 | ||
434 | static void | ||
435 | frm_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,40 +457,12 @@ action (observer_t o, size_t type) | ... | @@ -427,40 +457,12 @@ 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 | ||
429 | int | 459 | int |
430 | main (int argc, char **argv) | 460 | frm (char *mailbox_name) |
431 | { | 461 | { |
432 | char *mailbox_name = NULL; | 462 | int status; |
433 | 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 | |||
452 | if (argc) | ||
453 | mailbox_name = argv[0]; | ||
454 | |||
455 | /* register the formats. */ | ||
456 | mu_register_all_mbox_formats (); | ||
457 | |||
458 | /* Construct the mailbox_t, attach a notification and destroy */ | ||
459 | { | ||
460 | mailbox_t mbox; | 463 | mailbox_t mbox; |
461 | observer_t observer; | ||
462 | observable_t observable; | ||
463 | url_t url = NULL; | 464 | url_t url = NULL; |
465 | size_t total = 0; | ||
464 | 466 | ||
465 | status = mailbox_create_default (&mbox, mailbox_name); | 467 | status = mailbox_create_default (&mbox, mailbox_name); |
466 | if (status != 0) | 468 | if (status != 0) |
... | @@ -481,62 +483,103 @@ main (int argc, char **argv) | ... | @@ -481,62 +483,103 @@ main (int argc, char **argv) |
481 | mailbox_get_url (mbox, &url); | 483 | mailbox_get_url (mbox, &url); |
482 | 484 | ||
483 | status = mailbox_open (mbox, MU_STREAM_READ); | 485 | status = mailbox_open (mbox, MU_STREAM_READ); |
484 | if (status != 0) | ||
485 | { | ||
486 | if (status == ENOENT) | 486 | if (status == ENOENT) |
487 | goto cleanup1; | 487 | /* nothing to do */; |
488 | else | 488 | else if (status != 0) |
489 | { | 489 | { |
490 | mu_error (_("Couldn't open mailbox <%s>: %s."), | 490 | mu_error (_("Couldn't open mailbox <%s>: %s."), |
491 | url_to_string (url), mu_strerror (status)); | 491 | url_to_string (url), mu_strerror (status)); |
492 | goto cleanup; | 492 | frm_abort (&mbox); |
493 | } | ||
494 | } | 493 | } |
495 | 494 | else | |
496 | if (! be_quiet) | ||
497 | { | 495 | { |
496 | observer_t observer; | ||
497 | observable_t observable; | ||
498 | |||
498 | observer_create (&observer, mbox); | 499 | observer_create (&observer, mbox); |
499 | observer_set_action (observer, action, mbox); | 500 | observer_set_action (observer, action, mbox); |
500 | mailbox_get_observable (mbox, &observable); | 501 | mailbox_get_observable (mbox, &observable); |
501 | observable_attach (observable, MU_EVT_MESSAGE_ADD, observer); | 502 | observable_attach (observable, MU_EVT_MESSAGE_ADD, observer); |
502 | } | 503 | |
504 | memset (&counter, 0, sizeof counter); | ||
503 | 505 | ||
504 | status = mailbox_scan (mbox, 1, &total); | 506 | status = mailbox_scan (mbox, 1, &total); |
505 | if (status != 0) | 507 | if (status != 0) |
506 | { | 508 | { |
507 | mu_error (_("Couldn't scan mailbox <%s>: %s."), | 509 | mu_error (_("Couldn't scan mailbox <%s>: %s."), |
508 | url_to_string (url), mu_strerror (status)); | 510 | url_to_string (url), mu_strerror (status)); |
509 | goto cleanup; | 511 | frm_abort (&mbox); |
510 | } | 512 | } |
511 | 513 | ||
512 | if (! be_quiet) | ||
513 | { | ||
514 | observable_detach (observable, observer); | 514 | observable_detach (observable, observer); |
515 | observer_destroy (&observer, mbox); | 515 | observer_destroy (&observer, mbox); |
516 | } | ||
517 | 516 | ||
518 | cleanup: | 517 | if ((status = mailbox_close (mbox)) != 0) |
519 | if (mailbox_close (mbox) != 0) | ||
520 | { | 518 | { |
521 | mu_error (_("Couldn't close <%s>: %s."), | 519 | mu_error (_("Couldn't close <%s>: %s."), |
522 | url_to_string (url), mu_strerror (status)); | 520 | url_to_string (url), mu_strerror (status)); |
523 | return -1; | 521 | exit (3); |
522 | } | ||
524 | } | 523 | } |
525 | |||
526 | mailbox_destroy (&mbox); | 524 | mailbox_destroy (&mbox); |
527 | 525 | ||
528 | if (status != 0) | 526 | if (show_summary) |
529 | return 3; | 527 | { |
528 | if (total == 0) | ||
529 | printf (_("Folder contains no messages.")); | ||
530 | else | ||
531 | { | ||
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 = ", "; | ||
530 | } | 543 | } |
531 | 544 | ||
532 | cleanup1: | 545 | if (counter.unread) |
533 | if (show_summary) | 546 | { |
534 | printf (ngettext ("You have %d message.\n", | 547 | printf ("%s", delim); |
535 | "You have %d messages.\n", | 548 | |
536 | total), | 549 | printf (ngettext ("%lu unread message", |
537 | total); | 550 | "%lu unread messages", |
538 | if (show_query && have_new_mail) | 551 | counter.unread), |
539 | printf (_("You have new mail.\n")); | 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." | ||
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 | } | ||
540 | 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 |
... | @@ -553,3 +596,38 @@ cleanup: | ... | @@ -553,3 +596,38 @@ cleanup: |
553 | 596 | ||
554 | return status; | 597 | return status; |
555 | } | 598 | } |
599 | |||
600 | int | ||
601 | main (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 | } | ... | ... |
-
Please register or sign in to post a comment