msglist.c
3.78 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2005 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 2, 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#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 (!isdigit ((unsigned char)*s))
{
result = 0;
break;
}
}
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 (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"))
{
size_t j;
mu_mailbox_messages_count (mbox, &total);
for (j = 1; j < total; j++)
addset (set, n, j);
}
/* 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;
int found = 0;
for (j = 1; j <= total; j++)
{
char buf[128];
size_t len = 0;
off_t offset = 0;
message_t msg = NULL;
stream_t stream = NULL;
mu_mailbox_get_message (mbox, j, &msg);
message_get_stream (msg, &stream);
while (stream_readline (stream, buf, sizeof buf, offset, &len) == 0 && len > 0)
{
if (strstr (buf, argv[i]) != NULL)
{
addset (set, n, j);
found = 1;
break;
}
offset += len;
}
if (found && !show_all)
break;
}
}
else if (isdigit (argv[i][0]))
{
/* single message */
addset (set, n, strtol (argv[i], NULL, 10));
}
}
return 0;
}