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
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-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 scan command */
#include <mh.h>
#ifdef HAVE_TERMCAP_H
# include <termcap.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <mailutils/observer.h>
static char prog_doc[] = N_("Produce a one line per message scan listing");
static char args_doc[] = N_("[MSGLIST]");
static int clear;
static int width;
static int reverse;
static int header;
static mh_format_t format;
static mh_fvm_t fvm;
static mu_msgset_t msgset;
static struct mu_option options[] = {
{ "clear", 0, NULL, MU_OPTION_DEFAULT,
N_("clear screen after displaying the list"),
mu_c_bool, &clear },
{ "form", 0, N_("FILE"), MU_OPTION_DEFAULT,
N_("read format from given file"),
mu_c_string, &format, mh_opt_parse_formfile },
{ "format", 0, N_("FORMAT"), MU_OPTION_DEFAULT,
N_("use this format string"),
mu_c_string, &format, mh_opt_parse_format },
{ "header", 0, NULL, MU_OPTION_DEFAULT,
N_("display header"),
mu_c_bool, &header },
{ "width", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
N_("set output width"),
mu_c_int, &width },
{ "reverse", 0, NULL, MU_OPTION_DEFAULT,
N_("list messages in reverse order"),
mu_c_bool, &reverse },
{ "file", 0, N_("FILE"), MU_OPTION_HIDDEN,
N_("[not yet implemented]"),
mu_c_string, NULL, mh_opt_notimpl },
MU_OPTION_END
};
static void print_header (mu_mailbox_t mbox);
static void clear_screen (void);
static int
list_message (size_t num MU_ARG_UNUSED, mu_message_t msg,
void *data MU_ARG_UNUSED)
{
mh_fvm_run (fvm, msg);
return 0;
}
/* Observable Action this is called at every message discover. */
static int
action (mu_observer_t o, size_t type, void *data, void *action_data)
{
static int counter;
mu_mailbox_t mbox;
mu_message_t msg = NULL;
if (type == MU_EVT_MESSAGE_ADD)
{
mbox = mu_observer_get_owner (o);
counter++;
mu_mailbox_get_message (mbox, counter, &msg);
mh_fvm_run (fvm, msg);
}
return 0;
}
int
main (int argc, char **argv)
{
mu_mailbox_t mbox;
int status;
size_t total = 0;
mh_getopt (&argc, &argv, options, MH_GETOPT_DEFAULT_FOLDER,
args_doc, prog_doc, NULL);
if (!format)
format = mh_scan_format ();
mh_fvm_create (&fvm, MH_FMT_FORCENL);
mh_fvm_set_format (fvm, format);
mh_fvm_set_width (fvm, width ? width : mh_width ());
mh_format_destroy (&format);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_READ);
if ((argc == 0 || strcmp (argv[0], "all") == 0) && !reverse)
{
/* Fast approach */
mu_observer_t observer;
mu_observable_t observable;
print_header (mbox);
mu_observer_create (&observer, mbox);
mu_observer_set_action (observer, action, mbox);
mu_mailbox_get_observable (mbox, &observable);
mu_observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
status = mu_mailbox_scan (mbox, 1, &total);
}
else
{
mu_mailbox_messages_count (mbox, &total);
mh_msgset_parse (&msgset, mbox, argc, argv, "all");
print_header (mbox);
status = mu_msgset_foreach_dir_message (msgset, reverse,
list_message, NULL);
}
if (total == 0)
{
mu_url_t url = NULL;
mu_mailbox_get_url (mbox, &url);
mu_error (_("no messages in %s"), mu_url_to_string (url));
}
clear_screen ();
mh_fvm_destroy (&fvm);
mh_global_save_state ();
mu_mailbox_close (mbox);
mu_mailbox_destroy (&mbox);
return status;
}
static void
print_header (mu_mailbox_t mbox)
{
if (header)
{
mu_url_t url = NULL;
char datestr[64];
time_t t;
mu_mailbox_get_url (mbox, &url);
time (&t);
strftime (datestr, sizeof datestr, "%c", localtime (&t));
printf (_("Folder %s %s\n"), mu_url_to_string (url), datestr);
}
}
#ifdef HAVE_TERMCAP_H
/* A subroutine for tputs */
int
putstdout(char c)
{
return putc(c, stdout);
}
#endif
static void
clear_screen (void)
{
if (clear)
{
#ifdef HAVE_TERMCAP_H
if (isatty (1))
{
char termcap_buf[1024];
char *buffer = termcap_buf;
char *termname;
if ((termname = getenv("TERM")) == NULL)
/* No terminal; Try ansi */
termname = "ansi";
if (tgetent(termcap_buf, termname) == 1)
{
char *clr = tgetstr ("cl", &buffer);
if (clr)
{
tputs(clr, 1, (int (*)())putstdout);
return;
}
}
}
#endif
/* Fall back to formfeed */
fprintf (stdout, "\f");
}
}
|