1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
7 #include <ccan/bytestring/bytestring.h>
9 /* #define CCAN_RFC822_DEBUG 1 */
15 * rfc822_set_allocation_failure_handler - set function to call on allocation
17 * @h: failure handler function pointer
19 * Normally functions in this module abort() on allocation failure for
20 * simplicity. You can change this behaviour by calling this function
21 * to set an alternative callback for allocation failures. The
22 * callback is called with a string describing where the failure
23 * occurred, which can be used to log a more useful error message.
26 * static void my_handler(const char *str)
28 * fprintf(stderr, "Allocation failure: %s\n", str);
32 * static void do_rfc822_stuff(void *buf, size_t len)
34 * rfc822_set_allocation_failure_handler(&my_handler);
35 * rfc822_start(NULL, buf, len);
38 void rfc822_set_allocation_failure_handler(void (*h)(const char *));
41 static inline bool rfc822_iswsp(char c)
43 return (c == ' ') || (c == '\t');
47 * rfc822_check - check validity of an rfc822_msg context
48 * @msg: message to validate
50 * This debugging function checks the validity of the internal data
51 * structures in an active rfc822_msg context. If @abortstr is
52 * non-NULL, that will be printed in a diagnostic if the state is
53 * inconsistent, and the function will abort. If the state of the
54 * structure is valid it returns it unchanged.
56 * Returns the list head if the list is consistent, NULL if not (it
57 * can never return NULL if @abortstr is set).
59 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
60 const char *abortstr);
63 * rfc822_start - start parsing a new rfc822 message
64 * @ctx: talloc context to make allocations in
65 * @p: pointer to a buffer containing the message text
66 * @len: length of the message text
68 * This function creates a new rfc822_msg context for parsing an
69 * rfc822 message, initialized based on the message text given by the
72 struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len);
75 * rfc822_free - free an rfc822 message
76 * @msg: message to free
78 * Frees an rfc822_msg context, including all subsiduary data
81 void rfc822_free(struct rfc822_msg *msg);
84 * rfc822_first_header - retrieve the first header of an rfc822 message
87 * Finds the first header field of @msg and returns a struct
88 * rfc822_header pointer representing it.
90 #define rfc822_first_header(msg) (rfc822_next_header((msg), NULL))
93 * rfc822_next_header - retrieve the next header of an rfc822 message
95 * @hdr: current header field
97 * Finds the header field of @msg which immediately follows @hdr and
98 * returns a struct rfc822_header pointer for it. If @hdr is NULL,
99 * returns the first header in the message.
101 struct rfc822_header *rfc822_next_header(struct rfc822_msg *msg,
102 struct rfc822_header *hdr);
104 #define rfc822_for_each_header(msg, hdr) \
105 for ((hdr) = rfc822_first_header((msg)); \
107 (hdr) = rfc822_next_header((msg), (hdr)))
110 * rfc822_body - retrieve the body of an rfc822 message
113 * Finds the body of @msg and returns a bytestring containing its
116 struct bytestring rfc822_body(struct rfc822_msg *msg);
118 enum rfc822_header_errors {
119 RFC822_HDR_NO_COLON = 1,
120 RFC822_HDR_BAD_NAME_CHARS = 2,
123 enum rfc822_header_errors rfc822_header_errors(struct rfc822_msg *msg,
124 struct rfc822_header *hdr);
127 * rfc822_header_raw_content - retrieve the raw content of an rfc822 header
128 * @hdr: a header handle
130 * This returns a bytestring containing the complete contents (name
131 * and value) of @hdr. This will work even if the header is badly
132 * formatted and cannot otherwise be parsed.
134 struct bytestring rfc822_header_raw_content(struct rfc822_msg *msg,
135 struct rfc822_header *hdr);
139 * rfc822_header_raw_name - retrieve the name of an rfc822 header
140 * @hdr: a header handle
142 * This returns a bytestring containing the header name of @hdr. This
143 * could include any invalid characters, in the case of a badly
146 struct bytestring rfc822_header_raw_name(struct rfc822_msg *msg,
147 struct rfc822_header *hdr);
150 * rfc822_header_raw_value - retrieve the unprocessed value of an rfc822 header
151 * @hdr: a header handle
153 * This returns a bytestring containing the complete contents of
154 * @hdr's value. This includes the terminating and any internal
157 struct bytestring rfc822_header_raw_value(struct rfc822_msg *msg,
158 struct rfc822_header *hdr);
161 * rfc822_header_unfolded_value - retrieve the unfolded value of an rfc822 header
162 * @hdr: a header handle
164 * This returns a bytestring containing the unfolded contents of
165 * @hdr's value. That is, the header value with any internal and the
166 * terminating newline removed.
168 struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
169 struct rfc822_header *hdr);
171 #endif /* CCAN_RFC822_H_ */