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
|
#include "skype_proto.h"
#include <m_chat.h>
#include <m_message.h>
#include <m_history.h>
bool CSkypeProto::IsChatRoom(HANDLE hContact)
{
return ::DBGetContactSettingByte(hContact, this->m_szModuleName, "ChatRoom", 0) > 0;
}
HANDLE CSkypeProto::GetChatRoomByID(const char *cid)
{
HANDLE hContact = ::db_find_first();
while (hContact)
{
if (this->IsProtoContact(hContact) && this->IsChatRoom(hContact))
{
char *chatID = ::DBGetString(hContact, this->m_szModuleName, "ChatRoomID");
if (chatID && ::strcmp(cid, chatID) == 0)
return hContact;
}
hContact = ::db_find_next(hContact);
}
return 0;
}
HANDLE CSkypeProto::AddChatRoomByID(const char* cid, const char* name, DWORD flags)
{
HANDLE hContact = this->GetChatRoomByID(cid);
if ( !hContact)
{
hContact = (HANDLE)::CallService(MS_DB_CONTACT_ADD, 0, 0);
::CallService(MS_PROTO_ADDTOCONTACT, (WPARAM)hContact, (LPARAM)this->m_szModuleName);
::DBWriteContactSettingByte(hContact, this->m_szModuleName, "ChatRoom", 1);
::DBWriteContactSettingString(hContact, this->m_szModuleName, "ChatRoomID", cid);
::DBWriteContactSettingString(hContact, this->m_szModuleName, "Nick", name);
::DBWriteContactSettingWord(hContact, this->m_szModuleName, "Status", ID_STATUS_OFFLINE);
::DBWriteContactSettingWord(hContact, this->m_szModuleName, "ApparentMode", ID_STATUS_OFFLINE);
wchar_t *defaultGroup = ::DBGetStringW(NULL, "Chat", "AddToGroup");
if (defaultGroup)
{
::DBWriteContactSettingWString(hContact, "CList", "Group", defaultGroup);
}
}
return hContact;
}
void CSkypeProto::ChatValidateContact(HANDLE hItem, HWND hwndList, const char *contacts)
{
if ( !this->IsProtoContact(hItem) || this->IsChatRoom(hItem))
{
/*HANDLE hContact = (HANDLE)::SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXT, (LPARAM)hItem);
char *sid = ::DBGetString(hContact, this->m_szModuleName, "sid");
if (!sid)
::SendMessage(hwndList, CLM_DELETEITEM, (WPARAM)hItem, 0);
else if(contacts && ::strstr(contacts, sid))*/
::SendMessage(hwndList, CLM_DELETEITEM, (WPARAM)hItem, 0);
}
}
void CSkypeProto::ChatPrepare(HANDLE hItem, HWND hwndList, const char *contacts)
{
if (hItem == NULL)
hItem = (HANDLE)::SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_ROOT, 0);
while (hItem)
{
HANDLE hItemN = (HANDLE)::SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXT, (LPARAM)hItem);
if (IsHContactGroup(hItem))
{
HANDLE hItemT = (HANDLE)::SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_CHILD, (LPARAM)hItem);
if (hItemT) this->ChatPrepare(hItemT, hwndList, contacts);
}
else if (IsHContactContact(hItem))
this->ChatValidateContact(hItem, hwndList, contacts);
hItem = hItemN;
}
}
void CSkypeProto::GetInviteContacts(HANDLE hItem, HWND hwndList, SEStringList &chatTargets)
{
if (hItem == NULL)
hItem = (HANDLE)::SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_ROOT, 0);
while (hItem)
{
if (IsHContactGroup(hItem))
{
HANDLE hItemT = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_CHILD, (LPARAM)hItem);
if (hItemT) this->GetInviteContacts(hItemT, hwndList, chatTargets);
}
else
{
int chk = SendMessage(hwndList, CLM_GETCHECKMARK, (WPARAM)hItem, 0);
if (chk)
{
if (IsHContactInfo(hItem))
{
TCHAR buf[128] = _T("");
SendMessage(hwndList, CLM_GETITEMTEXT, (WPARAM)hItem, (LPARAM)buf);
if (buf[0]) chatTargets.append(mir_t2a(buf));
}
else
{
char *sid = ::DBGetString(hItem, this->m_szModuleName, "sid");
if (sid) chatTargets.append(sid);
}
}
}
hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXT, (LPARAM)hItem);
}
}
static const COLORREF crCols[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
void CSkypeProto::InitChat()
{
GCREGISTER gcr = {0};
gcr.cbSize = sizeof(gcr);
gcr.dwFlags = GC_TYPNOTIF | GC_CHANMGR;
gcr.iMaxText = 0;
gcr.nColors = 16;
gcr.pColors = (COLORREF*)crCols;
gcr.pszModuleDispName = ::mir_u2a(this->m_tszUserName);
gcr.pszModule = this->m_szModuleName;
CallServiceSync(MS_GC_REGISTER, 0, (LPARAM)&gcr);
this->HookEvent(ME_GC_EVENT, &CSkypeProto::OnGCEventHook);
this->HookEvent(ME_GC_BUILDMENU, &CSkypeProto::OnGCMenuHook);
}
char *CSkypeProto::StartChat(const char *cid, const SEStringList &invitedContacts)
{
char *chatID;
SEString data;
CConversation::Ref conversation;
if (cid)
{
g_skype->GetConversationByIdentity(cid, conversation);
conversation->GetJoinBlob(data);
g_skype->GetConversationByBlob(data, conversation, false);
conversation->Join();
chatID = ::mir_strdup(cid);
}
else
{
g_skype->CreateConference(conversation);
conversation->SetOption(CConversation::P_OPT_JOINING_ENABLED, true);
conversation->SetOption(CConversation::P_OPT_ENTRY_LEVEL_RANK, CParticipant::WRITER);
conversation->SetOption(CConversation::P_OPT_DISCLOSE_HISTORY, 1);
conversation->GetPropIdentity(data);
chatID = ::mir_strdup(data);
}
conversation->AddConsumers(invitedContacts);
conversation->GetPropDisplayname(data);
char *chatName;
if (data.length())
chatName = ::mir_utf8decodeA(data);
else
chatName = Translate("New conference");
GCSESSION gcw = {0};
gcw.cbSize = sizeof(gcw);
gcw.iType = GCW_CHATROOM;
gcw.pszModule = this->m_szModuleName;
gcw.pszName = chatName;
gcw.pszID = chatID;
::CallServiceSync(MS_GC_NEWSESSION, 0, (LPARAM)&gcw);
GCDEST gcd = { m_szModuleName, { NULL }, GC_EVENT_ADDGROUP };
gcd.pszID = chatID;
GCEVENT gce = {0};
gce.cbSize = sizeof(GCEVENT);
gce.pDest = &gcd;
gce.pszStatus = ::Translate("Me");
::CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
gcd.iType = GC_EVENT_ADDGROUP;
gce.pszStatus = ::Translate("Others");
::CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
gcd.iType = GC_EVENT_CONTROL;
::CallServiceSync(MS_GC_EVENT, SESSION_INITDONE, (LPARAM)&gce);
::CallServiceSync(MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce);
::CallServiceSync(MS_GC_EVENT, WINDOW_VISIBLE, (LPARAM)&gce);
::mir_free(chatName);
return chatID;
}
void CSkypeProto::JoinToChat(const char *cid, bool showWindow)
{
char *chatID = ::mir_strdup(cid);
SEString data;
CConversation::Ref conversation;
g_skype->GetConversationByIdentity(cid, conversation);
conversation->GetJoinBlob(data);
g_skype->GetConversationByBlob(data, conversation, false);
conversation->Join();
conversation->GetPropDisplayname(data);
char *chatName = ::mir_utf8decodeA((const char *)data);
GCSESSION gcw = {0};
gcw.cbSize = sizeof(gcw);
gcw.iType = GCW_CHATROOM;
gcw.pszModule = this->m_szModuleName;
gcw.pszName = chatName;
gcw.pszID = chatID;
::CallServiceSync(MS_GC_NEWSESSION, 0, (LPARAM)&gcw);
GCDEST gcd = { m_szModuleName, { NULL }, GC_EVENT_ADDGROUP };
gcd.pszID = chatID;
GCEVENT gce = {0};
gce.cbSize = sizeof(GCEVENT);
gce.pDest = &gcd;
gce.pszStatus = ::Translate("Me");
::CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
gcd.iType = GC_EVENT_ADDGROUP;
gce.pszStatus = ::Translate("Others");
::CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
gcd.iType = GC_EVENT_CONTROL;
::CallServiceSync(MS_GC_EVENT, showWindow ? SESSION_INITDONE : WINDOW_HIDDEN, (LPARAM)&gce);
::CallServiceSync(MS_GC_EVENT, SESSION_ONLINE, (LPARAM)&gce);
//::CallServiceSync(MS_GC_EVENT, showWindow ? WINDOW_VISIBLE : WINDOW_HIDDEN, (LPARAM)&gce);
CParticipant::Refs participants;
conversation->GetParticipants(participants, CConversation::OTHER_CONSUMERS);
for (uint i = 0; i < participants.size(); i++)
{
participants[i]->GetPropIdentity(data);
this->AddChatContact(cid, ::mir_strdup(data));
}
::mir_free(chatName);
::mir_free(chatID);
}
void CSkypeProto::LeaveChat(const char *cid)
{
char *chatID = ::mir_strdup(cid);
GCDEST gcd = { m_szModuleName, { NULL }, GC_EVENT_CONTROL };
gcd.pszID = chatID;
GCEVENT gce = {0};
gce.cbSize = sizeof(GCEVENT);
gce.pDest = &gcd;
::CallServiceSync(MS_GC_EVENT, SESSION_OFFLINE, (LPARAM)&gce);
::CallServiceSync(MS_GC_EVENT, SESSION_TERMINATE, (LPARAM)&gce);
::mir_free(chatID);
}
void CSkypeProto::RaiseChatEvent(const char *cid, const char *sid, int evt, const char *message)
{
char *idt = ::mir_strdup(cid);
char *snt = ::mir_strdup(sid);
HANDLE hContact = this->GetContactBySid(sid);
char *nick = hContact ? (char *)::CallService(MS_CLIST_GETCONTACTDISPLAYNAME, WPARAM(hContact), 0) : snt;
GCDEST gcd = { this->m_szModuleName, { NULL }, evt };
gcd.pszID = idt;
GCEVENT gce = {0};
gce.cbSize = sizeof(gce);
gce.dwFlags = GCEF_ADDTOLOG;
gce.pDest = &gcd;
gce.pszNick = nick;
gce.pszUID = snt;
gce.bIsMe = ::stricmp(sid, this->login) == 0;
gce.pszStatus = gce.bIsMe ? ::Translate("Me") : ::Translate("Others");
gce.pszText = message;
gce.time = time(NULL);
::CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
::mir_free(snt);
::mir_free(idt);
}
void CSkypeProto::SendChatMessage(const char *cid, const char *sid, const char *message)
{
this->RaiseChatEvent(cid, sid, GC_EVENT_MESSAGE, message);
}
void CSkypeProto::AddChatContact(const char *cid, const char *sid)
{
this->RaiseChatEvent(cid, sid, GC_EVENT_JOIN);
}
void CSkypeProto::KickChatContact(const char *cid, const char *sid)
{
this->RaiseChatEvent(cid, sid, GC_EVENT_KICK);
}
void CSkypeProto::RemoveChatContact(const char *cid, const char *sid)
{
this->RaiseChatEvent(cid, sid, GC_EVENT_QUIT);
}
INT_PTR __cdecl CSkypeProto::OnJoinChat(WPARAM wParam, LPARAM)
{
HANDLE hContact = (HANDLE)wParam;
if (hContact)
{
this->JoinToChat(::DBGetString(hContact, this->m_szModuleName, "ChatRoomID"));
}
return 0;
}
INT_PTR __cdecl CSkypeProto::OnLeaveChat(WPARAM wParam, LPARAM)
{
HANDLE hContact = (HANDLE)wParam;
if (hContact)
{
char *cid = ::DBGetString(hContact, this->m_szModuleName, "ChatRoomID");
this->LeaveChat(cid);
::mir_free(cid);
}
return 0;
}
int __cdecl CSkypeProto::OnGCEventHook(WPARAM, LPARAM lParam)
{
GCHOOK *gch = (GCHOOK *)lParam;
if (!gch) return 1;
if (::strcmp(gch->pDest->pszModule, this->m_szModuleName))
return 0;
char *chatID = ::mir_strdup(gch->pDest->pszID);
char *sid = ::mir_strdup(gch->pszUID);
switch (gch->pDest->iType)
{
case GC_SESSION_TERMINATE:
{
CConversation::Ref conversation;
if (g_skype->GetConversationByIdentity(chatID, conversation, false))
{
Participant::Refs participants;
conversation->GetParticipants(participants, CConversation::MYSELF);
participants[0]->Retire();
}
}
break;
case GC_USER_MESSAGE:
if (gch->pszText && gch->pszText[0])
{
CConversation::Ref conversation;
if (g_skype->GetConversationByIdentity(chatID, conversation, false))
{
CMessage::Ref message;
char *text = ::mir_utf8encode(gch->pszText);
conversation->PostText(text, message);
char *nick = (char *)::DBGetString(NULL, this->m_szModuleName, "Nick");
if (::stricmp(nick, "") == 0)
{
nick = this->login;
}
this->SendChatMessage(
chatID,
nick,
gch->pszText);
}
}
break;
case GC_USER_CHANMGR:
DialogBoxParam(
g_hInstance,
MAKEINTRESOURCE(IDD_CHATROOM_INVITE),
NULL,
CSkypeProto::InviteToChatProc,
LPARAM(new InviteChatParam(chatID, NULL, this)));
break;
case GC_USER_PRIVMESS:
::CallService(MS_MSG_SENDMESSAGE, (WPARAM)this->GetContactBySid(sid), 0);
break;
case GC_USER_LOGMENU:
switch(gch->dwData)
{
case 10:
::DialogBoxParam(
g_hInstance,
MAKEINTRESOURCE(IDD_CHATROOM_INVITE),
NULL,
CSkypeProto::InviteToChatProc,
LPARAM(new InviteChatParam(chatID, NULL, this)));
break;
case 20:
this->LeaveChat(chatID);
break;
}
break;
case GC_USER_NICKLISTMENU:
switch (gch->dwData)
{
case 10:
::CallService(MS_USERINFO_SHOWDIALOG, (WPARAM)this->GetContactBySid(sid), 0);
break;
case 20:
CallService(MS_HISTORY_SHOWCONTACTHISTORY, (WPARAM)this->GetContactBySid(sid), 0);
break;
case 110:
this->LeaveChat(chatID);
break;
}
break;
case GC_USER_TYPNOTIFY:
break;
}
::mir_free(sid);
::mir_free(chatID);
return 0;
}
int __cdecl CSkypeProto::OnGCMenuHook(WPARAM, LPARAM lParam)
{
GCMENUITEMS *gcmi = (GCMENUITEMS*) lParam;
if (gcmi == NULL || ::stricmp(gcmi->pszModule, this->m_szModuleName)) return 0;
if (gcmi->Type == MENU_ON_LOG)
{
static const struct gc_item Items[] =
{
{ TranslateT("&Invite user..."), 10, MENU_ITEM, FALSE },
{ TranslateT("&Leave chat session"), 20, MENU_ITEM, FALSE }
};
gcmi->nItems = SIZEOF(Items);
gcmi->Item = (gc_item*)Items;
}
else if (gcmi->Type == MENU_ON_NICKLIST)
{
char* id = mir_t2a(gcmi->pszUID);
if (!::stricmp(this->login, id))
{
static const struct gc_item Items[] =
{
{ TranslateT("User &details"), 10, MENU_ITEM, FALSE },
{ TranslateT("User &history"), 20, MENU_ITEM, FALSE },
{ _T(""), 100, MENU_SEPARATOR, FALSE },
{ TranslateT("&Leave chat session"), 110, MENU_ITEM, FALSE }
};
gcmi->nItems = SIZEOF(Items);
gcmi->Item = (gc_item*)Items;
}
else
{
static const struct gc_item Items[] =
{
{ TranslateT("User &details"), 10, MENU_ITEM, FALSE },
{ TranslateT("User &history"), 20, MENU_ITEM, FALSE }
};
gcmi->nItems = SIZEOF(Items);
gcmi->Item = (gc_item*)Items;
}
mir_free(id);
}
return 0;
}
char *CSkypeProto::GetChatUsers(const char *cid)
{
GC_INFO gci = {0};
gci.Flags = USERS;
gci.pszModule = this->m_szModuleName;
gci.pszID = ::mir_a2t(cid);
::CallService(MS_GC_GETINFO, 0, (LPARAM)(GC_INFO *) &gci);
::mir_free(gci.pszID);
//StringList users(gci.pszUsers, " ");
return gci.pszUsers;
}
|