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: Thu Jan 20 11:11:36 1994
27 * Update Count : 8
28 * Status : Released
29 *
30 * HISTORY
31 * 20-Jan-1994 Chris Liebman
32 * Last Modified: Tue Jan 18 17:37:14 1994 #7 (Chris Liebman)
33 * Now only parse addresses here. Header and mailbox parsing is now
34 * done in other files.
35 *
36 * 14-Jan-1994 Chris Liebman
37 * Last Modified: Fri Jan 14 10:09:52 1994 #3 (Chris Liebman)
38 * Added support for teh status header.
39 * Improved mail parsing, now detects when we are in the body and
40 * will not match headers there!
41 *
42 * PURPOSE
43 * Routines to parse the mail file and to parse a mail address.
44 */
45
46 #ifndef lint
47 static char *RCSid = "$Id: mail_parse.c,v 1.7 1994/03/07 20:30:49 liebman Exp $";
48 #endif
49
50 #include "faces.h"
51
52 void
MailParseAddress(from,user,host)53 MailParseAddress(from, user, host)
54 String from;
55 String *user;
56 String *host;
57 {
58 String phost;
59 String puser;
60 String data;
61 String data1;
62
63 data1 = data = XtNewString(from);
64
65 /*
66 * Make lower case.
67 */
68
69 for(phost = data; *phost != '\0'; ++phost)
70 {
71 if (isupper(*phost))
72 {
73 *phost = tolower(*phost);
74 }
75 }
76
77 /*
78 * space out any comment.
79 */
80
81 while ((phost = index(data, '(')) != NULL)
82 {
83 while((*phost != ')') && (*phost != '\0'))
84 {
85 *phost++ = ' ';
86 }
87
88 if (*phost == ')')
89 {
90 *phost = ' ';
91 }
92 }
93
94 /*
95 * Skip any leading spaces.
96 */
97
98 while(isspace(*data))
99 {
100 ++data;
101 }
102
103 /*
104 * See if there is an address in <>'s.
105 */
106
107 if ((phost = index(data, '<')) != NULL)
108 {
109 data = phost+1;
110 }
111
112 /*
113 * drop any junk off the end.
114 */
115
116 for (phost = data; *phost != '\0'; ++phost)
117 {
118 if (isspace(*phost) || (*phost == ',') || (*phost == '>'))
119 {
120 *phost = '\0';
121 break;
122 }
123 }
124
125 /*
126 * Skip past any leading '@'.
127 */
128
129 if (*data == '@')
130 {
131 ++data;
132 }
133
134 /*
135 * Look for user@host first.
136 */
137
138 if ((phost = index(data, '@')) != NULL)
139 {
140 *(phost++) = '\0';
141
142 /*
143 * Look for host!user
144 */
145
146 if ((puser = rindex(data, '!')) != NULL)
147 {
148 /*
149 * Yep!, wipe the ! and point to the user name.
150 */
151
152 *(puser++) = '\0';
153
154 /*
155 * Now locate the host name.
156 */
157
158 if ((phost = rindex(data, '!')) != NULL)
159 {
160 phost++;
161 }
162 else
163 {
164 phost = data;
165 }
166
167 /*
168 * Return the user and host.
169 */
170
171 *host = XtNewString(phost);
172 *user = XtNewString(puser);
173 XtFree(data1);
174 return;
175 }
176
177 /*
178 * Grab the user skipping past any RFC822 routing garbage.
179 */
180
181 if ((puser = index(data, ':')) != NULL)
182 {
183 ++puser;
184 }
185 else
186 {
187 puser = data;
188 }
189
190 /*
191 * Return the user and host.
192 */
193
194 *host = XtNewString(phost);
195 *user = XtNewString(puser);
196 XtFree(data1);
197 return;
198 }
199
200 /*
201 * Ok, try uucp style: host!host!user
202 */
203
204 if ((puser = rindex(data, '!')) != NULL)
205 {
206 /*
207 * Wipe ! and move to user name.
208 */
209
210 *(puser++) = '\0';
211
212 /*
213 * Now locate the host name.
214 */
215
216 if ((phost = rindex(data, '!')) != NULL)
217 {
218 phost++;
219 }
220 else
221 {
222 phost = data;
223 }
224
225 /*
226 * Return the user and host.
227 */
228
229 *host = XtNewString(phost);
230 *user = XtNewString(puser);
231 XtFree(data1);
232 return;
233 }
234
235 /*
236 * Must be local address!
237 */
238
239 *user = XtNewString(data);
240 *host = XtNewString("LOCAL");
241 XtFree(data1);
242 return;
243 }
244