summaryrefslogtreecommitdiff
path: root/icqj_mod/directpackets.c
blob: cb1d2496c6f0fa0cfcfc4baf858ca39b9f28f7d0 (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
// ---------------------------------------------------------------------------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 Joe Kucera
// 
// 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: /cvsroot/miranda/miranda/protocols/IcqOscarJ/directpackets.c,v $
// Revision       : $Revision: 2874 $
// Last change on : $Date: 2006-05-16 23:38:00 +0200 (Tue, 16 May 2006) $
// Last change by : $Author: ghazan $
//
// DESCRIPTION:
//
//  Describe me here please...
//
// -----------------------------------------------------------------------------

#include "icqoscar.h"


extern HANDLE hsmsgrequest;
extern CRITICAL_SECTION modeMsgsMutex;
extern WORD wListenPort;

extern packEmptyMsg(icq_packet *packet);


void packDirectMsgHeader(icq_packet* packet, WORD wDataLen, WORD wCommand, DWORD dwCookie, BYTE bMsgType, BYTE bMsgFlags, WORD wX1, WORD wX2)
{
  directPacketInit(packet, 29 + wDataLen);
  packByte(packet, 2);        /* channel */
  packLEDWord(packet, 0);     /* space for crypto */
  packLEWord(packet, wCommand);
  packLEWord(packet, 14);      /* unknown */
  packLEWord(packet, (WORD)dwCookie);
  packLEDWord(packet, 0);      /* unknown */
  packLEDWord(packet, 0);      /* unknown */
  packLEDWord(packet, 0);      /* unknown */
  packByte(packet, bMsgType);
  packByte(packet, bMsgFlags);
  packLEWord(packet, wX1);    /* unknown. Is 1 for getawaymsg, 0 otherwise */
  packLEWord(packet, wX2); // this is probably priority
}



void icq_sendDirectMsgAck(directconnect* dc, WORD wCookie, BYTE bMsgType, BYTE bMsgFlags, char* szCap)
{
  icq_packet packet;

  packDirectMsgHeader(&packet, (WORD)(bMsgType==MTYPE_PLAIN ? (szCap ? 53 : 11) : 3), DIRECT_ACK, wCookie, bMsgType, bMsgFlags, 0, 0);
  packEmptyMsg(&packet);   /* empty message */

  if (bMsgType == MTYPE_PLAIN)
  {
    packMsgColorInfo(&packet);

    if (szCap)
    {
      packLEDWord(&packet, 0x26);     /* CLSID length */
      packBuffer(&packet, szCap, 0x26); /* GUID */
    }
  }
  EncryptDirectPacket(dc, &packet);
  sendDirectPacket(dc, &packet);

  NetLog_Direct("Sent acknowledgement thru direct connection");
}



DWORD icq_sendGetAwayMsgDirect(HANDLE hContact, int type)
{
  icq_packet packet;
  DWORD dwCookie;
  message_cookie_data *pCookieData;

  if (ICQGetContactSettingWord(hContact, "Version", 0) == 9)
    return 0; // v9 DC protocol does not support this message

  pCookieData = CreateMessageCookie(MTYPE_AUTOAWAY, (BYTE)type);
  dwCookie = AllocateCookie(CKT_MESSAGE, 0, ICQGetContactSettingUIN(hContact), (void*)pCookieData);

  packDirectMsgHeader(&packet, 3, DIRECT_MESSAGE, dwCookie, (BYTE)type, 3, 1, 0);
  packEmptyMsg(&packet);  // message

  if (SendDirectMessage(hContact, &packet))
    return dwCookie; // Success
  else 
    return 0;
}



void icq_sendAwayMsgReplyDirect(directconnect* dc, WORD wCookie, BYTE msgType, const char** szMsg)
{
  icq_packet packet;
  WORD wMsgLen;


  if (validateStatusMessageRequest(dc->hContact, msgType))
  {
    NotifyEventHooks(hsmsgrequest, (WPARAM)msgType, (LPARAM)dc->dwRemoteUin);
    
    EnterCriticalSection(&modeMsgsMutex);

    if (szMsg && *szMsg)
    {
      char* szAnsiMsg;

      // prepare Ansi message - only Ansi supported
      wMsgLen = strlennull(*szMsg) + 1;
      szAnsiMsg = (char*)_alloca(wMsgLen);
      utf8_decode_static(*szMsg, szAnsiMsg, wMsgLen);
      wMsgLen = strlennull(szAnsiMsg);
      packDirectMsgHeader(&packet, (WORD)(3 + wMsgLen), DIRECT_ACK, wCookie, msgType, 3, 0, 0);
      packLEWord(&packet, (WORD)(wMsgLen + 1));
      packBuffer(&packet, szAnsiMsg, (WORD)(wMsgLen + 1));
      EncryptDirectPacket(dc, &packet);

      sendDirectPacket(dc, &packet);
    }

    LeaveCriticalSection(&modeMsgsMutex);
  }
}



void icq_sendFileAcceptDirect(HANDLE hContact, filetransfer* ft)
{ // v7 packet
  icq_packet packet;

  packDirectMsgHeader(&packet, 18, DIRECT_ACK, ft->dwCookie, MTYPE_FILEREQ, 0, 0, 0);
  packLEWord(&packet, 1);    // description
  packByte(&packet, 0);
  packWord(&packet, wListenPort);
  packLEWord(&packet, 0);
  packLEWord(&packet, 1);    // filename
  packByte(&packet, 0);     // TODO: really send filename
  packLEDWord(&packet, ft->dwTotalSize);  // file size 
  packLEDWord(&packet, wListenPort);    // FIXME: ideally we want to open a new port for this

  SendDirectMessage(hContact, &packet);

  NetLog_Direct("Sent file accept direct, port %u", wListenPort);
}



void icq_sendFileDenyDirect(HANDLE hContact, filetransfer* ft, char *szReason)
{ // v7 packet
  icq_packet packet;

  packDirectMsgHeader(&packet, (WORD)(18+strlennull(szReason)), DIRECT_ACK, ft->dwCookie, MTYPE_FILEREQ, 0, 1, 0);
  packLEWord(&packet, (WORD)(1+strlennull(szReason)));  // description
  if (szReason) packBuffer(&packet, szReason, (WORD)strlennull(szReason));
  packByte(&packet, 0);
  packWord(&packet, 0);
  packLEWord(&packet, 0);
  packLEWord(&packet, 1);   // filename
  packByte(&packet, 0);     // TODO: really send filename
  packLEDWord(&packet, 0);  // file size 
  packLEDWord(&packet, 0);  

  SendDirectMessage(hContact, &packet);

  NetLog_Direct("Sent file deny direct.");
}



int icq_sendFileSendDirectv7(filetransfer *ft, const char* pszFiles)
{
  icq_packet packet;
  WORD wDescrLen = strlennull(ft->szDescription), wFilesLen = strlennull(pszFiles);

  packDirectMsgHeader(&packet, (WORD)(18 + wDescrLen + wFilesLen), DIRECT_MESSAGE, (WORD)ft->dwCookie, MTYPE_FILEREQ, 0, 0, 0);
  packLEWord(&packet, (WORD)(wDescrLen + 1));
  packBuffer(&packet, ft->szDescription, (WORD)(wDescrLen + 1));
  packLEDWord(&packet, 0);   // listen port
  packLEWord(&packet, (WORD)(wFilesLen + 1));
  packBuffer(&packet, pszFiles, (WORD)(wFilesLen + 1));
  packLEDWord(&packet, ft->dwTotalSize);
  packLEDWord(&packet, 0);    // listen port (again)

  NetLog_Direct("Sending v%u file transfer request direct", 7);

  return SendDirectMessage(ft->hContact, &packet);
}



int icq_sendFileSendDirectv8(filetransfer *ft, const char *pszFiles)
{
  icq_packet packet;
  WORD wDescrLen = strlennull(ft->szDescription), wFilesLen = strlennull(pszFiles);

  packDirectMsgHeader(&packet, (WORD)(0x2E + 22 + wDescrLen + wFilesLen + 1), DIRECT_MESSAGE, (WORD)ft->dwCookie, MTYPE_PLUGIN, 0, 0, 0);
  packEmptyMsg(&packet);  // message
  packPluginTypeId(&packet, MTYPE_FILEREQ);

  packLEDWord(&packet, (WORD)(18 + wDescrLen + wFilesLen + 1)); // Remaining length
  packLEDWord(&packet, wDescrLen);          // Description
  packBuffer(&packet, ft->szDescription, wDescrLen);
  packWord(&packet, 0x8c82); // Unknown (port?), seen 0x80F6
  packWord(&packet, 0x0222); // Unknown, seen 0x2e01
  packLEWord(&packet, (WORD)(wFilesLen + 1));
  packBuffer(&packet, pszFiles, (WORD)(wFilesLen + 1));
  packLEDWord(&packet, ft->dwTotalSize);
  packLEDWord(&packet, 0x0008c82); // Unknown, (seen 0xf680 ~33000)

  NetLog_Direct("Sending v%u file transfer request direct", 8);

  return SendDirectMessage(ft->hContact, &packet);
}



DWORD icq_SendDirectMessage(DWORD dwUin, HANDLE hContact, const char *szMessage, int nBodyLength, WORD wPriority, message_cookie_data *pCookieData, char *szCap)
{
  icq_packet packet;
  DWORD dwCookie;


  dwCookie = AllocateCookie(CKT_MESSAGE, 0, dwUin, (void*)pCookieData);

  // Pack the standard header
  packDirectMsgHeader(&packet, (WORD)(nBodyLength + (szCap ? 53:11)), DIRECT_MESSAGE, dwCookie, (BYTE)pCookieData->bMessageType, 0, 0, 0);

  packLEWord(&packet, (WORD)(nBodyLength+1));            // Length of message
  packBuffer(&packet, szMessage, (WORD)(nBodyLength+1)); // Message
  packMsgColorInfo(&packet);
  if (szCap)
  {
    packLEDWord(&packet, 0x00000026);                    // length of GUID
    packBuffer(&packet, szCap, 0x26);                    // UTF-8 GUID
  }

  if (SendDirectMessage(hContact, &packet))
    return dwCookie; // Success
  else
  {
    FreeCookie(dwCookie); // release cookie

    return 0; // Failure
  }
}



void icq_sendXtrazRequestDirect(DWORD dwUin, HANDLE hContact, DWORD dwCookie, char* szBody, int nBodyLen, WORD wType)
{
  icq_packet packet;

  packDirectMsgHeader(&packet, (WORD)(11 + getPluginTypeIdLen(wType) + nBodyLen), DIRECT_MESSAGE, dwCookie, MTYPE_PLUGIN, 0, 0, 1);
  packEmptyMsg(&packet);  // message (unused)
  packPluginTypeId(&packet, wType);

  packLEDWord(&packet, nBodyLen + 4);
  packLEDWord(&packet, nBodyLen);
  packBuffer(&packet, szBody, (WORD)nBodyLen);

  SendDirectMessage(hContact, &packet);
}



void icq_sendXtrazResponseDirect(DWORD dwUin, HANDLE hContact, WORD wCookie, char* szBody, int nBodyLen, WORD wType)
{
  icq_packet packet;

  packDirectMsgHeader(&packet, (WORD)(getPluginTypeIdLen(wType) + 11 + nBodyLen), DIRECT_ACK, wCookie, MTYPE_PLUGIN, 0, 0, 0);
  //
  packEmptyMsg(&packet);  // Message (unused)

  packPluginTypeId(&packet, wType);

  packLEDWord(&packet, nBodyLen + 4);
  packLEDWord(&packet, nBodyLen);
  packBuffer(&packet, szBody, (WORD)nBodyLen);

  SendDirectMessage(hContact, &packet);
}