1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2002-2025 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
/* MH show command */
#include <mh.h>
static char prog_doc[] = N_("Display GNU MH messages");
static char args_doc[] = N_("[MSGLIST]");
int use_draft;
int use_showproc = 1;
int header_option = 1;
char *file;
const char *showproc;
char **showargv;
size_t showargc;
size_t showargmax;
const char *mhnproc;
static void
addarg (const char *arg)
{
if (showargc == showargmax)
showargv = mu_2nrealloc (showargv, &showargmax, sizeof showargv[0]);
showargv[showargc++] = (char*) arg;
}
static void
insarg (char *arg)
{
addarg (arg);
if (showargc > 2)
{
char *p = showargv[showargc-1];
memmove (showargv + 2, showargv + 1,
sizeof (showargv[0]) * (showargc - 2));
showargv[1] = p;
}
}
static void
finisarg (void)
{
struct mu_wordsplit ws;
char *p;
mu_wordsplit (showproc, &ws, MU_WRDSF_DEFFLAGS);
if (ws.ws_wordc > 1)
{
size_t i;
if (showargc + ws.ws_wordc > showargmax)
{
showargmax = showargc + ws.ws_wordc;
showargv = mu_realloc (showargv, showargmax * sizeof showargv[0]);
}
memmove (showargv + ws.ws_wordc, showargv + 1,
sizeof (showargv[0]) * (showargc - 1));
for (i = 1; i < ws.ws_wordc; i++)
showargv[i] = ws.ws_wordv[i];
showargc += ws.ws_wordc - 1;
showproc = ws.ws_wordv[0];
}
p = strrchr (showproc, '/');
showargv[0] = (char*) (p ? p + 1 : showproc);
addarg (NULL);
}
static void
set_draft (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
if (use_draft || file)
{
mu_parseopt_error (po, _("only one file at a time!"));
exit (po->po_exit_error);
}
use_draft = 1;
}
static void
set_file (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
if (use_draft || file)
{
mu_parseopt_error (po, _("only one file at a time!"));
exit (po->po_exit_error);
}
file = mh_expand_name (NULL, arg, NAME_FILE);
}
static void
add_show_arg (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
{
char *name = mu_alloc (strlen (opt->opt_long) + 2);
name[0] = '-';
strcpy (name + 1, opt->opt_long);
addarg (name);
if (arg)
addarg (arg);
}
static struct mu_option options[] = {
{ "draft", 0, NULL, MU_OPTION_DEFAULT,
N_("show the draft file"),
mu_c_string, NULL, set_draft },
{ "file", 0, N_("FILE"), MU_OPTION_DEFAULT,
N_("show this file"),
mu_c_string, NULL, set_file },
{ "header", 0, NULL, MU_OPTION_DEFAULT,
N_("display a description of the message before the message itself"),
mu_c_bool, &header_option },
{ "showproc", 0, N_("PROGRAM"), MU_OPTION_DEFAULT,
N_("use PROGRAM to show messages"),
mu_c_string, &showproc },
{ "noshowproc", 0, NULL, MU_OPTION_DEFAULT,
N_("disable the use of the \"showproc:\" profile component"),
mu_c_int, &use_showproc, NULL, "0" },
{ "form", 0, N_("FILE"), MU_OPTION_DEFAULT,
N_("read format from given file"),
mu_c_string, NULL, add_show_arg },
{ "moreproc", 0, N_("PROG"), MU_OPTION_DEFAULT,
N_("use this PROG as pager"),
mu_c_string, NULL, add_show_arg },
{ "nomoreproc", 0, NULL, MU_OPTION_DEFAULT,
N_("disable the use of moreproc"),
mu_c_string, NULL, add_show_arg },
{ "length", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
N_("set output screen length"),
mu_c_string, NULL, add_show_arg },
{ "width", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
N_("set output width"),
mu_c_string, NULL, add_show_arg },
MU_OPTION_END
};
static int
resolve_mime (size_t num, mu_message_t msg, void *data)
{
int *ismime = data;
mu_message_is_multipart (msg, ismime);
return *ismime;
}
static int
resolve_msg (size_t num, mu_message_t msg, void *data)
{
char *path = data;
mh_message_number (msg, &num);
addarg (mh_safe_make_file_name (path, mu_umaxtostr (0, num)));
return 0;
}
static int
adduid (size_t num, void *data)
{
addarg (mu_umaxtostr (0, num));
return 0;
}
static int
printheader (size_t num, void *data)
{
mu_printf ("(Message %s:%lu)\n", (char*) data, (unsigned long)num);
return 1;
}
static void
checkfile (char *file)
{
int ismime = 0;
if (!showproc)
{
mu_message_t msg = mh_file_to_message (NULL, file);
mu_message_is_multipart (msg, &ismime);
mu_message_unref (msg);
}
if (ismime)
{
showproc = mhnproc;
insarg ("-show");
addarg ("-file");
}
addarg (file);
}
int
main (int argc, char **argv)
{
mu_mailbox_t mbox;
mu_msgset_t msgset = NULL;
const char *p;
const char *mode = "cur";
showargmax = 2;
showargc = 1;
showargv = mu_calloc (showargmax, sizeof showargv[0]);
mh_getopt (&argc, &argv, options, MH_GETOPT_DEFAULT_FOLDER,
args_doc, prog_doc, NULL);
if (strcmp (mu_program_name, "next") == 0
|| strcmp (mu_program_name, "prev") == 0)
mode = mu_program_name;
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
if (use_draft || file)
{
if (argc)
{
mu_error (_("only one file at a time!"));
exit (1);
}
}
else
mh_msgset_parse (&msgset, mbox, argc, argv, mode);
/* 1. If !use_showproc set showproc to /bin/cat and go to X.
2. If -file or -draft is given
2.a. If showproc is set, go to 2.c
2.b. If the file is a multipart one, set showproc to mhn with -file
and -show options and go to X
2.c. Add file to the argument list and go to X.
3. Scan all messages to see if there are MIME ones. If so, set
showproc to mhn with the -show option, transfer message UIDs
to showproc arguments, and go to X
4. Otherwise set showproc from the showproc variable in .mh_profile
X. If showproc is not set, set it to PAGER ("/usr/bin/more" by default).
Y. Execute showproc with the collected argument list.
*/
if (!use_showproc)
showproc = "/bin/cat";
else
showproc = mh_global_profile_get ("showproc", NULL);
mhnproc = mh_global_profile_get ("mhnproc", "mhn");
if (use_draft || file)
{
if (file)
checkfile (file);
else
{
const char *dfolder = mh_global_profile_get ("Draft-Folder", NULL);
if (dfolder)
{
int ismime = 0;
mu_mailbox_close (mbox);
mu_mailbox_destroy (&mbox);
mbox = mh_open_folder (dfolder, MU_STREAM_RDWR);
mh_msgset_parse (&msgset, mbox, 0, NULL, mode);
mu_msgset_foreach_message (msgset, resolve_mime, &ismime);
if (ismime)
{
showproc = mhnproc;
insarg ("-show");
addarg ("-file");
}
mu_msgset_foreach_message (msgset, resolve_msg,
(void *) dfolder);
}
else
checkfile (mh_expand_name (mu_folder_directory (),
"draft", NAME_ANY));
}
}
else
{
mu_url_t url;
size_t n;
int ismime = 0;
const char *path;
mu_mailbox_get_url (mbox, &url);
mu_url_sget_path (url, &path);
if (!showproc)
{
mu_msgset_foreach_message (msgset, resolve_mime, &ismime);
if (ismime)
{
showproc = mhnproc;
insarg ("-show");
}
}
if (ismime)
mu_msgset_foreach_msguid (msgset, adduid, NULL);
else
mu_msgset_foreach_message (msgset, resolve_msg, (void*) path);
p = mh_global_profile_get ("Unseen-Sequence", NULL);
if (p)
{
mh_seq_delete (mbox, p, msgset, 0);
mh_global_save_state ();
}
if (header_option && mu_msgset_count (msgset, &n) == 0 && n == 1)
mu_msgset_foreach_msguid (msgset, printheader, (void*) path);
}
if (!mu_msgset_is_empty (msgset))
mh_mailbox_set_cur (mbox, mh_msgset_last (msgset, RET_UID));
mu_mailbox_close (mbox);
mu_mailbox_destroy (&mbox);
mu_stream_flush (mu_strout);
if (!showproc)
{
showproc = getenv ("PAGER");
if (!showproc)
showproc = "/usr/bin/more";
}
finisarg ();
execvp (showproc, showargv);
mu_error (_("unable to exec %s: %s"), showargv[0], mu_strerror (errno));
return 1;
}
|