1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2005-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 <frm.h>
18 
19 int count_only;
20 char *sender_option;
21 char *mailbox_name;
22 
23 static struct mu_option from_options[] = {
24   { "count",  'c', NULL,   MU_OPTION_DEFAULT,
25     N_("just print a count of messages and exit"),
26     mu_c_bool, &count_only },
27   { "sender", 's', N_("ADDRESS"), MU_OPTION_DEFAULT,
28     N_("print only mail from addresses containing the supplied string"),
29     mu_c_string, &sender_option },
30   { "file",   'f', N_("FILE"), MU_OPTION_DEFAULT,
31     N_("read mail from FILE"),
32     mu_c_string, &mailbox_name },
33   { "debug",  'd', NULL,   MU_OPTION_DEFAULT,
34     N_("enable debugging output"),
35     mu_c_incr, &frm_debug },
36   MU_OPTION_END
37 }, *options[] = { from_options, NULL };
38 
39 static struct mu_cli_setup cli = {
40   options,
41   NULL,
42   N_("GNU from -- display from and subject."),
43   N_("[OPTIONS] [USER]"),
44 };
45 
46 static char *capa[] = {
47   "debug",
48   "mailbox",
49   "locking",
50   NULL
51 };
52 
53 static int
from_select(size_t index,mu_message_t msg)54 from_select (size_t index, mu_message_t msg)
55 {
56   if (count_only)
57     return 0;
58 
59   if (sender_option)
60     {
61       int rc = 0;
62       mu_header_t hdr = NULL;
63       char *sender;
64       mu_message_get_header (msg, &hdr);
65 
66       if (mu_header_aget_value_unfold (hdr, MU_HEADER_FROM, &sender) == 0)
67 	{
68 	  if (strstr (sender, sender_option))
69 	    rc = 1;
70 	  free (sender);
71 	}
72 
73       return rc;
74     }
75 
76   return 1;
77 }
78 
79 int
main(int argc,char ** argv)80 main (int argc, char **argv)
81 {
82   size_t total;
83 
84   /* Native Language Support */
85   MU_APP_INIT_NLS ();
86 
87   /* register the formats.  */
88   mu_register_all_mbox_formats ();
89 
90   mu_auth_register_module (&mu_auth_tls_module);
91   mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);
92 
93   if (argc > 1)
94     {
95       mu_error (_("too many arguments"));
96       exit (1);
97     }
98   else if (argc > 0)
99     {
100       if (mailbox_name)
101 	{
102 	  mu_error (_("both --from option and user name are specified"));
103 	  exit (1);
104 	}
105 
106       mailbox_name = mu_alloc (strlen (argv[0]) + 2);
107       mailbox_name[0] = '%';
108       strcpy (mailbox_name + 1, argv[0]);
109     }
110 
111   init_output (0);
112 
113   frm_scan (mailbox_name, from_select, &total);
114 
115   if (count_only)
116     {
117       mu_printf (ngettext ("There is %lu message in your incoming mailbox.\n",
118 			"There are %lu messages in your incoming mailbox.\n",
119 			total),
120 	      (unsigned long) total);
121     }
122   return 0;
123 }
124 
125