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