Commit 1cee2a45 1cee2a4592e502a9d49991074b23afd29d4cdd33 by Alain Magloire

from/from.c

new file
1 parent 39429508
...@@ -103,327 +103,83 @@ This document was produced for version @value{VERSION} of @sc{gnu} ...@@ -103,327 +103,83 @@ This document was produced for version @value{VERSION} of @sc{gnu}
103 103
104 @menu 104 @menu
105 * Introduction:: GNU @sc{Mailutils} Programmer's manual. 105 * Introduction:: GNU @sc{Mailutils} Programmer's manual.
106 * Maildrop:: Creating a maildrop.
107 * URL:: Unified Ressource Locator.
108 * Maildrop Macros:: Maildrop Helper Macros.
109 * Mailbox:: Mailbox API. 106 * Mailbox:: Mailbox API.
110 * Mailbox Macros:: Mailbox Helper Macros. 107 * Mailer:: Protocol Use to Send Mail.
108 * Maildrop:: Creating a Maildrop.
109 * URL:: Unified Ressource Locator.
111 * Headers:: Headers API. 110 * Headers:: Headers API.
112 * Headers Macros:: Header helper Macros.
113 * Headers Parsed:: Alternatives for Headers.
114 * Headers Regex:: ERE with Headers.
115 * Mime:: Mime API. 111 * Mime:: Mime API.
116 * Encoding:: Encoding API. 112 * Encoding:: Encoding API.
117 * RFC1522:: RFC1522
118 * Quoted Printable:: QP Encoding
119 * Base64:: B64 Encoding
120 * Reporting Bugs:: Reporting Bugs. 113 * Reporting Bugs:: Reporting Bugs.
121 * Acknowledgement:: Thanks and Credits. 114 * Acknowledgement:: Thanks and Credits.
122 * Concept Index:: Topics in this manual. 115 * Concept Index:: Topics in this Manual.
123 * Index:: All @sc{Mailutils} functions. 116 * Index:: All @sc{Mailutils} Functions.
124 @end menu 117 @end menu
125 118
126 @node Introduction, Maildrop, Top, Top 119 @node Introduction, Mailbox, Top, Top
127 @comment node-name, next, previous, up 120 @comment node-name, next, previous, up
128 @chapter Introduction 121 @chapter Introduction
129 @cindex Introduction 122 @cindex Introduction
130 123
131 @sc{gnu} @sc{Mailutils} offers a general purpose library aimed to provide 124 @sc{gnu} @sc{Mailutils} offers a general purpose library aimed to provide
132 a rich set of functions for accessing different mail formats and maildrops. 125 a rich set of functions for accessing different mailbox formats and mailers.
133 For example writing a simple from command that will list @var{From} and 126 For example writing a simple from command that will list @var{From} and
134 @var{Subject} of a folder. 127 @var{Subject} of a folder.
135 128
136 @example 129 @example
137 /* sfrom, Simple From */ 130 @include sfrom.c.texi
138 #include <stdio.h>
139 #include <unistd.h>
140 #include <string.h>
141 #include <mailutils.h>
142
143 #define MAILDIR "/usr/spool/mail"
144 #define DATA_SIZE 4096
145
146 int main (int argc, char ** argv)
147 @{
148 char buffer[DATA_SIZE];
149 char * data;
150 maildrop_t mdrop;
151 mailbox_t mbox;
152 header_t rfc822;
153 int status, id, msg_no;
154 int size = DATA_SIZE;
155
156 if (argc == 2)
157 strncpy (buffer, argv[1], sizeof(buffer);
158 else
159 @{
160 char * maildir = getenv ("MAILDIR");
161 char * logname = getlogin();
162 snprintf (buffer, sizeof (buffer), "%s/%s",
163 (maildir) ? maildir : MAILDIR,
164 (logname) ? logname : "nobody");
165 @}
166
167 status = maildrop_create (&mdrop, NULL, buffer, 0);
168 if (status != 0)
169 @{
170 fprintf (stderr, "maildrop_create(%s), %s\n",
171 buffer, strerror(status));
172 exit (EXIT_FAILURE);
173 @}
174
175 maildrop_get_mailbox (mdrop, &mbox);
176
177 status = mailbox_open (mbox);
178 if (status != 0)
179 @{
180 fprintf (stderr, "mailbox_open(%s), %s\n",
181 buffer, strerror(status));
182 exit (EXIT_FAILURE);
183 @}
184
185 header_create (HEADER_RFC822 | HEADER_RFC1522, &rfc822);
186
187 data = xmalloc (size);
188
189 mailbox_stat (mbox, &msg_no, NULL);
190
191 for (id = 0; id < msg_no; id++)
192 @{
193 mailbox_get_header_size (mbox, id, &hd_size));
194 if (hd_size > size)
195 @{
196 data = xrealloc (data, hd_size);
197 size = hd_size;
198 @}
199 mailbox_get_header (mbox, id, data, size);
200 header_get_value(rfc822, "From", buffer, sizeof (buffer);
201 printf ("%s \t", buffer);
202 header_get_value(rfc822, "Subject", buffer, sizeof (buffer);
203 printf ("%s\n", buffer);
204 @}
205
206 free (data);
207 header_destroy (rfc822);
208 return maildrop_destroy (mdrop);
209 @}
210 @end example 131 @end example
211 132
212 @node Maildrop, URL, Introduction, Top 133 @node Mailbox, Mailer, Introduction, Top
213 @comment node-name, next, previous, up
214 @chapter Maildrop
215 @cindex Maildrop
216
217 For sending or receiving mail you must create a maildrop indicating the
218 address/folder, this is done by calling @code{maildrop_create} and
219 giving it an @var{url}.
220
221 @findex maildrop_create
222 @findex maildrop_destroy
223 @example
224
225 int maildrop_create (maildrop_t * @var{mid}, const char * @var{url}
226 char * @var{address}, int @var{flags})
227 int maildrop_destroy (maildrop_t * @var{mid})
228
229 @end example
230
231 @noindent
232 @var{mid} will contain maildrop id,
233 @var{url} a url object,
234 @var{addr} is a string that contains the maildrop address,
235 @var{flags} specify execution flags.
236
237 @example
238 struct _maildrop
239 @{
240 /* PRIVATE */
241 url_t url;
242 mailbox_t mbox;
243
244 /* PUBLIC */
245 int (*_get_mailbox) (maildrop_t, mailbox_t *);
246 int (*_get_url) (maildrop_t, url_t *);
247 @};
248
249 typedef struct _maildrop * maildrop_t;
250
251 @end example
252
253 @node URL, Maildrop Macros, Maildrop, Top
254 @comment node-name, next, previous, up
255 @subsection URL
256 @cindex URL
257
258 See rfc2368, rfc2369, rfc2384
259
260 @node Maildrop Macros, Mailbox, URL, Top
261 @comment node-name, next, previous, up
262 @subsection Maildrop Macros
263 @cindex Maildrop Macros
264
265 The predefined macros --taken directly from @file{mailutils.h}--are:
266 @findex maildrop_get_mailbox
267 @findex maildrop_get_url
268 @example
269
270 __inline__ int maildrop_get_mailbox (maildrop_t mdrop, mailbox_t * mbox)
271 @{ *mbox = mdrop->mbox; return (*mbox) 0 : EINVAL; @}
272
273 __inline__ int maildrop_get_url (maildrop_t mdrop, url_t * url)
274 @{ *url = mdrop->url; return (*url) 0 : EINVAL; @}
275
276 @end example
277
278 @node Mailbox, Mailbox Macros, Maildrop Macros, Top
279 @comment node-name, next, previous, up 134 @comment node-name, next, previous, up
280 @chapter Mailbox 135 @chapter Mailbox
281 @cindex Mailbox 136 @cindex Mailbox
282 137
283 Those are different formats to access a folder. 138 @include mailbox.texi
284
285 @table @samp
286 @item POP3
287 Post Office Protocol, Not Implemented.
288 @item IMAP
289 Not Implemented
290 @item QMAIL
291 Not Implemented
292 @item UNIX
293 Sketchy
294 @end table
295
296 @findex mailbox_create
297 @findex mailbox_destroy
298 @example
299
300 int mailbox_create (mailbox_t * @var{mailbox}, int @var{flags})
301
302 int maildrop_destroy (mailbox_t * @var{mailbox})
303
304 @end example
305 139
306 All implementation shall provide the minimum set of the API : 140 @node Mailer, Maildrop , Mailbox, Top
307
308 @example
309
310 struct _mailbox
311 @{
312 /* Private Data */
313 .....
314 /* Public API */
315 int (*_open) (url_t);
316 int (*_close) (mailbox_t);
317 int (*_set_header) (mailbox_t, size_t *, char *, size_t);
318 int (*_get_header) (mailbox_t, size_t, char *, size_t);
319 int (*_get_header_size) (mailbox_t, size_t, size_t *);
320 int (*_delete) (mailbox_t, size_t);
321 int (*_undelete) (mailbox_t, size_t);
322 int (*_set_body) (mailbox_t, size_t, char *, size_t);
323 int (*_get_body) (mailbox_t, size_t, char *, size_t);
324 int (*_get_body_size) (mailbox_t, size_t, size_t *);
325 int (*_set_timeout) (mailbox_t, size_t);
326 int (*_get_timeout) (mailbox_t, size_t *);
327 int (*_send_envelop) (mailbox_t, size_t, size_t);
328 int (*_stat) (mailbox_t, size_t *, size_t *);
329 int (*_update) (mailbox_t);
330 @};
331
332 typedef struct _mailbox * mailbox_t;
333 @end example
334
335 @node Mailbox Macros, Headers, Mailbox, Top
336 @comment node-name, next, previous, up 141 @comment node-name, next, previous, up
337 @subsection Mailbox Macros 142 @chapter Mailer
338 @cindex Mailbox Macros 143 @cindex Mailer
339 144
340 @findex maildrop_get_header 145 @include mailer.texi
341 @findex maildrop_get_header_size
342 @example
343
344 __inline__ int mailbox_get_header (mailbox_t mbox, int id, char * hdr, int sz)
345 @{ return mbox->_get_header (mbox, id, hdr, sz); @}
346 146
347 __inline__ int mailbox_get_header_size (mailbox_t mbox, int id, int * sz) 147 @node Maildrop , URL, Mailer, Top
348 @{ return mbox->_get_header_size (mbox, id, sz); @}
349
350 @end example
351
352 @node Headers, Headers Macros, Mailbox Macros, Top
353 @comment node-name, next, previous, up 148 @comment node-name, next, previous, up
354 @chapter Headers 149 @chapter Maildrop
355 @cindex Headers 150 @cindex Maildrop
356
357 So far we plan support for RFC822 and RFC1522;
358
359 @example
360 int header_create (int flags, header_t * header);
361
362 int header_destroy (header_t header);
363
364 struct _header
365 @{
366 /* Private */
367 ...
368 /* Public */
369 char * (*_get_value) (header_t, char *);
370 @};
371
372 typedef struct _header * header_t;
373 @end example
374
375 @node Headers Macros, Headers Parsed, Headers, Top
376 @comment node-name, next, previous, up
377 @subsection Headers Macros
378 @cindex Headers Macros
379 151
380 @example 152 @include maildrop.texi
381 __inline__ int header_get_value (header_t hdr, char * h, char buf, size_t sz)
382 @{ return hdr->_get_value (hdr, h, buf, sz); @}
383 @end example
384 153
385 @node Headers Parsed, Headers Regex, Headers Macros, Top 154 @node URL, Headers , Maildrop, Top
386 @comment node-name, next, previous, up 155 @comment node-name, next, previous, up
387 @subsection Headers Parsed 156 @chapter URL
388 @cindex Headers Parsed 157 @cindex URL
389 158
390 Return a hash table, a dictionnary, ??? 159 @include url.texi
391 160
392 @node Headers Regex, Mime, Headers Parsed, Top 161 @node Headers, Mime, URL , Top
393 @comment node-name, next, previous, up 162 @comment node-name, next, previous, up
394 @subsection Headers Parsed 163 @chapter Headers
395 @cindex Headers Parsed 164 @cindex Headers
396 165
397 Using regular expression ? 166 @include headers.texi
398 167
399 @node Mime, Encoding, Headers Regex, Top 168 @node Mime , Encoding, Headers, Top
400 @comment node-name, next, previous, up 169 @comment node-name, next, previous, up
401 @chapter Mime 170 @subsection Mime
402 @cindex Mime 171 @cindex Mime
403 172
404 Mime stuff in the Body. 173 @include mime.texi
405 174
406 @node Encoding, RFC1522, Mime, Top 175 @node Encoding, Reporting Bugs, Mime, Top
407 @comment node-name, next, previous, up 176 @comment node-name, next, previous, up
408 @chapter Encoding 177 @chapter Encoding
409 @cindex Encoding 178 @cindex Encoding
410 179
411 @node RFC1522, Quoted Printable, Encoding, Top 180 @include encoding.texi
412 @comment node-name, next, previous, up
413 @subsection RFC1522
414 @cindex RFC1522
415
416 @node Quoted Printable, Base64, RFC1522, Top
417 @comment node-name, next, previous, up
418 @subsection Quoted Printable
419 @cindex Quoted Printable
420
421 @node Base64, Reporting Bugs, Quoted Printable, Top
422 @comment node-name, next, previous, up
423 @subsection Base64
424 @cindex Base64
425 181
426 @node Reporting Bugs, Acknowledgement, Base64, Top 182 @node Reporting Bugs, Acknowledgement, Encoding, Top
427 @comment node-name, next, previous, up 183 @comment node-name, next, previous, up
428 @chapter Reporting Bugs 184 @chapter Reporting Bugs
429 @cindex Reporting Bugs 185 @cindex Reporting Bugs
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 /**
19 *
20 * Created as an example of using libmailbox
21 * Sean 'Shaleh' Perry <shaleh@debian.org>, 1999
22 * Alain Magloire alainm@gnu.org
23 *
24 **/
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <mailbox.h>
31 #include <sys/types.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #ifdef HAVE_PATHS_H
38 # include <paths.h>
39 #endif
40
41 #include <paths.h>
42
43 #ifndef _PATH_MAILDIR
44 # define _PATH_MAILDIR "/var/spool/mail"
45 #endif
46
47 #ifndef VERSION
48 # define VERSION "unknow"
49 #endif
50
51 #include "getopt.h"
52
53 /* Short options. */
54 static char const short_options[] =
55 "H:s:m:hv";
56
57 static int opt_from;
58 static int opt_to;
59 static int opt_cc;
60 static int opt_date;
61 static int opt_subject;
62 static int opt_status_new;;
63 static int opt_status_read;;
64 static int opt_status_old;;
65 static int opt_size;
66 static int opt_number;
67 static int opt_mailbox;
68
69 /* long options equivalence */
70 static struct option long_options[] =
71 {
72 {"date", no_argument, &opt_date, 1},
73 {"from", no_argument, &opt_from, 1},
74 {"to", no_argument, &opt_to, 1},
75 {"cc", no_argument, &opt_cc, 1},
76 {"subject", no_argument, &opt_subject, 1},
77 {"header", required_argument, NULL, 'H'},
78 {"status", required_argument, NULL, 's'},
79 {"size", no_argument, &opt_size, 1},
80 {"number", no_argument, &opt_number, 1},
81 {"mailbox", required_argument, &opt_mailbox, 'm'},
82 {"help", no_argument, NULL, 'h'},
83 {"version", no_argument, NULL, 'v'},
84 {NULL, 0, NULL, 0}
85 };
86
87 /* program name */
88 static char *program;
89
90 static void
91 usage (int type)
92 {
93 switch (type)
94 {
95 case 1:
96 printf("%s (GNU mailutils), version %s\n", program, VERSION);
97 break;
98 case 2:
99 /* Fall Through */
100 default:
101 printf ("Usage: %s [OPTION] [mailbox]\n", program);
102 printf ("Retrieve information from a mailbox\n\
103 \n\
104 --date equivalent to --header=Date\n\
105 --from equivalent to --header=From\n\
106 --to equivalent to --header=To\n\
107 --cc equivalent to --header=Cc\n\
108 --subject equivalent to --header=Subject\n\
109 --header=HEADER Specify the header to display\n\
110 HEADER is 'Date', 'From', 'To', 'Cc', 'Subject'\n\
111 default is --header=From --header=Subject\n\
112 --status=STATUS Display only message with a given status\n\
113 STATUS is 'new', 'read', 'unread'\n\
114 defaut is --status=new --status=read --status=unread\n\
115 --size Display mailbox size\n\
116 --number Display total of number of messages\n\
117 --mailbox=MAILBOX Specify another mailbox\n\
118 MAILBOX is url(pop://pop.server/user),
119 a file /var/mail/user, or user.
120 -v, --version Print version information and exit\n\
121 -h, --help Show this message\n\
122 \n\
123 Mailbox :
124 $MAIL is check for the default mailbox path, if not set
125 the environ variables $USER or $LOGNAME are use in the default mail spool.\n\
126 \n\
127 Report bugs to <bug-mailutils@gnu.org>.\n");
128 }
129 }
130
131 int
132 main(int argc, char *argv[])
133 {
134 mailbox_t mbox;
135 size_t rvalue, i;
136 size_t count = 0, size;
137 char *user = NULL;
138 char *mailbox_name = NULL;
139 int opt;
140 char buffer[BUFSIZ];
141
142 /* set program name */
143 program = argv[0];
144 if (program && strrchr (program, '/'))
145 {
146 program = strrchr (program, '/') + 1;
147 }
148
149 while ((opt = getopt_long (argc, argv, short_options, long_options, NULL))
150 != -1)
151 {
152 switch (opt)
153 {
154 case 'H':
155 if (strcasecmp (optarg, "From") == 0)
156 opt_from = 1;
157 else if (strcasecmp (optarg, "To") == 0)
158 opt_to = 1;
159 else if (strcasecmp (optarg, "Cc") == 0)
160 opt_cc = 1;
161 else if (strcasecmp (optarg, "Date") == 0)
162 opt_date = 1;
163 else if (strcasecmp (optarg, "Subject") == 0)
164 opt_subject = 1;
165 else
166 {
167 fprintf (stderr, "Unknown header\n");
168 }
169 break;
170 case 's':
171 if (strcasecmp (optarg, "new") == 0)
172 opt_status_new = 1;
173 else if (strcasecmp (optarg, "read") == 0)
174 opt_status_read = 1;
175 else if (strcasecmp (optarg, "old") == 0)
176 opt_status_old = 1;
177 else
178 {
179 fprintf (stderr, "Unknown status\n");
180 }
181 break;
182 case 'm':
183 mailbox_name = optarg;
184 break;
185 case 'v':
186 usage (1);
187 exit (0);
188 break;
189 case 'h':
190 usage (2);
191 exit (0);
192 break;
193 default:
194 usage (2);
195 exit (1);
196 break;
197 }
198 }
199
200 /* have an argument */
201 if (optind > 0)
202 {
203 mailbox_name = argv[optind];
204 /* is it a URL */
205 if (strchr (mailbox_name, ':') == NULL)
206 {
207 /* is it a filename */
208 if (mailbox_name[0] != '/')
209 {
210 user = mailbox_name; /* a user name */
211 mailbox_name = NULL;
212 }
213 }
214 }
215 else if (getenv ("MAIL"))
216 {
217 mailbox_name = getenv ("MAIL");
218 }
219 else
220 {
221 user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER");
222 if (user == NULL)
223 {
224 fprintf (stderr, "who am I?\n");
225 exit (1);
226 }
227 }
228 if (user)
229 {
230 snprintf (buffer, sizeof(buffer), "%s/%s", _PATH_MAILDIR, user);
231 mailbox_name = buffer;
232 }
233 if (mailbox_init (&mbox, mailbox_name, 0) != 0
234 || mailbox_open (mbox, MU_MB_RDONLY) != 0)
235 {
236 fprintf (stderr, "could not open\n");
237 exit (2);
238 }
239 mailbox_scan (mbox, &count);
240 for(i = 0; i < count; ++i) {
241 rvalue = mailbox_get_header (mbox, i, 0, buffer, sizeof (buffer), &size);
242 if (rvalue != 0)
243 {
244 fprintf (stderr, "header %s\n", strerror (rvalue));
245 exit(2);
246 }
247 printf("%s\n", buffer);
248 }
249 mailbox_close(mbox);
250 mailbox_destroy(&mbox);
251 exit(0);
252 }