remove gcc34
[dragonfly.git] / crypto / heimdal-0.6.3 / appl / popper / pop_dropinfo.c
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>
8 RCSID("$Id: pop_dropinfo.c,v 1.24 1999/09/16 20:38:49 assar Exp $");
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  */
17 static
18 char *
19 find_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
46 error:
47   return "ErrorUIDL";
48 }
49 #endif
50
51 void
52 parse_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
95 int
96 add_missing_headers(POP *p, MsgInfoList *mp)
97 {
98 #if defined(UIDL) || defined(XOVER)
99     if (mp->msg_id == NULL) {
100         asprintf(&mp->msg_id, "no-message-id-%d", mp->number);
101         if(mp->msg_id == NULL) {
102             fclose (p->drop);
103             p->msg_count = 0;
104             return pop_msg (p,POP_FAILURE,
105                             "Can't build message list for '%s': Out of memory",
106                             p->user);
107         }
108     }
109 #endif      
110 #ifdef XOVER
111     if (mp->subject == NULL)
112         mp->subject = "<none>";
113     if (mp->from == NULL)
114         mp->from = "<unknown>";
115     if (mp->date == NULL)
116         mp->date = "<unknown>";
117 #endif
118     return POP_SUCCESS;
119 }
120
121 /* 
122  *  dropinfo:   Extract information about the POP maildrop and store 
123  *  it for use by the other POP routines.
124  */
125
126 int
127 pop_dropinfo(POP *p)
128 {
129     char                    buffer[BUFSIZ];         /*  Read buffer */
130     MsgInfoList         *   mp;                     /*  Pointer to message 
131                                                         info list */
132     int                     msg_num;                /*  Current message 
133                                                         counter */
134     int                     nchar;                  /*  Bytes written/read */
135     int blank_line = 1; /* previous line was blank */
136     int in_header = 0; /* if we are in a header block */
137     
138     /*  Initialize maildrop status variables in the POP parameter block */
139     p->msg_count = 0;
140     p->msgs_deleted = 0;
141     p->last_msg = 0;
142     p->bytes_deleted = 0;
143     p->drop_size = 0;
144
145     /*  Allocate memory for message information structures */
146     p->msg_count = ALLOC_MSGS;
147     p->mlp = (MsgInfoList *)calloc((unsigned)p->msg_count,sizeof(MsgInfoList));
148     if (p->mlp == NULL){
149         fclose (p->drop);
150         p->msg_count = 0;
151         return pop_msg (p,POP_FAILURE,
152             "Can't build message list for '%s': Out of memory", p->user);
153     }
154
155     rewind (p->drop);
156
157     /*  Scan the file, loading the message information list with 
158         information about each message */
159
160     for (msg_num = p->drop_size = 0, mp = p->mlp - 1;
161              fgets(buffer,MAXMSGLINELEN,p->drop);) {
162
163         nchar  = strlen(buffer);
164
165         if (blank_line && strncmp(buffer,"From ",5) == 0) {
166             in_header = 1;
167             if (++msg_num > p->msg_count) {
168                 p->mlp=(MsgInfoList *) realloc(p->mlp,
169                     (p->msg_count+=ALLOC_MSGS)*sizeof(MsgInfoList));
170                 if (p->mlp == NULL){
171                     fclose (p->drop);
172                     p->msg_count = 0;
173                     return pop_msg (p,POP_FAILURE,
174                         "Can't build message list for '%s': Out of memory",
175                             p->user);
176                 }
177                 mp = p->mlp + msg_num - 2;
178             }
179             ++mp;
180             mp->number = msg_num;
181             mp->length = 0;
182             mp->lines = 0;
183             mp->offset = ftell(p->drop) - nchar;
184             mp->flags = 0;
185 #if defined(UIDL) || defined(XOVER)
186             mp->msg_id = 0;
187 #endif
188 #ifdef XOVER
189             mp->subject = 0;
190             mp->from = 0;
191             mp->date = 0;
192 #endif
193 #ifdef DEBUG
194             if(p->debug)
195                 pop_log(p, POP_DEBUG,
196                         "Msg %d at offset %ld being added to list",
197                         mp->number, mp->offset);
198 #endif /* DEBUG */
199         } else if(in_header)
200             parse_header(mp, buffer);
201         blank_line = (strncmp(buffer, "\n", nchar) == 0);
202         if(blank_line) {
203             int e;
204             in_header = 0;
205             e = add_missing_headers(p, mp);
206             if(e != POP_SUCCESS)
207                 return e;
208         }
209         mp->length += nchar;
210         p->drop_size += nchar;
211         mp->lines++;
212     }
213     p->msg_count = msg_num;
214
215 #ifdef DEBUG
216     if(p->debug && msg_num > 0) {
217         int i;
218         for (i = 0, mp = p->mlp; i < p->msg_count; i++, mp++)
219 #ifdef UIDL
220             pop_log(p,POP_DEBUG,
221                     "Msg %d at offset %ld is %ld octets long and has %u lines and id %s.",
222                     mp->number,mp->offset,mp->length,mp->lines, mp->msg_id);
223 #else   
224             pop_log(p,POP_DEBUG,
225                 "Msg %d at offset %d is %d octets long and has %u lines.",
226                     mp->number,mp->offset,mp->length,mp->lines);
227 #endif
228     }
229 #endif /* DEBUG */
230
231     return(POP_SUCCESS);
232 }