from.c
6.49 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
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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/**
*
* Created as an example for using mailutils API
* Sean 'Shaleh' Perry <shaleh@debian.org>, 1999
* Alain Magloire alainm@gnu.org
*
**/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif
#include <mailbox.h>
#include <header.h>
/* FIXME: this should be in the mailbox header */
#ifndef _PATH_MAILDIR
# define _PATH_MAILDIR "/var/spool/mail"
#endif
#ifndef VERSION
# define VERSION "unknown"
#endif
#include "getopt.h"
/* Short options. */
static char const short_options[] =
"H:s:m:hv";
static int opt_from;
static int opt_to;
static int opt_cc;
static int opt_date;
static int opt_subject;
static int opt_status_new;;
static int opt_status_read;;
static int opt_status_old;;
static int opt_size;
static int opt_number;
static int opt_mailbox;
/* long options equivalence */
static struct option long_options[] =
{
{"date", no_argument, &opt_date, 1},
{"from", no_argument, &opt_from, 1},
{"to", no_argument, &opt_to, 1},
{"cc", no_argument, &opt_cc, 1},
{"subject", no_argument, &opt_subject, 1},
{"header", required_argument, NULL, 'H'},
{"status", required_argument, NULL, 's'},
{"size", no_argument, &opt_size, 1},
{"number", no_argument, &opt_number, 1},
{"mailbox", required_argument, &opt_mailbox, 'm'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
/* program name */
static char *program;
static void
usage (int type)
{
switch (type)
{
case 1:
printf("%s (GNU mailutils), version %s\n", program, VERSION);
break;
case 2:
/* Fall Through */
default:
printf ("Usage: %s [OPTION] [mailbox]\n", program);
printf ("Retrieve information from a mailbox\n\
\n\
--date equivalent to --header=Date\n\
--from equivalent to --header=From\n\
--to equivalent to --header=To\n\
--cc equivalent to --header=Cc\n\
--subject equivalent to --header=Subject\n\
--header=HEADER Specify the header to display\n\
HEADER is 'Date', 'From', 'To', 'Cc', 'Subject'\n\
default is --header=From --header=Subject\n\
--status=STATUS Display only message with a given status\n\
STATUS is 'new', 'read', 'unread'\n\
defaut is --status=new --status=read --status=unread\n\
--size Display mailbox size\n\
--number Display total of number of messages\n\
--mailbox=MAILBOX Specify another mailbox\n\
MAILBOX is url(pop://pop.server/user),
a file /var/mail/user, or user.
-v, --version Print version information and exit\n\
-h, --help Show this message\n\
\n\
Mailbox :
$MAIL is check for the default mailbox path, if not set
the environ variables $USER or $LOGNAME are use in the default mail spool.\n\
\n\
Report bugs to <bug-mailutils@gnu.org>.\n");
}
}
int
main(int argc, char *argv[])
{
mailbox_t mbox;
size_t rvalue, i;
size_t count = 0, size;
char *user = NULL;
char *mailbox_name = NULL;
int opt;
char buffer[BUFSIZ];
char from[BUFSIZ];
char subject[BUFSIZ];
/* set program name */
program = argv[0];
if (program && strrchr (program, '/'))
{
program = strrchr (program, '/') + 1;
}
while ((opt = getopt_long (argc, argv, short_options, long_options, NULL))
!= -1)
{
switch (opt)
{
case 'H':
if (strcasecmp (optarg, "From") == 0)
opt_from = 1;
else if (strcasecmp (optarg, "To") == 0)
opt_to = 1;
else if (strcasecmp (optarg, "Cc") == 0)
opt_cc = 1;
else if (strcasecmp (optarg, "Date") == 0)
opt_date = 1;
else if (strcasecmp (optarg, "Subject") == 0)
opt_subject = 1;
else
{
fprintf (stderr, "Unknown header\n");
}
break;
case 's':
if (strcasecmp (optarg, "new") == 0)
opt_status_new = 1;
else if (strcasecmp (optarg, "read") == 0)
opt_status_read = 1;
else if (strcasecmp (optarg, "old") == 0)
opt_status_old = 1;
else
{
fprintf (stderr, "Unknown status\n");
}
break;
case 'm':
mailbox_name = optarg;
break;
case 'v':
usage (1);
exit (0);
break;
case 'h':
usage (2);
exit (0);
break;
default:
//usage (2);
//exit (1);
break;
}
}
/* have an argument */
if (optind > argc)
{
mailbox_name = argv[optind];
/* is it a URL */
if (strchr (mailbox_name, ':') == NULL)
{
/* is it a filename */
if (mailbox_name[0] != '/')
{
user = mailbox_name; /* a user name */
mailbox_name = NULL;
}
}
}
else if (getenv ("MAIL"))
{
mailbox_name = getenv ("MAIL");
}
else
{
user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER");
if (user == NULL)
{
fprintf (stderr, "who am I?\n");
exit (1);
}
}
if (user)
{
snprintf (buffer, sizeof(buffer), "%s/%s", _PATH_MAILDIR, user);
mailbox_name = buffer;
}
if (mailbox_init (&mbox, mailbox_name, 0) != 0
|| mailbox_open (mbox, MU_MB_RDONLY) != 0)
{
fprintf (stderr, "could not open\n");
exit (2);
}
mailbox_scan (mbox, &count);
for(i = 0; i < count; ++i) {
rvalue = mailbox_get_header (mbox, i, 0, buffer, sizeof (buffer), &size);
if (rvalue != 0)
{
fprintf (stderr, "header %s\n", strerror (rvalue));
exit(2);
}
header_gvalue (buffer, size, MU_HDR_FROM, from, sizeof (from), NULL);
header_gvalue (buffer, size, MU_HDR_SUBJECT, subject,
sizeof (subject), NULL);
printf("%s %s\n", from, subject);
}
mailbox_close(mbox);
mailbox_destroy(&mbox);
exit(0);
}