Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtmail / dtmail / DmxUtils.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: DmxUtils.C /main/3 1996/04/21 19:55:51 drk $ */
24
25 /*
26  *+SNOTICE
27  *
28  *      $:$
29  *
30  *      RESTRICTED CONFIDENTIAL INFORMATION:
31  *      
32  *      The information in this document is subject to special
33  *      restrictions in a confidential disclosure agreement between
34  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
35  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
36  *      Sun's specific written approval.  This document and all copies
37  *      and derivative works thereof must be returned or destroyed at
38  *      Sun's request.
39  *
40  *      Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
41  *
42  *+ENOTICE
43  */
44 /*
45  *                   Common Desktop Environment
46  *
47  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
48  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
49  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
50  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
51  *   (c) Copyright 1995 Digital Equipment Corp.
52  *   (c) Copyright 1995 Fujitsu Limited
53  *   (c) Copyright 1995 Hitachi, Ltd.
54  *                                                                   
55  *
56  *                     RESTRICTED RIGHTS LEGEND                              
57  *
58  *Use, duplication, or disclosure by the U.S. Government is subject to
59  *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
60  *Technical Data and Computer Software clause in DFARS 252.227-7013.  Rights
61  *for non-DOD U.S. Government Departments and Agencies are as set forth in
62  *FAR 52.227-19(c)(1,2).
63
64  *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
65  *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A. 
66  *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
67  *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
68  *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
69  *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
70  *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
71  */
72
73 #include "Dmx.h"
74
75 // fn proto for mailx fn
76 static char * dispname(const char *hdr);
77
78 // error-handling: should do something with minor codes
79
80 DtMailBoolean
81 handleError (DtMailEnv &dterror, char *msg)
82 {
83         if (dterror.isSet () == DTM_TRUE)
84         {
85                 fprintf (stderr, "dtmailpr: (%s) %s\n", 
86                         msg, (const char *)dterror);
87                 dterror.logError (DTM_FALSE, "dtmailpr: (%s) %s\n",
88                         msg, (const char *)dterror);
89
90                 dterror.clear ();
91                 return DTM_TRUE;
92         }
93
94         dterror.clear ();
95         return DTM_FALSE;
96 }
97         
98
99 char *
100 errorString (DmxHeaders hdr)
101 {
102         switch (hdr)
103         {
104                 case DMXFROM:
105                         return "(unknown)";
106                 case DMXSUBJ:
107                         return "(no subject)";
108                 case DMXCLENGTH:
109                         return "0";
110                 case DMXSTATUS:
111                         return "  ";
112                 case DMXDATE:
113                         return "(unknown date)";
114                 case DMXTO:
115                         return " ";
116                 case DMXNUMHDRS:
117                 default:
118                         return " ";
119         }
120 }
121
122 char *
123 getStandardHeaders (DtMailHeaderLine &info)
124 {
125         int     i = 0, length = 0;
126         int     buflength = 0;
127         char    *fbuf;
128
129         const char      *header [DMXNUMHDRS];
130
131         for (i = 0; i < DMXNUMHDRS; i++)
132         {
133                 length = info.header_values[i].length ();
134                 if (length == 0)
135                 {
136                         header [i] = errorString ((DmxHeaders) i);
137                 } else {
138                         header [i] = *((info.header_values[i])[0]);
139                 }
140         }
141
142         for (i = 0; i < DMXNUMHDRS; i++)
143         {
144                 buflength += strlen (header [i]);
145         }
146
147         fbuf = new char [buflength + 64];
148
149         sprintf(fbuf,
150                 "From: %s\nDate: %s\nTo: %s\nSubject: %s\n",
151                 dispname (header [DMXFROM]),
152                 header [DMXDATE],
153                 header [DMXTO],
154                 header [DMXSUBJ]);
155
156         return (fbuf); //need to free this after using it
157 }
158
159 // stuff grabbed from mailx...it's ugly, but it looks pretty
160
161 #define NOSTR ((char *) 0)    /* Nill string pointer */
162 #define LINESIZE 5120            /* max readable line width */
163 static char *phrase(char *, int , int );
164
165 /*
166  * Return a pointer to a dynamic copy of the argument.
167  */
168 // changed salloc to malloc
169 char *
170 savestr(char *str)
171 {
172         register char *cp, *cp2, *top;
173
174         for (cp = str; *cp; cp++)
175                 ;
176         top = (char *)malloc((unsigned)(cp-str + 1));
177         if (top == NOSTR)
178                 return(NOSTR);
179         for (cp = str, cp2 = top; *cp; cp++)
180                 *cp2++ = *cp;
181         *cp2 = 0;
182         return(top);
183 }
184
185
186 char *
187 skin(char *name)
188 {
189         return phrase(name, 0, 0);
190 }
191 /*
192  * Return the full name from an RFC-822 header line
193  * or the last two (or one) component of the address.
194  */
195
196 static char *
197 dispname(const char *hdr)
198 // made it a const char * instead of a char *
199 {
200         char *cp, *cp2;
201
202         if (hdr == 0)
203                 return 0;
204         if (((cp = strchr(hdr, '<')) != 0) && (cp > hdr)) {
205                 *cp = 0;
206                 if ((*hdr == '"') && ((cp = strrchr(++hdr, '"')) != 0))
207                         *cp = 0;
208                 return (char *)hdr;
209         } else if ((cp = strchr(hdr, '(')) != 0) {
210                 hdr = ++cp;
211                 if ((cp = strchr(hdr, '+')) != 0)
212                         *cp = 0;
213                 if ((cp = strrchr(hdr, ')')) != 0)
214                         *cp = 0;
215                 return (char *)hdr;
216         }
217         cp = skin((char *)hdr);
218         if ((cp2 = strrchr(cp, '!')) != 0) {
219                 while (cp2 >= cp && *--cp2 != '!');
220                 cp = ++cp2;
221         }
222         return cp;
223 }
224
225
226 #define equal(a, b)     (strcmp(a,b)==0)/* A nice function to string compare */
227
228 /*
229  * Skin an arpa net address according to the RFC 822 interpretation
230  * of "host-phrase."
231  */
232 // changed salloc to malloc
233 static char *
234 phrase(char *name, int token, int comma)
235 {
236         register char c;
237         register char *cp, *cp2;
238         char *bufend, *nbufp;
239         int gotlt, lastsp, didq;
240         char nbuf[LINESIZE];
241         int nesting;
242
243         if (name == NOSTR)
244                 return(NOSTR);
245         if (strlen(name) >= (unsigned)LINESIZE)
246                 nbufp = (char *)malloc(strlen(name));
247         else
248                 nbufp = nbuf;
249         gotlt = 0;
250         lastsp = 0;
251         bufend = nbufp;
252         for (cp = name, cp2 = bufend; (c = *cp++) != 0;) {
253                 switch (c) {
254                 case '(':
255                         /*
256                                 Start of a comment, ignore it.
257                         */
258                         nesting = 1;
259                         while ((c = *cp) != 0) {
260                                 cp++;
261                                 switch(c) {
262                                 case '\\':
263                                         if (*cp == 0) goto outcm;
264                                         cp++;
265                                         break;
266                                 case '(':
267                                         nesting++;
268                                         break;
269                                 case ')':
270                                         --nesting;
271                                         break;
272                                 }
273                                 if (nesting <= 0) break;
274                         }
275                 outcm:
276                         lastsp = 0;
277                         break;
278                 case '"':
279                         /*
280                                 Start a quoted string.
281                                 Copy it in its entirety.
282                         */
283                         didq = 0;
284                         while ((c = *cp) != 0) {
285                                 cp++;
286                                 switch (c) {
287                                 case '\\':
288                                         if ((c = *cp) == 0) goto outqs;
289                                         cp++;
290                                         break;
291                                 case '"':
292                                         goto outqs;
293                                 }
294                                 if (gotlt == 0 || gotlt == '<') {
295                                         if (lastsp) {
296                                                 lastsp = 0;
297                                                 *cp2++ = ' ';
298                                         }
299                                         if (!didq) {
300                                                 *cp2++ = '"';
301                                                 didq++;
302                                         }
303                                         *cp2++ = c;
304                                 }
305                         }
306                 outqs:
307                         if (didq)
308                                 *cp2++ = '"';
309                         lastsp = 0;
310                         break;
311
312                 case ' ':
313                 case '\t':
314                 case '\n':
315                         if (token && (!comma || c == '\n')) {
316                         done:
317                                 cp[-1] = 0;
318                                 return cp;
319                         }
320                         lastsp = 1;
321                         break;
322
323                 case ',':
324                         *cp2++ = c;
325                         if (gotlt != '<') {
326                                 if (token)
327                                         goto done;
328                                 bufend = cp2 + 1;
329                                 gotlt = 0;
330                         }
331                         break;
332
333                 case '<':
334                         cp2 = bufend;
335                         gotlt = c;
336                         lastsp = 0;
337                         break;
338
339                 case '>':
340                         if (gotlt == '<') {
341                                 gotlt = c;
342                                 break;
343                         }
344
345                         /* FALLTHROUGH . . . */
346
347                 default:
348                         if (gotlt == 0 || gotlt == '<') {
349                                 if (lastsp) {
350                                         lastsp = 0;
351                                         *cp2++ = ' ';
352                                 }
353                                 *cp2++ = c;
354                         }
355                         break;
356                 }
357         }
358         *cp2 = 0;
359         return (token ? --cp : equal(name, nbufp) ? name :
360             nbufp == nbuf ? savestr(nbuf) : nbufp);
361 }
362
363
364