1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include "mail.h"
18 
19 /* Simple summary dysplaying a blurb on the name of the
20    mailbox and how many new:deleted:read messages.
21    The side effect is that it sets the cursor
22    to the newest or read message number.  */
23 int
mail_summary(int argc MU_ARG_UNUSED,char ** argv MU_ARG_UNUSED)24 mail_summary (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
25 {
26   mu_message_t msg;
27   mu_attribute_t attr;
28   size_t msgno;
29   size_t count = 0;
30   unsigned long mseen = 0, mnew = 0, mdelete = 0;
31   size_t first_new = 0, first_unread = 0;
32 
33   mu_mailbox_messages_count (mbox, &count);
34   for (msgno = 1; msgno <= count; msgno++)
35     {
36       if ((mu_mailbox_get_message (mbox, msgno, &msg) == 0)
37 	  && (mu_message_get_attribute (msg, &attr) == 0))
38 	    {
39 	      int deleted = mu_attribute_is_deleted (attr);
40 
41 	      if (deleted)
42 		mdelete++;
43 	      if (mu_attribute_is_seen (attr) && ! mu_attribute_is_read (attr))
44 		{
45 		  mseen++;
46 		  if (!deleted && !first_unread)
47 		    first_unread = msgno;
48 		}
49 	      if (mu_attribute_is_recent (attr))
50 		{
51 		  mnew++;
52 		  if (!deleted && !first_new)
53 		    first_new = msgno;
54 		}
55 	}
56     }
57 
58   /* Print the mailbox name.  */
59   {
60     mu_url_t url = NULL;
61     mu_mailbox_get_url (mbox, &url);
62     mu_printf ("\"%s\": ", util_url_to_string (url));
63   }
64   mu_printf (ngettext ("%lu message", "%lu messages",
65 		       (unsigned long) count), (unsigned long) count);
66   if (mnew > 0)
67     mu_printf (ngettext (" %lu new", " %lu new", mnew), mnew);
68   if (mseen > 0)
69     mu_printf (ngettext (" %lu unread", " %lu unread", mseen), mseen);
70   if (mdelete > 0)
71     mu_printf (ngettext (" %lu deleted", " %lu deleted", mdelete),
72 	       mdelete);
73   mu_printf ("\n");
74 
75   /* Set the cursor.  */
76   set_cursor ((first_new == 0) ? ((first_unread == 0) ?
77 				    1 : first_unread) : first_new) ;
78   return 0;
79 }
80