1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2003-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 /* MH annotate command */
18 
19 #include <mh.h>
20 
21 static char prog_doc[] = N_("Annotate messages");
22 static char args_doc[] = N_("[MSGLIST]");
23 
24 //static int inplace;       /* Annotate the message in place */
25 static int anno_date = 1; /* Add date to the annotation */
26 static char *component;   /* header field */
27 static char *anno_text;   /* header field value */
28 
29 static struct mu_option options[] = {
30   { "inplace", 0, NULL, MU_OPTION_HIDDEN,
31     N_("annotate the message in place"),
32     mu_c_bool, NULL, mh_opt_notimpl_warning },
33   { "date", 0, NULL, MU_OPTION_DEFAULT,
34     N_("add FIELD: date header"),
35     mu_c_bool, &anno_date },
36   { "component", 0, N_("FIELD"), MU_OPTION_DEFAULT,
37     N_("add this FIELD to the message header"),
38     mu_c_string, &component },
39   { "text", 0, N_("STRING"), MU_OPTION_DEFAULT,
40     N_("field value for the component"),
41     mu_c_string, &anno_text },
42   MU_OPTION_END
43 };
44 
45 int
anno(size_t n,mu_message_t msg,void * call_data)46 anno (size_t n, mu_message_t msg, void *call_data)
47 {
48   mh_annotate (msg, component, anno_text, anno_date);
49   return 0;
50 }
51 
52 int
main(int argc,char ** argv)53 main (int argc, char **argv)
54 {
55   int rc;
56   mu_mailbox_t mbox;
57   mu_msgset_t msgset;
58   size_t len;
59 
60   mh_getopt (&argc, &argv, options, MH_GETOPT_DEFAULT_FOLDER,
61 	     args_doc, prog_doc, NULL);
62   if (anno_text)
63     {
64       char *arg = anno_text;
65       mh_quote (arg, &anno_text);
66       free (arg);
67     }
68 
69   mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
70 
71   if (!component)
72     {
73       size_t size = 0;
74       char *p;
75 
76       if (isatty (0))
77 	{
78 	  mu_printf (_("Component name: "));
79 	  mu_stream_flush (mu_strout);
80 	}
81       rc = mu_stream_getline (mu_strin, &component, &size, NULL);
82       if (rc)
83 	{
84 	  mu_error (_("error reading input stream: %s"), mu_strerror (rc));
85 	  exit (1);
86 	}
87       p = mu_str_stripws (component);
88       if (*p == 0)
89 	{
90 	  mu_error (_("invalid component name"));
91 	  exit (1);
92 	}
93       if (p > component)
94 	memmove (component, p, strlen (p) + 1);
95     }
96 
97   if (!anno_text && !anno_date)
98     exit (0);
99 
100   len = strlen (component);
101   if (len > 0 && component[len-1] == ':')
102     component[len-1] = 0;
103 
104   mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
105   rc = mu_msgset_foreach_message (msgset, anno, NULL);
106   if (rc)
107     {
108       mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_foreach_message", NULL, rc);
109       exit (1);
110     }
111 
112   mh_mailbox_set_cur (mbox, mh_msgset_first (msgset, RET_UID));
113   mu_msgset_free (msgset);
114   mh_global_save_state ();
115   mu_mailbox_sync (mbox);
116   mu_mailbox_close (mbox);
117   mu_mailbox_destroy (&mbox);
118   return rc;
119 }
120 
121 
122 
123 
124 
125 
126 
127