-
Notifications
You must be signed in to change notification settings - Fork 0
/
PST2ICALApp.cpp
516 lines (452 loc) · 14 KB
/
PST2ICALApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// PST2MBOXApp.cpp: implementation of the CPST2ICALApp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "mapistuff.h"
#include "PSTProc.h"
#include "PST2ICALApp.hpp"
#include "XMLProcSuppl.hpp"
#include "XMLProc.hpp"
#include "XMLCalProc.hpp"
#include "XMLNameValue.hpp"
#include "Calendar.hpp"
#include "CalendarItem.hpp"
#include "Config.hpp"
#include "DirUtils.hpp"
#include "XGetopt.h"
#include "Resources.h"
#include "resource.h"
#include "inistuff.h"
HINSTANCE hInst = 0;
#include <time.h>
//#define EXPIRE30
#if defined EXPIRE30
#define BOMB 1100405
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPST2ICALApp::CPST2ICALApp() :
m_pstProc(new CPSTProc()), m_xmlProc(new CXMLCalProc()), m_verbose(false), m_printFormsOnly(false)
{
}
CPST2ICALApp::~CPST2ICALApp()
{
}
_TCHAR *CPST2ICALApp::PrefixWithCurDir(_TCHAR *fileName, _TCHAR *fileNameFull)
{
if ((_tcschr(fileName, _T(':')) == NULL &&
_tcschr(fileName, _T('\\')) == NULL) ||
(_tcsncmp(fileName, _T(".\\"), 2) == 0))
{
TCHAR szCurrentDir[MAX_PATH+1] = {0};
ULONG cchCurrentDir = 0;
cchCurrentDir = GetCurrentDirectory(MAX_PATH, szCurrentDir);
if (
szCurrentDir[cchCurrentDir - 1] != _T(':') &&
szCurrentDir[cchCurrentDir - 1] != _T('\\'))
{
_tcscat(szCurrentDir, _T("\\"));
}
_tcscpy(fileNameFull, szCurrentDir);
if (_tcsncmp(fileName, _T(".\\"), 2) != 0)
_tcscat(fileNameFull, fileName);
else
_tcscat(fileNameFull, fileName + 2);
}
else
_tcscpy(fileNameFull, fileName);
return fileNameFull;
}
// Process all command line options
int CPST2ICALApp::ProcessCommandLine(int argc, _TCHAR *argv[])
{
HINSTANCE hInst = ::GetModuleHandle(NULL);
_TCHAR prompt[512];
int c;
_TCHAR szInputFile[MAX_PATH+1] = {0};
if (argc == 1) {
ShowUsage();
return MIGRATION_ERR_NO_PARAMETERS;
}
while ((c = getopt(argc, argv, _TEXT("ai:l:o:p:x:v"))) != EOF)
{
switch (c)
{
case _T('a'):
{
m_pstProc->SetAppendLogFile(true);
}
break;
case _T('i'):
{
m_pstProc->SetInputDir(argv[optind-1]);
}
break;
case _T('l'):
{
m_pstProc->SetLogFile(PrefixWithCurDir(argv[optind-1], szInputFile));
}
break;
case _T('o'):
{
m_pstProc->SetOutputDir(argv[optind-1]);
}
break;
case _T('p'):
{
m_pstProc->SetOnlyPSTFile(PrefixWithCurDir(argv[optind-1], szInputFile));
}
break;
case _T('x'):
{
m_xmlProc->SetXmlConfig(PrefixWithCurDir(argv[optind-1], szInputFile));
}
break;
case _T('v'):
{
m_verbose = true;
}
break;
// case _T('!'):
// //TRACE(_TEXT("ERROR: illegal option %s\n"), argv[optind-1]);
// ::LoadString(hInst, IDS_OPT_ERR_MISSEDARG, prompt, 256);
// ::_tprintf(_TEXT("%s %s %d %d\n"), prompt, argv[optind-1], optind, argc);
// ShowUsage();
// return MIGRATION_ERR_MISSEDARG;
// break;
case _T('?'):
//TRACE(_TEXT("ERROR: illegal option %s\n"), argv[optind-1]);
// ::LoadString(hInst, IDS_OPT_ERR_ILLEGAL_OPTION, prompt, 256);
// ::_tprintf(_TEXT("%s %s\n"), prompt, argv[optind-1]);
ShowUsage();
return MIGRATION_ERR_ILLEGAL_OPTION;
break;
default:
{
//TRACE(_TEXT("WARNING: no handler for option %c\n"), c);
::LoadString(hInst, IDS_OPT_ERR_NOHANDLER, prompt, 256);
::_tprintf(_TEXT("%s %s\n"), prompt, argv[optind-1]);
}
return MIGRATION_ERR_NO_OPTION_HANDLER;
break;
}
}
//
// check for non-option args here
//
if (optind != argc) {
::LoadString(hInst, IDS_OPT_ERR_MISSEDARG, prompt, 256);
::_tprintf(_TEXT("%s %s\n"), prompt, argv[1]);
ShowUsage();
return MIGRATION_ERR_MISSEDARG;
}
_TCHAR tempDir[256];
::GetEnvironmentVariable(_T("TEMP"), tempDir, 256);
if (_tcslen(tempDir) == 0) {
::GetEnvironmentVariable(_T("TMP"), tempDir, 256);
}
// _tprintf(_T("before ExtractSchemaFile\n"));
if (ExtractSchemaFile(tempDir) != MIGRATION_STEP_SUCCESS) {
return MIGRATION_ERR_FAILED;
}
if (m_verbose) {
m_xmlProc->SetVerbose(true);
}
// _tprintf(_T("before GetXmlConfig\n"));
const _TCHAR* xmlConfig = m_xmlProc->GetXmlConfig();
if (_tcslen(xmlConfig) == 0) {
_TCHAR xmlFileName[512];
::GetModuleFileName(hInst, xmlFileName, 512);
for(int i = _tcslen(xmlFileName) - 1; i >= 0; i--) {
if (xmlFileName[i] == _T('\\')) {
xmlFileName[i] = _T('\0');
break;
}
}
if (i > 0) {
_tcscat(xmlFileName, _T("\\"));
}
_tcscat(xmlFileName, DEFAULT_CONFIG_FILE);
// _tprintf(_T("xmlFileName: %s\n"), xmlFileName);
m_xmlProc->SetXmlConfig(xmlFileName);
}
// _tprintf(_T("before Parse\n"));
if (m_xmlProc->Parse() != MIGRATION_STEP_SUCCESS) {
// _tprintf(_T("m_xmlProc->Parse() failed!\n"));
::DeleteFile(m_xmlProc->GetSchemaName());
return MIGRATION_ERR_FAILED;
}
::DeleteFile(m_xmlProc->GetSchemaName());
// _tprintf(_T("xmlConfig len: %d\n"), _tcslen(xmlConfig));
if (_tcslen(xmlConfig) != 0) {
const _TCHAR *logDir;
logDir = getConfigSettingValue(XMLCONFIG_LOGDIR);
// _tprintf(_T("logDir: %s\n"), logDir);
if (_tcslen(m_pstProc->GetLogFile()) != 0)
INI_LogInit(m_pstProc->GetLogFile(), true, m_pstProc->GetAppendLogFile());
else if (logDir != NULL) {
if (CDirUtils::MakeDir(logDir) != 0) {
_tprintf(_T("!= 0\n"));
// ::LoadString(hInst, IDS_ERR_DIR_NOT_CREATED, usage, 512);
// ::_tprintf(usage, output_dir, field_textW);
// ???
}
else {
// _tprintf(_T("before INI_LogInit\n"));
unsigned int logSizeWrap = (unsigned int)_ttoi(m_xmlProc->getConfigSettingValue(XMLCONFIG_LOGSIZEWRAP));
if (logSizeWrap > 0)
INI_SetLogSizeWrap(logSizeWrap);
INI_LogInit(logDir, false, m_pstProc->GetAppendLogFile());
}
}
if (_tcslen(m_pstProc->GetFilter()) == 0) {
m_pstProc->SetFilter(m_xmlProc->getConfigSettingValue(XMLCONFIG_FILTER));
}
if (_tcslen(m_pstProc->GetPSTFilePassword()) == 0) {
m_pstProc->SetPSTFilePassword(m_xmlProc->getConfigSettingValue(XMLCONFIG_PSTFILEPWD));
}
if (_tcslen(m_pstProc->GetOutputDir()) == 0) {
m_pstProc->SetOutputDir(m_xmlProc->getConfigSettingValue(XMLCONFIG_OUTPUTDIR));
}
if (_tcslen(m_pstProc->GetInputDir()) == 0) {
_TCHAR *attrValue = (_TCHAR *)m_xmlProc->getConfigSettingValue(XMLCONFIG_INPUTDIR);
_TCHAR *ext = attrValue;
if (_tcslen(attrValue) > _tcslen(DEFAULT_PST_EXT))
ext = attrValue + _tcslen(attrValue) - _tcslen(DEFAULT_PST_EXT);
// check if we have directory or single file
if (_tcslen(attrValue) > _tcslen(DEFAULT_PST_EXT) &&
_tcsicmp(ext, DEFAULT_PST_EXT) == 0)
m_pstProc->SetOnlyPSTFile(attrValue);
else
m_pstProc->SetInputDir(attrValue);
}
if (_tcslen(m_pstProc->GetSmtpAddrFile()) == 0) {
m_pstProc->SetSmtpAddrFile(m_xmlProc->getConfigSettingValue(XMLCONFIG_SMTPADR));
}
if (_tcslen(m_pstProc->GetSmtpPartsSep()) == 0) {
m_pstProc->SetSmtpPartsSep(m_xmlProc->getConfigSettingValue(XMLCONFIG_SMTPPARTSSEP));
}
}
if (m_verbose) {
::LoadString(hInst, IDS_CONFIG_SETTING, prompt, 256);
::_tprintf(prompt, XMLCONFIG_FILTER, m_pstProc->GetFilter());
}
return MIGRATION_PARAMETERS_OK;
}
// Show utility full title and copyright
void CPST2ICALApp::ShowUsage()
{
_TCHAR usage[256];
HINSTANCE hInst = ::GetModuleHandle(NULL);
// Usage: PST2MBOX [OPTION]...
// -f Generate a list of Lotus Notes Form names (optional, see User Guide)
// -i <dir> Input directory for NSF files (optional, supersedes XML file setting)
// -l <logFile> Log file (optional, supersedes XML file setting)
// -n <fileName> NSF file to convert (optional, supersedes XML file setting)
// -o <dir> Output directory for PST files (optional, supersedes XML file setting)
// -x <xmlConfig> XML configuration file (required)
// -v Verbose/debug logging (optional, see User Guide)
::LoadString(hInst, IDS_USAGE, usage, 256);
::_tprintf(_TEXT("%s\n\n"), usage);
// -a Append log file
::LoadString(hInst, IDS_OPTION_LOGFILE_APPEND, usage, 256);
::_tprintf(_TEXT("%s\n"), usage);
// -i <dir> Input directory for NSF files (optional, supersedes XML file setting)
::LoadString(hInst, IDS_OPTION_INPUTDIR, usage, 256);
::_tprintf(_TEXT("%s\n"), usage);
// -l <logFile> Log file (optional, supersedes XML file setting)
::LoadString(hInst, IDS_OPTION_LOGFILE, usage, 256);
::_tprintf(_TEXT("%s\n"), usage);
// -o <dir> Output directory for PST files (optional, supersedes XML file setting)
::LoadString(hInst, IDS_OPTION_OUTPUTDIR, usage, 256);
::_tprintf(_TEXT("%s\n"), usage);
// -p <fileName> PST file to convert (optional, supersedes XML file setting)
::LoadString(hInst, IDS_OPTION_FILENAME, usage, 256);
::_tprintf(_TEXT("%s\n"), usage);
// -x <xmlConfig> XML configuration file (required)
::LoadString(hInst, IDS_OPTION_XMLCONFIG, usage, 256);
::_tprintf(_TEXT("%s\n"), usage);
// -v Verbose/debug logging (optional, see User Guide)
::LoadString(hInst, IDS_OPTION_VERBOSE, usage, 256);
::_tprintf(_TEXT("%s\n"), usage);
}
CCalendar* CPST2ICALApp::getCalendarInfo()
{
return m_xmlProc->getCalendarInfo();
}
CCalendarItem* CPST2ICALApp::GetNextCalendarItem(_TCHAR *partCondition)
{
return m_xmlProc->GetNextCalendarItem(partCondition);
}
CCalendarItem* CPST2ICALApp::GetNextCalendarSMTPRequiredItem()
{
return m_xmlProc->GetNextCalendarSMTPRequiredItem();
}
CEntryTransformer* CPST2ICALApp::getNextEntryTransformer()
{
return m_xmlProc->getNextEntryTransformer();
}
void CPST2ICALApp::resetConversionRuleIndex()
{
m_xmlProc->resetConversionRuleIndex();
}
void CPST2ICALApp::resetEntryListIndex()
{
m_xmlProc->resetEntryListIndex();
}
void CPST2ICALApp::resetEntryTransformerListIndex()
{
m_xmlProc->resetEntryTransformerListIndex();
}
bool CPST2ICALApp::IsSMTPLookupRequired()
{
return m_xmlProc->IsSMTPLookupRequired();
}
void CPST2ICALApp::InitializeCalendarItemList()
{
m_xmlProc->InitializeCalendarItemList();
}
int CPST2ICALApp::GetCurrentEntryIndex()
{
return m_xmlProc->GetCurrentEntryIndex();
}
void CPST2ICALApp::SetCurrentEntryIndex(int currentEntryIndex)
{
m_xmlProc->SetCurrentEntryIndex(currentEntryIndex);
}
CConversionRuleEntry* CPST2ICALApp::getNextConversionRuleEntry()
{
return m_xmlProc->getNextConversionRuleEntry();
}
const _TCHAR* CPST2ICALApp::getConfigSettingValue(const _TCHAR* settingName)
{
return m_xmlProc->getConfigSettingValue(settingName);
}
CXMLNameValue* CPST2ICALApp::getNextNameValue(_TCHAR* selector, const _TCHAR *name_attr, const _TCHAR *value_attr)
{
return m_xmlProc->getNextNameValue(selector, name_attr, value_attr);
}
bool CPST2ICALApp::matchesRegExp(char *regExpPattern, char* matchString)
{
return m_xmlProc->matchesRegExp(regExpPattern, matchString);
}
bool CPST2ICALApp::IsSMTPLookupRequested()
{
return m_xmlProc->IsSMTPLookupRequested();
}
int CPST2ICALApp::ExtractSchemaFile(_TCHAR* tempDir)
{
_TCHAR usage[256];
HINSTANCE hInst = ::GetModuleHandle(NULL);
_TCHAR current_method[] = _TEXT("CPST2ICALApp::ExtractSchemaFile");
if (IsVerbose()){
::LoadString(hInst, IDS_ENTER_METHOD, usage, 256);
::_tprintf(_TEXT("%s %s\n"), usage, current_method);
}
int retCode = MIGRATION_STEP_SUCCESS;
HRSRC hSchema = ::FindResourceEx(hInst, _T("Schema"), MAKEINTRESOURCE(IDR_SCHEMA), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
HGLOBAL hResource;
FILE *f;
_TCHAR fullSchemaName[256];
if (hSchema != NULL) {
unsigned long schemaSize = SizeofResource(hInst, hSchema);
// now get a handle to the resource
if ((hResource = LoadResource(hInst, hSchema)) == NULL) {
retCode = MIGRATION_ERR_FAILED;
return retCode;
}
// finally get and return a pointer to the resource
UCHAR* pResource = ((UCHAR*)LockResource(hResource));
_tcscpy(fullSchemaName, tempDir);
_tcscat(fullSchemaName, _T("\\"));
_tcscat(fullSchemaName, DEFAULT_SCHEMA_FILE);
f = _tfopen(fullSchemaName, _TEXT("w"));
if (f == NULL) {
retCode = MIGRATION_ERR_FAILED;
return retCode;
}
unsigned int itemsWritten = fwrite(pResource, sizeof(UCHAR), schemaSize, f);
if (itemsWritten < schemaSize) {
fclose(f);
retCode = MIGRATION_ERR_FAILED;
return retCode;
}
fclose(f);
m_xmlProc->SetSchemaName(DEFAULT_SCHEMA_PREFIX, fullSchemaName);
}
else {
// _tprintf(_T("hSchema == NULL\n"));
retCode = MIGRATION_ERR_FAILED;
return retCode;
}
if (IsVerbose()){
::LoadString(hInst, IDS_EXIT_METHOD, usage, 256);
::_tprintf(_TEXT("%s %s\n"), usage, current_method);
}
return retCode;
}
int CPST2ICALApp::StartMigration(_TCHAR* exec)
{
_TCHAR usage[256];
HINSTANCE hInst = ::GetModuleHandle(NULL);
_TCHAR current_method[] = _TEXT("CPST2ICALApp::StartMigration");
if (IsVerbose()){
::LoadString(hInst, IDS_ENTER_METHOD, usage, 256);
::_tprintf(_TEXT("%s %s\n"), usage, current_method);
}
m_pstProc->SetVerbose(m_verbose);
m_xmlProc->SetVerbose(m_verbose);
// ::_tprintf(L"GetServer2: %s\n", exec);
int retCode = m_pstProc->Start(exec, this);
if (IsVerbose()){
::LoadString(hInst, IDS_EXIT_METHOD, usage, 256);
::_tprintf(_TEXT("%s %s\n"), usage, current_method);
}
return retCode;
}
#if defined EXPIRE30
void exp() {
time_t timer;
struct tm *today;
int daysleft = 1;
unsigned long currdate;
unsigned long bomb = BOMB;
time(&timer);
today = localtime(&timer);
currdate = (today->tm_year)*10000 + (today->tm_mon+1)*100 + today->tm_mday;
// _tprintf(_T("currdate: %lu, bomb: %lu\n"), currdate, bomb);
if (currdate > bomb)
daysleft = 0;
if (daysleft == 0) {
_tprintf(_T("Expired!\n"));
exit(-1);
}
}
#endif
int wmain(int argc, _TCHAR* argv[])
{
int retCode;
_TCHAR usage[256];
hInst = ::GetModuleHandle(NULL);
#if defined EXPIRE30
exp();
#endif
CPST2ICALApp *theApp = new CPST2ICALApp();
CResources::ShowTitle(IDS_TITLE, IDS_VERSION);
retCode = theApp->ProcessCommandLine(argc, argv);
if (retCode == MIGRATION_PARAMETERS_OK) {
retCode = theApp->StartMigration(argv[0]);
if (retCode == MIGRATION_STEP_SUCCESS) {
retCode = MIGRATION_SUCCESS;
}
}
if (theApp->IsVerbose()){
::LoadString(hInst, IDS_EXIT_CODE, usage, 256);
::_tprintf(_TEXT("%s %d\n"), usage, retCode);
}
INI_LogWriteWithResourcePrefixNum(IDS_LOG_EXIT_CODE, retCode);
INI_LogClose();
theApp->~CPST2ICALApp();
return retCode;
}