1 /*                             -*- Mode: C++-C -*-
2  *
3  *		 Copyright 1994 Christopher B. Liebman
4  *
5  *     Permission to use, copy, modify, distribute, and sell this software
6  *     and its documentation for any purpose is hereby granted without fee,
7  *     provided that the above copyright notice appear in all copies and that
8  *     both that copyright notice and this permission notice appear in
9  *     supporting documentation, and that the name Christopher B. Liebman not
10  *     be used in advertising or publicity pertaining to distribution of this
11  *     software without specific, written prior permission.
12  *
13  *    THIS SOFTWARE IS PROVIDED `AS-IS'.  CHRISTOPHER B. LIEBMAN, DISCLAIMS
14  *    ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
15  *    LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16  *    PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL CHRISTOPHER
17  *    B. LIEBMAN, BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING SPECIAL,
18  *    INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA, OR
19  *    PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
20  *    WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF
21  *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author          : Chris Liebman
24  * Created On      : Tue Jan 11 14:11:30 1994
25  * Last Modified By: Chris Liebman
26  * Last Modified On: Sun Feb 20 14:09:37 1994
27  * Update Count    : 53
28  * Status          : Released
29  *
30  * HISTORY
31  *
32  * PURPOSE
33  * 	Routines to handle mail message bodys.
34 */
35 
36 #ifndef lint
37 static char *RCSid = "$Id: mail_body.c,v 1.2 1994/02/23 13:17:02 liebman Exp $";
38 #endif
39 
40 #include "faces.h"
41 
42 #define	BUFFER_SIZE	4096
43 #define	BUFFER_SIZE_INC	4096
44 
45 static char*	buffer = NULL;
46 static int	buffer_size = 0;
47 
48 /*
49  *   Read the mail body lines.  The body ends with
50  * a line starts with the MailSeperator.  Note: This should be
51  * configurable.
52 */
53 
54 char *
MailBodyRead(content_length)55 MailBodyRead(content_length)
56 int content_length;
57 {
58     int		ch;
59     int		string_length = 0;
60     int		get_more;
61     char*	str;
62     char	sepbuf[MAX_MAILSEP_SIZE+1];
63 
64     /*
65      * If allowed, use the content length to skip the body.
66     */
67 
68     if (TheFacesResources.use_content_length && content_length >= 0)
69     {
70 	if (content_length == 0)
71 	{
72 	    return NULL;
73 	}
74 
75 	/*
76 	 *  Make sure the buffer is big enough.
77 	*/
78 
79 	if (buffer_size == 0)
80 	{
81 	    buffer_size = content_length + 1;
82 	    buffer = XtMalloc(buffer_size);
83 	}
84 	else if (buffer_size <= content_length)
85 	{
86 	    buffer_size = content_length + 1;
87 	    buffer = XtRealloc(buffer, buffer_size);
88 	}
89 
90 	MailFileReadString(buffer, buffer_size);
91 
92 	if (buffer[0] == '\0')
93 	{
94 	    return NULL;
95 	}
96 
97 	/*
98 	 *  Allocate a buffer that we can return.
99 	*/
100 
101 	str = XtMalloc(string_length + 1);
102 	strcpy(str, buffer);
103 
104 	return str;
105     }
106 
107     /*
108      * See if this is the start of a new message,
109     */
110 
111     MailFilePeekString(sepbuf, MailSeperatorLength);
112 
113     if (strcmp(sepbuf, MailSeperator) == 0)
114     {
115 	/*
116 	 *  If we do not want the seperator then skip it.
117 	*/
118 
119 	if (MailSeperatorSkip)
120 	{
121 	    MailFileSkipString(MailSeperatorLength);
122 	}
123 
124 	/*
125 	 * Null message body.
126 	*/
127 
128 	return NULL;
129     }
130 
131     /*
132      *  Read  the first char. Check for end of file.
133     */
134 
135     ch = MailFileReadChar();
136 
137     if (ch == EOF)
138     {
139 	return NULL;
140     }
141 
142     /*
143      * If the buffer has not beed created yet then create it.
144     */
145 
146     if (buffer_size == 0)
147     {
148 	buffer = XtMalloc(BUFFER_SIZE);
149 	buffer_size = BUFFER_SIZE;
150     }
151 
152     /*
153      * Ok, start collecting characters.
154     */
155 
156     do
157     {
158 	get_more = 1;
159 
160 	/*
161 	 * Collect the rest of this line.
162 	*/
163 
164 	while((ch != '\n') && (ch != EOF))
165 	{
166 	    if (string_length >= buffer_size)
167 	    {
168 		/*
169 		 * Grow buffer.
170 		*/
171 
172 		buffer = XtRealloc(buffer, buffer_size + BUFFER_SIZE_INC);
173 		buffer_size += BUFFER_SIZE_INC;
174 	    }
175 
176 	    buffer[string_length++] = ch;
177 
178 	    ch = MailFileReadChar();
179 	}
180 
181 	if (ch == EOF)
182 	{
183 	    get_more = 0;
184 	}
185 	else
186 	{
187 	    /*
188 	     * Ok, We have gotten to the end of a line.  Peek at the start
189 	     * of the next line to see if it is the seperator for the
190 	     * next message.
191 	    */
192 
193 	    MailFilePeekString(sepbuf, MailSeperatorLength);
194 
195 	    if (strcmp(sepbuf, MailSeperator) == 0)
196 	    {
197 		/*
198 		 *  If we do not want the seperator then skip it.
199 		*/
200 
201 		if (MailSeperatorSkip)
202 		{
203 		    MailFileSkipString(MailSeperatorLength);
204 		}
205 
206 		get_more = 0;
207 	    }
208 	}
209     } while (get_more);
210 
211     /*
212      * Terminate the string.
213     */
214 
215     buffer[string_length] = '\0';
216 
217     /*
218      *  Allocate a buffer that we can return.
219     */
220 
221     str = XtMalloc(string_length + 1);
222     strcpy(str, buffer);
223 
224     return (str);
225 }
226 
227 /*
228  *   Skip the mail body lines.  The body ends with
229  * a line starts with the MailSeperator.  Note: This should be
230  * configurable.
231 */
232 
233 void
MailBodySkip(content_length)234 MailBodySkip(content_length)
235 int content_length;
236 {
237     int		ch;
238     int		get_more;
239     char	sepbuf[MAX_MAILSEP_SIZE];
240 
241     /*
242      * If allowed, use the content length to skip the body.
243     */
244 
245     if (TheFacesResources.use_content_length && content_length >= 0)
246     {
247 	if (content_length == 0)
248 	{
249 	    return;
250 	}
251 
252 	MailFileSkipString(content_length);
253 	return;
254     }
255 
256     /*
257      * See if this is the start of a new message,
258     */
259 
260     MailFilePeekString(sepbuf, MailSeperatorLength);
261 
262     if (strcmp(sepbuf, MailSeperator) == 0)
263     {
264 	/*
265 	 *  If we do not want the seperator then skip it.
266 	*/
267 
268 	if (MailSeperatorSkip)
269 	{
270 	    MailFileSkipString(MailSeperatorLength);
271 	}
272 
273 	/*
274 	 * Null message body.
275 	*/
276 
277 	return;
278     }
279 
280     /*
281      *  Read  the first char. Check for end of file.
282     */
283 
284     ch = MailFileReadChar();
285 
286     if (ch == EOF)
287     {
288 	return;
289     }
290 
291     /*
292      * Ok, start collecting characters.
293     */
294 
295     do
296     {
297 	get_more = 1;
298 
299 	/*
300 	 * Collect the rest of this line.
301 	*/
302 
303 	while((ch != '\n') && (ch != EOF))
304 	{
305 	    ch = MailFileReadChar();
306 	}
307 
308 	if (ch == EOF)
309 	{
310 	    get_more = 0;
311 	}
312 	else
313 	{
314 	    /*
315 	     * Ok, We have gotten to the end of a line.  Peek at the start
316 	     * of the next line to see if it is the MailSeperator for the
317 	     * next message.
318 	    */
319 
320 	    MailFilePeekString(sepbuf, MailSeperatorLength);
321 
322 	    if (strcmp(sepbuf, MailSeperator) == 0)
323 	    {
324 		/*
325 		 *  If we do not want the seperator then skip it.
326 		*/
327 
328 		if (MailSeperatorSkip)
329 		{
330 		    MailFileSkipString(MailSeperatorLength);
331 		}
332 
333 		get_more = 0;
334 	    }
335 	}
336     } while (get_more && (ch = MailFileReadChar()) != EOF);
337 }
338 
339