Mercurial > pidgin
annotate plugins/icq/filesession.c @ 1840:00aef397a1fe
[gaim-migrate @ 1850]
reworked some of the proxy stuff so that it's non-blocking now. next thing to do is to get IRC, MSN, Napster, and Jabber to use the new proxy_connect code. After that, Oscar and Yahoo (maybe Zephyr too? not likely)
committer: Tailor Script <tailor@pidgin.im>
| author | Eric Warmenhoven <eric@warmenhoven.org> |
|---|---|
| date | Sat, 12 May 2001 01:38:04 +0000 |
| parents | e06dcc3025a9 |
| children | 8ed70631ed15 |
| rev | line source |
|---|---|
| 1152 | 1 #include <stdlib.h> |
| 2 #include <fcntl.h> | |
| 3 #include <stdio.h> | |
| 4 #include <string.h> | |
| 5 #include <sys/stat.h> | |
| 6 | |
| 7 #ifdef _MSVC_ | |
| 8 #include <io.h> | |
| 9 #define open _open | |
| 10 #define close _close | |
| 11 #define read _read | |
| 12 #define write _write | |
| 13 #endif | |
| 14 | |
|
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
15 #include "icqlib.h" |
| 1152 | 16 #include "filesession.h" |
| 17 #include "list.h" | |
| 18 #include "icqpacket.h" | |
| 19 #include "stdpackets.h" | |
| 20 | |
| 21 icq_FileSession *icq_FileSessionNew(ICQLINK *icqlink) | |
| 22 { | |
| 23 icq_FileSession *p=(icq_FileSession *)malloc(sizeof(icq_FileSession)); | |
| 24 | |
| 25 if (p) | |
| 26 { | |
| 27 p->status=0; | |
| 28 p->id=0L; | |
| 29 p->icqlink=icqlink; | |
|
1498
0ef6603d986e
[gaim-migrate @ 1508]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1432
diff
changeset
|
30 p->tcplink=NULL; |
| 1152 | 31 p->current_fd=-1; |
| 32 p->current_file_num=0; | |
| 33 p->current_file_progress=0; | |
| 34 p->current_file_size=0; | |
| 35 p->files=0L; | |
| 36 p->current_speed=100; | |
| 37 p->total_bytes=0; | |
| 38 p->total_files=0; | |
| 39 p->total_transferred_bytes=0; | |
| 40 p->working_dir[0]=0; | |
|
1531
e06dcc3025a9
[gaim-migrate @ 1541]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1498
diff
changeset
|
41 p->user_data=NULL; |
|
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
42 list_insert(icqlink->d->icq_FileSessions, 0, p); |
| 1152 | 43 } |
| 44 | |
| 45 return p; | |
| 46 } | |
| 47 | |
| 48 void icq_FileSessionDelete(void *pv) | |
| 49 { | |
| 50 icq_FileSession *p=(icq_FileSession *)pv; | |
| 51 | |
|
1432
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
52 invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_CLOSE, 0, |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
53 NULL); |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
54 |
| 1152 | 55 if(p->files) { |
| 56 char **p2=p->files; | |
| 57 while(*p2) | |
| 58 free(*(p2++)); | |
| 59 free(p->files); | |
| 60 } | |
| 61 | |
| 62 if (p->current_fd > -1 ) { | |
| 63 close(p->current_fd); | |
| 64 p->current_fd=-1; | |
| 65 } | |
| 66 | |
| 67 free(p); | |
| 68 } | |
| 69 | |
| 70 int _icq_FindFileSession(void *p, va_list data) | |
| 71 { | |
| 72 icq_FileSession *psession=(icq_FileSession *)p; | |
| 73 DWORD uin=va_arg(data, DWORD); | |
| 74 unsigned long id=va_arg(data, unsigned long); | |
| 75 | |
| 76 return (psession->remote_uin == uin) && ( id ? (psession->id == id) : 1 ); | |
| 77 | |
| 78 } | |
| 79 | |
| 80 icq_FileSession *icq_FindFileSession(ICQLINK *icqlink, DWORD uin, | |
| 81 unsigned long id) | |
| 82 { | |
|
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
83 return list_traverse(icqlink->d->icq_FileSessions, _icq_FindFileSession, |
| 1152 | 84 uin, id); |
| 85 } | |
| 86 | |
| 87 void icq_FileSessionSetStatus(icq_FileSession *p, int status) | |
| 88 { | |
| 89 if(status!=p->status) | |
| 90 { | |
| 91 p->status=status; | |
|
1432
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
92 if(p->id) |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
93 invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_STATUS, |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
94 status, NULL); |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
95 if (status == FILE_STATUS_SENDING) |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
96 icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE, |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
97 icq_FileSessionSendData, p); |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
98 else |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
99 icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE, NULL, NULL); |
| 1152 | 100 } |
| 101 } | |
| 102 | |
| 103 void icq_FileSessionSetHandle(icq_FileSession *p, const char *handle) | |
| 104 { | |
| 105 strncpy(p->remote_handle, handle, 64); | |
| 106 } | |
| 107 | |
| 108 void icq_FileSessionSetCurrentFile(icq_FileSession *p, const char *filename) | |
| 109 { | |
| 110 struct stat file_status; | |
| 111 char file[1024]; | |
| 112 | |
| 113 strcpy(file, p->working_dir); | |
| 114 strcat(file, filename); | |
| 115 | |
| 116 if (p->current_fd>-1) { | |
| 117 close(p->current_fd); | |
| 118 p->current_fd=-1; | |
| 119 } | |
| 120 | |
| 121 strncpy(p->current_file, file, 64); | |
| 122 p->current_file_progress=0; | |
| 123 | |
| 124 /* does the file already exist? */ | |
| 125 if (stat(file, &file_status)==0) { | |
| 126 p->current_file_progress=file_status.st_size; | |
| 127 p->total_transferred_bytes+=file_status.st_size; | |
| 128 p->current_fd=open(file, O_WRONLY | O_APPEND); | |
| 129 } else { | |
| 130 #ifdef _WIN32 | |
| 131 p->current_fd=open(file, O_WRONLY | O_CREAT); | |
| 132 #else | |
| 133 p->current_fd=open(file, O_WRONLY | O_CREAT, S_IRWXU); | |
| 134 #endif | |
| 135 } | |
| 136 | |
| 137 /* make sure we have a valid filehandle */ | |
| 138 if (p->current_fd == -1) | |
| 139 perror("couldn't open file: "); | |
| 140 | |
| 141 } | |
| 142 | |
| 143 void icq_FileSessionPrepareNextFile(icq_FileSession *p) | |
| 144 { | |
| 145 int i=0; | |
| 146 char **files=p->files; | |
| 147 | |
| 148 p->current_file_num++; | |
| 149 | |
| 150 while(*files) { | |
| 151 i++; | |
| 152 if(i==p->current_file_num) | |
| 153 break; | |
| 154 else | |
| 155 files++; | |
| 156 } | |
| 157 | |
| 158 if(*files) { | |
| 159 struct stat file_status; | |
| 160 | |
| 161 if (p->current_fd>-1) { | |
| 162 close(p->current_fd); | |
| 163 p->current_fd=-1; | |
| 164 } | |
| 165 | |
| 166 if (stat(*files, &file_status)==0) { | |
| 167 char *basename=*files; | |
| 168 char *pos=strrchr(basename, '/'); | |
| 169 if(pos) basename=pos+1; | |
| 170 strncpy(p->current_file, basename, 64); | |
| 171 p->current_file_progress=0; | |
| 172 p->current_file_size=file_status.st_size; | |
| 173 p->current_fd=open(*files, O_RDONLY); | |
| 174 } | |
| 175 | |
| 176 /* make sure we have a valid filehandle */ | |
| 177 if (p->current_fd == -1) | |
| 178 perror("couldn't open file: "); | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 void icq_FileSessionSendData(icq_FileSession *p) | |
| 183 { | |
| 184 /* for now just send a packet at a time */ | |
| 185 char buffer[2048]; | |
| 186 int count=read(p->current_fd, buffer, 2048); | |
| 187 | |
| 188 if(count>0) { | |
| 189 icq_Packet *p2=icq_TCPCreateFile06Packet(count, buffer); | |
| 190 icq_TCPLinkSend(p->tcplink, p2); | |
| 191 p->total_transferred_bytes+=count; | |
| 192 p->current_file_progress+=count; | |
| 193 icq_FileSessionSetStatus(p, FILE_STATUS_SENDING); | |
|
1432
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
194 |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
195 invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_DATAPACKET, |
|
4c510ca3563f
[gaim-migrate @ 1442]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1309
diff
changeset
|
196 count, buffer); |
| 1152 | 197 } |
| 198 | |
| 199 /* done transmitting if read returns less that 2048 bytes */ | |
| 200 if(count<2048) | |
| 201 icq_FileSessionClose(p); | |
| 202 | |
| 203 return; | |
| 204 } | |
| 205 | |
| 206 /* public */ | |
| 207 | |
| 208 void icq_FileSessionSetSpeed(icq_FileSession *p, int speed) | |
| 209 { | |
| 210 icq_Packet *packet=icq_TCPCreateFile05Packet(speed); | |
| 211 | |
| 212 icq_TCPLinkSend(p->tcplink, packet); | |
| 213 } | |
| 214 | |
| 215 void icq_FileSessionClose(icq_FileSession *p) | |
| 216 { | |
| 217 icq_TCPLink *plink=p->tcplink; | |
| 218 | |
| 219 /* TODO: handle closing already unallocated filesession? */ | |
| 220 | |
| 221 /* if we're attached to a tcplink, unattach so the link doesn't try | |
| 222 * to close us, and then close the tcplink */ | |
| 223 if (plink) | |
| 224 { | |
| 225 plink->session=0L; | |
| 226 icq_TCPLinkClose(plink); | |
| 227 } | |
| 228 | |
| 229 icq_FileSessionDelete(p); | |
| 230 | |
|
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
231 list_remove(p->icqlink->d->icq_FileSessions, p); |
| 1152 | 232 } |
| 233 | |
| 234 void icq_FileSessionSetWorkingDir(icq_FileSession *p, const char *dir) | |
| 235 { | |
| 236 strncpy(p->working_dir, dir, 512); | |
| 237 } | |
| 238 | |
| 239 void icq_FileSessionSetFiles(icq_FileSession *p, char **files) | |
| 240 { | |
| 241 p->files=files; | |
| 242 } | |
| 243 |
