]> git.deb.at Git - pkg/abook.git/blob - getname.c
Fixed some warnings
[pkg/abook.git] / getname.c
1
2 /*
3  * This code was taken from hypermail http://www.hypermail.org/
4  *
5  * license: GNU GENERAL PUBLIC LICENSE, Version 2
6  *
7  * modified by Jaakko Heinonen <jheinonen@users.sourceforge.net>
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 /*#include "hypermail.h"*/
14 #include "getname.h"
15 /*#include "setup.h"*/
16
17 /*extern char *set_domainaddr;*/
18 const char *set_domainaddr = "";
19 const int use_domainaddr = 0;
20
21 #define NAMESTRLEN   80
22 #define MAILSTRLEN   80
23
24 #define NONAME      "(no name)"
25 #define NOEMAIL     "(no email)"
26
27 #ifndef FALSE
28 #       define FALSE    0
29 #endif
30
31 #ifndef TRUE
32 #       define TRUE     1
33 #endif
34
35 #define hm_strchr(X, XX) strchr(X, XX)
36 #define strsav(X) ((X == NULL) ? strdup("") : strdup(X))
37
38 /*const int set_iso2022jp = 0;*/
39
40 void strcpymax(char *dest, const char *src, int n)
41 {
42         int i;
43         
44         if (n) {
45                 n--; /* decrease one to allow for the termination byte */
46                 for (i = 0; *src && (i < n); i++)
47                         *dest++ = *src++;
48         }
49         *dest = 0;
50 }
51
52 static int blankstring(char *str)
53 {
54     register char *cp;
55     for (cp = str; *cp; cp++) {
56         if (*cp != ' ' && *cp != '\t' && *cp != '\r' && *cp != '\n')
57             return (0);
58     }
59     return (1);
60 }
61
62 #if 0
63 char *spamify(char *input)
64 {
65   /* we should replace the @-letter in the email
66      address */
67   int newlen=strlen(input)+4;
68   char *atptr=strchr(input, '@');
69   if(atptr) {
70     char *newbuf = malloc(newlen);
71     int index=atptr-input;
72     /* copy the part before the @ */
73     memcpy(newbuf, input, index);
74     /* append _at_ */
75     memcpy(newbuf+index, "_at_", 4);
76     /* append the part after the @ */
77     strcpy(newbuf+index+4, input+index+1);
78     /* correct the pointer and free the old */
79     free(input);
80     return newbuf;
81   }
82   /* weird email, bail out */
83   return input;
84 }
85 #endif
86
87 /*
88 ** Grabs the name and email address from a From: header.
89 ** This could get tricky; I've tried to keep it simple.
90 ** Should be able to handle all the addresses below:
91 **
92 **   From: user                   [no @]
93 **   From: kent (Kent Landfield)  [no @ - with comment]
94 **   From: <user@node.domain>     [no text name, use email as text name]
95 **   From: Kent Landfield <kent>  [text name but no @]
96 **   From: (kent)                 [comment - no email address]
97 **   From: "" <kent>              [email address but null comment]
98 **   From:                        [blank From: line]
99 **   From: uu.net!kent            [uucp addresses - no comment]
100 **   From: uu.net!kent (kent)     [uucp addresses - with comment]
101 **   From: "(Joe Bloggs)" <joe@anorg.com> 
102 **   From: "Roy T. Fielding" <fielding@kiwi.ics.uci.edu>
103 **   From: kent@localhost
104 **   From: kent@uu.net (Kent Landfield)
105 **   From: (George Burgyan) <gburgyan@cybercon.com>
106 **   From: <gburgyan@cybercon.com> (George Burgyan) 
107 **   From:              Kent B. Landfield <kent@landfield.com>
108 **   From:      IN%"fekete+reply@c2.net" 26-JAN-1997 13:28:55.36
109 **   From:      IN%"vicric@panix.com"  "Vicki Richman" 13-AUG-1996 10:54:33.38
110 **   From:      US2RMC::"lwv26@cas.org" "Larry W. Virden, x2487" 22-OCT-1994 09:44:21.44
111 **   From:          Mail Delivery Subsystem <postmaster@igc.apc.org>
112 **   From:          Self <ehasbrouck>
113 **   From:         adam@eden.apana.org.au (Adam Frey)
114 **   From:        faqserv@penguin-lust.mit.edu
115 **   From:    nc0548@freebsd.netcom.com (Mark Hittinger)
116 **   From: "- Pam Greene, one of the *.answers moderators" <pgreene@MIT.EDU>
117 **   From: "Felan shena Thoron'edras" <felan@netcom.com>
118 **   From: David Muir Sharnoff <muir@idiom.com>
119 **   From: A.J.Doherty@reading.ac.uk (Andy Doherty)
120 **   From: Jordan Hubbard                        <jkh@vector.eikon.e-technik.tu-muenchen.de>
121 **   From: ZXPAN%SLACVM.BITNET@MITVMA.MIT.EDU
122 **   From: afs!piz!alf@uu5.psi.com (Alf the Poet)
123 **   From: answers@cp.tn.tudelft.nl ("Moderator *.answers")
124 **   From: mdw%merengue@merengue.oit.unc.edu (Matt Welsh)
125 **   From: bgoffe@whale.st.usm.edu (William L. Goffe)
126 **
127 ** This is an interesting new one (1998-11-26):
128 ** From: <name.hidden@era.ericsson.se>\9bName.Hidden@era.ericsson.se\9c
129 */
130
131 void getname(char *line, char **namep, char **emailp)
132 {
133     int i;
134     int len;
135     char *c;
136     int comment_fnd;
137
138     char email[MAILSTRLEN];
139     char name[NAMESTRLEN];
140
141     len = MAILSTRLEN - 1;
142     comment_fnd = 0;
143
144     /*
145      * Zero out data storage.
146      */
147     memset(email, 0, MAILSTRLEN);
148     memset(name, 0, NAMESTRLEN);
149
150     *namep = NULL;
151     *emailp = NULL;
152
153     /* EMail Processing First:
154        ** First, is there an '@' sign we can use as an anchor ?
155      */
156     if ((c = hm_strchr(line, '@')) == NULL) {
157         /* 
158            ** No '@' sign here so ...
159          */
160         if (strchr(line, '(')) {        /* From: bob (The Big Guy) */
161             c = strchr(line, ':') + 1;
162             while (*c == ' ' || *c == '\t')
163                 c++;
164             for (i = 0; *c && *c != '(' && *c != ' ' &&
165                  *c != '\t' && *c != '\n' && i < len; c++)
166                 email[i++] = *c;
167             email[i] = '\0';
168         }
169         else if ((c = strchr(line, '<'))) {     /* From: <kent> */
170             c++;
171             for (i = 0; *c && *c != '>' && *c != ' ' &&
172                  *c != '\t' && *c != '\n' && i < len; c++)
173                 email[i++] = *c;
174             email[i] = '\0';
175         }
176         else {
177             /* 
178              *    - check to see if the From: line is blank, (taken care of)
179              *    - check if From: uu.net!kent formatted line
180              *    - check if "From: kent" formatted line
181              */
182             c = strchr(line, ':') + 1;
183             while (*c == ' ' || *c == '\t')
184                 c++;
185             for (i = 0; *c && *c != ' ' && *c != '\t' &&
186                  *c != '\n' && *c != ',' && i < len; c++)
187                 email[i++] = *c;
188             email[i] = '\0';
189
190         }
191
192         if (email[0] == '\0')   /* Was it a junk From line ? */
193             strcpymax(email, NOEMAIL, MAILSTRLEN);
194
195         else if (use_domainaddr) {
196             /*
197              * check if site domainizes addresses 
198              * but don't modify uucp addresses
199              */
200             if ((c = strchr(email, '!')) == NULL) {
201                 strcat(email, "@");
202                 strcat(email, set_domainaddr);
203             }
204         }
205     }
206     else {
207         while (*c != ' ' && *c != '\t' && *c != '<' && *c != '"' &&
208                *c != ':') c--;
209         c++;
210         for (i = 0; *c && *c != '>' && *c != ' ' && *c != '\t' &&
211              *c != '"' && *c != '\n' && *c != ']' && *c != ',' &&
212              i < len; c++)
213             email[i++] = *c;
214         email[i] = '\0';
215     }
216
217     /*
218      * NAME Processing - Boy are there a bunch of funky formats here.
219      *                   No promises... I'll do my best. Let me know
220      *                   what I missed...
221      */
222
223     if (strchr(line, '<')) {
224         c = strchr(line, ':') + 1;
225         while (*c == ' ' || *c == '\t')
226             c++;
227
228         /* if a comment then just look for the end point */
229
230         if (*c == '\"') {
231             int rmparen = 0;
232
233             ++c;
234             if (*c == '(') {
235                 ++c;
236                 rmparen = 1;
237             }
238             for (i = 0, len = NAMESTRLEN - 1;
239                  *c && *c != '\"' && *c != '\n' && i < len; c++)
240                 name[i++] = *c;
241
242             if (rmparen && name[(i - 1)] == ')')
243                 --i;            /* get rid of "(name-comment)" parens */
244
245             comment_fnd = 1;
246         }
247         else if (hm_strchr(line, '(')) {
248             c = hm_strchr(line, '(') + 1;
249             if (*c == '"')      /* is there a comment in the comment ? */
250                 c++;
251         }
252         else if (*c == '<') {   /* Comment may be on the end */
253             /* From: <bill@celestial.com> Bill Campbell */
254             c = strchr(line, '>') + 1;
255             for (i = 0, len = NAMESTRLEN - 1; *c && *c != '\n' && i < len;
256                  c++)
257                 name[i++] = *c;
258
259             comment_fnd = 1;
260         }
261     }
262     else if (strchr(line, '(')) {
263         c = strchr(line, '(');
264         c++;
265         if (*c == '"')          /* is there a comment in the comment ? */
266             c++;
267         while (*c == ' ' || *c == '\t')
268             c++;
269     }
270     else if (strchr(line, '[')) {
271         c = strchr(line, ':') + 1;
272         while (*c == ' ' || *c == '\t')
273             c++;
274
275         for (i = 0, len = NAMESTRLEN - 1;
276              *c && *c != '\"' && *c != '[' && *c != '\n' && i < len; c++)
277             name[i++] = *c;
278
279         name[--i] = '\0';
280         comment_fnd = 1;
281     }
282     else {
283         /*
284          * Is there an email address available 
285          * that we can use for the name ?
286          */
287         if (!strcmp(email, NOEMAIL))    /* No */
288             strcpymax(name, NONAME, NAMESTRLEN);
289         else {
290             c = email + strlen(email) - 1;
291             while (isspace((unsigned char)*c))
292                 *c-- = '\0';
293             strcpymax(name, email, NAMESTRLEN); /* Yes */
294         }
295         *namep = strsav(name);
296         *emailp = strsav(email);
297         return;
298     }
299
300     if (!comment_fnd) {
301         /*int in_ascii = TRUE, esclen = 0;*/
302         for (i = 0, len = NAMESTRLEN - 1;
303              *c && *c != '<' && *c != '\"' && *c != ')' && *c != '(' &&
304              *c != '\n' && i < len; c++)
305         {
306                 /*if (set_iso2022jp) {
307                         iso2022_state(c, &in_ascii, &esclen);
308                         if (esclen) {
309                                 for (; esclen; esclen--, c++) name[i++] = *c;
310                                 for (; in_ascii == FALSE && i < len;
311                                      c++, iso2022_state(c, &in_ascii, &esclen)) {
312                                         name[i++] = *c;
313                                 }
314                                 c--;
315                         } else {
316                                 name[i++] = *c;
317                         }
318                 } else {*/
319                         name[i++] = *c;
320                 /*}*/
321         }
322     }
323
324     if (i > 0 && name[i-1] == ' ' && (*c == '<' || *c == '('))
325         name[--i] = '\0';
326     else
327         name[i] = '\0';
328
329     /*
330      * Is the name string blank ? If so then 
331      * force it to get filled with something.
332      */
333     if (blankstring(name))
334         name[0] = '\0';
335
336     /* Bailing and taking the easy way out... */
337
338     if (name[0] == '\0') {
339         if (email[0] == '\0')
340             strcpymax(name, NONAME, NAMESTRLEN);
341         else
342             strcpymax(name, email, NAMESTRLEN);
343     }
344
345     /* 
346      * need to strip spaces off the end of 
347      * the email and name strings 
348      */
349
350     c = email + (strlen(email) - 1);
351     while (c > email && isspace((unsigned char)*c))
352         *c-- = '\0';
353
354     *namep = strsav(name);
355     *emailp = strsav(email);
356 }
357