File: rfc822.html

package info (click to toggle)
courier-authlib 0.58-4%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,212 kB
  • ctags: 1,896
  • sloc: ansic: 21,550; sh: 14,071; makefile: 866; perl: 842; cpp: 284
file content (1392 lines) | stat: -rw-r--r-- 25,950 bytes parent folder | download | duplicates (6)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML
><HEAD
><link rel='stylesheet' type='text/css' href='manpage.css'>
  <!-- $Id: rfc822.sgml,v 1.1 2001/12/23 19:37:14 mrsam Exp $ -->
  <!-- Copyright 2001 Double Precision, Inc.  See COPYING for -->
  <!-- distribution information. -->
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<link rel="icon" href="icon.gif" type="image/gif" />
<TITLE
>rfc822</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"></HEAD
><BODY
CLASS="REFENTRY"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><H1
><A
NAME="RFC822"
></A
>rfc822</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN10"
></A
><H2
>Name</H2
>rfc822&nbsp;--&nbsp;RFC 822 parsing library</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN13"
></A
><H2
>Synopsis</H2
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN14"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#include &#60;rfc822.h&#62;

#include &#60;rfc2047.h&#62;

cc ... -lrfc822</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN16"
></A
><H2
>DESCRIPTION</H2
><P
>The rfc822 library provides functions for parsing E-mail headers in the RFC
822 format. This library also includes some functions to help with encoding
and decoding 8-bit text, as defined by RFC 2047.</P
><P
>The format used by E-mail headers to encode sender and recipient
information is defined by
<A
HREF="http://www.rfc-editor.org/rfc/rfc822.txt"
TARGET="_top"
>RFC 822</A
>
(and its successor,
<A
HREF="http://www.rfc-editor.org/rfc/rfc2822.txt"
TARGET="_top"
>RFC 2822</A
>).
The format allows the actual E-mail
address and the sender/recipient name to be expressed together, for example:
<TT
CLASS="LITERAL"
>John Smith &#60;jsmith@example.com&#62;</TT
></P
><P
>The main purposes of the rfc822 library is to:</P
><P
>1) Parse a text string containing a list of RFC 822-formatted address into
its logical components: names and E-mail addresses.</P
><P
>2) Access those individual components.</P
><P
>3) Allow some limited modifications of the parsed structure, and then
convert it back into a text string.</P
><DIV
CLASS="REFSECT2"
><A
NAME="AEN27"
></A
><H3
>Tokenizing an E-mail header</H3
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN29"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>struct rfc822t *tokens=rfc822t_alloc_new(const char *header,
                void (*err_func)(const char *, int, void *),
                void *func_arg);

void rfc822t_free(tokens);</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>The <TT
CLASS="FUNCTION"
>rfc822t_alloc_new</TT
>() function (superceeds
<TT
CLASS="FUNCTION"
>rfc822t_alloc</TT
>(), which is now
obsolete) accepts an E-mail <TT
CLASS="PARAMETER"
><I
>header</I
></TT
>, and parses it into
individual tokens. This function allocates and returns a pointer to an
<CODE
CLASS="STRUCTNAME"
>rfc822t</CODE
>
structure, which is later used by
<TT
CLASS="FUNCTION"
>rfc822a_alloc</TT
>() to extract
individual addresses from these tokens.</P
><P
>If <TT
CLASS="PARAMETER"
><I
>err_func</I
></TT
> argument, if not NULL, is a pointer
to a callback
function.  The function is called in the event that the E-mail header is
corrupted to the point that it cannot even be parsed.  This is a rare instance
-- most forms of corruption are still valid at least on the lexical level. 
The only time this error is reported is in the event of mismatched
parenthesis, angle brackets, or quotes.  The callback function receives the
<TT
CLASS="PARAMETER"
><I
>header</I
></TT
> pointer, an index to the syntax error in the
header string, and the <TT
CLASS="PARAMETER"
><I
>func_arg</I
></TT
> argument.</P
><P
>The semantics of <TT
CLASS="PARAMETER"
><I
>err_func</I
></TT
> are subject to change.  It is recommended
to leave this argument as NULL in the current version of the library.</P
><P
><TT
CLASS="FUNCTION"
>rfc822t_alloc</TT
>() returns a pointer to a
dynamically-allocated <CODE
CLASS="STRUCTNAME"
>rfc822t</CODE
>
structure. A NULL pointer is returned if there's insufficient memory to
allocate this structure. The <TT
CLASS="FUNCTION"
>rfc822t_free</TT
>() function
destroys
<CODE
CLASS="STRUCTNAME"
>rfc822t</CODE
> structure and frees all
dynamically allocated memory.</P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
>NOTE:</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Until <TT
CLASS="FUNCTION"
>rfc822t_free</TT
>() is called, the contents of
<TT
CLASS="PARAMETER"
><I
>header</I
></TT
> MUST
NOT be destroyed or altered in any way. The contents of
<TT
CLASS="PARAMETER"
><I
>header</I
></TT
> are not
modified by <TT
CLASS="FUNCTION"
>rfc822t_alloc</TT
>(), however the
<CODE
CLASS="STRUCTNAME"
>rfc822t</CODE
> structure contains
pointers to portions of the supplied <TT
CLASS="PARAMETER"
><I
>header</I
></TT
>,
and they must remain valid.</P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN56"
></A
><H3
>Extracting E-mail addresses</H3
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN58"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>struct rfc822a *addrs=rfc822a_alloc(struct rfc822t *tokens);

void rfc822a_free(addrs);</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>The <TT
CLASS="FUNCTION"
>rfc822a_alloc</TT
>() function returns a
dynamically-allocated <CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
>
structure, that contains individual addresses that were logically parsed
from a <CODE
CLASS="STRUCTNAME"
>rfc822t</CODE
> structure.  The
<TT
CLASS="FUNCTION"
>rfc822a_alloc</TT
>() function returns NULL if
there was insufficient memory to allocate the <CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure. The
<TT
CLASS="FUNCTION"
>rfc822a_free</TT
>() function destroys the <CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> function, and frees all
associated dynamically-allocated memory. The <CODE
CLASS="STRUCTNAME"
>rfc822t</CODE
> structure passed
to <TT
CLASS="FUNCTION"
>rfc822a_alloc</TT
>() must not be destroyed before <TT
CLASS="FUNCTION"
>rfc822a_free</TT
>() destroys the
<CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure.</P
><P
>The <CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure has the following fields:</P
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN74"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>struct rfc822a {
        struct rfc822addr *addrs;
        int     naddrs;
} ;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>The <CODE
CLASS="STRUCTFIELD"
>naddrs</CODE
> field gives the number of
<CODE
CLASS="STRUCTNAME"
>rfc822addr</CODE
> structures
that are pointed to by <CODE
CLASS="STRUCTFIELD"
>addrs</CODE
>, which is an array.
Each <CODE
CLASS="STRUCTNAME"
>rfc822addr</CODE
>
structure represents either an address found in the original E-mail header,
<I
CLASS="EMPHASIS"
>or the contents of some legacy "syntactical sugar"</I
>.
For example, the
following is a valid E-mail header:</P
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN82"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>To: recipient-list: tom@example.com, john@example.com;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>Typically, all of this, except for "<TT
CLASS="LITERAL"
>To:</TT
>",
is tokenized by <TT
CLASS="FUNCTION"
>rfc822t_alloc</TT
>(), then parsed by
<TT
CLASS="FUNCTION"
>rfc822a_alloc</TT
>().
"<TT
CLASS="LITERAL"
>recipient-list:</TT
>" and
the trailing semicolon is a legacy mailing list specification that is no
longer in widespread use, but must still must be accounted for. The resulting
<CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure will have four
<CODE
CLASS="STRUCTNAME"
>rfc822addr</CODE
> structures: one for
"<TT
CLASS="LITERAL"
>recipient-list:</TT
>";
one for each address; and one for the trailing semicolon.
Each <CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure has the following
fields:</P
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN93"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>struct rfc822addr {
        struct rfc822token *tokens;
        struct rfc822token *name;
} ;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>If <CODE
CLASS="STRUCTFIELD"
>tokens</CODE
> is a null pointer, this structure
represents some
non-address portion of the original header, such as
"<TT
CLASS="LITERAL"
>recipient-list:</TT
>" or a
semicolon.  Otherwise it points to a structure that represents the E-mail
address in tokenized form.</P
><P
><CODE
CLASS="STRUCTFIELD"
>name</CODE
> either points to the tokenized form of a
non-address portion of
the original header, or to a tokenized form of the recipient's name.
<CODE
CLASS="STRUCTFIELD"
>name</CODE
> will be NULL if the recipient name was not provided. For the
following address:
<TT
CLASS="LITERAL"
>Tom Jones &#60;tjones@example.com&#62;</TT
> - the
<CODE
CLASS="STRUCTFIELD"
>tokens</CODE
> field points to the tokenized form of
"<TT
CLASS="LITERAL"
>tjones@example.com</TT
>",
and <CODE
CLASS="STRUCTFIELD"
>name</CODE
> points to the tokenized form of
"<TT
CLASS="LITERAL"
>Tom Jones</TT
>".</P
><P
>Each <CODE
CLASS="STRUCTNAME"
>rfc822token</CODE
> structure contains the following
fields:</P
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN108"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>struct rfc822token {
        struct rfc822token *next;
        int token;
        const char *ptr;
        int len;
} ;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>The <CODE
CLASS="STRUCTFIELD"
>next</CODE
> pointer builds a linked list of all
tokens in this name or
address.  The possible values for the <CODE
CLASS="STRUCTFIELD"
>token</CODE
> field
are:</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>0x00</DT
><DD
><P
>This is a simple atom - a sequence of non-special characters that
is delimited by whitespace or special characters (see below).</P
></DD
><DT
>0x22</DT
><DD
><P
>The value of the ascii quote - this is a quoted string.</P
></DD
><DT
>Open parenthesis: '('</DT
><DD
><P
>This is an old style comment.  A deprecated form of E-mail
addressing uses - for example -
"<TT
CLASS="LITERAL"
>john@example.com (John Smith)</TT
>" instead of
"<TT
CLASS="LITERAL"
>John Smith &#60;john@example.com&#62;</TT
>".
This old-style notation defined
parenthesized content as arbitrary comments.
The <CODE
CLASS="STRUCTNAME"
>rfc822token</CODE
> with
<CODE
CLASS="STRUCTFIELD"
>token</CODE
> set to '(' is created for the contents of
the entire comment.</P
></DD
><DT
>Symbols: '&#60;', '&#62;', '@', and many others</DT
><DD
><P
>The remaining possible values of <CODE
CLASS="STRUCTFIELD"
>token</CODE
> include all
the characters in RFC 822 headers that have special significance.</P
></DD
></DL
></DIV
><P
>When a <CODE
CLASS="STRUCTNAME"
>rfc822token</CODE
> structure does not represent a
special character, the <CODE
CLASS="STRUCTFIELD"
>ptr</CODE
> field points to a text
string giving its contents.
The contents are NOT null-terminated, the <CODE
CLASS="STRUCTFIELD"
>len</CODE
>
field contains the number of characters included.
The macro rfc822_is_atom(token) indicates whether
<CODE
CLASS="STRUCTFIELD"
>ptr</CODE
> and <CODE
CLASS="STRUCTFIELD"
>len</CODE
> are used for
the given <CODE
CLASS="STRUCTFIELD"
>token</CODE
>.
Currently <TT
CLASS="FUNCTION"
>rfc822_is_atom</TT
>() returns true if
<CODE
CLASS="STRUCTFIELD"
>token</CODE
> is a zero byte, '<TT
CLASS="LITERAL"
>"</TT
>', or
'<TT
CLASS="LITERAL"
>(</TT
>'.</P
><P
>Note that it's possible that <CODE
CLASS="STRUCTFIELD"
>len</CODE
> might be zero.
This happens with null addresses used as return addresses for delivery status
notifications.</P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN148"
></A
><H3
>Working with E-mail addresses</H3
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN150"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>void rfc822_deladdr(struct rfc822a *addrs, int index);

void rfc822tok_print(const struct rfc822token *list,
        void (*func)(char, void *), void *func_arg);

void rfc822_print(const struct rfc822a *addrs,
        void (*print_func)(char, void *),
        void (*print_separator)(const char *, void *), void *callback_arg);
 
void rfc822_addrlist(const struct rfc822a *addrs,
                void (*print_func)(char, void *),
                void *callback_arg);
 
void rfc822_namelist(const struct rfc822a *addrs,
                void (*print_func)(char, void *),
                void *callback_arg);

void rfc822_praddr(const struct rfc822a *addrs,
                int index,
                void (*print_func)(char, void *),
                void *callback_arg);

void rfc822_prname(const struct rfc822a *addrs,
                int index,
                void (*print_func)(char, void *),
                void *callback_arg);

void rfc822_prname_orlist(const struct rfc822a *addrs,
                int index,
                void (*print_func)(char, void *),
                void *callback_arg);

char *rfc822_gettok(const struct rfc822token *list);
char *rfc822_getaddrs(const struct rfc822a *addrs);
char *rfc822_getaddr(const struct rfc822a *addrs, int index);
char *rfc822_getname(const struct rfc822a *addrs, int index);
char *rfc822_getname_orlist(const struct rfc822a *addrs, int index);

char *rfc822_getaddrs_wrap(const struct rfc822a *, int);</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>These functions are used to work with individual addresses that are parsed
by <TT
CLASS="FUNCTION"
>rfc822a_alloc</TT
>().</P
><P
><TT
CLASS="FUNCTION"
>rfc822_deladdr</TT
>() removes a single
<CODE
CLASS="STRUCTNAME"
>rfc822addr</CODE
> structure, whose
<TT
CLASS="PARAMETER"
><I
>index</I
></TT
> is given, from the address array in
<CODE
CLASS="STRUCTNAME"
>rfc822addr</CODE
>.
<CODE
CLASS="STRUCTFIELD"
>naddrs</CODE
> is decremented by one.</P
><P
><TT
CLASS="FUNCTION"
>rfc822tok_print</TT
>() converts a tokenized
<TT
CLASS="PARAMETER"
><I
>list</I
></TT
> of <CODE
CLASS="STRUCTNAME"
>rfc822token</CODE
>
objects into a text string. The callback function,
<TT
CLASS="PARAMETER"
><I
>func</I
></TT
>, is called one
character at a time, for every character in the tokenized objects. An
arbitrary pointer, <TT
CLASS="PARAMETER"
><I
>func_arg</I
></TT
>, is passed unchanged as
the additional argument to the callback function.
<TT
CLASS="FUNCTION"
>rfc822tok_print</TT
>() is not usually the most
convenient and efficient function, but it has its uses.</P
><P
><TT
CLASS="FUNCTION"
>rfc822_print</TT
>() takes an entire
<CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure, and uses the
callback functions to print the contained addresses, in their original form,
separated by commas. The function pointed to by
<TT
CLASS="PARAMETER"
><I
>print_func</I
></TT
> is used to
print each individual address, one character at a time.  Between the
addresses, the <TT
CLASS="PARAMETER"
><I
>print_separator</I
></TT
> function is called to
print the address separator, usually the string ", ".
The <TT
CLASS="PARAMETER"
><I
>callback_arg</I
></TT
> argument is passed
along unchanged, as an additional argument to these functions.</P
><P
>The functions <TT
CLASS="FUNCTION"
>rfc822_addrlist</TT
>() and
<TT
CLASS="FUNCTION"
>rfc822_namelist</TT
>() also print the
contents of the entire <CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure, but in a
different way.
<TT
CLASS="FUNCTION"
>rfc822_addrlist</TT
>() prints just the actual E-mail
addresses, not the recipient
names or comments.  Each E-mail address is followed by a newline character.
<TT
CLASS="FUNCTION"
>rfc822_namelist</TT
>() prints just the names or comments,
followed by newlines.</P
><P
>The functions <TT
CLASS="FUNCTION"
>rfc822_praddr</TT
>() and
<TT
CLASS="FUNCTION"
>rfc822_prname</TT
>() are just like
<TT
CLASS="FUNCTION"
>rfc822_addrlist</TT
>() and
<TT
CLASS="FUNCTION"
>rfc822_namelist</TT
>(), except that they print a single name
or address in the <CODE
CLASS="STRUCTNAME"
>rfc822a</CODE
> structure, given its
<TT
CLASS="PARAMETER"
><I
>index</I
></TT
>. The
functions <TT
CLASS="FUNCTION"
>rfc822_gettok</TT
>(),
<TT
CLASS="FUNCTION"
>rfc822_getaddrs</TT
>(), <TT
CLASS="FUNCTION"
>rfc822_getaddr</TT
>(),
and <TT
CLASS="FUNCTION"
>rfc822_getname</TT
>() are equivalent to
<TT
CLASS="FUNCTION"
>rfc822tok_print</TT
>(), <TT
CLASS="FUNCTION"
>rfc822_print</TT
>(),
<TT
CLASS="FUNCTION"
>rfc822_praddr</TT
>() and <TT
CLASS="FUNCTION"
>rfc822_prname</TT
>(),
but, instead of using a callback function
pointer, these functions write the output into a dynamically allocated buffer.
That buffer must be destroyed by <TT
CLASS="FUNCTION"
>free</TT
>(3) after use.
These functions will
return a null pointer in the event of a failure to allocate memory for the
buffer.</P
><P
><TT
CLASS="FUNCTION"
>rfc822_prname_orlist</TT
>() is similar to
<TT
CLASS="FUNCTION"
>rfc822_prname</TT
>(), except that it will
also print the legacy RFC822 group list syntax (which are also parsed by
<TT
CLASS="FUNCTION"
>rfc822a_alloc</TT
>()).  <TT
CLASS="FUNCTION"
>rfc822_praddr</TT
>()
will print an empty string for an index
that corresponds to a group list name (or terminated semicolon).
<TT
CLASS="FUNCTION"
>rfc822_prname</TT
>() will also print an empty string.
<TT
CLASS="FUNCTION"
>rfc822_prname_orlist</TT
>() will
instead print either the name of the group list, or a single string ";".
<TT
CLASS="FUNCTION"
>rfc822_getname_orlist</TT
>() will instead save it into a
dynamically allocated buffer.</P
><P
>The function <TT
CLASS="FUNCTION"
>rfc822_getaddrs_wrap</TT
>() is similar to
<TT
CLASS="FUNCTION"
>rfc822_getaddrs</TT
>(), except
that the generated text is wrapped on or about the 73rd column, using
newline characters.</P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN206"
></A
><H3
>Working with dates</H3
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN208"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>time_t timestamp=rfc822_parsedt(const char *datestr)
const char *datestr=rfc822_mkdate(time_t timestamp);
void rfc822_mkdate_buf(time_t timestamp, char *buffer);</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>These functions convert between timestamps and dates expressed in the
<TT
CLASS="LITERAL"
>Date:</TT
> E-mail header format.</P
><P
><TT
CLASS="FUNCTION"
>rfc822_parsedt</TT
>() returns the timestamp corresponding to
the given date string (0 if there was a syntax error).</P
><P
><TT
CLASS="FUNCTION"
>rfc822_mkdate</TT
>() returns a date string corresponding to
the given timestamp.
<TT
CLASS="FUNCTION"
>rfc822_mkdate_buf</TT
>() writes the date string into the
given buffer instead,
which must be big enough to accommodate it.</P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN217"
></A
><H3
>Working with 8-bit MIME-encoded headers</H3
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN219"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>int error=rfc2047_decode(const char *text,
                int (*callback_func)(const char *, int, const char *, void *),
                void *callback_arg);
 
extern char *str=rfc2047_decode_simple(const char *text);
 
extern char *str=rfc2047_decode_enhanced(const char *text,
                const char *charset);
 
void rfc2047_print(const struct rfc822a *a,
        const char *charset,
        void (*print_func)(char, void *),
        void (*print_separator)(const char *, void *), void *);

 
char *buffer=rfc2047_encode_str(const char *string,
                const char *charset);
 
int error=rfc2047_encode_callback(const char *string,
        const char *charset,
        int (*func)(const char *, size_t, void *),
        void *callback_arg);
 
char *buffer=rfc2047_encode_header(const struct rfc822a *a,
        const char *charset);</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>These functions provide additional logic to encode or decode 8-bit content
in 7-bit RFC 822 headers, as specified in RFC 2047.</P
><P
><TT
CLASS="FUNCTION"
>rfc2047_decode</TT
>() is a basic RFC 2047 decoding function.
It receives a
pointer to some 7bit RFC 2047-encoded text, and a callback function.  The
callback function is repeatedly called. Each time it's called it receives a
piece of decoded text. The arguments are: a pointer to a text fragment, number
of bytes in the text fragment, followed by a pointer to the character set of
the text fragment. The character set pointer is NULL for portions of the
original text that are not RFC 2047-encoded.</P
><P
>The callback function also receives <TT
CLASS="PARAMETER"
><I
>callback_arg</I
></TT
>, as
its last
argument. If the callback function returns a non-zero value,
<TT
CLASS="FUNCTION"
>rfc2047_decode</TT
>()
terminates, returning that value.  Otherwise,
<TT
CLASS="FUNCTION"
>rfc2047_decode</TT
>() returns 0 after
a successful decoding. <TT
CLASS="FUNCTION"
>rfc2047_decode</TT
>() returns -1 if it
was unable to allocate sufficient memory.</P
><P
><TT
CLASS="FUNCTION"
>rfc2047_decode_simple</TT
>() and
<TT
CLASS="FUNCTION"
>rfc2047_decode_enhanced</TT
>() are alternatives to
<TT
CLASS="FUNCTION"
>rfc2047_decode</TT
>() which forego a callback function, and
return the decoded text
in a dynamically-allocated memory buffer. The buffer must be
<TT
CLASS="FUNCTION"
>free</TT
>(3)-ed after
use. <TT
CLASS="FUNCTION"
>rfc2047_decode_simple</TT
>() discards all character set
specifications, and
merely decodes any 8-bit text. <TT
CLASS="FUNCTION"
>rfc2047_decode_enhanced</TT
>()
is a compromise to
discarding all character set information.  The local character set being used
is specified as the second argument to
<TT
CLASS="FUNCTION"
>rfc2047_decode_enhanced</TT
>().  Any RFC
2047-encoded text in a different character set will be prefixed by the name of
the character set, in brackets, in the resulting output.</P
><P
><TT
CLASS="FUNCTION"
>rfc2047_decode_simple</TT
>() and
<TT
CLASS="FUNCTION"
>rfc2047_decode_enhanced</TT
>() return a null pointer
if they are unable to allocate sufficient memory.</P
><P
>The <TT
CLASS="FUNCTION"
>rfc2047_print</TT
>() function is equivalent to
<TT
CLASS="FUNCTION"
>rfc822_print</TT
>(), followed by
<TT
CLASS="FUNCTION"
>rfc2047_decode_enhanced</TT
>() on the result.  The callback
functions are used in
an identical fashion, except that they receive text that's already
decoded.</P
><P
>The function <TT
CLASS="FUNCTION"
>rfc2047_encode_str</TT
>() takes a
<TT
CLASS="PARAMETER"
><I
>string</I
></TT
> and <TT
CLASS="PARAMETER"
><I
>charset</I
></TT
>
being the name of the local character set, then encodes any 8-bit portions of
<TT
CLASS="PARAMETER"
><I
>string</I
></TT
> using RFC 2047 encoding.
<TT
CLASS="FUNCTION"
>rfc2047_encode_str</TT
>() returns a
dynamically-allocated buffer with the result, which must be
<TT
CLASS="FUNCTION"
>free</TT
>(3)-ed after
use, or NULL if there was insufficient memory to allocate the buffer.</P
><P
>The function <TT
CLASS="FUNCTION"
>rfc2047_encode_callback</TT
>() is similar to
<TT
CLASS="FUNCTION"
>rfc2047_encode_str</TT
>()
except that the callback function is repeatedly called to received the
encoding string.  Each invocation of the callback function receives a pointer
to a portion of the encoded text, the number of characters in this portion,
and <TT
CLASS="PARAMETER"
><I
>callback_arg</I
></TT
>.</P
><P
>The function <TT
CLASS="FUNCTION"
>rfc2047_encode_header</TT
>() is basically
equivalent to <TT
CLASS="FUNCTION"
>rfc822_getaddrs</TT
>(), followed by
<TT
CLASS="FUNCTION"
>rfc2047_encode_str</TT
>();</P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN259"
></A
><H3
>Working with subjects</H3
><DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><A
NAME="AEN261"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>char *basesubj=rfc822_coresubj(const char *subj);

char *basesubj=rfc822_coresubj_nouc(const char *subj);</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>This function takes the contents of the subject header, and returns the
"core" subject header that's used in the specification of the IMAP THREAD
function. This function is designed to strip all subject line artifacts that
might've been added in the process of forwarding or replying to a message.
Currently, <TT
CLASS="FUNCTION"
>rfc822_coresubj</TT
>() performs the following transformations:</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>Whitespace</DT
><DD
><P
>Leading and trailing whitespace is removed.  Consecutive
whitespace characters are collapsed into a single whitespace character.
All whitespace characters are replaced by a space.</P
></DD
><DT
>Re:, (fwd) [foo]</DT
><DD
><P
>These artifacts (and several others) are removed from
the subject line.</P
></DD
></DL
></DIV
><P
>Note that this function does NOT do MIME decoding.  In order to
implement IMAP THREAD, it is necessary to call something like
<TT
CLASS="FUNCTION"
>rfc2047_decode</TT
>() before
calling <TT
CLASS="FUNCTION"
>rfc822_coresubj</TT
>().</P
><P
>This function returns a pointer to a dynamically-allocated buffer, which
must be <TT
CLASS="FUNCTION"
>free</TT
>(3)-ed after use.</P
><P
><TT
CLASS="FUNCTION"
>rfc822_coresubj_nouc</TT
>() is like
<TT
CLASS="FUNCTION"
>rfc822_coresubj</TT
>(), except that the subject
is not converted to uppercase.</P
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN282"
></A
><H2
>SEE ALSO</H2
><P
><A
HREF="rfc2045.html"
TARGET="_top"
><SPAN
CLASS="CITEREFENTRY"
><SPAN
CLASS="REFENTRYTITLE"
>rfc2045</SPAN
>(3)</SPAN
></A
>,
<A
HREF="reformail.html"
TARGET="_top"
><SPAN
CLASS="CITEREFENTRY"
><SPAN
CLASS="REFENTRYTITLE"
>reformail</SPAN
>(1)</SPAN
></A
>,
<A
HREF="reformime.html"
TARGET="_top"
><SPAN
CLASS="CITEREFENTRY"
><SPAN
CLASS="REFENTRYTITLE"
>reformime</SPAN
>(1)</SPAN
></A
>.</P
></DIV
></BODY
></HTML
>