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
|
// Copyright © 2010 sss
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "commonheaders.h"
TCHAR* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,const char* szSetting, TCHAR* szDef)
{
DBVARIANT dbv = {DBVT_DELETED};
TCHAR* szRes;
if (DBGetContactSettingTString(hContact, szModule, szSetting, &dbv))
return _tcsdup(szDef);
if(dbv.pszVal)
szRes = _tcsdup(dbv.ptszVal);
DBFreeVariant(&dbv);
return szRes;
}
char* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,const char* szSetting, char* szDef)
{
DBVARIANT dbv = {DBVT_DELETED};
char* szRes;
if (DBGetContactSettingString(hContact, szModule, szSetting, &dbv))
return _strdup(szDef);
if(dbv.pszVal)
szRes = _strdup(dbv.pszVal);
DBFreeVariant(&dbv);
return szRes;
}
char *date()
{
static char d[11];
char *tmp = __DATE__, m[4], mn[3] = "01";
m[0]=tmp[0];
m[1]=tmp[1];
m[2]=tmp[2];
if(strstr(m,"Jan"))
strcpy(mn,"01");
else if(strstr(m,"Feb"))
strcpy(mn,"02");
else if(strstr(m,"Mar"))
strcpy(mn,"03");
else if(strstr(m,"Apr"))
strcpy(mn,"04");
else if(strstr(m,"May"))
strcpy(mn,"05");
else if(strstr(m,"Jun"))
strcpy(mn,"06");
else if(strstr(m,"Jul"))
strcpy(mn,"07");
else if(strstr(m,"Aug"))
strcpy(mn,"08");
else if(strstr(m,"Sep"))
strcpy(mn,"09");
else if(strstr(m,"Oct"))
strcpy(mn,"10");
else if(strstr(m,"Nov"))
strcpy(mn,"11");
else if(strstr(m,"Dec"))
strcpy(mn,"12");
d[0]=tmp[7];
d[1]=tmp[8];
d[2]=tmp[9];
d[3]=tmp[10];
d[4]='.';
d[5]=mn[0];
d[6]=mn[1];
d[7]='.';
if (tmp[4] == ' ')
d[8] = '0';
else
d[8]=tmp[4];
d[9]=tmp[5];
return d;
}
void GetFilePath(TCHAR *WindowTittle, char *szSetting, TCHAR *szExt, TCHAR *szExtDesc)
{
TCHAR str[MAX_PATH+2] = {0}, *tmp;
OPENFILENAME ofn={0};
TCHAR filter[512], *pfilter;
ofn.lStructSize=CDSIZEOF_STRUCT(OPENFILENAME,lpTemplateName);
ofn.Flags=OFN_EXPLORER;
ofn.lpstrTitle=TranslateW(WindowTittle);
_tcscpy(filter,TranslateW(szExtDesc));
pfilter=filter+_tcslen(filter)+1;
_tcscpy(pfilter, szExt);
pfilter[_tcslen(pfilter)+1] = '\0';
pfilter[_tcslen(pfilter)+2] = '\0';
ofn.lpstrFilter=filter;
tmp = UniGetContactSettingUtf(0, szGPGModuleName, szSetting, _T(""));
_tcscpy(str, tmp);
mir_free(tmp);
if(_tcslen(str)< 2)
str[0] = '\0';
ofn.lpstrFile=str;
ofn.nMaxFile=_MAX_PATH;
ofn.nMaxFileTitle=MAX_PATH;
if(!GetOpenFileName(&ofn))
return;
DBWriteContactSettingTString(0, szGPGModuleName, szSetting, str);
}
TCHAR *GetFilePath(TCHAR *WindowTittle, TCHAR *szExt, TCHAR *szExtDesc, bool save_file)
{
TCHAR *str = new TCHAR [MAX_PATH+2];
OPENFILENAME ofn={0};
TCHAR filter[512], *pfilter;
ofn.lStructSize=CDSIZEOF_STRUCT(OPENFILENAME,lpTemplateName);
ofn.Flags=OFN_EXPLORER;
ofn.lpstrTitle=TranslateW(WindowTittle);
_tcscpy(filter,TranslateW(szExtDesc));
pfilter=filter+_tcslen(filter)+1;
_tcscpy(pfilter, szExt);
pfilter[_tcslen(pfilter)+1] = '\0';
pfilter[_tcslen(pfilter)+2] = '\0';
ofn.lpstrFilter=filter;
_tcscpy(str, _T(""));
if(_tcslen(str)< 2)
str[0] = '\0';
ofn.lpstrFile=str;
ofn.nMaxFile=_MAX_PATH;
ofn.nMaxFileTitle=MAX_PATH;
if(!save_file)
{
if(!GetOpenFileName(&ofn))
{
delete [] str;
return NULL;
}
}
else
{
if(!GetSaveFileName(&ofn))
{
delete [] str;
return NULL;
}
}
return str;
}
void GetFolderPath(TCHAR *WindowTittle, char *szSetting)
{
BROWSEINFO pbi = {0};
pbi.lpszTitle = WindowTittle;
pbi.ulFlags = BIF_EDITBOX|BIF_NEWDIALOGSTYLE|BIF_SHAREABLE;
LPITEMIDLIST pidl = SHBrowseForFolder(&pbi);
if (pidl != 0)
{
TCHAR path[MAX_PATH];
if (SHGetPathFromIDList(pidl, path))
{
DBWriteContactSettingTString(NULL, szGPGModuleName, "szHomePath", path);
}
IMalloc * imalloc = 0;
if (SUCCEEDED(SHGetMalloc(&imalloc)))
{
imalloc->Free(pidl);
imalloc->Release();
}
}
}
int LoadKey(WPARAM w, LPARAM l)
{
void ShowLoadPublicKeyDialog();
extern map<int, HANDLE> user_data;
extern int item_num;
item_num = 0; //black magic here
user_data[1] = (HANDLE)w;
ShowLoadPublicKeyDialog();
return 0;
}
int SendKey(WPARAM w, LPARAM l)
{
HANDLE hContact = (HANDLE)w;
char *szMessage = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
if(strlen(szMessage) > 1)
{
BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)szMessage);
HistoryLog(hContact, "Public key sent", EVENTTYPE_MESSAGE, DBEF_SENT);
DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc);
}
mir_free(szMessage);
return 0;
}
extern HANDLE hLoadPublicKey, hToggleEncryption;
int ToggleEncryption(WPARAM w, LPARAM l)
{
HANDLE hContact = (HANDLE)w;
BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc?0:1);
enc = enc?0:1;
CLISTMENUITEM mi = {0};
mi.cbSize=sizeof(mi);
mi.flags = CMIM_NAME;
enc?mi.pszName="Turn off GPG encryption":mi.pszName="Turn on GPG encryption";
CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hToggleEncryption, (LPARAM)&mi);
return 0;
}
int OnPreBuildContactMenu(WPARAM w, LPARAM l)
{
HANDLE hContact = (HANDLE)w;
CLISTMENUITEM mi = {0};
mi.cbSize=sizeof(mi);
mi.flags = CMIM_NAME;
mi.pszName = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0)?"Turn off GPG encryption":"Turn on GPG encryption";
CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hToggleEncryption, (LPARAM)&mi);
return 0;
}
void storeOutput(HANDLE ahandle, string *output)
{
BOOL success;
char readbuffer[4096] = {0};
unsigned long transfered, available;
do {
PeekNamedPipe(ahandle,NULL,0,NULL,&available,NULL);
if (!available)
continue;
success=ReadFile(ahandle,readbuffer,sizeof(readbuffer),&transfered,NULL);
if (success && transfered)
output->append(readbuffer, 4096);
} while (available>0);
}
void cp866_to_cp1251(string *str)
{
string::iterator p;
for(p = str->begin(); p != str->end(); p++)
{
if(*p > -129 && *p < -80)
*p += 64;
else if(*p > -33 && *p < -16)
*p += 16;
else if(*p == -16)
*p -= 72;
else if(*p == -17)
*p -= 57;
}
}
void HistoryLog(HANDLE hContact, char *data, int event_type, int flags)
{
PBYTE pCurBlob;
WORD wTextLen;
char szText[MAX_PATH];
DBEVENTINFO Event = {0};
Event.cbSize = sizeof(Event);
Event.szModule = szGPGModuleName;
Event.eventType = event_type;
Event.flags = flags, DBEF_UTF;
Event.timestamp = (DWORD)time(NULL);
Event.cbBlob = strlen(data)+1;
Event.pBlob = (PBYTE)_strdup(data);
CallService(MS_DB_EVENT_ADD, (WPARAM)(HANDLE)hContact,(LPARAM)&Event);
}
static int ControlAddStringUtf(HWND ctrl, DWORD msg, const TCHAR *szString)
{
int item = -1;
item = SendMessage(ctrl, msg, 0, (LPARAM)szString);
return item;
}
int ComboBoxAddStringUtf(HWND hCombo, const TCHAR *szString, DWORD data)
{
int item = ControlAddStringUtf(hCombo, CB_ADDSTRING, szString);
SendMessage(hCombo, CB_SETITEMDATA, item, data);
return item;
}
int GetJabberInterface(WPARAM w, LPARAM l) //get interface for all jabber accounts, options later
{
extern list <JabberAccount*> Accounts;
void AddHandlers();
int count = 0;
PROTOACCOUNT **accounts;
ProtoEnumAccounts(&count, &accounts);
list <JabberAccount*>::iterator p;
Accounts.clear();
Accounts.push_back(new JabberAccount);
p = Accounts.begin();
(*p)->setAccountNumber(0);
for(int i = 0; i < count; i++) //get only jabber accounts from all accounts
{
IJabberInterface *JIftmp = getJabberApi(accounts[i]->szModuleName);
int a = 0;
if(JIftmp)
{
(*p)->setJabberInterface(JIftmp);
(*p)->setAccountName(accounts[i]->tszAccountName);
(*p)->setAccountNumber(a);
(*p)->setSendHandler(INVALID_HANDLE_VALUE);
a++;
p++;
Accounts.push_back(new JabberAccount);
}
}
Accounts.pop_back();
AddHandlers();
return 0;
}
static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pUserData)
{
return FALSE;
}
void AddHandlers()
{
extern list <JabberAccount*> Accounts;
list <JabberAccount*>::iterator p = Accounts.begin();
for(unsigned int i = 0; i < Accounts.size(); i++, p++)
{
if(!(*p))
break;
if((*p)->getSendHandler() == INVALID_HANDLE_VALUE)
{
(*p)->setSendHandler((*p)->getJabberInterface()->Net()->AddSendHandler((JABBER_HANDLER_FUNC)SendHandler));
}
}
}
|