2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: DmxMessage.C /main/6 1998/07/24 16:18:17 mgreess $ */
30 * RESTRICTED CONFIDENTIAL INFORMATION:
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
40 * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
45 * Common Desktop Environment
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.
56 * RESTRICTED RIGHTS LEGEND
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).
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
75 #include <LocaleXlate.h>
77 #if !defined(USL) && !defined(__uxp__)
80 #include <EUSCompat.h>
83 #include "Application.h"
84 #include "DtMail/DtMail.hh"
85 #include "DtMailTypes.h"
86 #include "MailSession.hh"
88 #include "str_utils.h"
93 // initialize everything
97 cachedValues = DTM_FALSE;
99 msgHeader.header_values = 0;
100 msgHeader.number_of_names = 0;
108 DmxMsg::setHandle (DtMailMessageHandle &h)
115 DmxMsg::setHeader (DtMailHeaderLine &h)
122 DmxMsg::setMessage (DtMail::Message *m)
129 DmxMsg::setInfo (char *info)
131 addlInfo = strdup (info);
136 DmxMsg::getMessageHeader (DmxHeaders which)
138 int i = 0, length = 0;
143 if (which >= DMXNUMHDRS)
144 return (char *) NULL;
146 length = msgHeader.header_values[which].length ();
148 str = errorString(which);
150 str = *((msgHeader.header_values[which])[0]);
152 //need to free this after using it
161 DmxMsg::getHeaders (DtMailBoolean abbreviated_only)
164 DtMail::Session *m_session = theRoamApp.session()->session();
165 DtMail::MailRc *mail_rc = m_session->mailRc(error);
166 DtMail::Envelope *env = message->getEnvelope(error);
168 DtMailHeaderHandle hdr_hnd;
170 DtMailValueSeq value;
172 // Code from MsgScrollingList - display_message().
173 // We're trying to reduce heap size by not allocating and
174 // deleting space in every loop iteration. So just have a
175 // fixed size buffer initially.
178 // Initial line size. When not enough, allocate more.
179 int buffer_size = 2048;
180 char *buffer = new char [buffer_size];
183 char *newline = "\n";
184 char *separator = ": ";
188 // Iterate through each header in the message and add it
191 for (hdr_hnd = env->getFirstHeader(error, &name, value), *buffer = '\0';
192 hdr_hnd && !error.isSet();
193 hdr_hnd = env->getNextHeader(error, hdr_hnd, &name, value), hdr_num++)
195 if (abbreviated_only == DTM_TRUE &&
196 (hdr_num != 0 || strcmp(name, "From") != 0))
199 if (mail_rc->ignore(ierror, name))
207 for (val=0; val<value.length(); val++)
208 count += strlen(name) +
209 strlen(*(value[val])) +
213 if (count > buffer_size)
215 // Need to increase buffer size.
219 new_buffer = new char [buffer_size];
220 memset(new_buffer, 0, buffer_size);
222 strcpy(new_buffer, buffer);
227 for (val=0; val<value.length(); val++)
229 strcat(buffer, name);
231 if (hdr_num != 0 || strcmp(name, "From") != 0)
232 strcat(buffer, separator);
236 strcat(buffer, *(value[val]));
237 strcat(buffer, newline);
244 // Need to free this after using;
253 DmxMsg::getPrintedHeaders (DmxPrintHeadersEnum header_format)
255 char *newline = "\n";
258 switch (header_format)
260 case DMX_PRINT_HEADERS_NONE:
261 buffer = new char [strlen(newline) + 1];
262 strcpy(buffer, (const char *) newline);
264 case DMX_PRINT_HEADERS_STANDARD:
265 buffer = getStandardHeaders(msgHeader);
267 case DMX_PRINT_HEADERS_ABBREV:
268 buffer = getHeaders(DTM_TRUE);
270 case DMX_PRINT_HEADERS_ALL:
271 buffer = getHeaders(DTM_FALSE);
274 fprintf (stderr, "error in DmxMsg::display\n");
279 // Need to free this after using;
287 DmxPrintHeadersEnum header_format,
288 DmxPrintOutputProc print_proc,
292 DtMailBoolean FirstIsText = DTM_FALSE;
293 DtMail::BodyPart *firstPart = NULL, *nextpart = NULL;
298 *sunDataDescription = NULL,
301 void *contents = NULL;
302 unsigned long length = 0;
312 // read in body part info
313 if (cachedValues != DTM_TRUE)
316 firstPart = bodyParts [0];
318 firstPart->getContents(env,
319 (const void **) &contents,
326 if (handleError(env, "getContents") == DTM_TRUE)
330 DtMailValueSeq value;
331 DtMailBoolean err = DTM_FALSE;
333 // Get the bodypart's charset - Try MIME first then V3
334 firstPart->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
335 if (env.isNotSet()) {
336 mime_cs = firstPart->csFromContentType(value);
340 firstPart->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
341 if (env.isNotSet()) {
342 strcpy(v3_cs, *(value[0]));
350 // If cannot obtain bodypart's charset header, then maybe this message
351 // has only one bodypart, then in this case the charset header maybe
352 // among the message's envelope (main message headers).
353 // Get the envelope of the message (in order to access the headers)
354 DtMail::Envelope *envelope = NULL;
355 if (err == DTM_TRUE) {
356 envelope = message->getEnvelope(env);
361 "DEBUG dtmailpr: Looking at main message header\n");
365 // Check for MIME charset header and then for V3 charset header
366 if (envelope != NULL) {
367 envelope->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
368 if (env.isNotSet()) {
369 mime_cs = firstPart->csFromContentType(value);
374 if (mime_cs == NULL || err == DTM_TRUE) {
376 envelope->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
377 if (env.isNotSet()) {
378 strcpy(v3_cs, *(value[0]));
386 env.logError(DTM_FALSE, "DEBUG dtmailpr: envelope is null\n");
391 // Default codeset in case mime_cs and v3_cs are both null.
392 if ((mime_cs == NULL) && (strlen(v3_cs) == 0)) {
394 firstPart->DtXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,
395 setlocale(LC_CTYPE, NULL),
399 strcpy(v3_cs, "DEFAULT");
405 // Get iconv from and to codeset and do conversion.
408 from_cs = firstPart->csToConvName(mime_cs);
410 env.logError(DTM_FALSE, "DEBUG dtmailpr: mime_cs = %s\n", mime_cs);
413 from_cs = firstPart->csToConvName(v3_cs);
415 env.logError(DTM_FALSE, "DEBUG dtmailpr: v3_cs = %s\n", v3_cs);
418 to_cs = firstPart->locToConvName();
421 if ( from_cs == NULL )
422 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs is NULL\n");
424 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs = %s\n", from_cs);
427 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs is NULL\n");
429 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs = %s\n", to_cs);
432 if ( from_cs && to_cs ) {
433 if ( strcasecmp(from_cs, to_cs) != 0 ) {
434 converted = firstPart->csConvert(
441 env.logError(DTM_FALSE,
442 "DEBUG dtmailpr: converted = %d\n", converted);
453 // End of For CHARSET
455 newline = new char [2];
460 // Print out the message headers.
462 buf = getPrintedHeaders(header_format);
463 print_proc(stream, buf);
464 print_proc(stream, newline);
468 // Print out the message body.
470 buf = new char [length + 1];
471 memset (buf, 0, (unsigned int) length + 1);
472 memmove (buf, contents, (unsigned int) length);
473 buf [length] = '\0'; // null-terminate that puppy
474 print_proc(stream, buf);
475 print_proc(stream, newline);
479 if (converted && contents)
482 // No attachments? We're done.
486 int i = 0, attbuflen = 0;
490 print_proc(stream, newline);
491 for (i = 1; i < numBPs ; i++)
493 nextpart = bodyParts [i];
495 if (nextpart == NULL)
496 fprintf (stderr, "Error getting part!\n");
500 sunDataDescription = "";
505 nextpart->getContents(env,
511 &sunDataDescription);
512 if (handleError (env, "getContents") == DTM_TRUE)
516 type = "(type unknown)";
519 name = "(name unknown)";
521 if (sunDataDescription == NULL)
525 // should add bracket or something
526 sunbuf = new char [strlen (sunDataDescription) + 10];
527 sprintf(sunbuf, " (%s)", sunDataDescription);
528 description = sunbuf;
531 attbuflen = strlen(name) + strlen(type) + strlen(description);
532 attbuf = new char [attbuflen + 64];
534 "[%d] \"%s\"%s, %s, %ld bytes",
540 print_proc(stream, attbuf);
541 print_proc(stream, newline);
554 // store the body parts for later reference
557 DtMailBoolean FirstIsText = DTM_FALSE;
558 DtMail::BodyPart *part = NULL, *nextpart = NULL;
559 char *type = NULL, *attr = NULL;
561 int bc = message->getBodyCount (env);
562 if (handleError (env, "getBodyCount") == DTM_TRUE)
565 part = message->getFirstBodyPart (env);
566 if (handleError (env, "getFirstBodyPart") == DTM_TRUE)
569 part->getContents (env, NULL, NULL, &type, NULL, NULL, NULL);
570 if (handleError (env, "getContents") == DTM_TRUE)
573 bodyParts = new (DtMail::BodyPart *[bc]);
574 cachedValues = DTM_TRUE;
577 bodyParts [0] = part;
583 attr = DtDtsDataTypeToAttributeValue (type,
588 FirstIsText = DTM_TRUE;
590 //free (type); // it's allocating some data for us
592 FirstIsText = DTM_FALSE;
595 // No attachments? We're done.
601 for (i = 1; i < bc; i++)
604 nextpart = message->getNextBodyPart (env,
606 if (handleError (env, "getNextBodyPart") == DTM_TRUE)
609 if (nextpart == NULL)
610 fprintf (stderr, "Error getting part!\n");
613 bodyParts [i] = nextpart;