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 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 
21 #include <stdio.h>
22 
23 #include <mailutils/mailutils.h>
24 #include <mailutils/cli.h>
25 
26 static int messages_count (const char *);
27 
28 /* are we loud or quiet? */
29 static int silent = 0;
30 
31 static struct mu_option messages_options[] = {
32   { "quiet",	'q',	NULL,	MU_OPTION_DEFAULT,
33     N_("only display number of messages"),
34     mu_c_bool, &silent },
35   {"silent",	's',	NULL,	MU_OPTION_ALIAS },
36   MU_OPTION_END
37 };
38 
39 static char *capa[] = {
40   "debug",
41   "mailbox",
42   "locking",
43   NULL
44 };
45 
46 static struct mu_option *options[] = { messages_options, NULL };
47 
48 struct mu_cli_setup cli = {
49   options,
50   NULL,
51   N_("GNU messages -- count the number of messages in a mailbox"),
52   N_("[mailbox...]")
53 };
54 
55 int
main(int argc,char ** argv)56 main (int argc, char **argv)
57 {
58   int err = 0;
59 
60   /* Native Language Support */
61   MU_APP_INIT_NLS ();
62 
63   /* register the formats.  */
64   mu_register_all_mbox_formats ();
65 
66   mu_auth_register_module (&mu_auth_tls_module);
67   mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);
68 
69   if (argc == 0 && messages_count (NULL) < 0)
70     err = 1;
71   else if (argc >= 1)
72     {
73       size_t i;
74 
75       for (i = 0; i < argc; i++)
76 	{
77 	  if (messages_count (argv[i]) < 0)
78 	    err = 1;
79 	}
80     }
81 
82   return err;
83 }
84 
85 static int
messages_count(const char * box)86 messages_count (const char *box)
87 {
88   mu_mailbox_t mbox;
89   mu_url_t url = NULL;
90   size_t count;
91   int status = 0;
92 
93   status =  mu_mailbox_create_default (&mbox, box);
94   if (status != 0)
95     {
96       if (box)
97 	mu_error (_("could not create mailbox `%s': %s"),
98 		  box, mu_strerror (status));
99       else
100 	mu_error (_("could not create default mailbox: %s"),
101 		  mu_strerror (status));
102       return -1;
103     }
104 
105   mu_mailbox_get_url (mbox, &url);
106   box = mu_url_to_string (url);
107 
108   status =  mu_mailbox_open (mbox, MU_STREAM_READ);
109   if (status != 0)
110     {
111       mu_error (_("could not open mailbox `%s': %s"),
112 		box, mu_strerror (status));
113       return -1;
114     }
115 
116   status = mu_mailbox_messages_count (mbox, &count);
117   if (status != 0)
118     {
119       mu_error (_("could not count messages in mailbox `%s': %s"),
120 		box, mu_strerror (status));
121       return -1;
122     }
123 
124   if (silent)
125     mu_printf ("%lu\n", (unsigned long) count);
126   else
127     mu_printf (_("Number of messages in %s: %lu\n"), box,
128 	       (unsigned long) count);
129 
130   status = mu_mailbox_close (mbox);
131   if (status != 0)
132     {
133       mu_error (_("could not close `%s': %s"),
134 		box, mu_strerror (status));
135       return -1;
136     }
137 
138   mu_mailbox_destroy (&mbox);
139   return count;
140 }
141