home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmisc / mime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.1 KB  |  210 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. #include "xp.h"
  21. #include "mime.h"
  22. #include "mkutils.h"
  23. /* there used to be problems including msgcom.h with mime.h having to do with
  24.     differing prototypes for some of the msg_linebuffer calls, but that
  25.     seems to be fixed.And we need msgcom.h for win16
  26. */
  27. #include "msgcom.h"
  28. #include "libi18n.h"
  29.  
  30. /* for XP_GetString() */
  31. #include "xpgetstr.h"
  32. extern int MK_MSG_NO_RETURN_ADDRESS;
  33. extern int MK_MSG_NO_RETURN_ADDRESS_AT;
  34. extern int MK_MSG_NO_RETURN_ADDRESS_DOT;
  35. extern int MK_OUT_OF_MEMORY;
  36. extern int MK_SIGNATURE_TOO_LONG;
  37. extern int MK_SIGNATURE_TOO_WIDE;
  38.  
  39. const int16 default_csid = 0;
  40. const int    address_max_length = 42;
  41.  
  42. #define mime_enc_7bit 0
  43. #define mime_enc_8bit 1
  44. #define mime_enc_qp   2
  45. #define mime_enc_b64  3
  46.  
  47. /* build a mailto: url address given a to field
  48.  *
  49.  * returns a malloc'd string
  50.  */
  51. PUBLIC char *
  52. MIME_BuildMailtoURLAddress(const char * to)
  53. {
  54.     char * rv=0;
  55.  
  56.     StrAllocCopy(rv,"mailto:");
  57.     StrAllocCat(rv, to);
  58.  
  59.     return(rv);
  60. }
  61.  
  62.  
  63. /* build a news: url address given a partial news post
  64.  * URL and the newsgroups line
  65.  *
  66.  * returns a malloc'd string
  67.  */
  68. PUBLIC char *
  69. MIME_BuildNewspostURLAddress(const char *partial_newspost_url,
  70.                              const char *newsgroups)
  71. {
  72.     char * rv=0;
  73.  
  74.     if (!partial_newspost_url)
  75.       partial_newspost_url = "news:";
  76.     StrAllocCopy(rv, partial_newspost_url);
  77.     StrAllocCat(rv, newsgroups);
  78.  
  79.     return(rv);
  80. }
  81.  
  82.  
  83. /*
  84.     Returns the appropriate contents of a From: field of a mail message
  85.     originating from the current user.  This calls FE_UsersFullName()
  86.     and FE_UsersMailAddress() and correctly munges the values.
  87.     
  88.     A new string is returned, which you must free when you're done with it.
  89.  
  90.     An email address must be provided but a user name is optional.  If no
  91.     address has been entered or other errors are encountered return NULL.
  92. */
  93. PUBLIC char *
  94. MIME_MakeFromField (void)
  95. {
  96.     return MSG_MakeFullAddress(FE_UsersFullName(), FE_UsersMailAddress());
  97. }
  98.  
  99. /* Ok, this doesn't really belong here... */
  100.  
  101. void
  102. MISC_ValidateSignature (MWContext *context, const char *signature)
  103. {
  104.   int max_columns = 0;
  105.   int column = 0;
  106.   int newlines = 0;
  107.   const char *sig = signature;
  108.  
  109.   /* ensure that sig is valid */
  110.   if (!sig) return;
  111.  
  112.   for (; *sig; sig++)
  113.     {
  114.       if (*sig == '\n' || *sig == '\r')
  115.         {
  116.           /* Treat CR, LF, CRLF, and LFCR as a single line-break. */
  117.           if ((sig[0] == '\n' && sig[1] == '\r') ||
  118.               (sig[0] == '\r' && sig[1] == '\n'))
  119.             sig++;
  120.  
  121.           if (column > max_columns)
  122.             max_columns = column;
  123.           newlines++;
  124.           column = 0;
  125.         }
  126.       else
  127.         {
  128.           column++;
  129.         }
  130.     }
  131.   /* If the last line doesn't end in a newline, pretend it does. */
  132.   if (column != 0)
  133.     newlines++;
  134.   if (column > max_columns)
  135.     max_columns = column;
  136.  
  137.   /* If the signature begins with "--" followed by whitespace or a newline,
  138.      that means that the pseudo-standard sig delimiter "-- \n" is actually
  139.      in the file, so don't count that as a "line". */
  140.   if (signature [0] == '-' &&
  141.       signature [1] == '-' &&
  142.       (signature [2] == ' ' ||
  143.        (signature [2] == '\012' ||
  144.         signature [2] == '\015')))
  145.     newlines--;
  146.  
  147.   if (newlines > 4)
  148.     {
  149.       FE_Alert (context, XP_GetString (MK_SIGNATURE_TOO_LONG));
  150.     }
  151.   else if (max_columns > 79)
  152.     {
  153.       FE_Alert (context, XP_GetString (MK_SIGNATURE_TOO_WIDE));
  154.     }
  155. }
  156.  
  157. int
  158. MISC_ValidateReturnAddress (MWContext *context, const char *addr)
  159. {
  160.   char *at;
  161.   char *dot;
  162.   char *fmt = 0;
  163.  
  164. #if defined(XP_WIN) || defined(XP_OS2)
  165.   if(FE_IsAltMailUsed(context)) 
  166.       return 0; 
  167. #endif
  168.  
  169.   if (addr)
  170.     while (XP_IS_SPACE (*addr))
  171.       addr++;
  172.  
  173.   if (!addr || !*addr)
  174.     {
  175.       FE_Alert (context, XP_GetString (MK_MSG_NO_RETURN_ADDRESS));
  176. #ifdef XP_MAC
  177.       FE_EditPreference(PREF_EmailAddress);
  178. #endif
  179.       return -1;
  180.     }
  181.  
  182.   at = XP_STRRCHR (addr, '@');
  183.   if (!at)
  184.     {
  185.       fmt = XP_GetString (MK_MSG_NO_RETURN_ADDRESS_AT);
  186.       goto FAIL;
  187.     }
  188.   dot = XP_STRCHR (at, '.');
  189.   if (!dot)
  190.     {
  191.       fmt = XP_GetString (MK_MSG_NO_RETURN_ADDRESS_DOT);
  192.       goto FAIL;
  193.     }
  194.  
  195.   return 0;
  196.  
  197. FAIL:
  198.   {
  199.     char *buf = (char *)XP_ALLOC (XP_STRLEN (addr) + XP_STRLEN (fmt) + 20);
  200.     if (!buf) return MK_OUT_OF_MEMORY;
  201.     
  202.     if ( ( XP_STRLEN (addr) > address_max_length ) && ( context != NULL ) )
  203.          INTL_MidTruncateString( default_csid, addr, addr, address_max_length );
  204.          
  205.     XP_SPRINTF (buf, fmt, addr);
  206.     FE_Alert (context, buf);
  207.     return -1;
  208.   }
  209. }
  210.