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