summaryrefslogtreecommitdiff
path: root/miranda-wine/protocols/IcqOscarJ/oscar_filetransfer.c
blob: ce7cbdca3b5a282e25f9d7cb702d057adce8f40e (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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
// ---------------------------------------------------------------------------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/oscar_filetransfer.c,v $
// Revision       : $Revision: 3720 $
// Last change on : $Date: 2006-09-07 00:56:27 +0400 (Чтв, 07 Сен 2006) $
// Last change by : $Author: jokusoftware $
//
// DESCRIPTION:
//
//  Describe me here please...
//
// -----------------------------------------------------------------------------

#include "icqoscar.h"


typedef struct {
  int type;
  int incoming;
  HANDLE hContact;
  HANDLE hConnection;
  DWORD dwRemoteIP;
  oscar_filetransfer *ft;
  oscar_listener *listener;
} oscarthreadstartinfo;


// small utility function
extern void NormalizeBackslash(char* path);


static DWORD __stdcall oft_connectionThread(oscarthreadstartinfo *otsi);
static void sendOscarPacket(oscar_connection *oc, icq_packet *packet);
static int oft_handlePackets(oscar_connection *oc, unsigned char *buf, int len);
static int oft_handleFileData(oscar_connection *oc, unsigned char *buf, int len);
static void handleOFT2FramePacket(oscar_connection *oc, WORD datatype, BYTE *pBuffer, WORD wLen);
static void sendOFT2FramePacket(oscar_connection *oc, WORD datatype);

void oft_sendPeerInit(oscar_connection *oc);

static CRITICAL_SECTION oftMutex;
static int oftTransferCount = 0;
static oscar_filetransfer** oftTransferList = NULL;

static oscar_filetransfer* CreateOscarTransfer();
static void SafeReleaseOscarTransfer(oscar_filetransfer *ft);
static int IsValidOscarTransfer(void *ft);
static oscar_filetransfer* FindOscarTransfer(HANDLE hContact, DWORD dwID1, DWORD dwID2);


//
// Utility functions
/////////////////////////////


static oscar_filetransfer* CreateOscarTransfer()
{
  oscar_filetransfer* ft = (oscar_filetransfer*)SAFE_MALLOC(sizeof(oscar_filetransfer));

  EnterCriticalSection(&oftMutex);

  oftTransferList = (oscar_filetransfer**)realloc(oftTransferList, sizeof(oscar_filetransfer*)*(oftTransferCount + 1));
  oftTransferList[oftTransferCount++] = ft;

  LeaveCriticalSection(&oftMutex);

  return ft;
}



static void SafeReleaseOscarTransfer(oscar_filetransfer *ft)
{
  int i;

  EnterCriticalSection(&oftMutex);

  for (i = 0; i < oftTransferCount; i++)
  {
    if (oftTransferList[i] == ft)
    {
      oftTransferCount--;
      SafeReleaseFileTransfer(&oftTransferList[i]);
      oftTransferList[i] = oftTransferList[oftTransferCount];
      oftTransferList = (oscar_filetransfer**)realloc(oftTransferList, sizeof(oscar_filetransfer*)*oftTransferCount);
      break;
    }
  }

  LeaveCriticalSection(&oftMutex);
}



static int IsValidOscarTransfer(void *ft)
{
  int i;

  EnterCriticalSection(&oftMutex);

  for (i = 0; i < oftTransferCount; i++)
  {
    if (oftTransferList[i] == ft)
    {
      LeaveCriticalSection(&oftMutex);

      return 1;
    }
  }

  LeaveCriticalSection(&oftMutex);

  return 0;
}



static oscar_filetransfer* FindOscarTransfer(HANDLE hContact, DWORD dwID1, DWORD dwID2)
{
  int i;

  EnterCriticalSection(&oftMutex);

  for (i = 0; i < oftTransferCount; i++)
  {
    if (oftTransferList[i]->hContact == hContact && oftTransferList[i]->pMessage.dwMsgID1 == dwID1 && oftTransferList[i]->pMessage.dwMsgID2 == dwID2)
    {
      LeaveCriticalSection(&oftMutex);

      return oftTransferList[i];
    }
  }

  LeaveCriticalSection(&oftMutex);

  return NULL;
}



// Release file transfer structure
void SafeReleaseFileTransfer(void **ft)
{
  basic_filetransfer **bft = (basic_filetransfer**)ft;

  if (*bft)
  {
    if ((*bft)->ft_magic == FT_MAGIC_ICQ)
    { // release ICQ filetransfer structure and its contents
      filetransfer *ift = (filetransfer*)(*bft);

      SAFE_FREE(&ift->szFilename);
      SAFE_FREE(&ift->szDescription);
      SAFE_FREE(&ift->szSavePath);
      SAFE_FREE(&ift->szThisFile);
      SAFE_FREE(&ift->szThisSubdir);
      if (ift->files)
      {
        int i;

        for (i = 0; i < (int)ift->dwFileCount; i++)
          SAFE_FREE(&ift->files[i]);
        SAFE_FREE((char**)&ift->files);
      }
      SAFE_FREE(ft);
    }
    else
    { // release oscar filetransfer structure and its contents
      oscar_filetransfer *oft = (oscar_filetransfer*)(*bft);
      // FIX ME: release all dynamic members
      if (oft->listener)
        ReleaseOscarListener((oscar_listener**)&oft->listener);
      SAFE_FREE(&oft->rawFileName);
      SAFE_FREE(&oft->szSavePath);
      SAFE_FREE(&oft->szThisFile);
      if (oft->files)
      {
        int i;

        for (i = 0; i < oft->wFilesCount; i++)
          SAFE_FREE(&oft->files[i]);
        SAFE_FREE((char**)&oft->files);
      }
      SAFE_FREE(ft);
    }
  }
}




// Calculate oft checksum of buffer
// --------------------------------
// Information was gathered from Gaim's sources, thanks
//
DWORD oft_calc_checksum(int offset, const BYTE *buffer, int len, DWORD dwChecksum)
{
  DWORD checksum;
  int i;

  checksum = (dwChecksum >> 16) & 0xffff;
  for (i = 0; i < len; i++)
  {
    WORD val = buffer[i];
    DWORD oldchecksum = checksum;

    if (((i + offset) & 1) == 0)
      val = val << 8;

    if (checksum < val)
      checksum -= val + 1;
    else // simulate carry
      checksum -= val;
  }
  checksum = ((checksum & 0x0000ffff) + (checksum >> 16));
  checksum = ((checksum & 0x0000ffff) + (checksum >> 16));
  return checksum << 16;
}



oscar_listener* CreateOscarListener(oscar_filetransfer *ft, NETLIBNEWCONNECTIONPROC_V2 handler)
{
  oscar_listener *listener = (oscar_listener*)SAFE_MALLOC(sizeof(oscar_listener));

  listener->ft = ft;
  if (listener->hBoundPort = NetLib_BindPort(handler, listener, &listener->wPort, NULL))
    return listener; // Success

  SAFE_FREE(&listener);

  return NULL; // Failure
}



void ReleaseOscarListener(oscar_listener **pListener)
{
  oscar_listener *listener = *pListener;

  if (listener)
  { // Close listening port
    if (listener->hBoundPort)
      NetLib_SafeCloseHandle(&listener->hBoundPort, FALSE);
  }
  SAFE_FREE(pListener);
}


//
// Miranda FT interface handlers & services
/////////////////////////////

void InitOscarFileTransfer()
{
  InitializeCriticalSection(&oftMutex);
}



void UninitOscarFileTransfer()
{
  DeleteCriticalSection(&oftMutex);
}



void handleRecvServMsgOFT(unsigned char *buf, WORD wLen, DWORD dwUin, char *szUID, DWORD dwID1, DWORD dwID2, WORD wCommand)
{
  if (wCommand == 0)
  { // this is OFT request
    oscar_tlv_chain* chain = readIntoTLVChain(&buf, wLen, 0);

    if (chain)
    {
      HANDLE hContact = HContactFromUID(dwUin, szUID, NULL);
      WORD wAckType = getWordFromChain(chain, 0x0A, 1);

      if (wAckType == 1)
      { // This is first request in this OFT
        oscar_filetransfer *ft = CreateOscarTransfer();
        char* pszFileName = NULL;
        char* pszDescription = NULL;
        WORD wFilenameLength;

        NetLog_Server("This is a file request");

        // This TLV chain may contain the following TLVs:
        // TLV(A): Acktype 0x0001 - file request / abort request
        //                 0x0002 - file ack
        // TLV(F): Unknown
        // TLV(E): Language ?
        // TLV(2): Proxy IP
        // TLV(16): Proxy IP Check
        // TLV(3): External IP
        // TLV(4): Internal IP
        // TLV(5): Port
        // TLV(17): Port Check
        // TLV(10): Proxy Flag
        // TLV(D): Charset of User Message
        // TLV(C): User Message (ICQ_COOL_FT)
        // TLV(2711): FT info
        // TLV(2712): Charset of file name

        ft->ft_magic = FT_MAGIC_OSCAR;
        // init filetransfer structure
        ft->pMessage.dwMsgID1 = dwID1;
        ft->pMessage.dwMsgID2 = dwID2;
        ft->bUseProxy = getTLV(chain, 0x10, 1) ? 1 : 0;
        ft->dwProxyIP = getDWordFromChain(chain, 0x02, 1);
        ft->dwRemoteExternalIP = getDWordFromChain(chain, 0x03, 1);
        ft->dwRemoteInternalIP = getDWordFromChain(chain, 0x04, 1);
        ft->dwRemotePort = getWordFromChain(chain, 0x05, 1);

        { // User Message
          oscar_tlv* tlv = getTLV(chain, 0x0C, 1);

          if (tlv)
          { // parse User Message
            BYTE* tBuf = tlv->pData;

            pszDescription = (char*)_alloca(tlv->wLen + 1);
            unpackString(&tBuf, pszDescription, tlv->wLen);
            pszDescription[tlv->wLen] = '\0';
            { // apply User Message encoding
              oscar_tlv *charset = getTLV(chain, 0x0D, 1);
              char *str = pszDescription;
              char *bTag,*eTag;

              if (charset)
              { // decode charset
                char *szEnc = (char*)_alloca(charset->wLen + 1);

                strncpy(szEnc, charset->pData, charset->wLen);
                szEnc[charset->wLen] = '\0';
                str = ApplyEncoding(pszDescription, szEnc);
              }
              // decode XML tags
              str = DemangleXml(str, strlennull(str));
              if (charset) SAFE_FREE(&pszDescription);
              pszDescription = str;

              bTag = strstr(str, "<FS>");
              if (bTag)
              { // take only <FS> - Description tag if present
                eTag = strstr(bTag, "</FS>");
                if (eTag)
                {
                  *eTag = '\0';
                  pszDescription = null_strdup(bTag + 4);
                  SAFE_FREE(&str);
                }
              }
            }

          }
          if (!strlennull(pszDescription)) pszDescription = ICQTranslate("No description given");
        }
        { // parse File Transfer Info block
          oscar_tlv* tlv = getTLV(chain, 0x2711, 1);
          BYTE* tBuf = tlv->pData;
          WORD tLen = tlv->wLen;

          tBuf += 2; // FT flag
          unpackWord(&tBuf, &ft->wFilesCount);
          unpackDWord(&tBuf, &ft->dwTotalSize);
          tLen -= 8;
          // Filename
          wFilenameLength = tLen - 1;
          pszFileName = (char*)_alloca(tLen); 
          unpackString(&tBuf, pszFileName, wFilenameLength);
          pszFileName[wFilenameLength] = '\0';
          { // apply Filename encoding
            oscar_tlv* charset = getTLV(chain, 0x2712, 1);

            if (charset)
            {
              char* szEnc = (char*)_alloca(charset->wLen + 1);

              strncpy(szEnc, charset->pData, charset->wLen);
              szEnc[charset->wLen] = '\0';
              pszFileName = ApplyEncoding(pszFileName, szEnc);
            }
            else // needs to be alloced
              pszFileName = null_strdup(pszFileName);
          }
        }
        {
          CCSDATA ccs;
          PROTORECVEVENT pre;
          char* szBlob;
          int bAdded;
          HANDLE hContact = HContactFromUID(dwUin, szUID, &bAdded);

          ft->hContact = hContact;
          ft->szFilename = pszFileName;
          ft->szDescription = pszDescription;
          ft->fileId = -1;
    
          // Send chain event
          szBlob = (char*)_alloca(sizeof(DWORD) + strlennull(pszFileName) + strlennull(pszDescription) + 2);
          *(PDWORD)szBlob = (DWORD)ft;
          strcpy(szBlob + sizeof(DWORD), pszFileName);
          strcpy(szBlob + sizeof(DWORD) + strlennull(pszFileName) + 1, pszDescription);
          ccs.szProtoService = PSR_FILE;
          ccs.hContact = hContact;
          ccs.wParam = 0;
          ccs.lParam = (LPARAM)&pre;
          pre.flags = 0;
          pre.timestamp = time(NULL);
          pre.szMessage = szBlob;
          pre.lParam = 0;

          CallService(MS_PROTO_CHAINRECV, 0, (LPARAM)&ccs);
        }
      }
      else if (wAckType == 2)
      { // First attempt failed, reverse requested
      }
      else if (wAckType == 3)
      { // Reverse attempt failed, trying proxy
      }

      disposeChain(&chain);
    }
    else
      NetLog_Server("Error: Missing TLV chain in OFT request");
  }
  else
  { // TODO: handle other commands / cancels
  }
}



DWORD oftFileAllow(HANDLE hContact, WPARAM wParam, LPARAM lParam)
{
  oscar_filetransfer* ft = (oscar_filetransfer*)wParam;

  ft->szSavePath = null_strdup((char *)lParam);

  OpenOscarConnection(hContact, ft, ft->bUseProxy ? OCT_PROXY: OCT_NORMAL);

  return wParam; // Success
}



DWORD oftFileDeny(HANDLE hContact, WPARAM wParam, LPARAM lParam)
{
  oscar_filetransfer* ft = (oscar_filetransfer*)wParam;
  DWORD dwUin;
  uid_str szUid;

  if (ICQGetContactSettingUID(hContact, &dwUin, &szUid))
    return 1; // Invalid contact

  oft_sendFileDeny(dwUin, szUid, ft);

  SafeReleaseOscarTransfer(ft);

  return 0; // Success
}



DWORD oftFileCancel(HANDLE hContact, WPARAM wParam, LPARAM lParam)
{
  oscar_filetransfer* ft = (oscar_filetransfer*)wParam;
  DWORD dwUin;
  uid_str szUid;

  if (ft->hContact != hContact)
    return 1; // Bad contact or hTransfer

  if (ICQGetContactSettingUID(hContact, &dwUin, &szUid))
    return 1; // Invalid contact

  oft_sendFileDeny(dwUin, szUid, ft);

  ICQBroadcastAck(hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0);

  if (ft->connection)
  { 
    CloseOscarConnection((oscar_connection*)ft->connection);
  }
  // Release structure
  SafeReleaseOscarTransfer(ft);

  return 0; // Success
}



void oftFileResume(oscar_filetransfer *ft, int action, const char *szFilename)
{
  oscar_connection *oc;
  int openFlags;

  if (ft->connection == NULL)
    return;

  oc = (oscar_connection*)ft->connection;

  switch (action)
  {
    case FILERESUME_RESUME: // FIX ME: this needs to request resume!!!
      openFlags = _O_BINARY | _O_WRONLY;
      break;

    case FILERESUME_OVERWRITE:
      openFlags = _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY;
      ft->dwFileBytesDone = 0;
      break;

    case FILERESUME_SKIP:
      openFlags = _O_BINARY | _O_WRONLY;
      ft->dwFileBytesDone = ft->dwThisFileSize;
      break;

    case FILERESUME_RENAME:
      openFlags = _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY;
      SAFE_FREE(&ft->szThisFile);
      ft->szThisFile = null_strdup(szFilename);
      ft->dwFileBytesDone = 0;
      break;
  }

  // TODO: unicode support
  ft->fileId = _open(ft->szThisFile, openFlags, _S_IREAD | _S_IWRITE);
  if (ft->fileId == -1)
  {
    icq_LogMessage(LOG_ERROR, "Your file receive has been aborted because Miranda could not open the destination file in order to write to it. You may be trying to save to a read-only folder.");

    CloseOscarConnection(oc);
    return;
  }

  if (action == FILERESUME_RESUME)
    ft->dwFileBytesDone = _lseek(ft->fileId, 0, SEEK_END);
  else
    _lseek(ft->fileId, ft->dwFileBytesDone, SEEK_SET);

  ft->dwBytesDone += ft->dwFileBytesDone;

  // Send "we are ready"
  oc->status = OCS_DATA;

  sendOFT2FramePacket(oc, OFT_TYPE_READY);
  ICQBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0);

  if (!ft->dwThisFileSize)
  { // if the file is empty we will not receive any data
    BYTE buf;
    oft_handleFileData(oc, &buf, 0);
  }
}



static void oft_buildProtoFileTransferStatus(oscar_filetransfer* ft, PROTOFILETRANSFERSTATUS* pfts)
{
  ZeroMemory(pfts, sizeof(PROTOFILETRANSFERSTATUS));
  pfts->cbSize = sizeof(PROTOFILETRANSFERSTATUS);
  pfts->hContact = ft->hContact;
  pfts->sending = ft->sending;
  if (ft->sending)
    pfts->files = ft->files;
  else
    pfts->files = NULL;  /* FIXME */
  pfts->totalFiles = ft->wFilesCount;
  pfts->currentFileNumber = ft->iCurrentFile;
  pfts->totalBytes = ft->dwTotalSize;
  pfts->totalProgress = ft->dwBytesDone;
  pfts->workingDir = ft->szSavePath;
  pfts->currentFile = ft->szThisFile;
  pfts->currentFileSize = ft->dwThisFileSize;
  pfts->currentFileTime = ft->dwThisFileDate;
  pfts->currentFileProgress = ft->dwFileBytesDone;
}



void CloseOscarConnection(oscar_connection *oc)
{
  EnterCriticalSection(&oftMutex);

  if (oc)
  {
    oc->type = OCT_CLOSING;

    if (oc->hConnection)
    { // we need this for Netlib handle consistency
      NetLib_SafeCloseHandle(&oc->hConnection, FALSE);
    }
  }
  LeaveCriticalSection(&oftMutex);
}



void OpenOscarConnection(HANDLE hContact, oscar_filetransfer *ft, int type)
{
  pthread_t tid;
  oscarthreadstartinfo *otsi = (oscarthreadstartinfo*)SAFE_MALLOC(sizeof(oscarthreadstartinfo));

  otsi->hContact = hContact;
  otsi->type = type;
  otsi->ft = ft;

  tid.hThread = (HANDLE)forkthreadex(NULL, 0, oft_connectionThread, otsi, 0, &tid.dwThreadId);
  CloseHandle(tid.hThread);
}



// This function is called from the Netlib when someone is connecting to our oscar_listener
void oft_newConnectionReceived(HANDLE hNewConnection, DWORD dwRemoteIP, void *pExtra)
{
  pthread_t tid;
  oscarthreadstartinfo *otsi = (oscarthreadstartinfo*)SAFE_MALLOC(sizeof(oscarthreadstartinfo));
  oscar_listener* listener = (oscar_listener*)pExtra;

  otsi->type = listener->ft->sending ? OCT_NORMAL : OCT_REVERSE;
  otsi->incoming = 1;
  otsi->hConnection = hNewConnection;
  otsi->dwRemoteIP = dwRemoteIP;
  otsi->listener = listener;

  // Start a new thread for the incomming connection
  tid.hThread = (HANDLE)forkthreadex(NULL, 0, oft_connectionThread, otsi, 0, &tid.dwThreadId);
  CloseHandle(tid.hThread);
}



static DWORD __stdcall oft_connectionThread(oscarthreadstartinfo *otsi)
{
  oscar_connection oc = {0};
  oscar_listener *source;
  NETLIBPACKETRECVER packetRecv={0};
  HANDLE hPacketRecver;

  oc.hContact = otsi->hContact;
  oc.hConnection = otsi->hConnection;
  oc.type = otsi->type;
  oc.incoming = otsi->incoming;
  oc.ft = otsi->ft;
  source = otsi->listener;
  if (oc.incoming)
  {
    if (IsValidOscarTransfer(source->ft))
    {
      oc.ft = source->ft;
      oc.ft->dwRemoteExternalIP = otsi->dwRemoteIP;
      oc.hContact = oc.ft->hContact;
      oc.ft->connection = &oc;
    }
    else
    { // FT is already over, kill listener
      NetLog_Direct("Received unexpected connection, closing.");

      CloseOscarConnection(&oc);
      ReleaseOscarListener(&source);

      return 0;
    }
  }
  SAFE_FREE(&otsi);

  if (oc.hContact)
  { // Load contact information
    ICQGetContactSettingUID(oc.hContact, &oc.dwUin, &oc.szUid);
  }

  // Load local IP information
  oc.dwLocalExternalIP = ICQGetContactSettingDword(NULL, "IP", 0);
  oc.dwLocalInternalIP = ICQGetContactSettingDword(NULL, "RealIP", 0);

  if (!oc.incoming)
  { // FIX ME: this needs more work for proxy & sending
    if (oc.type == OCT_NORMAL)
    { // create outgoing connection to peer
      NETLIBOPENCONNECTION nloc = {0};
      IN_ADDR addr = {0};

      nloc.cbSize = sizeof(nloc);

      // FIX ME: Just for testing reverse
      if (oc.ft->dwRemoteExternalIP == oc.dwLocalExternalIP && oc.ft->dwRemoteInternalIP)
        addr.S_un.S_addr = htonl(oc.ft->dwRemoteInternalIP);
      else
        addr.S_un.S_addr = htonl(oc.ft->dwRemoteExternalIP);

      // Inform UI that we will attempt to connect
      ICQBroadcastAck(oc.ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, oc.ft, 0);

      if (!addr.S_un.S_addr)
      { // IP to connect to is empty, request reverse
        oscar_listener* listener = CreateOscarListener(oc.ft, oft_newConnectionReceived);

        if (listener)
        { // we got listening port, fine send request
          oc.ft->listener = listener;
          oft_sendFileRedirect(oc.dwUin, oc.szUid, oc.ft, oc.dwLocalInternalIP, listener->wPort, FALSE);
// FIX ME: FT UI should support this
//          ICQBroadcastAck(oc.ft->hContact, ACKTYPE_FILE, ACKRESULT_LISTENING, oc.ft, 0);
        }
        // FIX ME: try proxy
        return 0;
      }
      nloc.szHost = inet_ntoa(addr);
      nloc.wPort = (WORD)oc.ft->dwRemotePort;
      NetLog_Direct("%sConnecting to %s:%u", oc.type==OCT_REVERSE?"Reverse ":"", nloc.szHost, nloc.wPort);

      oc.hConnection = NetLib_OpenConnection(ghDirectNetlibUser, &nloc);
      if (!oc.hConnection)
      { // connection failed, try reverse
        oscar_listener* listener = CreateOscarListener(oc.ft, oft_newConnectionReceived);

        if (listener)
        { // we got listening port, fine send request
          oc.ft->listener = listener;
          oft_sendFileRedirect(oc.dwUin, oc.szUid, oc.ft, oc.dwLocalInternalIP, listener->wPort, FALSE);
// FIX ME: FT UI should support this
//          ICQBroadcastAck(oc.ft->hContact, ACKTYPE_FILE, ACKRESULT_LISTENING, oc.ft, 0);
        }
        // FIX ME: try proxy
        return 0;
      }
      oc.ft->connection = &oc;
      // acknowledge OFT - connection is ready
      oft_sendFileAccept(oc.dwUin, oc.szUid, oc.ft);
    }
  }
  if (!oc.hConnection)
  { // one more sanity chech
    NetLog_Direct("Error: No OFT connection.");
    return 0;
  }
  // Connected, notify FT UI
  ICQBroadcastAck(oc.ft->hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, oc.ft, 0);
  // Init connection
  if (oc.ft->sending)
    oft_sendPeerInit(&oc); // only if sending file...

  hPacketRecver = (HANDLE)CallService(MS_NETLIB_CREATEPACKETRECVER, (WPARAM)oc.hConnection, 8192);
  packetRecv.cbSize = sizeof(packetRecv);

  // Packet receiving loop

  while (oc.hConnection)
  {
    int recvResult;

    packetRecv.dwTimeout = oc.wantIdleTime ? 0 : 60000;

    recvResult = CallService(MS_NETLIB_GETMOREPACKETS, (WPARAM)hPacketRecver, (LPARAM)&packetRecv);
    if (!recvResult)
    {
      NetLog_Direct("Clean closure of oscar socket (%p)", oc.hConnection);
      break;
    }

    if (recvResult == SOCKET_ERROR)
    {
      if (GetLastError() == ERROR_TIMEOUT)
      { // TODO: this will not work on some systems
        if (oc.wantIdleTime)
        {
          // here we should want to send file data packets
        }
        else
        {
          NetLog_Direct("Connection timeouted, closing.");
          break;
        }
      }
      else
      {
        NetLog_Direct("Abortive closure of oscar socket (%p) (%d)", oc.hConnection, GetLastError());
        break;
      }
    }

    if (oc.type == OCT_CLOSING)
      packetRecv.bytesUsed = packetRecv.bytesAvailable;
    else
      packetRecv.bytesUsed = oft_handlePackets(&oc, packetRecv.buffer, packetRecv.bytesAvailable);
  }

  // End of packet receiving loop

  NetLib_SafeCloseHandle(&hPacketRecver, FALSE);

  CloseOscarConnection(&oc);

  // FIX ME: Clean up, error handling
  if (IsValidOscarTransfer(oc.ft))
  {
    oc.ft->connection = NULL; // release link
  }

  return 0;
}



static void sendOscarPacket(oscar_connection *oc, icq_packet *packet)
{
  if (oc->hConnection)
  {
    int nResult;

    nResult = Netlib_Send(oc->hConnection, (const char*)packet->pData, packet->wLen, 0);

    if (nResult == SOCKET_ERROR)
    {
      NetLog_Direct("Oscar %p socket error: %d, closing", oc->hConnection, GetLastError());
      CloseOscarConnection(oc);
    }
  }
  
  SAFE_FREE(&packet->pData);
}



static int oft_handlePackets(oscar_connection *oc, unsigned char *buf, int len)
{
  BYTE *pBuf; 
  DWORD dwHead;
  WORD datalen;
  WORD datatype;
  int bytesUsed = 0;

  while (len > 0)
  {
    if (oc->status == OCS_DATA)
    {
      return oft_handleFileData(oc, buf, len);
    }
    if (len < 6)
      break;

    pBuf = buf;
    unpackDWord(&pBuf, &dwHead);
    if (dwHead != 0x4F465432)
    { // bad packet
      CloseOscarConnection(oc);
      break;
    }

    unpackWord(&pBuf, &datalen);

    if (len < datalen) // wait for whole packet
      break;

    unpackWord(&pBuf, &datatype);
#ifdef _DEBUG
    NetLog_Direct("OFT2: Type %u, Length %u bytes", datatype, datalen);
#endif
    handleOFT2FramePacket(oc, datatype, pBuf, (WORD)(datalen - 8));

    /* Increase pointers so we can check for more data */
    buf += datalen;
    len -= datalen;
    bytesUsed += datalen;
  }

  return bytesUsed;
}



static int oft_handleFileData(oscar_connection *oc, unsigned char *buf, int len)
{
  oscar_filetransfer *ft = oc->ft;
  DWORD dwLen = len;
  int bytesUsed = 0;

  // do not accept more data than expected
  if (ft->dwThisFileSize - ft->dwFileBytesDone < dwLen) 
    dwLen = ft->dwThisFileSize - ft->dwFileBytesDone;

  if (ft->fileId == -1)
  { // something went terribly bad
    CloseOscarConnection(oc);
    return 0;
  }
  _write(ft->fileId, buf, dwLen);
  // update checksum
  ft->dwRecvFileCheck = oft_calc_checksum(ft->dwFileBytesDone, buf, dwLen, ft->dwRecvFileCheck);
  bytesUsed += dwLen;
  ft->dwBytesDone += dwLen;
  ft->dwFileBytesDone += dwLen;

  if (GetTickCount() > ft->dwLastNotify + 700 || ft->dwFileBytesDone == ft->dwThisFileSize) 
  { // notify FT UI of our progress, at most every 700ms - do not be faster than Miranda
    PROTOFILETRANSFERSTATUS pfts;

    oft_buildProtoFileTransferStatus(ft, &pfts);
    ICQBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&pfts);
    oc->ft->dwLastNotify = GetTickCount();
  }
  if (ft->dwFileBytesDone == ft->dwThisFileSize)
  {
    /* EOF */
    _close(ft->fileId);
    ft->fileId = -1;

    if (ft->dwRecvFileCheck != ft->dwThisFileCheck)
    {
      NetLog_Direct("Error: File checksums does not match!");
      // FIX ME: here we should inform user and end transfer
    }

    if ((DWORD)(ft->iCurrentFile + 1) == ft->wFilesCount)
    {
      ft->bHeaderFlags = 0x01; // the whole process is over
      // ack received file
      sendOFT2FramePacket(oc, OFT_TYPE_DONE);
      oc->type = OCT_CLOSING;
      CloseOscarConnection(oc); // close connection
      NetLog_Direct("File Transfer completed successfully.");
      ICQBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft, 0);
      // Release structure
      SafeReleaseOscarTransfer(ft);
    }
    else
    { // ack received file
      sendOFT2FramePacket(oc, OFT_TYPE_DONE);
      oc->status = OCS_NEGOTIATION;
    }

  }
  return bytesUsed;
}



static void handleOFT2FramePacket(oscar_connection *oc, WORD datatype, BYTE *pBuffer, WORD wLen)
{
  DWORD dwID1;
  DWORD dwID2;

  if (wLen < 248)
  {
    NetLog_Direct("Error: Malformed OFT2 Frame, ignoring.");
    return;
  }

  unpackLEDWord(&pBuffer, &dwID1);
  wLen -= 4;
  unpackLEDWord(&pBuffer, &dwID2);
  wLen -= 4;

  if (datatype == OFT_TYPE_REQUEST && !oc->ft->initalized)
  { // first request does not contain MsgIDs we need to send them in ready packet
    dwID1 = oc->ft->pMessage.dwMsgID1;
    dwID2 = oc->ft->pMessage.dwMsgID2;
  }

  if (oc->ft->pMessage.dwMsgID1 != dwID1 || oc->ft->pMessage.dwMsgID2 != dwID2)
  { // this is not the right packet - bad Message IDs
    NetLog_Direct("Error: Invalid Packet Cookie, closing.");
    CloseOscarConnection(oc);
    return;
  }

  switch (datatype)
  {
  case OFT_TYPE_REQUEST:
    { // Sender ready
      oscar_filetransfer *ft = oc->ft;
      char *szFullPath;

      // Read Frame data
      if (!ft->initalized)
      {
        unpackWord(&pBuffer, &ft->wEncrypt);
        unpackWord(&pBuffer, &ft->wCompress);
        unpackWord(&pBuffer, &ft->wFilesCount);
      }
      else
        pBuffer += 6;
      unpackWord(&pBuffer, &ft->wFilesLeft);
      ft->iCurrentFile = ft->wFilesCount - oc->ft->wFilesLeft;
      if (!ft->initalized)
        unpackWord(&pBuffer, &ft->wPartsCount);
      else
        pBuffer += 2;
      unpackWord(&pBuffer, &ft->wPartsLeft);
      if (!ft->initalized)
        unpackDWord(&pBuffer, &ft->dwTotalSize);
      else
        pBuffer += 4;
      unpackDWord(&pBuffer, &ft->dwThisFileSize);
      unpackDWord(&pBuffer, &ft->dwThisFileDate);
      unpackDWord(&pBuffer, &ft->dwThisFileCheck);
      unpackDWord(&pBuffer, &ft->dwRecvForkCheck);
      unpackDWord(&pBuffer, &ft->dwThisForkSize);
      unpackDWord(&pBuffer, &ft->dwThisFileCreation);
      unpackDWord(&pBuffer, &ft->dwThisForkCheck);
      unpackDWord(&pBuffer, &ft->dwBytesDone);
      unpackDWord(&pBuffer, &ft->dwRecvFileCheck);
      if (!ft->initalized)
        unpackString(&pBuffer, ft->rawIDString, 32);
      else
        pBuffer += 32;
      unpackByte(&pBuffer, &ft->bHeaderFlags);
      unpackByte(&pBuffer, &ft->bNameOff);
      unpackByte(&pBuffer, &ft->bSizeOff);
      if (!ft->initalized)
      {
        unpackString(&pBuffer, ft->rawDummy, 69);
        unpackString(&pBuffer, ft->rawMacInfo, 16);
      }
      else
        pBuffer += 85;
      unpackWord(&pBuffer, &ft->wEncoding);
      unpackWord(&pBuffer, &ft->wSubEncoding);
      ft->cbRawFileName = wLen - 184;
      SAFE_FREE(&ft->rawFileName); // release previous buffers
      SAFE_FREE(&ft->szThisFile);
      ft->rawFileName = (char*)SAFE_MALLOC(ft->cbRawFileName);
      unpackString(&pBuffer, ft->rawFileName, ft->cbRawFileName);
      // Prepare file
      if (ft->wEncoding == 2)
      { // FIX ME: this is bad
        ft->szThisFile = ApplyEncoding(ft->rawFileName, "unicode-2-0");
        // FIX ME: need to convert dir markings
      }
      else
      {
        DWORD i;

        ft->szThisFile = null_strdup(ft->rawFileName);

        for (i = 0; i < strlennull(ft->szThisFile); i++)
        { // convert dir markings to normal backslashes
          if (ft->szThisFile[i] == 0x01) ft->szThisFile[i] = '\\';
        }
      }

      ft->initalized = 1; // First Frame Processed

      NetLog_Direct("File '%s', %d Bytes", ft->szThisFile, ft->dwThisFileSize);

      { // Prepare Path Information
        char* szFile = strrchr(ft->szThisFile, '\\');

        SAFE_FREE(&ft->szThisSubdir); // release previous path
        if (szFile)
        {
          char *szNewDir;

          ft->szThisSubdir = ft->szThisFile;
          szFile[0] = '\0'; // split that strings
          ft->szThisFile = null_strdup(szFile + 1);
          // no cheating with paths
          if (strstr(ft->szThisSubdir, "..\\") || strstr(ft->szThisSubdir, "../") ||
              strstr(ft->szThisSubdir, ":\\") || strstr(ft->szThisSubdir, ":/") ||
              ft->szThisSubdir[0] == '\\' || ft->szThisSubdir[0] == '/')
          {
            NetLog_Direct("Invalid path information");
            break;
          }
          szNewDir = (char*)_alloca(strlennull(ft->szSavePath)+strlennull(ft->szThisSubdir)+2);
          strcpy(szNewDir, ft->szSavePath);
          NormalizeBackslash(szNewDir);
          strcat(szNewDir, ft->szThisSubdir);
          _mkdir(szNewDir); // FIX ME: this will fail for multi sub-sub-sub-dirs at once
        }
        else
          ft->szThisSubdir = null_strdup(""); 
      }

      /* no cheating with paths */
      if (strstr(ft->szThisFile, "..\\") || strstr(ft->szThisFile, "../") ||
        strstr(ft->szThisFile, ":\\") || strstr(ft->szThisFile, ":/") ||
        ft->szThisFile[0] == '\\' || ft->szThisFile[0] == '/')
      {
        NetLog_Direct("Invalid path information");
        break;
      }
      szFullPath = (char*)SAFE_MALLOC(strlennull(ft->szSavePath)+strlennull(ft->szThisSubdir)+strlennull(ft->szThisFile)+3);
      strcpy(szFullPath, ft->szSavePath);
      NormalizeBackslash(szFullPath);
      strcat(szFullPath, ft->szThisSubdir);
      NormalizeBackslash(szFullPath);
      _chdir(szFullPath); // set current dir - not very useful
      strcat(szFullPath, ft->szThisFile);
      // we joined the full path to dest file
      SAFE_FREE(&ft->szThisFile);
      ft->szThisFile = szFullPath;

      ft->dwFileBytesDone = 0;

      {
        /* file resume */
        PROTOFILETRANSFERSTATUS pfts = {0};

        oft_buildProtoFileTransferStatus(ft, &pfts);
        if (ICQBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, ft, (LPARAM)&pfts))
          break; /* UI supports resume: it will call PS_FILERESUME */

        // TODO: Unicode support
        ft->fileId = _open(ft->szThisFile, _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE);
        if (ft->fileId == -1)
        { // FIX ME: this needs some UI interaction
          icq_LogMessage(LOG_ERROR, "Your file receive has been aborted because Miranda could not open the destination file in order to write to it. You may be trying to save to a read-only folder.");
          CloseOscarConnection(oc);
          return;
        }
      }
      // Send "we are ready"
      oc->status = OCS_DATA;

      sendOFT2FramePacket(oc, OFT_TYPE_READY);
      ICQBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0);
      if (!ft->dwThisFileSize)
      { // if the file is empty we will not receive any data
        BYTE buf;

        oft_handleFileData(oc, &buf, 0);
      }
      return;
    }
  }
}


//
// Direct packets
/////////////////////////////

static void oft_sendPeerInit(oscar_connection *oc)
{
  // prepare init frame

  // sendOFT2FramePacket(oc, OFT_TYPE_REQUEST);
}



static void sendOFT2FramePacket(oscar_connection *oc, WORD datatype)
{
  oscar_filetransfer *ft = oc->ft;
  icq_packet packet;
  
  packet.wLen = 192 + ft->cbRawFileName;
  init_generic_packet(&packet, 0);
  // Basic Oscar Frame
  packDWord(&packet, 0x4F465432); // Magic
  packWord(&packet, packet.wLen);
  packWord(&packet, datatype);
  // Cookie
  packLEDWord(&packet, ft->pMessage.dwMsgID1);
  packLEDWord(&packet, ft->pMessage.dwMsgID2);
  packWord(&packet, ft->wEncrypt);
  packWord(&packet, ft->wCompress);
  packWord(&packet, ft->wFilesCount);
  packWord(&packet, ft->wFilesLeft);
  packWord(&packet, ft->wPartsCount);
  packWord(&packet, ft->wPartsLeft);
  packDWord(&packet, ft->dwTotalSize);
  packDWord(&packet, ft->dwThisFileSize);
  packDWord(&packet, ft->dwThisFileDate);
  packDWord(&packet, ft->dwThisFileCheck);
  packDWord(&packet, ft->dwRecvForkCheck);
  packDWord(&packet, ft->dwThisForkSize);
  packDWord(&packet, ft->dwThisFileCreation);
  packDWord(&packet, ft->dwThisForkCheck);
  packDWord(&packet, ft->dwBytesDone);
  packDWord(&packet, ft->dwRecvFileCheck);
  packBuffer(&packet, ft->rawIDString, 32);
  packByte(&packet, ft->bHeaderFlags);
  packByte(&packet, ft->bNameOff);
  packByte(&packet, ft->bSizeOff);
  packBuffer(&packet, ft->rawDummy, 69);
  packBuffer(&packet, ft->rawMacInfo, 16);
  packWord(&packet, ft->wEncoding);
  packWord(&packet, ft->wSubEncoding);
  packBuffer(&packet, ft->rawFileName, ft->cbRawFileName);

  sendOscarPacket(oc, &packet);
}