xref: /trunk/main/shell/source/unix/misc/senddoc.c (revision badc9ebb)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <process.h>
29 #include <time.h>
30 
31 #define INCL_DOS
32 #define INCL_DOSERRORS
33 #define INCL_PM
34 #include <os2.h>
35 
36 // OOo uses popen() to start us, so we cannot show PM dialogs.
37 // log message to disk.
logMessage(char * msg)38 void logMessage( char* msg)
39 {
40     PPIB	pib;
41     CHAR	szApplicationName[_MAX_PATH];
42     CHAR	szDrive[_MAX_PATH];
43     CHAR	szDir[_MAX_PATH];
44     CHAR	szFileName[_MAX_PATH];
45     CHAR	szExt[_MAX_PATH];
46     FILE*	log;
47     time_t	timeOfDay;
48     struct tm* localTime;
49 
50     // get executable fullpath
51     DosGetInfoBlocks(NULL, &pib);
52     DosQueryModuleName(pib->pib_hmte, sizeof(szApplicationName), szApplicationName);
53     _splitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
54     // log name
55     _makepath( szApplicationName, szDrive, szDir, szFileName, (".LOG") );
56     log = fopen( szApplicationName, "a");
57     if (!log)
58 	return;
59     time( &timeOfDay);
60     localTime = localtime( &timeOfDay);
61     fprintf( log, "%04d/%02d/%02d %02d:%02d:%02d %s\n",
62 	localTime->tm_year+1900, localTime->tm_mon+1, localTime->tm_mday,
63 	localTime->tm_hour, localTime->tm_min, localTime->tm_sec, msg);
64     fclose( log);
65 }
66 
67 // dump comand line arguments
dumpArgs(int argc,char * argv[])68 void dumpArgs( int argc, char *argv[] )
69 {
70     int	i;
71 
72     logMessage( "Start of command line arguments dump:");
73     for( i=0; i<argc; i++)
74 	logMessage( argv[i]);
75 }
76 
77 /*
78  * The intended use of this tool is to pass the argument to
79  * the default mail handler.
80  */
main(int argc,char * argv[])81 int main(int argc, char *argv[] )
82 {
83     APIRET	rc;
84     RESULTCODES result = {0};
85     char 		szAppFromINI[_MAX_PATH];
86     char 		szDirFromINI[_MAX_PATH];
87     char 		szCmdLine[1024];
88     char     	szFail[ _MAX_PATH];
89     ULONG 	ulSID;
90     PID 		pid;
91     int		i;
92     BOOL		bMailClient = FALSE;
93 
94     // check parameters
95     if (argc < 5)
96     {
97 	logMessage( "Usage: senddoc --mailclient <client> --attach <uri>");
98 	dumpArgs( argc, argv);
99         return -1;
100     }
101 
102     // check configuration
103     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
104                           "DefaultMailExe", "",
105                           szAppFromINI, sizeof(szAppFromINI));
106     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
107                           "DefaultMailWorkingDir", "",
108                           szDirFromINI, sizeof(szDirFromINI));
109     if (*szAppFromINI == 0 || *szDirFromINI == 0)
110     {
111 	logMessage( "Unable to find default mail handler in USER.INI; exiting.");
112 	dumpArgs( argc, argv);
113         return -1;
114     }
115 
116     // get default parameter list, at leat -compose is required
117     rc = PrfQueryProfileString(HINI_USER, "WPURLDEFAULTSETTINGS",
118                           "DefaultMailParameters", "",
119                           szCmdLine, sizeof(szCmdLine));
120     if (strstr( szCmdLine, "-compose") == 0)
121 	strcat( szCmdLine, " -compose"); // add if missing!
122 
123     // parse cmdline arguments
124     for( i=1; i<argc; i++)
125     {
126 	if (!strcmp( argv[i], "--mailclient")) {
127 	    // we support only Thunderbird/Mozilla command line options, check exe name
128 	    if (strstr( argv[i+1], "thunderbird") == 0
129 		 && strstr( argv[i+1], "mozilla") == 0
130 		 && strstr( argv[i+1], "seamonkey") == 0)
131 	    {
132 		logMessage( "Only Thunderbird/Mozilla is currently supported. Exiting.");
133 		dumpArgs( argc, argv);
134 		return -1;
135 	    }
136 	    // mail client found
137 	    bMailClient = TRUE;
138 	    i++;
139 	} else if (!strcmp( argv[i], "--attach")) {
140 	    strcat( szCmdLine, " attachment=file://");
141 	    strcat( szCmdLine, argv[i+1]);
142 	    i++;
143 	}
144 	// ignore other options (BTW currently none)
145     }
146     if (bMailClient == FALSE)
147     {
148 	logMessage( "No mail client specified. Exiting.");
149 	dumpArgs( argc, argv);
150 	return -1;
151     }
152 
153     // change default directory
154     _chdir( szDirFromINI);
155 
156     // start default handler
157     STARTDATA   SData;
158     CHAR        szObjBuf[CCHMAXPATH];
159 
160     SData.Length  = sizeof(STARTDATA);
161     SData.Related = SSF_RELATED_INDEPENDENT;
162     SData.FgBg    = (1) ? SSF_FGBG_FORE : SSF_FGBG_BACK;
163     SData.TraceOpt = SSF_TRACEOPT_NONE;
164 
165     SData.PgmTitle = (PSZ)szAppFromINI;
166 
167     SData.PgmName = (PSZ)szAppFromINI;
168     SData.PgmInputs = (PSZ)szCmdLine;
169 
170     SData.TermQ = NULL;
171     SData.Environment = 0;
172     SData.InheritOpt = SSF_INHERTOPT_PARENT;
173     SData.SessionType = SSF_TYPE_PM;
174     SData.IconFile = 0;
175     SData.PgmHandle = 0;
176 
177     SData.PgmControl = SSF_CONTROL_VISIBLE;
178 
179     SData.InitXPos  = 30;
180     SData.InitYPos  = 40;
181     SData.InitXSize = 200;
182     SData.InitYSize = 140;
183     SData.Reserved = 0;
184     SData.ObjectBuffer  = szFail;
185     SData.ObjectBuffLen = (ULONG)sizeof(szFail);
186 
187     rc = DosStartSession( &SData, &ulSID, &pid);
188     // show error dialog in case of problems
189     if (rc != NO_ERROR && rc != ERROR_SMG_START_IN_BACKGROUND) {
190 	    char     szMessage[ _MAX_PATH*2];
191 	    sprintf( szMessage, "Execution failed! rc: %d, failing module:%s", rc, szFail);
192 	    logMessage( szMessage);
193 	    dumpArgs( argc, argv);
194 	    return -1;
195     }
196 
197     // ok
198     return 0;
199 }
200 
201