1 /* $TOG: DmxMessage.C /main/6 1998/07/24 16:18:17 mgreess $ */
8 * RESTRICTED CONFIDENTIAL INFORMATION:
10 * The information in this document is subject to special
11 * restrictions in a confidential disclosure agreement between
12 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
13 * document outside HP, IBM, Sun, USL, SCO, or Univel without
14 * Sun's specific written approval. This document and all copies
15 * and derivative works thereof must be returned or destroyed at
18 * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
23 * Common Desktop Environment
25 * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
26 * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
27 * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
28 * (c) Copyright 1993, 1994, 1995 Novell, Inc.
29 * (c) Copyright 1995 Digital Equipment Corp.
30 * (c) Copyright 1995 Fujitsu Limited
31 * (c) Copyright 1995 Hitachi, Ltd.
34 * RESTRICTED RIGHTS LEGEND
36 *Use, duplication, or disclosure by the U.S. Government is subject to
37 *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
38 *Technical Data and Computer Software clause in DFARS 252.227-7013. Rights
39 *for non-DOD U.S. Government Departments and Agencies are as set forth in
40 *FAR 52.227-19(c)(1,2).
42 *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
43 *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A.
44 *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
45 *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
46 *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
47 *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
48 *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
53 #include <LocaleXlate.h>
55 #if !defined(USL) && !defined(__uxp__)
58 #include <EUSCompat.h>
61 #include "Application.h"
62 #include "DtMail/DtMail.hh"
63 #include "DtMailTypes.h"
64 #include "MailSession.hh"
66 #include "str_utils.h"
71 // initialize everything
75 cachedValues = DTM_FALSE;
81 DmxMsg::setHandle (DtMailMessageHandle &h)
88 DmxMsg::setHeader (DtMailHeaderLine &h)
95 DmxMsg::setMessage (DtMail::Message *m)
102 DmxMsg::setInfo (char *info)
104 addlInfo = strdup (info);
109 DmxMsg::getMessageHeader (DmxHeaders which)
111 int i = 0, length = 0;
116 if (which >= DMXNUMHDRS)
117 return (char *) NULL;
119 length = msgHeader.header_values[which].length ();
121 str = errorString(which);
123 str = *((msgHeader.header_values[which])[0]);
125 //need to free this after using it
134 DmxMsg::getHeaders (DtMailBoolean abbreviated_only)
137 DtMail::Session *m_session = theRoamApp.session()->session();
138 DtMail::MailRc *mail_rc = m_session->mailRc(error);
139 DtMail::Envelope *env = message->getEnvelope(error);
141 DtMailHeaderHandle hdr_hnd;
143 DtMailValueSeq value;
145 // Code from MsgScrollingList - display_message().
146 // We're trying to reduce heap size by not allocating and
147 // deleting space in every loop iteration. So just have a
148 // fixed size buffer initially.
151 // Initial line size. When not enough, allocate more.
152 int buffer_size = 2048;
153 char *buffer = new char [buffer_size];
156 char *newline = "\n";
157 char *separator = ": ";
161 // Iterate through each header in the message and add it
164 for (hdr_hnd = env->getFirstHeader(error, &name, value), *buffer = '\0';
165 hdr_hnd && !error.isSet();
166 hdr_hnd = env->getNextHeader(error, hdr_hnd, &name, value), hdr_num++)
168 if (abbreviated_only == DTM_TRUE &&
169 (hdr_num != 0 || strcmp(name, "From") != 0))
172 if (mail_rc->ignore(ierror, name))
180 for (val=0; val<value.length(); val++)
181 count += strlen(name) +
182 strlen(*(value[val])) +
186 if (count > buffer_size)
188 // Need to increase buffer size.
192 new_buffer = new char [buffer_size];
193 memset(new_buffer, 0, buffer_size);
195 strcpy(new_buffer, buffer);
200 for (val=0; val<value.length(); val++)
202 strcat(buffer, name);
204 if (hdr_num != 0 || strcmp(name, "From") != 0)
205 strcat(buffer, separator);
209 strcat(buffer, *(value[val]));
210 strcat(buffer, newline);
217 // Need to free this after using;
226 DmxMsg::getPrintedHeaders (DmxPrintHeadersEnum header_format)
228 char *newline = "\n";
231 switch (header_format)
233 case DMX_PRINT_HEADERS_NONE:
234 buffer = new char [strlen(newline) + 1];
235 strcpy(buffer, (const char *) newline);
237 case DMX_PRINT_HEADERS_STANDARD:
238 buffer = getStandardHeaders(msgHeader);
240 case DMX_PRINT_HEADERS_ABBREV:
241 buffer = getHeaders(DTM_TRUE);
243 case DMX_PRINT_HEADERS_ALL:
244 buffer = getHeaders(DTM_FALSE);
247 fprintf (stderr, "error in DmxMsg::display\n");
252 // Need to free this after using;
260 DmxPrintHeadersEnum header_format,
261 DmxPrintOutputProc print_proc,
265 DtMailBoolean FirstIsText = DTM_FALSE;
266 DtMail::BodyPart *firstPart = NULL, *nextpart = NULL;
271 *sunDataDescription = NULL,
274 void *contents = NULL;
275 unsigned long length = 0;
285 // read in body part info
286 if (cachedValues != DTM_TRUE)
289 firstPart = bodyParts [0];
291 firstPart->getContents(env,
292 (const void **) &contents,
299 if (handleError(env, "getContents") == DTM_TRUE)
303 DtMailValueSeq value;
304 DtMailBoolean err = DTM_FALSE;
306 // Get the bodypart's charset - Try MIME first then V3
307 firstPart->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
308 if (env.isNotSet()) {
309 mime_cs = firstPart->csFromContentType(value);
313 firstPart->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
314 if (env.isNotSet()) {
315 strcpy(v3_cs, *(value[0]));
323 // If cannot obtain bodypart's charset header, then maybe this message
324 // has only one bodypart, then in this case the charset header maybe
325 // among the message's envelope (main message headers).
326 // Get the envelope of the message (in order to access the headers)
327 DtMail::Envelope *envelope = NULL;
328 if (err == DTM_TRUE) {
329 envelope = message->getEnvelope(env);
334 "DEBUG dtmailpr: Looking at main message header\n");
338 // Check for MIME charset header and then for V3 charset header
339 if (envelope != NULL) {
340 envelope->getHeader(env, DtMailMessageContentType, DTM_TRUE, value);
341 if (env.isNotSet()) {
342 mime_cs = firstPart->csFromContentType(value);
347 if (mime_cs == NULL || err == DTM_TRUE) {
349 envelope->getHeader(env, DtMailMessageV3charset, DTM_TRUE, value);
350 if (env.isNotSet()) {
351 strcpy(v3_cs, *(value[0]));
359 env.logError(DTM_FALSE, "DEBUG dtmailpr: envelope is null\n");
364 // Default codeset in case mime_cs and v3_cs are both null.
365 if ((mime_cs == NULL) && (strlen(v3_cs) == 0)) {
367 firstPart->DtXlateOpToStdLocale(DtLCX_OPER_SETLOCALE,
368 setlocale(LC_CTYPE, NULL),
372 strcpy(v3_cs, "DEFAULT");
379 // Get iconv from and to codeset and do conversion.
382 from_cs = firstPart->csToConvName(mime_cs);
384 env.logError(DTM_FALSE, "DEBUG dtmailpr: mime_cs = %s\n", mime_cs);
387 from_cs = firstPart->csToConvName(v3_cs);
389 env.logError(DTM_FALSE, "DEBUG dtmailpr: v3_cs = %s\n", v3_cs);
392 to_cs = firstPart->locToConvName();
395 if ( from_cs == NULL )
396 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs is NULL\n");
398 env.logError(DTM_FALSE, "DEBUG dtmailpr: from_cs = %s\n", from_cs);
401 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs is NULL\n");
403 env.logError(DTM_FALSE, "DEBUG dtmailpr: to_cs = %s\n", to_cs);
406 if ( from_cs && to_cs ) {
407 if ( strcasecmp(from_cs, to_cs) != 0 ) {
408 converted = firstPart->csConvert(
415 env.logError(DTM_FALSE,
416 "DEBUG dtmailpr: converted = %d\n", converted);
427 // End of For CHARSET
429 newline = new char [2];
434 // Print out the message headers.
436 buf = getPrintedHeaders(header_format);
437 print_proc(stream, buf);
438 print_proc(stream, newline);
442 // Print out the message body.
444 buf = new char [length + 1];
445 memset (buf, 0, (unsigned int) length + 1);
446 memmove (buf, contents, (unsigned int) length);
447 buf [length] = '\0'; // null-terminate that puppy
448 print_proc(stream, buf);
449 print_proc(stream, newline);
453 if (converted && contents)
456 // No attachments? We're done.
460 int i = 0, attbuflen = 0;
464 print_proc(stream, newline);
465 for (i = 1; i < numBPs ; i++)
467 nextpart = bodyParts [i];
469 if (nextpart == NULL)
470 fprintf (stderr, "Error getting part!\n");
474 sunDataDescription = "";
479 nextpart->getContents(env,
485 &sunDataDescription);
486 if (handleError (env, "getContents") == DTM_TRUE)
490 type = "(type unknown)";
493 name = "(name unknown)";
495 if (sunDataDescription == NULL)
499 // should add bracket or something
500 sunbuf = new char [strlen (sunDataDescription) + 10];
501 sprintf(sunbuf, " (%s)", sunDataDescription);
502 description = sunbuf;
505 attbuflen = strlen(name) + strlen(type) + strlen(description);
506 attbuf = new char [attbuflen + 64];
508 "[%d] \"%s\"%s, %s, %ld bytes",
514 print_proc(stream, attbuf);
515 print_proc(stream, newline);
528 // store the body parts for later reference
531 DtMailBoolean FirstIsText = DTM_FALSE;
532 DtMail::BodyPart *part = NULL, *nextpart = NULL;
533 char *type = NULL, *attr = NULL;
535 int bc = message->getBodyCount (env);
536 if (handleError (env, "getBodyCount") == DTM_TRUE)
539 part = message->getFirstBodyPart (env);
540 if (handleError (env, "getFirstBodyPart") == DTM_TRUE)
543 part->getContents (env, NULL, NULL, &type, NULL, NULL, NULL);
544 if (handleError (env, "getContents") == DTM_TRUE)
547 bodyParts = new (DtMail::BodyPart *[bc]);
548 cachedValues = DTM_TRUE;
551 bodyParts [0] = part;
557 attr = DtDtsDataTypeToAttributeValue (type,
562 FirstIsText = DTM_TRUE;
564 //free (type); // it's allocating some data for us
566 FirstIsText = DTM_FALSE;
569 // No attachments? We're done.
575 for (i = 1; i < bc; i++)
578 nextpart = message->getNextBodyPart (env,
580 if (handleError (env, "getNextBodyPart") == DTM_TRUE)
583 if (nextpart == NULL)
584 fprintf (stderr, "Error getting part!\n");
587 bodyParts [i] = nextpart;