Tue, 29 May 2001 10:32:53 +0000
[gaim-migrate @ 1922]
new icqlib
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
1 | /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
2 | |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
3 | /* |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
4 | * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
5 | * Bill Soudan <soudan@kde.org> |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
6 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
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:
1531
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:
1531
diff
changeset
|
9 | * the Free Software Foundation; either version 2 of the License, or |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
10 | * (at your option) any later version. |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
11 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
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:
1531
diff
changeset
|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
15 | * GNU General Public License for more details. |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
16 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
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:
1531
diff
changeset
|
18 | * along with this program; if not, write to the Free Software |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
20 | * |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
21 | */ |
|
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
22 | |
| 1152 | 23 | #include <stdlib.h> |
| 24 | #include <fcntl.h> | |
| 25 | #include <sys/stat.h> | |
| 26 | ||
| 27 | #ifdef _MSVC_ | |
| 28 | #include <io.h> | |
| 29 | #define open _open | |
| 30 | #define close _close | |
| 31 | #define read _read | |
| 32 | #define write _write | |
| 33 | #endif | |
| 34 | ||
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
35 | #include "icqlib.h" |
| 1152 | 36 | #include "filesession.h" |
| 37 | #include "stdpackets.h" | |
| 38 | ||
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
39 | icq_FileSession *icq_FileSessionNew(icq_Link *icqlink) |
| 1152 | 40 | { |
| 41 | icq_FileSession *p=(icq_FileSession *)malloc(sizeof(icq_FileSession)); | |
| 42 | ||
| 43 | if (p) | |
| 44 | { | |
| 45 | p->status=0; | |
| 46 | p->id=0L; | |
| 47 | p->icqlink=icqlink; | |
|
1498
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1432
diff
changeset
|
48 | p->tcplink=NULL; |
| 1152 | 49 | p->current_fd=-1; |
| 50 | p->current_file_num=0; | |
| 51 | p->current_file_progress=0; | |
| 52 | p->current_file_size=0; | |
| 53 | p->files=0L; | |
| 54 | p->current_speed=100; | |
| 55 | p->total_bytes=0; | |
| 56 | p->total_files=0; | |
| 57 | p->total_transferred_bytes=0; | |
| 58 | p->working_dir[0]=0; | |
|
1531
a275218f3f4c
[gaim-migrate @ 1541]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1498
diff
changeset
|
59 | p->user_data=NULL; |
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
60 | icq_ListInsert(icqlink->d->icq_FileSessions, 0, p); |
| 1152 | 61 | } |
| 62 | ||
| 63 | return p; | |
| 64 | } | |
| 65 | ||
| 66 | void icq_FileSessionDelete(void *pv) | |
| 67 | { | |
| 68 | icq_FileSession *p=(icq_FileSession *)pv; | |
| 69 | ||
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
70 | invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_CLOSE, 0, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
71 | NULL); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
72 | |
| 1152 | 73 | if(p->files) { |
| 74 | char **p2=p->files; | |
| 75 | while(*p2) | |
| 76 | free(*(p2++)); | |
| 77 | free(p->files); | |
| 78 | } | |
| 79 | ||
| 80 | if (p->current_fd > -1 ) { | |
| 81 | close(p->current_fd); | |
| 82 | p->current_fd=-1; | |
| 83 | } | |
| 84 | ||
| 85 | free(p); | |
| 86 | } | |
| 87 | ||
| 88 | int _icq_FindFileSession(void *p, va_list data) | |
| 89 | { | |
| 90 | icq_FileSession *psession=(icq_FileSession *)p; | |
| 91 | DWORD uin=va_arg(data, DWORD); | |
| 92 | unsigned long id=va_arg(data, unsigned long); | |
| 93 | ||
| 94 | return (psession->remote_uin == uin) && ( id ? (psession->id == id) : 1 ); | |
| 95 | ||
| 96 | } | |
| 97 | ||
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
98 | icq_FileSession *icq_FindFileSession(icq_Link *icqlink, DWORD uin, |
| 1152 | 99 | unsigned long id) |
| 100 | { | |
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
101 | return icq_ListTraverse(icqlink->d->icq_FileSessions, _icq_FindFileSession, |
| 1152 | 102 | uin, id); |
| 103 | } | |
| 104 | ||
| 105 | void icq_FileSessionSetStatus(icq_FileSession *p, int status) | |
| 106 | { | |
| 107 | if(status!=p->status) | |
| 108 | { | |
| 109 | p->status=status; | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
110 | if(p->id) |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
111 | invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_STATUS, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
112 | status, NULL); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
113 | if (status == FILE_STATUS_SENDING) |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
114 | icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
115 | icq_FileSessionSendData, p); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
116 | else |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
117 | icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE, NULL, NULL); |
| 1152 | 118 | } |
| 119 | } | |
| 120 | ||
| 121 | void icq_FileSessionSetHandle(icq_FileSession *p, const char *handle) | |
| 122 | { | |
| 123 | strncpy(p->remote_handle, handle, 64); | |
| 124 | } | |
| 125 | ||
| 126 | void icq_FileSessionSetCurrentFile(icq_FileSession *p, const char *filename) | |
| 127 | { | |
| 128 | struct stat file_status; | |
| 129 | char file[1024]; | |
| 130 | ||
| 131 | strcpy(file, p->working_dir); | |
| 132 | strcat(file, filename); | |
| 133 | ||
| 134 | if (p->current_fd>-1) { | |
| 135 | close(p->current_fd); | |
| 136 | p->current_fd=-1; | |
| 137 | } | |
| 138 | ||
| 139 | strncpy(p->current_file, file, 64); | |
| 140 | p->current_file_progress=0; | |
| 141 | ||
| 142 | /* does the file already exist? */ | |
| 143 | if (stat(file, &file_status)==0) { | |
| 144 | p->current_file_progress=file_status.st_size; | |
| 145 | p->total_transferred_bytes+=file_status.st_size; | |
| 146 | p->current_fd=open(file, O_WRONLY | O_APPEND); | |
| 147 | } else { | |
| 148 | #ifdef _WIN32 | |
| 149 | p->current_fd=open(file, O_WRONLY | O_CREAT); | |
| 150 | #else | |
| 151 | p->current_fd=open(file, O_WRONLY | O_CREAT, S_IRWXU); | |
| 152 | #endif | |
| 153 | } | |
| 154 | ||
| 155 | /* make sure we have a valid filehandle */ | |
| 156 | if (p->current_fd == -1) | |
| 157 | perror("couldn't open file: "); | |
| 158 | ||
| 159 | } | |
| 160 | ||
| 161 | void icq_FileSessionPrepareNextFile(icq_FileSession *p) | |
| 162 | { | |
| 163 | int i=0; | |
| 164 | char **files=p->files; | |
| 165 | ||
| 166 | p->current_file_num++; | |
| 167 | ||
| 168 | while(*files) { | |
| 169 | i++; | |
| 170 | if(i==p->current_file_num) | |
| 171 | break; | |
| 172 | else | |
| 173 | files++; | |
| 174 | } | |
| 175 | ||
| 176 | if(*files) { | |
| 177 | struct stat file_status; | |
| 178 | ||
| 179 | if (p->current_fd>-1) { | |
| 180 | close(p->current_fd); | |
| 181 | p->current_fd=-1; | |
| 182 | } | |
| 183 | ||
| 184 | if (stat(*files, &file_status)==0) { | |
| 185 | char *basename=*files; | |
| 186 | char *pos=strrchr(basename, '/'); | |
| 187 | if(pos) basename=pos+1; | |
| 188 | strncpy(p->current_file, basename, 64); | |
| 189 | p->current_file_progress=0; | |
| 190 | p->current_file_size=file_status.st_size; | |
| 191 | p->current_fd=open(*files, O_RDONLY); | |
| 192 | } | |
| 193 | ||
| 194 | /* make sure we have a valid filehandle */ | |
| 195 | if (p->current_fd == -1) | |
| 196 | perror("couldn't open file: "); | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 200 | void icq_FileSessionSendData(icq_FileSession *p) | |
| 201 | { | |
| 202 | /* for now just send a packet at a time */ | |
| 203 | char buffer[2048]; | |
| 204 | int count=read(p->current_fd, buffer, 2048); | |
| 205 | ||
| 206 | if(count>0) { | |
| 207 | icq_Packet *p2=icq_TCPCreateFile06Packet(count, buffer); | |
| 208 | icq_TCPLinkSend(p->tcplink, p2); | |
| 209 | p->total_transferred_bytes+=count; | |
| 210 | p->current_file_progress+=count; | |
| 211 | icq_FileSessionSetStatus(p, FILE_STATUS_SENDING); | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
212 | |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
213 | invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_DATAPACKET, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
214 | count, buffer); |
| 1152 | 215 | } |
| 216 | ||
| 217 | /* done transmitting if read returns less that 2048 bytes */ | |
| 218 | if(count<2048) | |
| 219 | icq_FileSessionClose(p); | |
| 220 | ||
| 221 | return; | |
| 222 | } | |
| 223 | ||
| 224 | /* public */ | |
| 225 | ||
| 226 | void icq_FileSessionSetSpeed(icq_FileSession *p, int speed) | |
| 227 | { | |
| 228 | icq_Packet *packet=icq_TCPCreateFile05Packet(speed); | |
| 229 | ||
| 230 | icq_TCPLinkSend(p->tcplink, packet); | |
| 231 | } | |
| 232 | ||
| 233 | void icq_FileSessionClose(icq_FileSession *p) | |
| 234 | { | |
| 235 | icq_TCPLink *plink=p->tcplink; | |
| 236 | ||
| 237 | /* TODO: handle closing already unallocated filesession? */ | |
| 238 | ||
| 239 | /* if we're attached to a tcplink, unattach so the link doesn't try | |
| 240 | * to close us, and then close the tcplink */ | |
| 241 | if (plink) | |
| 242 | { | |
| 243 | plink->session=0L; | |
| 244 | icq_TCPLinkClose(plink); | |
| 245 | } | |
| 246 | ||
| 247 | icq_FileSessionDelete(p); | |
| 248 | ||
|
1912
77d18e17199b
[gaim-migrate @ 1922]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1531
diff
changeset
|
249 | icq_ListRemove(p->icqlink->d->icq_FileSessions, p); |
| 1152 | 250 | } |
| 251 | ||
| 252 | void icq_FileSessionSetWorkingDir(icq_FileSession *p, const char *dir) | |
| 253 | { | |
| 254 | strncpy(p->working_dir, dir, 512); | |
| 255 | } | |
| 256 | ||
| 257 | void icq_FileSessionSetFiles(icq_FileSession *p, char **files) | |
| 258 | { | |
| 259 | p->files=files; | |
| 260 | } | |
| 261 |