1/*
2 * Copyright (c) 1989 Regents of the University of California.
3 * All rights reserved.  The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#include <popper.h>
8RCSID("$Id$");
9
10#if defined(UIDL) || defined(XOVER)
11
12/*
13 * Copy the string found after after : into a malloced buffer. Stop
14 * copying at end of string or end of line. End of line delimiter is
15 * not part of the resulting copy.
16 */
17static
18char *
19find_value_after_colon(char *p)
20{
21  char *t, *tmp;
22
23  for (; *p != 0 && *p != ':'; p++) /* Find : */
24    ;
25
26  if (*p == 0)
27    goto error;
28
29  p++;				/* Skip over : */
30
31  for(; *p == ' ' || *p == '\t'; p++) /* Remove white space */
32    ;
33
34  for (t = p; *t != 0 && *t != '\n' && *t != '\r'; t++)	/* Find end of str */
35    ;
36
37  tmp = t = malloc(t - p + 1);
38  if (tmp == 0)
39    goto error;
40
41  for (; *p != 0 && *p != '\n' && *p != '\r'; p++, t++)	/* Copy characters */
42    *t = *p;
43  *t = 0;			/* Terminate string */
44  return tmp;
45
46error:
47  return "ErrorUIDL";
48}
49#endif
50
51void
52parse_header(MsgInfoList *mp, char *buffer)
53{
54#if defined(UIDL) || defined(XOVER)
55    if (strncasecmp("Message-Id:",buffer, 11) == 0) {
56	if (mp->msg_id == NULL)
57	    mp->msg_id = find_value_after_colon(buffer);
58    }
59#ifdef UIDL
60    else if (strncasecmp(buffer, "X-UIDL:", 7) == 0) {
61	/* Courtesy to Qualcomm, there really is no such
62	   thing as X-UIDL */
63	mp->msg_id = find_value_after_colon(buffer);
64    }
65#endif
66#endif
67#ifdef XOVER
68    else if (strncasecmp("Subject:", buffer, 8) == 0) {
69	if(mp->subject == NULL){
70	    char *p;
71	    mp->subject = find_value_after_colon(buffer);
72	    for(p = mp->subject; *p; p++)
73		if(*p == '\t') *p = ' ';
74	}
75    }
76    else if (strncasecmp("From:", buffer, 5) == 0) {
77	if(mp->from == NULL){
78	    char *p;
79	    mp->from = find_value_after_colon(buffer);
80	    for(p = mp->from; *p; p++)
81		if(*p == '\t') *p = ' ';
82	}
83    }
84    else if (strncasecmp("Date:", buffer, 5) == 0) {
85	if(mp->date == NULL){
86	    char *p;
87	    mp->date = find_value_after_colon(buffer);
88	    for(p = mp->date; *p; p++)
89		if(*p == '\t') *p = ' ';
90	}
91    }
92#endif
93}
94
95int
96add_missing_headers(POP *p, MsgInfoList *mp)
97{
98#if defined(UIDL) || defined(XOVER)
99    if (mp->msg_id == NULL) {
100	if (asprintf(&mp->msg_id, "no-message-id-%d", mp->number) == -1) {
101	    fclose (p->drop);
102	    p->msg_count = 0;
103	    return pop_msg (p,POP_FAILURE,
104			    "Can't build message list for '%s': Out of memory",
105                            p->user);
106	}
107    }
108#endif
109#ifdef XOVER
110    if (mp->subject == NULL)
111	mp->subject = "<none>";
112    if (mp->from == NULL)
113	mp->from = "<unknown>";
114    if (mp->date == NULL)
115	mp->date = "<unknown>";
116#endif
117    return POP_SUCCESS;
118}
119
120/*
121 *  dropinfo:   Extract information about the POP maildrop and store
122 *  it for use by the other POP routines.
123 */
124
125int
126pop_dropinfo(POP *p)
127{
128    char                    buffer[BUFSIZ];         /*  Read buffer */
129    MsgInfoList         *   mp;                     /*  Pointer to message
130                                                        info list */
131    int			    msg_num;                /*  Current message
132                                                        counter */
133    int                     nchar;                  /*  Bytes written/read */
134    int blank_line = 1; /* previous line was blank */
135    int in_header = 0; /* if we are in a header block */
136
137    /*  Initialize maildrop status variables in the POP parameter block */
138    p->msg_count = 0;
139    p->msgs_deleted = 0;
140    p->last_msg = 0;
141    p->bytes_deleted = 0;
142    p->drop_size = 0;
143
144    /*  Allocate memory for message information structures */
145    p->msg_count = ALLOC_MSGS;
146    p->mlp = (MsgInfoList *)calloc((unsigned)p->msg_count,sizeof(MsgInfoList));
147    if (p->mlp == NULL){
148        fclose (p->drop);
149        p->msg_count = 0;
150        return pop_msg (p,POP_FAILURE,
151            "Can't build message list for '%s': Out of memory", p->user);
152    }
153
154    rewind (p->drop);
155
156    /*  Scan the file, loading the message information list with
157        information about each message */
158
159    for (msg_num = p->drop_size = 0, mp = p->mlp - 1;
160             fgets(buffer,MAXMSGLINELEN,p->drop);) {
161
162        nchar  = strlen(buffer);
163
164        if (blank_line && strncmp(buffer,"From ",5) == 0) {
165	    in_header = 1;
166            if (++msg_num > p->msg_count) {
167                p->mlp=(MsgInfoList *) realloc(p->mlp,
168                    (p->msg_count+=ALLOC_MSGS)*sizeof(MsgInfoList));
169                if (p->mlp == NULL){
170                    fclose (p->drop);
171                    p->msg_count = 0;
172                    return pop_msg (p,POP_FAILURE,
173                        "Can't build message list for '%s': Out of memory",
174                            p->user);
175                }
176                mp = p->mlp + msg_num - 2;
177            }
178            ++mp;
179            mp->number = msg_num;
180            mp->length = 0;
181            mp->lines = 0;
182            mp->offset = ftell(p->drop) - nchar;
183            mp->flags = 0;
184#if defined(UIDL) || defined(XOVER)
185	    mp->msg_id = 0;
186#endif
187#ifdef XOVER
188	    mp->subject = 0;
189	    mp->from = 0;
190	    mp->date = 0;
191#endif
192#ifdef DEBUG
193            if(p->debug)
194                pop_log(p, POP_DEBUG,
195			"Msg %d at offset %ld being added to list",
196			mp->number, mp->offset);
197#endif /* DEBUG */
198        } else if(in_header)
199	    parse_header(mp, buffer);
200	blank_line = (strncmp(buffer, "\n", nchar) == 0);
201	if(blank_line) {
202	    int e;
203	    in_header = 0;
204	    e = add_missing_headers(p, mp);
205	    if(e != POP_SUCCESS)
206		return e;
207	}
208        mp->length += nchar;
209        p->drop_size += nchar;
210        mp->lines++;
211    }
212    p->msg_count = msg_num;
213
214#ifdef DEBUG
215    if(p->debug && msg_num > 0) {
216        int i;
217        for (i = 0, mp = p->mlp; i < p->msg_count; i++, mp++)
218#ifdef UIDL
219	    pop_log(p,POP_DEBUG,
220		    "Msg %d at offset %ld is %ld octets long and has %u lines and id %s.",
221                    mp->number,mp->offset,mp->length,mp->lines, mp->msg_id);
222#else
223            pop_log(p,POP_DEBUG,
224                "Msg %d at offset %d is %d octets long and has %u lines.",
225                    mp->number,mp->offset,mp->length,mp->lines);
226#endif
227    }
228#endif /* DEBUG */
229
230    return(POP_SUCCESS);
231}
232