This repository has been archived by the owner on Feb 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LarGDSPlugin.cpp
executable file
·302 lines (232 loc) · 8.12 KB
/
LarGDSPlugin.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
// LarGDSPlugin 1.01 by Larry Gadea
// For the latest version of LarGDSPlugin, please visit: http://www.trivex.net
#include "LarGDSPlugin.h"
using namespace std;
LarGDSPlugin::LarGDSPlugin(CLSID ClassID) {
ObjClassID = ClassID;
}
bool LarGDSPlugin::RegisterPlugin(string Title, string Description, string IconPath) {
return RegisterPluginWithExtensions(Title, Description, IconPath, vector <string>());
}
bool LarGDSPlugin::RegisterPluginWithExtensions(string Title, string Description, string IconPath, vector<string> Extensions) {
// Initialize COM (if required)
HRESULT ComInitResult = CoInitialize(NULL);
if (FAILED(ComInitResult) && ComInitResult != RPC_E_CHANGED_MODE) {
return false;
}
// Create connection with GDS
CComPtr<IGoogleDesktopSearchComponentRegistration> spRegistration;
CComPtr<IGoogleDesktopSearchComponentRegister> spRegister;
HRESULT hr;
hr = spRegister.CoCreateInstance(CLSID_GoogleDesktopSearchRegister);
if (SUCCEEDED(hr)) {
ATLASSERT(spRegister != NULL);
// Component description is 6 strings
CComSafeArray<VARIANT> arr_descr(6);
arr_descr.SetAt(0, CComVariant(L"Title"));
arr_descr.SetAt(1, CComVariant(Title.c_str()));
arr_descr.SetAt(2, CComVariant(L"Description"));
arr_descr.SetAt(3, CComVariant(Description.c_str()));
arr_descr.SetAt(4, CComVariant(L"Icon"));
arr_descr.SetAt(5, CComVariant(IconPath.c_str()));
// our CLSID in string format
CComBSTR clsid(ObjClassID);
// Wrap description array in variant
CComVariant descr(arr_descr.m_psa);
// and register
hr = spRegister->RegisterComponent(clsid, descr, &spRegistration);
if (FAILED(hr)) {
return false;
}
ATLASSERT(FAILED(hr) || spRegistration);
for (int CurExtension = 0; (SUCCEEDED(hr) && (CurExtension <= (int)(Extensions.size() - 1))); CurExtension++) {
hr = spRegistration->RegisterExtension(CComBSTR(Extensions[CurExtension].c_str()));
}
// revoke our registration in case any of our extensions fail to register
if (FAILED(hr)) {
// ignore the error - we've already failed
HRESULT ignore;
ignore = spRegister->UnregisterComponent(CComBSTR(clsid));
// Uninitialize COM (if required)
if (SUCCEEDED(ComInitResult)) {
CoUninitialize();
}
return false;
}
}
// Uninitialize COM (if required)
if (SUCCEEDED(ComInitResult)) {
CoUninitialize();
}
return true;
}
bool LarGDSPlugin::UnregisterPlugin() {
// Initialize COM (if required)
HRESULT ComInitResult = CoInitialize(NULL);
if (FAILED(ComInitResult) && ComInitResult != RPC_E_CHANGED_MODE) {
return false;
}
// Connect to GDS
CComPtr<IGoogleDesktopSearchComponentRegister> spRegister;
HRESULT hr;
hr = spRegister.CoCreateInstance(CLSID_GoogleDesktopSearchRegister);
if (SUCCEEDED(hr)) {
// our CLSID in string format
CComBSTR bstrClsid(ObjClassID);
hr = spRegister->UnregisterComponent(bstrClsid);
}
// Uninitialize COM (if required)
if (SUCCEEDED(ComInitResult)) {
CoUninitialize();
}
return true;
}
bool LarGDSPlugin::SendIMEvent(string Content, string UserName, string BuddyName, string Title, unsigned long ConversationID) {
SYSTEMTIME TimeNow;
GetSystemTime(&TimeNow);
return SendIMEvent(Content, UserName, BuddyName, Title, ConversationID, TimeNow, "");
}
bool LarGDSPlugin::SendIMEvent(string Content, string UserName, string BuddyName, string Title, unsigned long ConversationID, SYSTEMTIME MessageTime, string Format) {
// Convert MessageTime
double VarMessageTime;
SystemTimeToVariantTime(&MessageTime, &VarMessageTime);
// If no format was specified, assume it's text/html
if (Format == "") {
Format = "text/html";
}
// Init COM
HRESULT hr_coinit = CoInitialize(NULL);
if (FAILED(hr_coinit) && hr_coinit != RPC_E_CHANGED_MODE) {
return false;
}
// Assemble event
{
CComPtr<IGoogleDesktopSearchEventFactory> spFactory;
HRESULT hr = spFactory.CoCreateInstance(CLSID_GoogleDesktopSearch, NULL, CLSCTX_INPROC);
if (FAILED(hr)) {
return false;
}
CComPtr<IDispatch> spEventDisp;
hr = spFactory->CreateEvent(CComBSTR(ObjClassID), CComBSTR(L"Google.Desktop.IM"), &spEventDisp);
if (FAILED(hr)) {
return false;
}
CComQIPtr<IGoogleDesktopSearchEvent> spEvent(spEventDisp);
ATLASSERT(spEventDisp && spEvent);
if (spEvent == NULL) {
return false;
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"message_time"), CComVariant(VarMessageTime, VT_DATE));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"format"), CComVariant(Format.c_str()));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"content"), CComVariant(Content.c_str()));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"buddy_name"), CComVariant(BuddyName.c_str()));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"title"), CComVariant(Title.c_str()));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"conversation_id"), CComVariant(ConversationID));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"user_name"), CComVariant(UserName.c_str()));
}
if (FAILED(hr)) {
return false;
}
hr = spEvent->Send(EventFlagIndexable);
if (FAILED(hr)) {
return false;
}
}
if (SUCCEEDED(hr_coinit)) {
CoUninitialize();
}
return true;
}
bool LarGDSPlugin::SendTextFileEvent(string Content, string Path, SYSTEMTIME LastModified) {
// Convert date to proper format
double ModifiedTime;
SystemTimeToVariantTime(&LastModified, &ModifiedTime);
// Init COM
HRESULT hr_coinit = CoInitialize(NULL);
if (FAILED(hr_coinit) && hr_coinit != RPC_E_CHANGED_MODE) {
return false;
}
// Assemble event
{
CComPtr<IGoogleDesktopSearchEventFactory> spFactory;
HRESULT hr = spFactory.CoCreateInstance(CLSID_GoogleDesktopSearch, NULL, CLSCTX_INPROC);
if (FAILED(hr)) {
return false;
}
CComPtr<IDispatch> spEventDisp;
hr = spFactory->CreateEvent(CComBSTR(ObjClassID), CComBSTR(L"Google.Desktop.File"), &spEventDisp);
if (FAILED(hr)) {
return false;
}
CComQIPtr<IGoogleDesktopSearchEvent> spEvent(spEventDisp);
ATLASSERT(spEventDisp && spEvent);
if (spEvent == NULL) {
return false;
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"format"), CComVariant("text/plain"));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"content"), CComVariant(Content.c_str()));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"uri"), CComVariant(Path.c_str()));
}
if (SUCCEEDED(hr)) {
hr = spEvent->AddProperty(CComBSTR(L"last_modified_time"), CComVariant(ModifiedTime, VT_DATE));
}
if (FAILED(hr)) {
return false;
}
hr = spEvent->Send(EventFlagIndexable);
if (FAILED(hr)) {
return false;
}
}
// Uninit COM
if (SUCCEEDED(hr_coinit)) {
CoUninitialize();
}
return true;
}
unsigned long LarGDSPlugin::GetConversationID(string Identifier) {
// Default to a timeout of 15 minutes.
return GetConversationID(Identifier, 900);
}
unsigned long LarGDSPlugin::GetConversationID(string Identifier, int Timeout) {
bool CreateNewConversationID = false;
// Check if there are any conversations with OtherPartyName in memory
if (Conversations.find(Identifier) == Conversations.end()) {
// If no, flag for new ConversationID creation
CreateNewConversationID = true;
}
if (CreateNewConversationID == false) {
// Check to see if nothing has been said within the last 15 minutes
if (difftime(time(0), Conversations[Identifier].LastMessageTime) > Timeout) {
// If it has been longer than 15 minutes, flag for new ConversationID creation
CreateNewConversationID = true;
}
}
if (CreateNewConversationID == true) {
// If flagged to create a new ConversationID, randomize it.
srand(static_cast<unsigned>(time(0)));
ConversationStruct Conversation;
Conversation.ConversationID = (unsigned long) rand();
Conversations[Identifier] = Conversation;
}
Conversations[Identifier].LastMessageTime = time(0);
return Conversations[Identifier].ConversationID;
}