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>
79 #include "Application.h"
80 #include "DtMail/DtMail.hh"
81 #include "DtMailTypes.h"
82 #include "MailSession.hh"
84 #include "str_utils.h"
89 // initialize everything
93 cachedValues = DTM_FALSE;
95 msgHeader.header_values = 0;
96 msgHeader.number_of_names = 0;
104 DmxMsg::setHandle (DtMailMessageHandle &h)
111 DmxMsg::setHeader (DtMailHeaderLine &h)
118 DmxMsg::setMessage (DtMail::Message *m)
125 DmxMsg::setInfo (char *info)
127 addlInfo = strdup (info);
132 DmxMsg::getMessageHeader (DmxHeaders which)
134 int i = 0, length = 0;
139 if (which >= DMXNUMHDRS)
140 return (char *) NULL;
142 length = msgHeader.header_values[which].length ();
144 str = errorString(which);
146 str = *((msgHeader.header_values[which])[0]);
148 //need to free this after using it
157 DmxMsg::getHeaders (DtMailBoolean abbreviated_only)
160 DtMail::Session *m_session = theRoamApp.session()->session();
161 DtMail::MailRc *mail_rc = m_session->mailRc(error);
162 DtMail::Envelope *env = message->getEnvelope(error);
164 DtMailHeaderHandle hdr_hnd;
166 DtMailValueSeq value;
168 // Code from MsgScrollingList - display_message().
169 // We're trying to reduce heap size by not allocating and
170 // deleting space in every loop iteration. So just have a
171 // fixed size buffer initially.
174 // Initial line size. When not enough, allocate more.
175 int buffer_size = 2048;
176 char *buffer = new char [buffer_size];
179 char *newline = "\n";
180 char *separator = ": ";
184 // Iterate through each header in the message and add it
187 for (hdr_hnd = env->getFirstHeader(error, &name, value), *buffer = '\0';
188 hdr_hnd && !error.isSet();
189 hdr_hnd = env->getNextHeader(error, hdr_hnd, &name, value), hdr_num++)
191 if (abbreviated_only == DTM_TRUE &&
192 (hdr_num != 0 || strcmp(name, "From") != 0))
195 if (mail_rc->ignore(ierror, name))
203 for (val=0; val<value.length(); val++)
204 count += strlen(name) +
205 strlen(*(value[val])) +
209 if (count > buffer_size)
211 // Need to increase buffer size.
215 new_buffer = new char [buffer_size];
216 memset(new_buffer, 0, buffer_size);
218 strcpy(new_buffer, buffer);
223 for (val=0; val<value.length(); val++)
225 strcat(buffer, name);
227 if (hdr_num != 0 || strcmp(name, "From") != 0)
228 strcat(buffer, separator);
232 strcat(buffer, *(value[val]));
233 strcat(buffer, newline);
240 // Need to free this after using;
249 DmxMsg::getPrintedHeaders (DmxPrintHeadersEnum header_format)
251 char *newline = "\n";
254 switch (header_format)
256 case DMX_PRINT_HEADERS_NONE:
257 buffer = new char [strlen(newline) + 1];
258 strcpy(buffer, (const char *) newline);
260 case DMX_PRINT_HEADERS_STANDARD:
261 buffer = getStandardHeaders(msgHeader);
263 case DMX_PRINT_HEADERS_ABBREV:
264 buffer = getHeaders(DTM_TRUE);
266 case DMX_PRINT_HEADERS_ALL:
267 buffer = getHeaders(DTM_FALSE);
270 fprintf (stderr, "error in DmxMsg::display\n");
275 // Need to free this after using;
283 DmxPrintHeadersEnum header_format,
284 DmxPrintOutputProc print_proc,
288 DtMailBoolean FirstIsText = DTM_FALSE;
289 DtMail::BodyPart *firstPart = NULL, *nextpart = NULL;
294 *sunDataDescription = NULL,
297 void *contents = NULL;
298 unsigned long length = 0;
308 // read in body part info
309 if (cachedValues != DTM_TRUE)
312 firstPart = bodyParts [0];
314 firstPart->getContents(env,
315 (const void **) &contents,
322 if (handleError(env, "getContents") == DTM_TRUE)
326 DtMailValueSeq value;
327 DtMailBoolean err = DTM_FALSE;
329 // Get the bodypart's charset - Try MIME first then V3
330 firstPart->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
331 if (env.isNotSet()) {
332 mime_cs = firstPart->csFromContentType(value);
336 firstPart->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
337 if (env.isNotSet()) {
338 strcpy(v3_cs, *(value[0]));
346 // If cannot obtain bodypart's charset header, then maybe this message
347 // has only one bodypart, then in this case the charset header maybe
348 // among the message's envelope (main message headers).
349 // Get the envelope of the message (in order to access the headers)
350 DtMail::Envelope *envelope = NULL;
351 if (err == DTM_TRUE) {
352 envelope = message->getEnvelope(env);
357 "DEBUG dtmailpr: Looking at main message header\n");
361 // Check for MIME charset header and then for V3 charset header
362 if (envelope != NULL) {
363 envelope->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
364 if (env.isNotSet()) {
365 mime_cs = firstPart->csFromContentType(value);
370 if (mime_cs == NULL || err == DTM_TRUE) {
372 envelope->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
373 if (env.isNotSet()) {
374 strcpy(v3_cs, *(value[0]));
382 env.logError(DTM_FALSE, "DEBUG dtmailpr: envelope is null\n");
387 // Default codeset in case mime_cs and v3_cs are both null.
388 if ((mime_cs == NULL) && (strlen(v3_cs) == 0)) {
390 firstPart->DtXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,
391 setlocale(LC_CTYPE, NULL),
395 strcpy(v3_cs, "DEFAULT");
401 // Get iconv from and to codeset and do conversion.
404 from_cs = firstPart->csToConvName(mime_cs);
406 env.logError(DTM_FALSE, "DEBUG dtmailpr: mime_cs = %s\n", mime_cs);
409 from_cs = firstPart->csToConvName(v3_cs);
411 env.logError(DTM_FALSE, "DEBUG dtmailpr: v3_cs = %s\n", v3_cs);
414 to_cs = firstPart->locToConvName();
417 if ( from_cs == NULL )
418 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs is NULL\n");
420 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs = %s\n", from_cs);
423 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs is NULL\n");
425 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs = %s\n", to_cs);
428 if ( from_cs && to_cs ) {
429 if ( strcasecmp(from_cs, to_cs) != 0 ) {
430 converted = firstPart->csConvert(
437 env.logError(DTM_FALSE,
438 "DEBUG dtmailpr: converted = %d\n", converted);
449 // End of For CHARSET
451 newline = new char [2];
456 // Print out the message headers.
458 buf = getPrintedHeaders(header_format);
459 print_proc(stream, buf);
460 print_proc(stream, newline);
464 // Print out the message body.
466 buf = new char [length + 1];
467 memset (buf, 0, (unsigned int) length + 1);
468 memmove (buf, contents, (unsigned int) length);
469 buf [length] = '\0'; // null-terminate that puppy
470 print_proc(stream, buf);
471 print_proc(stream, newline);
475 if (converted && contents)
478 // No attachments? We're done.
482 int i = 0, attbuflen = 0;
486 print_proc(stream, newline);
487 for (i = 1; i < numBPs ; i++)
489 nextpart = bodyParts [i];
491 if (nextpart == NULL)
492 fprintf (stderr, "Error getting part!\n");
496 sunDataDescription = "";
501 nextpart->getContents(env,
507 &sunDataDescription);
508 if (handleError (env, "getContents") == DTM_TRUE)
512 type = "(type unknown)";
515 name = "(name unknown)";
517 if (sunDataDescription == NULL)
521 // should add bracket or something
522 sunbuf = new char [strlen (sunDataDescription) + 10];
523 sprintf(sunbuf, " (%s)", sunDataDescription);
524 description = sunbuf;
527 attbuflen = strlen(name) + strlen(type) + strlen(description);
528 attbuf = new char [attbuflen + 64];
530 "[%d] \"%s\"%s, %s, %ld bytes",
536 print_proc(stream, attbuf);
537 print_proc(stream, newline);
550 // store the body parts for later reference
553 DtMailBoolean FirstIsText = DTM_FALSE;
554 DtMail::BodyPart *part = NULL, *nextpart = NULL;
555 char *type = NULL, *attr = NULL;
557 int bc = message->getBodyCount (env);
558 if (handleError (env, "getBodyCount") == DTM_TRUE)
561 part = message->getFirstBodyPart (env);
562 if (handleError (env, "getFirstBodyPart") == DTM_TRUE)
565 part->getContents (env, NULL, NULL, &type, NULL, NULL, NULL);
566 if (handleError (env, "getContents") == DTM_TRUE)
569 bodyParts = new (DtMail::BodyPart *[bc]);
570 cachedValues = DTM_TRUE;
573 bodyParts [0] = part;
579 attr = DtDtsDataTypeToAttributeValue (type,
584 FirstIsText = DTM_TRUE;
586 //free (type); // it's allocating some data for us
588 FirstIsText = DTM_FALSE;
591 // No attachments? We're done.
597 for (i = 1; i < bc; i++)
600 nextpart = message->getNextBodyPart (env,
602 if (handleError (env, "getNextBodyPart") == DTM_TRUE)
605 if (nextpart == NULL)
606 fprintf (stderr, "Error getting part!\n");
609 bodyParts [i] = nextpart;