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 librararies 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;
103 DmxMsg::setHandle (DtMailMessageHandle &h)
110 DmxMsg::setHeader (DtMailHeaderLine &h)
117 DmxMsg::setMessage (DtMail::Message *m)
124 DmxMsg::setInfo (char *info)
126 addlInfo = strdup (info);
131 DmxMsg::getMessageHeader (DmxHeaders which)
133 int i = 0, length = 0;
138 if (which >= DMXNUMHDRS)
139 return (char *) NULL;
141 length = msgHeader.header_values[which].length ();
143 str = errorString(which);
145 str = *((msgHeader.header_values[which])[0]);
147 //need to free this after using it
156 DmxMsg::getHeaders (DtMailBoolean abbreviated_only)
159 DtMail::Session *m_session = theRoamApp.session()->session();
160 DtMail::MailRc *mail_rc = m_session->mailRc(error);
161 DtMail::Envelope *env = message->getEnvelope(error);
163 DtMailHeaderHandle hdr_hnd;
165 DtMailValueSeq value;
167 // Code from MsgScrollingList - display_message().
168 // We're trying to reduce heap size by not allocating and
169 // deleting space in every loop iteration. So just have a
170 // fixed size buffer initially.
173 // Initial line size. When not enough, allocate more.
174 int buffer_size = 2048;
175 char *buffer = new char [buffer_size];
178 char *newline = "\n";
179 char *separator = ": ";
183 // Iterate through each header in the message and add it
186 for (hdr_hnd = env->getFirstHeader(error, &name, value), *buffer = '\0';
187 hdr_hnd && !error.isSet();
188 hdr_hnd = env->getNextHeader(error, hdr_hnd, &name, value), hdr_num++)
190 if (abbreviated_only == DTM_TRUE &&
191 (hdr_num != 0 || strcmp(name, "From") != 0))
194 if (mail_rc->ignore(ierror, name))
202 for (val=0; val<value.length(); val++)
203 count += strlen(name) +
204 strlen(*(value[val])) +
208 if (count > buffer_size)
210 // Need to increase buffer size.
214 new_buffer = new char [buffer_size];
215 memset(new_buffer, 0, buffer_size);
217 strcpy(new_buffer, buffer);
222 for (val=0; val<value.length(); val++)
224 strcat(buffer, name);
226 if (hdr_num != 0 || strcmp(name, "From") != 0)
227 strcat(buffer, separator);
231 strcat(buffer, *(value[val]));
232 strcat(buffer, newline);
239 // Need to free this after using;
248 DmxMsg::getPrintedHeaders (DmxPrintHeadersEnum header_format)
250 char *newline = "\n";
253 switch (header_format)
255 case DMX_PRINT_HEADERS_NONE:
256 buffer = new char [strlen(newline) + 1];
257 strcpy(buffer, (const char *) newline);
259 case DMX_PRINT_HEADERS_STANDARD:
260 buffer = getStandardHeaders(msgHeader);
262 case DMX_PRINT_HEADERS_ABBREV:
263 buffer = getHeaders(DTM_TRUE);
265 case DMX_PRINT_HEADERS_ALL:
266 buffer = getHeaders(DTM_FALSE);
269 fprintf (stderr, "error in DmxMsg::display\n");
274 // Need to free this after using;
282 DmxPrintHeadersEnum header_format,
283 DmxPrintOutputProc print_proc,
287 DtMailBoolean FirstIsText = DTM_FALSE;
288 DtMail::BodyPart *firstPart = NULL, *nextpart = NULL;
293 *sunDataDescription = NULL,
296 void *contents = NULL;
297 unsigned long length = 0;
307 // read in body part info
308 if (cachedValues != DTM_TRUE)
311 firstPart = bodyParts [0];
313 firstPart->getContents(env,
314 (const void **) &contents,
321 if (handleError(env, "getContents") == DTM_TRUE)
325 DtMailValueSeq value;
326 DtMailBoolean err = DTM_FALSE;
328 // Get the bodypart's charset - Try MIME first then V3
329 firstPart->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
330 if (env.isNotSet()) {
331 mime_cs = firstPart->csFromContentType(value);
335 firstPart->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
336 if (env.isNotSet()) {
337 strcpy(v3_cs, *(value[0]));
345 // If cannot obtain bodypart's charset header, then maybe this message
346 // has only one bodypart, then in this case the charset header maybe
347 // among the message's envelope (main message headers).
348 // Get the envelope of the message (in order to access the headers)
349 DtMail::Envelope *envelope = NULL;
350 if (err == DTM_TRUE) {
351 envelope = message->getEnvelope(env);
356 "DEBUG dtmailpr: Looking at main message header\n");
360 // Check for MIME charset header and then for V3 charset header
361 if (envelope != NULL) {
362 envelope->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
363 if (env.isNotSet()) {
364 mime_cs = firstPart->csFromContentType(value);
369 if (mime_cs == NULL || err == DTM_TRUE) {
371 envelope->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
372 if (env.isNotSet()) {
373 strcpy(v3_cs, *(value[0]));
381 env.logError(DTM_FALSE, "DEBUG dtmailpr: envelope is null\n");
386 // Default codeset in case mime_cs and v3_cs are both null.
387 if ((mime_cs == NULL) && (strlen(v3_cs) == 0)) {
389 firstPart->DtXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,
390 setlocale(LC_CTYPE, NULL),
394 strcpy(v3_cs, "DEFAULT");
400 // Get iconv from and to codeset and do conversion.
403 from_cs = firstPart->csToConvName(mime_cs);
405 env.logError(DTM_FALSE, "DEBUG dtmailpr: mime_cs = %s\n", mime_cs);
408 from_cs = firstPart->csToConvName(v3_cs);
410 env.logError(DTM_FALSE, "DEBUG dtmailpr: v3_cs = %s\n", v3_cs);
413 to_cs = firstPart->locToConvName();
416 if ( from_cs == NULL )
417 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs is NULL\n");
419 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs = %s\n", from_cs);
422 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs is NULL\n");
424 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs = %s\n", to_cs);
427 if ( from_cs && to_cs ) {
428 if ( strcasecmp(from_cs, to_cs) != 0 ) {
429 converted = firstPart->csConvert(
436 env.logError(DTM_FALSE,
437 "DEBUG dtmailpr: converted = %d\n", converted);
448 // End of For CHARSET
450 newline = new char [2];
455 // Print out the message headers.
457 buf = getPrintedHeaders(header_format);
458 print_proc(stream, buf);
459 print_proc(stream, newline);
463 // Print out the message body.
465 buf = new char [length + 1];
466 memset (buf, 0, (unsigned int) length + 1);
467 memmove (buf, contents, (unsigned int) length);
468 buf [length] = '\0'; // null-terminate that puppy
469 print_proc(stream, buf);
470 print_proc(stream, newline);
474 if (converted && contents)
477 // No attachments? We're done.
481 int i = 0, attbuflen = 0;
485 print_proc(stream, newline);
486 for (i = 1; i < numBPs ; i++)
488 nextpart = bodyParts [i];
490 if (nextpart == NULL)
491 fprintf (stderr, "Error getting part!\n");
495 sunDataDescription = "";
500 nextpart->getContents(env,
506 &sunDataDescription);
507 if (handleError (env, "getContents") == DTM_TRUE)
511 type = "(type unknown)";
514 name = "(name unknown)";
516 if (sunDataDescription == NULL)
520 // should add bracket or something
521 sunbuf = new char [strlen (sunDataDescription) + 10];
522 sprintf(sunbuf, " (%s)", sunDataDescription);
523 description = sunbuf;
526 attbuflen = strlen(name) + strlen(type) + strlen(description);
527 attbuf = new char [attbuflen + 64];
529 "[%d] \"%s\"%s, %s, %ld bytes",
535 print_proc(stream, attbuf);
536 print_proc(stream, newline);
549 // store the body parts for later reference
552 DtMailBoolean FirstIsText = DTM_FALSE;
553 DtMail::BodyPart *part = NULL, *nextpart = NULL;
554 char *type = NULL, *attr = NULL;
556 int bc = message->getBodyCount (env);
557 if (handleError (env, "getBodyCount") == DTM_TRUE)
560 part = message->getFirstBodyPart (env);
561 if (handleError (env, "getFirstBodyPart") == DTM_TRUE)
564 part->getContents (env, NULL, NULL, &type, NULL, NULL, NULL);
565 if (handleError (env, "getContents") == DTM_TRUE)
568 bodyParts = new (DtMail::BodyPart *[bc]);
569 cachedValues = DTM_TRUE;
572 bodyParts [0] = part;
578 attr = DtDtsDataTypeToAttributeValue (type,
583 FirstIsText = DTM_TRUE;
585 //free (type); // it's allocating some data for us
587 FirstIsText = DTM_FALSE;
590 // No attachments? We're done.
596 for (i = 1; i < bc; i++)
599 nextpart = message->getNextBodyPart (env,
601 if (handleError (env, "getNextBodyPart") == DTM_TRUE)
604 if (nextpart == NULL)
605 fprintf (stderr, "Error getting part!\n");
608 bodyParts [i] = nextpart;