Tue, 29 May 2001 10:32:53 +0000
[gaim-migrate @ 1922]
new icqlib
| 1152 | 1 | /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | ||
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
3 | /* |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
4 | * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
5 | * Bill Soudan <soudan@kde.org> |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
6 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
7 | * This program is free software; you can redistribute it and/or modify |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
8 | * it under the terms of the GNU General Public License as published by |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
9 | * the Free Software Foundation; either version 2 of the License, or |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
10 | * (at your option) any later version. |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
11 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
12 | * This program is distributed in the hope that it will be useful, |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
15 | * GNU General Public License for more details. |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
16 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
17 | * You should have received a copy of the GNU General Public License |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
18 | * along with this program; if not, write to the Free Software |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
20 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
21 | */ |
| 1152 | 22 | |
| 23 | #ifdef _MSVC_ | |
| 24 | #include <io.h> | |
| 25 | #define open _open | |
| 26 | #define close _close | |
| 27 | #define read _read | |
| 28 | #define write _write | |
| 29 | #endif | |
| 30 | ||
| 31 | #include <errno.h> | |
| 32 | ||
| 33 | #include "icqlib.h" | |
| 34 | ||
| 35 | #include "tcp.h" | |
| 36 | #include "stdpackets.h" | |
| 37 | #include "filesession.h" | |
| 38 | ||
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
39 | void icq_TCPOnFileReqReceived(icq_Link *icqlink, DWORD uin, const char *message, |
| 1152 | 40 | const char *filename, unsigned long filesize, DWORD id) |
| 41 | { | |
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
42 | /* use the current system time for time received */ |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
43 | time_t t=time(0); |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
44 | struct tm *ptime=localtime(&t); |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
45 | |
| 1152 | 46 | #ifdef TCP_PACKET_TRACE |
| 47 | printf("file request packet received from %lu { sequence=%lx, message=%s }\n", | |
| 48 | uin, id, message); | |
| 49 | #endif | |
| 50 | ||
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
51 | invoke_callback(icqlink,icq_RecvFileReq)(icqlink, uin, ptime->tm_hour, |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
52 | ptime->tm_min, ptime->tm_mday, ptime->tm_mon+1, ptime->tm_year+1900, |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
53 | message, filename, filesize, id); |
| 1152 | 54 | |
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
55 | /* don't send an acknowledgement to the remote client! |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
56 | * GUI is responsible for sending acknowledgement once user accepts |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
57 | * or denies using icq_TCPSendFileAck */ |
| 1152 | 58 | } |
| 59 | ||
| 60 | void icq_TCPProcessFilePacket(icq_Packet *p, icq_TCPLink *plink) | |
| 61 | { | |
| 62 | icq_FileSession *psession=(icq_FileSession *)plink->session; | |
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
63 | icq_Link *icqlink = plink->icqlink; |
| 1152 | 64 | BYTE type; |
| 65 | DWORD num_files; | |
| 66 | DWORD total_bytes; | |
| 67 | DWORD speed; | |
| 68 | DWORD filesize; | |
| 69 | const char *name; | |
| 70 | int result; | |
| 71 | ||
| 72 | icq_Packet *presponse; | |
| 73 | ||
| 74 | icq_PacketBegin(p); | |
| 75 | ||
| 76 | type=icq_PacketRead8(p); | |
| 77 | ||
| 78 | switch(type) | |
| 79 | { | |
| 80 | case 0x00: | |
| 81 | (void)icq_PacketRead32(p); | |
| 82 | num_files=icq_PacketRead32(p); | |
| 83 | total_bytes=icq_PacketRead32(p); | |
| 84 | speed=icq_PacketRead32(p); | |
| 85 | name=icq_PacketReadString(p); | |
| 86 | psession->total_files=num_files; | |
| 87 | psession->total_bytes=total_bytes; | |
| 88 | psession->current_speed=speed; | |
| 89 | icq_FileSessionSetHandle(psession, name); | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
90 | icq_FileSessionSetStatus(psession, FILE_STATUS_INITIALIZING); |
| 1152 | 91 | |
| 92 | /* respond */ | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
93 | presponse=icq_TCPCreateFile01Packet(speed, icqlink->icq_Nick); |
| 1152 | 94 | |
| 95 | icq_TCPLinkSend(plink, presponse); | |
| 96 | #ifdef TCP_PACKET_TRACE | |
| 97 | printf("file 01 packet sent to uin %lu\n", plink->remote_uin); | |
| 98 | #endif | |
| 99 | ||
| 100 | break; | |
| 101 | ||
| 102 | case 0x01: | |
| 103 | speed=icq_PacketRead32(p); | |
| 104 | name=icq_PacketReadString(p); | |
| 105 | psession->current_speed=speed; | |
| 106 | icq_FileSessionSetHandle(psession, name); | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
107 | icq_FileSessionSetStatus(psession, FILE_STATUS_INITIALIZING); |
| 1152 | 108 | |
| 109 | /* respond */ | |
| 110 | icq_FileSessionPrepareNextFile(psession); | |
| 111 | presponse=icq_TCPCreateFile02Packet(psession->current_file, | |
| 112 | psession->current_file_size, psession->current_speed); | |
| 113 | ||
| 114 | icq_TCPLinkSend(plink, presponse); | |
| 115 | #ifdef TCP_PACKET_TRACE | |
| 116 | printf("file 02 packet sent to uin %lu\n", plink->remote_uin); | |
| 117 | #endif | |
| 118 | break; | |
| 119 | ||
| 120 | case 0x02: | |
| 121 | /* when files are skipped | |
| 122 | psession->total_transferred_bytes+= | |
| 123 | (psession->current_file_size-psession->current_file_progress); | |
| 124 | */ | |
| 125 | ||
| 126 | (void)icq_PacketRead8(p); | |
| 127 | name=icq_PacketReadString(p); | |
| 128 | (void)icq_PacketReadString(p); | |
| 129 | filesize=icq_PacketRead32(p); | |
| 130 | (void)icq_PacketRead32(p); | |
| 131 | speed=icq_PacketRead32(p); | |
| 132 | icq_FileSessionSetCurrentFile(psession, name); | |
| 133 | psession->current_file_size=filesize; | |
| 134 | psession->current_speed=speed; | |
| 135 | psession->current_file_num++; | |
| 136 | icq_FileSessionSetStatus(psession, FILE_STATUS_NEXT_FILE); | |
| 137 | ||
| 138 | /* respond */ | |
| 139 | presponse=icq_TCPCreateFile03Packet(psession->current_file_progress, | |
| 140 | speed); | |
| 141 | ||
| 142 | icq_TCPLinkSend(plink, presponse); | |
| 143 | #ifdef TCP_PACKET_TRACE | |
| 144 | printf("file 03 packet sent to uin %lu\n", plink->remote_uin); | |
| 145 | #endif | |
| 146 | break; | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
147 | |
| 1152 | 148 | case 0x03: |
| 149 | filesize=icq_PacketRead32(p); | |
| 150 | (void)icq_PacketRead32(p); | |
| 151 | speed=icq_PacketRead32(p); | |
| 152 | psession->current_file_progress=filesize; | |
| 153 | psession->total_transferred_bytes+=filesize; | |
| 154 | psession->current_speed=speed; | |
| 155 | ||
| 156 | icq_FileSessionSetStatus(psession, FILE_STATUS_NEXT_FILE); | |
| 157 | icq_FileSessionSetStatus(psession, FILE_STATUS_SENDING); | |
| 158 | break; | |
| 159 | ||
| 160 | case 0x04: | |
| 161 | (void)icq_PacketRead32(p); | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
162 | invoke_callback(icqlink, icq_FileNotify)(psession, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
163 | FILE_NOTIFY_STOP_FILE, 0, NULL); |
| 1152 | 164 | break; |
| 165 | ||
| 166 | case 0x05: | |
| 167 | speed=icq_PacketRead32(p); | |
| 168 | psession->current_speed=speed; | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
169 | invoke_callback(icqlink, icq_FileNotify)(psession, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
170 | FILE_NOTIFY_NEW_SPEED, speed, NULL); |
| 1152 | 171 | break; |
| 172 | ||
| 173 | case 0x06: | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
174 | { |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
175 | void *data = p->data+sizeof(BYTE); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
176 | int length = p->length-sizeof(BYTE); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
177 | |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
178 | invoke_callback(icqlink, icq_FileNotify)(psession, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
179 | FILE_NOTIFY_DATAPACKET, length, data); |
| 1152 | 180 | icq_FileSessionSetStatus(psession, FILE_STATUS_RECEIVING); |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
181 | result=write(psession->current_fd, data, length); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
182 | psession->current_file_progress+=length; |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
183 | psession->total_transferred_bytes+=length; |
| 1152 | 184 | break; |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
185 | } |
| 1152 | 186 | |
| 187 | default: | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
188 | icq_FmtLog(icqlink, ICQ_LOG_WARNING, "unknown file packet type %d!\n", type); |
| 1152 | 189 | |
| 190 | } | |
| 191 | } | |
| 192 | ||
| 193 | void icq_HandleFileHello(icq_TCPLink *plink) | |
| 194 | { | |
| 195 | ||
| 196 | /* once the hello packet has been processed and we know which uin this | |
| 197 | * link is for, we can link up with a file session */ | |
| 198 | icq_FileSession *pfile=icq_FindFileSession(plink->icqlink, | |
| 199 | plink->remote_uin, 0); | |
| 200 | ||
| 201 | if(pfile) | |
| 202 | { | |
| 203 | plink->id=pfile->id; | |
| 204 | plink->session=pfile; | |
| 205 | pfile->tcplink=plink; | |
| 206 | icq_FileSessionSetStatus(pfile, FILE_STATUS_CONNECTED); | |
| 207 | ||
| 208 | } else { | |
| 209 | ||
| 210 | icq_FmtLog(plink->icqlink, ICQ_LOG_WARNING, | |
| 211 | "unexpected file hello received from %d, closing link\n", | |
| 212 | plink->remote_uin); | |
| 213 | icq_TCPLinkClose(plink); | |
| 214 | } | |
| 215 | ||
| 216 | } | |
| 217 | ||
| 218 | void icq_HandleFileAck(icq_TCPLink *plink, icq_Packet *p, int port) | |
| 219 | { | |
| 220 | icq_TCPLink *pfilelink; | |
| 221 | icq_FileSession *pfile; | |
| 222 | icq_Packet *p2; | |
| 223 | ||
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
224 | invoke_callback(plink->icqlink, icq_RequestNotify)(plink->icqlink, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
225 | p->id, ICQ_NOTIFY_ACK, 0, NULL); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
226 | |
| 1152 | 227 | pfilelink=icq_TCPLinkNew(plink->icqlink); |
| 228 | pfilelink->type=TCP_LINK_FILE; | |
| 229 | pfilelink->id=p->id; | |
| 230 | ||
| 231 | /* once the ack packet has been processed, link up with the file session */ | |
| 232 | pfile=icq_FindFileSession(plink->icqlink, plink->remote_uin, 0); | |
| 233 | ||
| 234 | pfile->tcplink=pfilelink; | |
| 235 | pfilelink->id=pfile->id; | |
| 236 | ||
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
237 | invoke_callback(plink->icqlink, icq_RequestNotify)(plink->icqlink, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
238 | pfile->id, ICQ_NOTIFY_FILESESSION, sizeof(icq_FileSession), pfile); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
239 | invoke_callback(plink->icqlink, icq_RequestNotify)(plink->icqlink, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
240 | pfile->id, ICQ_NOTIFY_SUCCESS, 0, NULL); |
| 1152 | 241 | |
| 242 | icq_FileSessionSetStatus(pfile, FILE_STATUS_CONNECTING); | |
| 243 | icq_TCPLinkConnect(pfilelink, plink->remote_uin, port); | |
| 244 | ||
| 245 | pfilelink->session=pfile; | |
| 246 | ||
| 247 | p2=icq_TCPCreateFile00Packet( pfile->total_files, | |
| 248 | pfile->total_bytes, pfile->current_speed, plink->icqlink->icq_Nick); | |
| 249 | icq_TCPLinkSend(pfilelink, p2); | |
| 250 | ||
| 251 | } | |
| 252 |