1 /************************************************************************/
2 /*									*/
3 /*  Mail an RTF document.						*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"docRtfConfig.h"
8 
9 #   include	<string.h>
10 #   include	<stdio.h>
11 #   include	<ctype.h>
12 
13 #   include	<appDebugon.h>
14 
15 #   include	<sioGeneral.h>
16 #   include	<sioBase64.h>
17 #   include	"docRtfReadWrite.h"
18 #   include	"docPlainReadWrite.h"
19 #   include	<docTreeNode.h>
20 
docWriteRtfMail(SimpleOutputStream * sos,const char * mimeBoundary,BufferDocument * bd)21 int docWriteRtfMail(		SimpleOutputStream *		sos,
22 				const char *			mimeBoundary,
23 				BufferDocument *		bd )
24     {
25     SimpleOutputStream *	sosOpened= (SimpleOutputStream *)0;
26     SimpleOutputStream *	sosBody= (SimpleOutputStream *)0;
27     int				rval= 0;
28 
29     const DocumentProperties *	dp= &(bd->bdProperties);
30 
31     const char *		file= utilMemoryBufferGetString( &(dp->dpFilename) );
32 
33     if  ( file )
34 	{
35 	const char *		relative;
36 
37 	relative= strrchr( file, '/' );
38 	if  ( relative )
39 	    { file= relative+ 1;	}
40 	}
41 
42     if  ( ! file || ! file[0] )
43 	{ file= "file.rtf";	}
44 
45     sosBody= sos;
46 
47     if  ( mimeBoundary )
48 	{
49 	sioOutPutString( "--", sos );
50 	sioOutPutString( mimeBoundary, sos );
51 	sioOutPutString( "\r\n", sos );
52 	sioOutPutString( "Content-Type: text/plain", sos );
53 	sioOutPutString( "\r\n", sos );
54 	sioOutPutString( "Content-Transfer-Encoding: 8bit", sos );
55 	sioOutPutString( "\r\n", sos );
56 
57 	sioOutPutString( "\r\n", sos );
58 
59 	if  ( ! utilMemoryBufferIsEmpty( &(dp->dpTitle) )		&&
60 	      strcmp( mimeBoundary, utilMemoryBufferGetString( &(dp->dpTitle) ) )	)
61 	    {
62 	    sioOutPutString( utilMemoryBufferGetString( &(dp->dpTitle) ), sos );
63 	    sioOutPutString( "\r\n", sos );
64 	    }
65 	else{
66 	    DocumentPosition	dpFirst;
67 	    DocumentSelection	ds;
68 
69 	    if  ( docDocumentHead( &dpFirst, bd ) )
70 		{ sioOutPutString( "application/rtf\r\n", sos );	}
71 	    else{
72 		const int		fold= 1;
73 		const int		direction= 1;
74 
75 		struct BufferItem *	bi= dpFirst.dpNode;
76 
77 		docSetParaSelection( &ds, bi, direction,
78 						0, docParaStrlen( bi ) );
79 
80 		if  ( docPlainSaveDocument( sos, bd, &ds, fold ) )
81 		    { rval= -1; goto ready;	}
82 
83 		goto ready;
84 		}
85 	    }
86 
87 	sioOutPutString( "--", sos );
88 	sioOutPutString( mimeBoundary, sos );
89 	sioOutPutString( "\r\n", sos );
90 
91 	sioOutPutString( "Content-Type: ", sos );
92 	if  ( file && file[0] )
93 	    {
94 	    sioOutPutString( "application/rtf; name=\"", sos );
95 	    sioOutPutString( file, sos );
96 	    sioOutPutString( "\"", sos );
97 	    }
98 	else{
99 	    sioOutPutString( "application/rtf", sos );
100 	    }
101 	sioOutPutString( "\r\n", sos );
102 
103 	sioOutPutString( "Content-Transfer-Encoding: ", sos );
104 	sioOutPutString( "base64", sos );
105 	sioOutPutString( "\r\n", sos );
106 
107 	sioOutPutString( "\r\n", sos );
108 
109 	sosOpened= sioOutBase64Open( sos );
110 	if  ( ! sosOpened )
111 	    { XDEB(sosOpened); rval= -1; goto ready;	}
112 	sosBody= sosOpened;
113 	}
114 
115     rval= docRtfSaveDocument( sosBody, bd, (const DocumentSelection *)0, 0 );
116 
117     if  ( mimeBoundary )
118 	{
119 	sioOutClose( sosOpened ); sosOpened= (SimpleOutputStream *)0;
120 
121 	sioOutPutString( "--", sos );
122 	sioOutPutString( mimeBoundary, sos );
123 	sioOutPutString( "--", sos );
124 	sioOutPutString( "\r\n", sos );
125 	}
126 
127   ready:
128 
129     if  ( sosOpened )
130 	{ sioOutClose( sosOpened );	}
131 
132     return rval;
133     }
134 
135