1 /*	$OpenBSD: rfc2822.h,v 1.4 2015/11/05 08:55:09 gilles Exp $	*/
2 
3 /*
4  * Copyright (c) 2014 Gilles Chehade <gilles@poolp.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _RFC2822_H_
20 #define	_RFC2822_H_
21 
22 #ifdef HAVE_STDINT_H
23 #include <stdint.h>
24 #endif
25 
26 #define	RFC2822_MAX_LINE_SIZE		4096
27 
28 struct rfc2822_line {
29 	TAILQ_ENTRY(rfc2822_line)	next;
30 	char				buffer[RFC2822_MAX_LINE_SIZE+1];
31 };
32 
33 struct rfc2822_header {
34 	char				name[RFC2822_MAX_LINE_SIZE+1];
35 	TAILQ_HEAD(, rfc2822_line)	lines;
36 };
37 
38 struct rfc2822_hdr_cb {
39 	TAILQ_ENTRY(rfc2822_hdr_cb)	next;
40 
41 	char				name[RFC2822_MAX_LINE_SIZE+1];
42 	void			      (*func)(const struct rfc2822_header *, void *);
43 	void			       *arg;
44 };
45 
46 struct rfc2822_hdr_miss_cb {
47 	TAILQ_ENTRY(rfc2822_hdr_miss_cb)	next;
48 
49 	char					name[RFC2822_MAX_LINE_SIZE+1];
50 	void				      (*func)(const char *, void *);
51 	void				       *arg;
52 };
53 
54 struct rfc2822_line_cb {
55 	void			      (*func)(const char *, void *);
56 	void			       *arg;
57 };
58 
59 struct rfc2822_eoh_cb {
60 	void			      (*func)(void *);
61 	void			       *arg;
62 };
63 
64 struct rfc2822_parser {
65 	uint8_t					in_hdrs;	/* in headers */
66 
67 	TAILQ_HEAD(hdr_cb, rfc2822_hdr_cb)		hdr_cb;
68 	TAILQ_HEAD(hdr_miss_cb, rfc2822_hdr_miss_cb)	hdr_miss_cb;
69 
70 	uint8_t					in_hdr;		/* in specific header */
71 	struct rfc2822_header			header;
72 
73 	struct rfc2822_hdr_cb		        hdr_dflt_cb;
74 	struct rfc2822_eoh_cb		        eoh_cb;
75 	struct rfc2822_line_cb		        body_line_cb;
76 };
77 
78 
79 void	rfc2822_parser_init(struct rfc2822_parser *);
80 int	rfc2822_parser_feed(struct rfc2822_parser *, const char *);
81 void	rfc2822_parser_flush(struct rfc2822_parser *);
82 void	rfc2822_parser_reset(struct rfc2822_parser *);
83 void	rfc2822_parser_release(struct rfc2822_parser *);
84 int	rfc2822_header_callback(struct rfc2822_parser *, const char *,
85     void (*)(const struct rfc2822_header *, void *), void *);
86 int	rfc2822_missing_header_callback(struct rfc2822_parser *, const char *,
87     void (*)(const char *, void *), void *);
88 void	rfc2822_header_default_callback(struct rfc2822_parser *,
89     void (*)(const struct rfc2822_header *, void *), void *);
90 void	rfc2822_body_callback(struct rfc2822_parser *,
91     void (*)(const char *, void *), void *);
92 void	rfc2822_eoh_callback(struct rfc2822_parser *,
93     void (*)(void *), void *);
94 
95 #endif
96