aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/addrbook/msoutlook/com/server/Server.cxx
blob: 5adeba38057ccdd6d3745471b8fa2b8c5b57331f (plain)
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
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
#include "../../MAPIBitness.h"
#include "../../MAPISession.h"
#include "../../MsOutlookAddrBookContactSourceService.h"
#include "../../StringUtils.h"
#include "../MsOutlookAddrBookClient.h"
#include "../MsOutlookAddrBookServerClassFactory.h"
#include "../TypeLib.h"
#include "../../MsOutlookUtils.h"
#include "../../MAPINotification.h"

#include <stdio.h>
#include <string.h>
#include <TlHelp32.h>
#include <stdlib.h>

#define MAPI_NO_COINIT 8

void waitParentProcessStop();
static void Server_contactDeleted(LPSTR id);
static void Server_contactInserted(LPSTR id);
static void Server_contactUpdated(LPSTR id);
static void Server_calendarDeleted(LPSTR id);
static void Server_calendarInserted(LPSTR id);
static void Server_calendarUpdated(LPSTR id);
static void Server_deleted(LPSTR id, ULONG type);
static void Server_inserted(LPSTR id, ULONG type);
static void Server_updated(LPSTR id, ULONG type);

/**
 * Starts the COM server.
 */
int main(int argc, char** argv)
{
    HRESULT hr = E_FAIL;


    if(argc > 2)
    {
    	char* path = argv[1];
    	int loggerLevel = 0;
		char* loggerLevelString = argv[2];
		loggerLevel = atoi(loggerLevelString);
    	MsOutlookUtils_createLogger("msoutlookaddrbook_server.log", path,
    			loggerLevel);

    }

    MsOutlookUtils_logInfo(argv[1]);
    MsOutlookUtils_logInfo(argv[2]);
    MsOutlookUtils_logInfo("Starting the Outlook Server.");
    if((hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED)) != S_OK
            && hr != S_FALSE)
    {
    	MsOutlookUtils_logInfo("Error in initialization of the Outlook Server.[1]");
        return hr;
    }
    MAPISession_initLock();


    WCHAR * path = (WCHAR*) L"IMsOutlookAddrBookServer.tlb"; 
    LPTYPELIB typeLib = TypeLib_loadRegTypeLib(path);
    if(typeLib != NULL)
    {

    	MsOutlookUtils_logInfo("TLB initialized.");
        ClassFactory *classObject = new MsOutlookAddrBookServerClassFactory();
        if(classObject != NULL)
        {
        	MsOutlookUtils_logInfo("Server object created.");
            hr = classObject->registerClassObject();
            hr = ::CoResumeClassObjects();

            if(MsOutlookAddrBookContactSourceService_NativeMAPIInitialize(
                            MAPI_INIT_VERSION,
                            MAPI_MULTITHREAD_NOTIFICATIONS | MAPI_NO_COINIT,
                            (void*) Server_contactDeleted,
                            (void*) Server_contactInserted,
                            (void*) Server_contactUpdated)
                        != S_OK)
			{
				MsOutlookUtils_logInfo("Error in native MAPI initialization of the Outlook Server.[2]");
				CoUninitialize();
			}
            else
            {
				MAPINotification_registerCalendarNativeNotificationsDelegate(
						(void*) Server_calendarDeleted,
						(void*) Server_calendarInserted,
						(void*) Server_calendarUpdated);

				MsOutlookUtils_logInfo("Server started.");
				waitParentProcessStop();
            }

            MsOutlookUtils_logInfo("Stop waiting.[3]");
            hr = ::CoSuspendClassObjects();
            hr = classObject->revokeClassObject();

            classObject->Release();
        }
        else
        {
        	MsOutlookUtils_logInfo("Error - server object can't be created.");
        }
        TypeLib_releaseTypeLib(typeLib);
    }
    else
    {
    	MsOutlookUtils_logInfo("Error - TLB isn't initialized.");
    }
    MsOutlookAddrBookContactSourceService_NativeMAPIUninitialize();
    MsOutlookUtils_deleteLogger();
    MAPISession_freeLock();

    CoUninitialize();

    return hr;
}

/**
 * Wait that the parent process stops.
 */
void waitParentProcessStop()
{
	MsOutlookUtils_log("Waits parent process to stop.");
    HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if(handle != INVALID_HANDLE_VALUE)
    {
    	MsOutlookUtils_log("Valid handle is found.");
        PROCESSENTRY32 processEntry;
        memset(&processEntry, 0, sizeof(processEntry));
        processEntry.dwSize = sizeof(PROCESSENTRY32);
        DWORD id = GetCurrentProcessId();
        if(Process32First(handle, &processEntry))
        {
            do
            {
                // We have found this process
                if(processEntry.th32ProcessID == id)
                {
                    // Get the parent process handle.
                    HANDLE parentHandle
                        = OpenProcess(
                                SYNCHRONIZE
                                | PROCESS_QUERY_INFORMATION
                                | PROCESS_VM_READ,
                                FALSE,
                                processEntry.th32ParentProcessID);

                    // Wait for our parent to stop.
                    DWORD exitCode;
                    GetExitCodeProcess(parentHandle, &exitCode);
                    while(exitCode == STILL_ACTIVE)
                    {
                        WaitForSingleObject(parentHandle, INFINITE);
                        GetExitCodeProcess(parentHandle, &exitCode);
                    }
                    MsOutlookUtils_log("Stop waiting.[1]");
                    CloseHandle(parentHandle);
                    return;
                }
            }
            while(Process32Next(handle, &processEntry));
        }
        CloseHandle(handle);
    }
    else
    {
    	MsOutlookUtils_log("Error - not valid handle found.");
    }
    MsOutlookUtils_log("Stop waiting.[2]");
}

/**
 * Invoke the callback function of the COM client when a contact has been
 * deleted from MAPI.
 *
 * @param id The contact identifer.
 */
static void Server_deleted(LPSTR id, ULONG type)
{
    HRESULT hr =  E_FAIL;

    IMsOutlookAddrBookClient * msOutlookAddrBookClient = NULL;
    if((hr = CoCreateInstance(
            CLSID_MsOutlookAddrBookClient,
            NULL,
            CLSCTX_LOCAL_SERVER,
            IID_IMsOutlookAddrBookClient,
            (void**) &msOutlookAddrBookClient)) == S_OK)
    {
        LPWSTR idW = StringUtils::MultiByteToWideChar(id);
        BSTR res = SysAllocString(idW);
        msOutlookAddrBookClient->deleted(res, type);
        SysFreeString(res);
        free(idW);
        msOutlookAddrBookClient->Release();
    }
}

/**
 * Invoke the callback function of the COM client when a contact has been
 * created from MAPI.
 *
 * @param id The contact identifer.
 */
static void Server_inserted(LPSTR id, ULONG type)
{
    HRESULT hr =  E_FAIL;

    IMsOutlookAddrBookClient * msOutlookAddrBookClient = NULL;
    if((hr = CoCreateInstance(
            CLSID_MsOutlookAddrBookClient,
            NULL,
            CLSCTX_LOCAL_SERVER,
            IID_IMsOutlookAddrBookClient,
            (void**) &msOutlookAddrBookClient)) == S_OK)
    {
        LPWSTR idW = StringUtils::MultiByteToWideChar(id);
        BSTR res = SysAllocString(idW);
        msOutlookAddrBookClient->inserted(res, type);
        SysFreeString(res);
        free(idW);
        msOutlookAddrBookClient->Release();
    }
}

/**
 * Invoke the callback function of the COM client when a contact has been
 * modified from MAPI.
 *
 * @param id The contact identifer.
 */
static void Server_updated(LPSTR id, ULONG type)
{
    HRESULT hr =  E_FAIL;

    IMsOutlookAddrBookClient * msOutlookAddrBookClient = NULL;
    if((hr = CoCreateInstance(
            CLSID_MsOutlookAddrBookClient,
            NULL,
            CLSCTX_LOCAL_SERVER,
            IID_IMsOutlookAddrBookClient,
            (void**) &msOutlookAddrBookClient)) == S_OK)
    {
        LPWSTR idW = StringUtils::MultiByteToWideChar(id);
        BSTR res = SysAllocString(idW);
        msOutlookAddrBookClient->updated(res, type);
        SysFreeString(res);
        free(idW);
        msOutlookAddrBookClient->Release();
    }
}

static void Server_contactDeleted(LPSTR id)
{
	Server_deleted(id, CONTACTS_FOLDER_TYPE);
}

static void Server_contactInserted(LPSTR id)
{
	Server_inserted(id, CONTACTS_FOLDER_TYPE);
}

static void Server_contactUpdated(LPSTR id)
{
	Server_updated(id, CONTACTS_FOLDER_TYPE);
}

static void Server_calendarDeleted(LPSTR id)
{
	Server_deleted(id, CALENDAR_FOLDER_TYPE);
}

static void Server_calendarInserted(LPSTR id)
{
	Server_inserted(id, CALENDAR_FOLDER_TYPE);
}

static void Server_calendarUpdated(LPSTR id)
{
	Server_updated(id, CALENDAR_FOLDER_TYPE);
}