home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / ExternalMail.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  5.4 KB  |  184 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.     This file contains Navigator only code which supports "Send Page..." and
  21.     "mailto:" URLs using an external mail client registered with InternetConfig.
  22. */
  23.  
  24. #ifndef MOZ_MAIL_NEWS
  25.  
  26. #include "msgcom.h"            // MSG_MailDocument()
  27. #include "xlate.h"            // PrintSetup
  28. #include "shist.h"            // SHIST_GetCurrent()
  29. #include "xpgetstr.h"        // XP_GetString()
  30. #include "CURLDispatcher.h"    // CURLDispatcher::DispatchURL()
  31. #include "CNSContext.h"        // ExtractNSContext()
  32. #include "net.h"            // NET_Escape()
  33.  
  34. extern int MK_MSG_MSG_COMPOSITION;
  35.  
  36. #define MAX_MAIL_SIZE 300000
  37. extern "C" void FE_DoneMailTo(PrintSetup * print) ;
  38. extern "C" void FE_DoneMailTo(PrintSetup * print) 
  39. {
  40.     const char * prefix = "mailto:?body=";
  41.     const char * blankLines = "\r\n\r\n";
  42.     
  43.     XP_ASSERT(print);
  44.     if (!print)
  45.         return;
  46.         
  47.     XP_ASSERT(print->url);
  48.     if (!print->url)
  49.         return;
  50.         
  51.     fclose(print->out);        // don't need this for writing anymore.
  52.  
  53.     MWContext * context = (MWContext *) (print->carg);
  54.     if (!context)            // we'll require this later
  55.         return;
  56.  
  57.     char * buffer = (char *) malloc(MAX_MAIL_SIZE);
  58.     
  59.     if (buffer) {
  60.            strcpy(buffer, print->url->address);
  61.          strcat(buffer, blankLines);
  62.        
  63.            int    buflen = strlen(buffer);
  64.            
  65.         // now tack as much of the page onto the body as we have space for...
  66.         FILE * fp = fopen(print->filename, "r");
  67.         if (fp) {
  68.             int len = fread(buffer + buflen, 1, MAX_MAIL_SIZE - buflen - (5 /* slop? */), fp);
  69.             buffer[buflen + len] = '\0';
  70.             fclose(fp);  
  71.             
  72.             char *temp = NET_Escape (buffer, URL_XALPHAS);
  73.             
  74.             XP_FREE(buffer);
  75.             buffer = temp;
  76.             
  77.         } else {
  78.         
  79.             XP_FREE(buffer);
  80.             buffer = NULL;
  81.             
  82.         }
  83.     }
  84.                         
  85.     // get rid of the file and free the memory  
  86.     remove(print->filename);
  87.  
  88.     char *buffer2 = NULL;
  89.     
  90.     if (buffer) {
  91.     
  92.         buffer2 = (char *) malloc(strlen(prefix) + strlen(buffer) + 1);
  93.         
  94.         if (buffer2) {
  95.             strcpy(buffer2, prefix);                // start creating a "mailto:" URL
  96.             strcat(buffer2, buffer);                // the message
  97.         }
  98.     }
  99.     
  100.     if (buffer2 == NULL) {            // no buffer, or we don't have enough memory to use it, try to just send the URL...
  101.         if (buffer)
  102.             XP_FREE(buffer);            // if we're here, we can't use the buffer anyway...
  103.             
  104.         buffer = NET_Escape (print->url->address, URL_XALPHAS);
  105.         if (buffer == NULL)
  106.             return;            // not enough memory to do ANYTHING useful!
  107.             
  108.         buffer2 = (char *) malloc(strlen(prefix) + strlen(buffer) + 1);
  109.         if (buffer2 == NULL) {
  110.             XP_FREE(buffer);
  111.             return;            // not enough memory to do ANYTHING useful!
  112.         }
  113.         
  114.         strcpy(buffer2, prefix);                // start creating a "mailto:" URL
  115.         strcat(buffer2, buffer);                // the message
  116.     }
  117.  
  118.     XP_FREE(buffer);
  119.     
  120.     // XP_FREE(print->filename);
  121.     // print->filename = NULL;
  122.     CURLDispatcher::DispatchURL(buffer2, ExtractNSContext(context));
  123.     XP_FREE(buffer2);
  124. }
  125.  
  126. #ifndef MOZ_MAIL_COMPOSE
  127. extern MSG_Pane* MSG_MailDocument(MWContext *context)
  128. {
  129.     if(!context)
  130.         return NULL;
  131.  
  132.     History_entry * hist_ent = SHIST_GetCurrent(&(context->hist));
  133.         
  134.     // make sure there was a document loaded
  135.     if(!hist_ent)
  136.         return NULL;
  137.  
  138.  
  139.     //Set hist_ent to NULL if context->title is "Message Composition"
  140.     //This is a nasty way of determining if we're in here in response
  141.     //to "Mail Doc" or "New Mail Message".
  142.     //Also, if there's To: field info present(pBar->m_pszTo) then 
  143.     //we know that it's a Mailto: and set hist_ent to NULL 
  144.     //Without this differentiation the code always sends the contents
  145.     //of the previously mailed document even when someone chooses
  146.     //"New Mail Message" or "Mailto:"
  147.    
  148.     if(!strcmp(XP_GetString(MK_MSG_MSG_COMPOSITION), context->title))
  149.         return NULL;
  150.  
  151.     URL_Struct * URL_s = SHIST_CreateURLStructFromHistoryEntry(context, hist_ent);
  152.     if (!URL_s)
  153.         return NULL;
  154.  
  155.     // Zero out the saved data
  156.     memset(&URL_s->savedData, 0, sizeof(URL_s->savedData));
  157.  
  158.     PrintSetup print;                                
  159.  
  160.     XL_InitializeTextSetup(&print);
  161.     print.width = 68; 
  162.     print.prefix = "";
  163.     print.eol = "\r\n";
  164.  
  165.     char * name = WH_TempName(xpTemporary, NULL);
  166.     if(!name) {
  167.         // Leak URL_s here
  168.         return(FALSE);
  169.     }
  170.  
  171.     print.out  = fopen(name, "w");
  172.     print.completion = (XL_CompletionRoutine) FE_DoneMailTo;
  173.     print.carg = context;
  174.     print.filename = name;
  175.     print.url = URL_s;
  176.  
  177.     // leave pCompose window alive until completion routine
  178.     XL_TranslateText(context, URL_s, &print); 
  179.  
  180.     return NULL;
  181. }
  182. #endif // ! MOZ_MAIL_COMPOSE
  183.  
  184. #endif // !MOZ_MAIL_NEWS