summaryrefslogtreecommitdiff
path: root/icqj_s7_sss_mod/icq_xtraz.c
blob: f95d698c9f00a1d7ef60d17b3dccaedecb26dc56 (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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// ---------------------------------------------------------------------------80
//                ICQ plugin for Miranda Instant Messenger
//                ________________________________________
// 
// Copyright © 2000,2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001,2002 Jon Keating, Richard Hughes
// Copyright © 2002,2003,2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004,2005,2006,2007 Joe Kucera
// Copyright © 2006,2007 [sss], chaos.persei, [sin], Faith Healer, Theif, nullbie
// 
// 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.
//
// -----------------------------------------------------------------------------
//
// File name      : $Source$
// Revision       : $Revision: 43 $
// Last change on : $Date: 2007-08-20 01:51:06 +0300 (Пн, 20 авг 2007) $
// Last change by : $Author: sss123next $
//
// DESCRIPTION:
//
//  Internal Xtraz API
//
// -----------------------------------------------------------------------------

#include "icqoscar.h"


extern HANDLE hsmsgrequest;
extern HANDLE hxstatuschanged;


void handleXtrazNotify(DWORD dwUin, DWORD dwMID, DWORD dwMID2, WORD wCookie, char* szMsg, int nMsgLen, BOOL bThruDC)
{
  char *szWork, *szEnd, *szNotify, *szQuery;
  int nNotifyLen, nQueryLen;
  HANDLE hContact;

  szNotify = strstr(szMsg, "<NOTIFY>");
  szQuery = strstr(szMsg, "<QUERY>");

  hContact = HContactFromUIN(dwUin, NULL);
  if (hContact) // user sent us xtraz, he supports it
    SetContactCapabilities(hContact, CAPF_XTRAZ);

  if (szNotify && szQuery)
  { // valid request
    szNotify += 8;
    szQuery += 7;
    szEnd = strstr(szMsg, "</NOTIFY>");
    if (!szEnd) szEnd = szMsg + nMsgLen;
    nNotifyLen = (szEnd - szNotify);
    szEnd = strstr(szMsg, "</QUERY>");
    if (!szEnd) szEnd = szNotify;
    szNotify = DemangleXml(szNotify, nNotifyLen);
    nQueryLen = (szEnd - szQuery);
    szQuery = DemangleXml(szQuery, nQueryLen);
    szWork = strstr(szQuery, "<PluginID>");
    szEnd = strstr(szQuery, "</PluginID>");
#ifdef _DEBUG
    NetLog_Server("Query: %s", szQuery);
    NetLog_Server("Notify: %s", szNotify);
#endif
    if (szWork && szEnd)
    { // this is our plugin
      szWork += 10;
      *szEnd = '\0';

      if (!stricmp(szWork, "srvMng") && strstr(szNotify, "AwayStat"))
      {
        char* szSender = strstr(szNotify, "<senderId>");
        char* szEndSend = strstr(szNotify, "</senderId>");

        if (szSender && szEndSend)
        {
          szSender += 10;
          *szEndSend = '\0';

          if ((DWORD)atoi(szSender) == dwUin)
          {
            char *szResponse;
            int nResponseLen;
            char *szXName, *szXMsg, *tmp;
            BYTE dwXId = ICQGetContactXStatus(NULL);

            if (dwXId && validateStatusMessageRequest(hContact, MTYPE_SCRIPT_NOTIFY))
            { // apply privacy rules
              NotifyEventHooks(hsmsgrequest, (WPARAM)MTYPE_SCRIPT_NOTIFY, (LPARAM)dwUin);

              tmp = ICQGetContactSettingUtf(NULL, DBSETTING_XSTATUSNAME, "");
              szXName = MangleXml(tmp, strlennull(tmp));
              SAFE_FREE(&tmp);
              tmp = ICQGetContactSettingUtf(NULL, DBSETTING_XSTATUSMSG, "");
              szXMsg = MangleXml(tmp, strlennull(tmp));
              SAFE_FREE(&tmp);
              
              nResponseLen = 212 + strlennull(szXName) + strlennull(szXMsg) + UINMAXLEN + 2;
              szResponse = (char*)_alloca(nResponseLen + 1);
              // send response
              nResponseLen = null_snprintf(szResponse, nResponseLen, 
                "<ret event='OnRemoteNotification'>"
                "<srv><id>cAwaySrv</id>"
                "<val srv_id='cAwaySrv'><Root>"
                "<CASXtraSetAwayMessage></CASXtraSetAwayMessage>"
                "<uin>%d</uin>"
                "<index>%d</index>"
                "<title>%s</title>"
                "<desc>%s</desc></Root></val></srv></ret>",
                dwLocalUIN, dwXId, szXName, szXMsg);
			  {
				  CHECKCONTACT chk = {0};
				  chk.hContact=hContact;
				  chk.dwUin=dwUin;
				  chk.nottmpcontact=TRUE;
				  CheckContact(chk);
			  }

              SAFE_FREE(&szXName);
              SAFE_FREE(&szXMsg);

              {
                rate_record rr = {0};

                rr.bType = RIT_XSTATUS_RESPONSE;
                rr.dwUin = dwUin;
                rr.dwMid1 = dwMID;
                rr.dwMid2 = dwMID2;
                rr.wCookie = wCookie;
                rr.szData = szResponse;
                rr.bThruDC = bThruDC;
                rr.nRequestType = 0x102;
                EnterCriticalSection(&ratesMutex);
                rr.wGroup = ratesGroupFromSNAC(gRates, ICQ_MSG_FAMILY, ICQ_MSG_RESPONSE);
                LeaveCriticalSection(&ratesMutex);
                if (bThruDC || !handleRateItem(&rr, TRUE))
                  SendXtrazNotifyResponse(dwUin, dwMID, dwMID2, wCookie, szResponse, nResponseLen, bThruDC);
              }
			  if(!gbXStatusEnabled){
					CHECKCONTACT chk = {0};
					chk.dbeventflag=DBEF_READ;
					chk.dwUin=dwUin;
					chk.hContact=hContact;
					chk.logtofile=chk.popup=chk.historyevent=TRUE;
					chk.icqeventtype=ICQEVENTTYPE_CHECK_STATUS;
					chk.msg="(Xtraz notify when it's disabled)";
					chk.popuptype=POPTYPE_VIS;
					CheckContact(chk);
			  }
            }
			else if (dwXId){
              NetLog_Server("Privacy: Ignoring XStatus request");
				{
					CHECKCONTACT chk = {0};
					chk.dbeventflag=DBEF_READ;
					chk.dwUin=dwUin;
					chk.hContact=hContact;
					chk.logtofile=chk.popup=chk.historyevent=TRUE;
					chk.icqeventtype=ICQEVENTTYPE_CHECK_STATUS;
					chk.msg="(xstatus not set)";
					chk.popuptype=POPTYPE_VIS;
					CheckContact(chk);
				}
			}
			else{
              NetLog_Server("Error: We are not in XStatus, skipping");
				{
					CHECKCONTACT chk = {0};
					chk.dbeventflag=DBEF_READ;
					chk.dwUin=dwUin;
					chk.hContact=hContact;
					chk.logtofile=chk.popup=chk.historyevent=TRUE;
					chk.icqeventtype=ICQEVENTTYPE_CHECK_STATUS;
					chk.msg="(xtraz request)";
					chk.popuptype=POPTYPE_VIS;
					CheckContact(chk);
				}
			}
          }
          else
            NetLog_Server("Error: Invalid sender information");
        }
        else
          NetLog_Server("Error: Missing sender information");
      }
      else
        NetLog_Server("Error: Unknown plugin \"%s\" in Xtraz message", szWork);
    }
    else 
      NetLog_Server("Error: Missing PluginID in Xtraz message");
	{
		CHECKCONTACT chk = {0};
		chk.dwUin=dwUin;
		chk.hContact=hContact;
		chk.nottmpcontact=TRUE;
		CheckContact(chk);
	}

    SAFE_FREE(&szNotify);
    SAFE_FREE(&szQuery);
  }
  else 
    NetLog_Server("Error: Invalid Xtraz Notify message");
}



void handleXtrazNotifyResponse(DWORD dwUin, HANDLE hContact, WORD wCookie, char* szMsg, int nMsgLen)
{
  char *szMem, *szRes, *szEnd;
  int nResLen;

#ifdef _DEBUG
  NetLog_Server("Received Xtraz Notify Response");
#endif

  szRes = strstr(szMsg, "<RES>");
  szEnd = strstr(szMsg, "</RES>");

  if (szRes && szEnd)
  { // valid response
    char *szNode, *szWork;

    szRes += 5;
    nResLen = szEnd - szRes;

    szMem = szRes = DemangleXml(szRes, nResLen);

#ifdef _DEBUG
    NetLog_Server("Response: %s", szRes);
#endif

    ICQBroadcastAck(hContact, ICQACKTYPE_XTRAZNOTIFY_RESPONSE, ACKRESULT_SUCCESS, (HANDLE)wCookie, (LPARAM)szRes);

NextVal:
    szNode = strstr(szRes, "<val srv_id='");
    if (szNode) szEnd = strstr(szNode, ">"); else szEnd = NULL;

    if (szNode && szEnd)
    {
      *(szEnd-1) = '\0';
      szNode += 13;
      szWork = szEnd + 1;

      if (!stricmp(szNode, "cAwaySrv"))
      {
        szNode = strstr(szWork, "<uin>");
        szEnd = strstr(szWork, "</uin>");

        if (szNode && szEnd)
        {
          szNode += 5;
          *szEnd = '\0';

          if ((DWORD)atoi(szNode) == dwUin)
          {
            int bChanged = FALSE;

            *szEnd = ' ';
            szNode = strstr(szWork, "<index>");
            szEnd = strstr(szWork, "</index>");

            if (szNode && szEnd)
            {
              szNode += 7;
              *szEnd = '\0';
              if (atoi(szNode) != ICQGetContactXStatus(hContact))
              { // this is strange - but go on
                NetLog_Server("Warning: XStatusIds do not match!");
              }
              *szEnd = ' ';
            }
            szNode = strstr(szWork, "<title>");
            szEnd = strstr(szWork, "</title>");

            if (szNode && szEnd)
            { // we got XStatus title, save it
              char *szXName, *szOldXName;

              szNode += 7;
              *szEnd = '\0';
              szXName = DemangleXml(szNode, strlennull(szNode));
              // check if the name changed
              szOldXName = ICQGetContactSettingUtf(hContact, DBSETTING_XSTATUSNAME, NULL);
              if (strcmpnull(szOldXName, szXName))
                bChanged = TRUE;
              SAFE_FREE(&szOldXName);
              ICQWriteContactSettingUtf(hContact, DBSETTING_XSTATUSNAME, szXName);
              SAFE_FREE(&szXName);
              *szEnd = ' ';
            }
            szNode = strstr(szWork, "<desc>");
            szEnd = strstr(szWork, "</desc>");

            if (szNode && szEnd)
            { // we got XStatus mode msg, save it
              char *szXMsg, *szOldXMsg;

              szNode += 6;
              *szEnd = '\0';
              szXMsg = DemangleXml(szNode, strlennull(szNode));
              // check if the decription changed
              szOldXMsg = ICQGetContactSettingUtf(hContact, DBSETTING_XSTATUSNAME, NULL);
              if (strcmpnull(szOldXMsg, szXMsg))
                bChanged = TRUE;
              SAFE_FREE(&szOldXMsg);
              ICQWriteContactSettingUtf(hContact, DBSETTING_XSTATUSMSG, szXMsg);
              SAFE_FREE(&szXMsg);
            }
            ICQBroadcastAck(hContact, ICQACKTYPE_XSTATUS_RESPONSE, ACKRESULT_SUCCESS, (HANDLE)wCookie, 0);

            if (bChanged)
              NotifyEventHooks(hxstatuschanged, (WPARAM)hContact, 0);
          }
          else
            NetLog_Server("Error: Invalid sender information");
        }
        else
          NetLog_Server("Error: Missing sender information");
      }
      else
      {
        char *szSrvEnd = strstr(szEnd, "</srv>");

        if (szSrvEnd && strstr(szSrvEnd, "<val srv_id='"))
        { // check all values !
          szRes = szSrvEnd + 6; // after first value
          goto NextVal;
        }
        // no next val, we were unable to handle packet, write error
        NetLog_Server("Error: Unknown serverId \"%s\" in Xtraz response", szNode);
      }
    }
    else
      NetLog_Server("Error: Missing serverId in Xtraz response");

    SAFE_FREE(&szMem);
  }
  else
    NetLog_Server("Error: Invalid Xtraz Notify response");
}



static char* getXmlPidItem(const char* szData, int nLen)
{
  char *szPid, *szEnd;

  szPid = strstr(szData, "<PID>");
  szEnd = strstr(szData, "</PID>");

  if (szPid && szEnd)
  {
    szPid += 5;

    return DemangleXml(szPid, szEnd - szPid);
  }
  return NULL;
}



void handleXtrazInvitation(DWORD dwUin, DWORD dwMID, DWORD dwMID2, WORD wCookie, char* szMsg, int nMsgLen, BOOL bThruDC)
{
  HANDLE hContact;
  char* szPluginID;

  hContact = HContactFromUIN(dwUin, NULL);
  if (hContact) // user sent us xtraz, he supports it
    SetContactCapabilities(hContact, CAPF_XTRAZ);

  szPluginID = getXmlPidItem(szMsg, nMsgLen);
  if (!strcmpnull(szPluginID, "ICQChatRecv"))
  { // it is a invitation to multi-user chat
  }
  else
  {
    NetLog_Uni(bThruDC, "Error: Unknown plugin \"%s\" in Xtraz message", szPluginID);
  }
  SAFE_FREE(&szPluginID);
}



void handleXtrazData(DWORD dwUin, DWORD dwMID, DWORD dwMID2, WORD wCookie, char* szMsg, int nMsgLen, BOOL bThruDC)
{
  HANDLE hContact;
  char* szPluginID;

  hContact = HContactFromUIN(dwUin, NULL);
  if (hContact) // user sent us xtraz, he supports it
    SetContactCapabilities(hContact, CAPF_XTRAZ);

  szPluginID = getXmlPidItem(szMsg, nMsgLen);
  if (!strcmpnull(szPluginID, "viewCard"))
  { // it is a greeting card
    char *szWork, *szEnd, *szUrl, *szNum;

    szWork = strstr(szMsg, "<InD>");
    szEnd = strstr(szMsg, "</InD>");
    if (szWork && szEnd)
    {
      int nDataLen = szEnd - szWork;

      szUrl = (char*)_alloca(nDataLen);
      memcpy(szUrl, szWork+5, nDataLen);
      szUrl[nDataLen - 5] = '\0';

      if (!strnicmp(szUrl, "view_", 5))
      {
        szNum = szUrl + 5;
        szWork = strstr(szUrl, ".html");
        if (szWork)
        {
          strcpy(szWork, ".php");
          strcat(szWork, szWork+5);
        }
        while (szWork = strstr(szUrl, "&amp;"))
        { // unescape &amp; code
          strcpy(szWork+1, szWork+5);
        }
        szWork = (char*)SAFE_MALLOC(nDataLen + MAX_PATH);
        ICQTranslateUtfStatic("Greeting card:", szWork, MAX_PATH);
        strcat(szWork, "\r\nhttp://www.icq.com/friendship/pages/view_page_");
        strcat(szWork, szNum);

        // Create message to notify user
        {
          CCSDATA ccs;
          PROTORECVEVENT pre = {0};
          int bAdded;

          ccs.szProtoService = PSR_MESSAGE;
          ccs.hContact = HContactFromUIN(dwUin, &bAdded);
          ccs.wParam = 0;
          ccs.lParam = (LPARAM)&pre;
          pre.timestamp = time(NULL);
          pre.szMessage = szWork;
          pre.flags = PREF_UTF;

          CallService(MS_PROTO_CHAINRECV, 0, (LPARAM)&ccs);
        }
        SAFE_FREE(&szWork);
      }
      else
        NetLog_Uni(bThruDC, "Error: Non-standard greeting card message");
    }
    else
      NetLog_Uni(bThruDC, "Error: Malformed greeting card message");
  }
  else
  {
    NetLog_Uni(bThruDC, "Error: Unknown plugin \"%s\" in Xtraz message", szPluginID);
  }
  SAFE_FREE(&szPluginID);
}



// Functions really sending Xtraz stuff
DWORD SendXtrazNotifyRequest(HANDLE hContact, char* szQuery, char* szNotify, int bForced)
{
  char *szQueryBody;
  char *szNotifyBody;
  DWORD dwUin;
  int nBodyLen;
  char *szBody;
  DWORD dwCookie;
  message_cookie_data* pCookieData;

  if (ICQGetContactSettingUID(hContact, &dwUin, NULL))
    return 0; // Invalid contact

  if (invis_for(dwUin,hContact))
	   return 0; //we do not want be detected

  if (!CheckContactCapabilities(hContact, CAPF_XTRAZ) && !bForced)
    return 0; // Contact does not support xtraz, do not send anything

  szQueryBody = MangleXml(szQuery, strlennull(szQuery));
  szNotifyBody = MangleXml(szNotify, strlennull(szNotify));
  nBodyLen = strlennull(szQueryBody) + strlennull(szNotifyBody) + 41;
  szBody = (char*)_alloca(nBodyLen);
  nBodyLen = null_snprintf(szBody, nBodyLen, "<N><QUERY>%s</QUERY><NOTIFY>%s</NOTIFY></N>", szQueryBody, szNotifyBody);
  SAFE_FREE(&szQueryBody);
  SAFE_FREE(&szNotifyBody);

  // Set up the ack type
  pCookieData = CreateMessageCookie(MTYPE_SCRIPT_NOTIFY, ACKTYPE_CLIENT);
  dwCookie = AllocateCookie(CKT_MESSAGE, 0, hContact, (void*)pCookieData);

  // have we a open DC, send through that
  if (gbDCMsgEnabled && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0))
    icq_sendXtrazRequestDirect(hContact, dwCookie, szBody, nBodyLen, MTYPE_SCRIPT_NOTIFY);
  else
    icq_sendXtrazRequestServ(dwUin, dwCookie, szBody, nBodyLen, pCookieData);

  return dwCookie;
}



void SendXtrazNotifyResponse(DWORD dwUin, DWORD dwMID, DWORD dwMID2, WORD wCookie, char* szResponse, int nResponseLen, BOOL bThruDC)
{
  char *szResBody = MangleXml(szResponse, nResponseLen);
  int nBodyLen = strlennull(szResBody) + 21;
  char *szBody = (char*)_alloca(nBodyLen);
  HANDLE hContact = HContactFromUIN(dwUin, NULL);

  if (hContact != INVALID_HANDLE_VALUE && !CheckContactCapabilities(hContact, CAPF_XTRAZ))
  {
    SAFE_FREE(&szResBody);
    return; // Contact does not support xtraz, do not send anything
  }

  nBodyLen = null_snprintf(szBody, nBodyLen, "<NR><RES>%s</RES></NR>", szResBody);
  SAFE_FREE(&szResBody);

  // Was request received thru DC and have we a open DC, send through that
  if (bThruDC && IsDirectConnectionOpen(hContact, DIRECTCONN_STANDARD, 0))
    icq_sendXtrazResponseDirect(hContact, wCookie, szBody, nBodyLen, MTYPE_SCRIPT_NOTIFY);
  else
    icq_sendXtrazResponseServ(dwUin, dwMID, dwMID2, wCookie, szBody, nBodyLen, MTYPE_SCRIPT_NOTIFY);
}