File: msglist.c

package info (click to toggle)
mailutils 1%3A3.20-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,932 kB
  • sloc: ansic: 187,772; sh: 111,430; yacc: 7,463; cpp: 3,834; makefile: 3,166; lex: 1,972; python: 1,617; exp: 1,563; awk: 152; lisp: 132; sed: 31
file content (269 lines) | stat: -rw-r--r-- 6,056 bytes parent folder | download | duplicates (2)
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
/* 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/>. */

#include "readmsg.h"

static int
addset (int **set, int *n, unsigned val)
{
  int *tmp;
  tmp = realloc (*set, (*n + 1) * sizeof (**set));
  if (tmp == NULL)
    {
      if (*set)
        free (*set);
      *n = 0;
      *set = NULL;
      return ENOMEM;
    }
  *set = tmp;
  (*set)[*n] = val;
  (*n)++;
  return 0;
}

static int
is_number (const char *s)
{
  int result = 1;
  if (*s == '\0')
    result = 0;
  for (; *s; s++)
    {
      if (!mu_isdigit ((unsigned char)*s))
	{
	  result = 0;
	  break;
	}
    }
  return result;
}

static int
match_hdr (mu_header_t hdr, void *pattern)
{
  int rc;
  mu_iterator_t itr;
  int result = 1;
  
  rc = mu_header_get_iterator (hdr, &itr);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_header_get_iterator", NULL, rc);
      return 1;
    }
  
  for (mu_iterator_first (itr); result && !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      const char *value;
      char *tmp, *s;
      
      mu_iterator_current (itr, (void**)&value);
      tmp = strdup (value);
      if (!tmp)
	{
	  mu_diag_funcall (MU_DIAG_ERROR, "strdup", NULL, errno);
	  continue;
	}
      rc = mu_string_unfold (tmp, NULL);
      if (rc)
	{
	  free (tmp);
	  mu_diag_funcall (MU_DIAG_ERROR, "mu_string_unfold", NULL, rc);
	  continue;
	}
      rc = mu_rfc2047_decode ("utf-8", tmp, &s);
      free (tmp);
      if (rc)
	{
	  mu_diag_funcall (MU_DIAG_ERROR, "mu_rfc2047_decode", NULL, rc);
	  continue;
	}	  
      tmp = s;
      if (pattern_match (pattern, tmp))
	result = 0;
      free (tmp);
    }
  mu_iterator_destroy (&itr);
  return result;
}

static int
matchmsgpart (mu_message_t msg, void *pattern)
{
  mu_header_t hdr;
  mu_stream_t str;
  char *buf = NULL;
  size_t bufsize = 0;
  size_t n;
  int result = 1;
  int rc;
  
  /* Get the headers. */
  rc = mu_message_get_header (msg, &hdr);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_message_get_header", NULL, rc);
      return 1;
    }
  if (match_hdr (hdr, pattern) == 0)
    return 0;

  if (message_body_stream (msg, 0, "utf-8", &str))
    return 1;

  /* Look for a matching line. */
  while ((rc = mu_stream_getline (str, &buf, &bufsize, &n)) == 0 &&
	 n > 0)
    {
      buf[n-1] = 0;
      if (pattern_match (pattern, buf))
	{
	  result = 0;
	  break;
	}
    }

  free (buf);
  mu_stream_destroy (&str);
  return result;
}

static int
matchmsg (mu_message_t msg, void *pattern)
{
  int rc;
  mu_header_t hdr;
  mu_iterator_t itr;
  int result = 1;

  mu_message_get_header (msg, &hdr);
  if (match_hdr (hdr, pattern) == 0)
    return 0;
  rc = mu_message_get_iterator (msg, &itr);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_message_get_iterator", NULL, rc);
      return 0;
    }
  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      mu_message_t partmsg;
      
      rc = mu_iterator_current (itr, (void**)&partmsg);
      if (rc)
	{
	  mu_diag_funcall (MU_DIAG_ERROR, "mu_iterator_current", NULL, rc);
	  continue;
	}
      if (matchmsgpart (partmsg, pattern) == 0)
	{
	  result = 0;
	  break;
	}
    }
  mu_iterator_destroy (&itr);
  return result;
}

/*
  According to ELM readmsg(1):

  1. A lone ``*'' means select all messages in the mailbox.

  2. A list of message numbers may be specified.  Values of ``0'' and
  ``$'' in the list both mean the last message in the mailbox.  For
  example:

  readmsg 1 3 0

  extracts three messages from the folder: the first, the third, and
  the last.

  3. Finally, the selection may be some text to match.  This will
  select a mail message which exactly matches the specified text. For
  example,

  readmsg staff meeting

  extracts the message which contains the words ``staff meeting.''
  Note that it will not match a message containing ``Staff Meeting'' -
  the matching is case sensitive.  Normally only the first message
  which matches the pattern will be printed.  The -a option discussed
  in a moment changes this.
*/

int
msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv,
	 int **set, int *n)
{
  int i = 0;
  size_t total = 0;

  mu_mailbox_messages_count (mbox, &total);

  for (i = 0; i < argc; i++)
    {
      /* 1. A lone ``*'' means select all messages in the mailbox. */
      if (!strcmp (argv[i], "*"))
	{
	  size_t j;
	  /* all messages */
	  for (j = 1; j <= total; j++)
	    addset (set, n, j);
	  j = argc + 1;
	}
      /* 2. A list of message numbers may be specified.  Values of
	 ``0'' and ``$'' in the list both mean the last message in the
	 mailbox. */
      else if (!strcmp (argv[i], "$") || !strcmp (argv[i], "0"))
	{
	  addset (set, n, total);
	}
      /* 3. Finally, the selection may be some text to match.  This
	 will select a mail message which exactly matches the
	 specified text. */
      else if (!is_number (argv[i]))
	{
	  size_t j;
	  void *pat = pattern_init (argv[i]);
	  if (!pat)
	    continue;
	  for (j = 1; j <= total; j++)
	    {
	      mu_message_t msg = NULL;

	      mu_mailbox_get_message (mbox, j, &msg);
	      if (matchmsg (msg, pat) == 0)
		{
		  addset (set, n, j);
		  if (!show_all)
		    break;
		}
	    }
	  pattern_free (pat);
	}
      else if (mu_isdigit (argv[i][0]))
	{
	  /* single message */
	  addset (set, n, strtol (argv[i], NULL, 10));
	}
    }

  return 0;
}