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 #include <mh.h>
18 
19 static int
_add_to_list(size_t num,mu_message_t msg,void * data)20 _add_to_list (size_t num, mu_message_t msg, void *data)
21 {
22   mu_list_t list = data;
23   return mu_list_append (list, msg);
24 }
25 
26 void
mh_whatnow_env_from_environ_early(struct mh_whatnow_env * wh)27 mh_whatnow_env_from_environ_early (struct mh_whatnow_env *wh)
28 {
29   memset (wh, 0, sizeof (*wh));
30 
31   wh->file = getenv ("mhdraft");
32   wh->msg = getenv ("mhaltmsg");
33   wh->draftfile = wh->file;
34   wh->editor = getenv ("mheditor");
35   wh->prompt = getenv ("mhprompt"); /* extension */
36 }
37 
38 
39 void
mh_whatnow_env_from_environ_late(struct mh_whatnow_env * wh)40 mh_whatnow_env_from_environ_late (struct mh_whatnow_env *wh)
41 {
42   char *folder = getenv ("mhfolder");
43 
44   if (folder)
45     {
46       wh->anno_field = getenv ("mhannotate");
47       if (wh->anno_field)
48 	{
49 	  char *p = getenv ("mhmessages");
50 	  if (!p)
51 	    wh->anno_field = NULL;
52 	  else
53 	    {
54 	      mu_msgset_t msgset;
55 	      mu_mailbox_t mbox = mh_open_folder (folder, MU_STREAM_RDWR);
56 
57 	      mh_msgset_parse_string (&msgset, mbox, p, "cur");
58 
59 	      wh->mbox = mbox;
60 	      mu_list_create (&wh->anno_list);
61 	      mu_msgset_foreach_message (msgset, _add_to_list, wh->anno_list);
62 	      mu_msgset_free (msgset);
63 	      /* FIXME:
64 		 wh->anno_inplace = getenv ("mhinplace");
65 	      */
66 	    }
67 	}
68     }
69 }
70 
71 void
mh_whatnow_env_to_environ(struct mh_whatnow_env * wh)72 mh_whatnow_env_to_environ (struct mh_whatnow_env *wh)
73 {
74   if (wh->file)
75     setenv ("mhdraft", wh->file, 1);
76   if (wh->msg)
77     setenv ("mhaltmsg", wh->msg, 1);
78   if (wh->editor)
79     setenv ("mheditor", wh->editor, 1);
80   if (wh->prompt)
81     setenv ("mhprompt", wh->prompt, 1);
82   if (wh->anno_field)
83     setenv ("mhannotate", wh->anno_field, 1);
84   if (wh->anno_list)
85     {
86       mu_opool_t opool;
87       mu_iterator_t itr;
88       size_t prev_uid = 0;
89       int mrange = 0;
90       const char *s;
91 
92       mu_opool_create (&opool, MU_OPOOL_ENOMEMABRT);
93       mu_list_get_iterator (wh->anno_list, &itr);
94       for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
95 	   mu_iterator_next (itr))
96 	{
97 	  mu_message_t msg;
98 	  size_t uid;
99 
100 	  mu_iterator_current (itr, (void**)&msg);
101 	  mu_message_get_uid (msg, &uid);
102 	  if (prev_uid == 0)
103 	    {
104 	      s = mu_umaxtostr (0, uid);
105 	      mu_opool_appendz (opool, s);
106 	      mrange = 0;
107 	    }
108 	  else if (uid == prev_uid + 1)
109 	    mrange = 1;
110 	  else
111 	    {
112 	      if (mrange)
113 		{
114 		  mu_opool_append_char (opool, '-');
115 		  s = mu_umaxtostr (0, prev_uid);
116 		  mu_opool_appendz (opool, s);
117 		}
118 	      mu_opool_append_char (opool, ' ');
119 	      s = mu_umaxtostr (0, uid);
120 	      mu_opool_appendz (opool, s);
121 	      mrange = 0;
122 	    }
123 	}
124 
125       if (mrange)
126 	{
127 	  mu_opool_append_char (opool, '-');
128 	  s = mu_umaxtostr (0, prev_uid);
129 	  mu_opool_appendz (opool, s);
130 	}
131       mu_opool_append_char (opool, 0);
132       s = mu_opool_finish (opool, NULL);
133       setenv ("mhmessages", s, 1);
134       mu_opool_destroy (&opool);
135     }
136 }
137