Thu, 22 Feb 2001 23:07:34 +0000
[gaim-migrate @ 1508]
updating icqlib
| 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
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
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
de75cc6a6d34
[gaim-migrate @ 1508]
Eric Warmenhoven <warmenhoven@yahoo.com>
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; | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
41 | list_insert(icqlink->d->icq_FileSessions, 0, p); |
| 1152 | 42 | } |
| 43 | ||
| 44 | return p; | |
| 45 | } | |
| 46 | ||
| 47 | void icq_FileSessionDelete(void *pv) | |
| 48 | { | |
| 49 | icq_FileSession *p=(icq_FileSession *)pv; | |
| 50 | ||
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
51 | invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_CLOSE, 0, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
52 | NULL); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
53 | |
| 1152 | 54 | if(p->files) { |
| 55 | char **p2=p->files; | |
| 56 | while(*p2) | |
| 57 | free(*(p2++)); | |
| 58 | free(p->files); | |
| 59 | } | |
| 60 | ||
| 61 | if (p->current_fd > -1 ) { | |
| 62 | close(p->current_fd); | |
| 63 | p->current_fd=-1; | |
| 64 | } | |
| 65 | ||
| 66 | free(p); | |
| 67 | } | |
| 68 | ||
| 69 | int _icq_FindFileSession(void *p, va_list data) | |
| 70 | { | |
| 71 | icq_FileSession *psession=(icq_FileSession *)p; | |
| 72 | DWORD uin=va_arg(data, DWORD); | |
| 73 | unsigned long id=va_arg(data, unsigned long); | |
| 74 | ||
| 75 | return (psession->remote_uin == uin) && ( id ? (psession->id == id) : 1 ); | |
| 76 | ||
| 77 | } | |
| 78 | ||
| 79 | icq_FileSession *icq_FindFileSession(ICQLINK *icqlink, DWORD uin, | |
| 80 | unsigned long id) | |
| 81 | { | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
82 | return list_traverse(icqlink->d->icq_FileSessions, _icq_FindFileSession, |
| 1152 | 83 | uin, id); |
| 84 | } | |
| 85 | ||
| 86 | void icq_FileSessionSetStatus(icq_FileSession *p, int status) | |
| 87 | { | |
| 88 | if(status!=p->status) | |
| 89 | { | |
| 90 | p->status=status; | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
91 | if(p->id) |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
92 | invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_STATUS, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
93 | status, NULL); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
94 | if (status == FILE_STATUS_SENDING) |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
95 | icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
96 | icq_FileSessionSendData, p); |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
97 | else |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
98 | icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE, NULL, NULL); |
| 1152 | 99 | } |
| 100 | } | |
| 101 | ||
| 102 | void icq_FileSessionSetHandle(icq_FileSession *p, const char *handle) | |
| 103 | { | |
| 104 | strncpy(p->remote_handle, handle, 64); | |
| 105 | } | |
| 106 | ||
| 107 | void icq_FileSessionSetCurrentFile(icq_FileSession *p, const char *filename) | |
| 108 | { | |
| 109 | struct stat file_status; | |
| 110 | char file[1024]; | |
| 111 | ||
| 112 | strcpy(file, p->working_dir); | |
| 113 | strcat(file, filename); | |
| 114 | ||
| 115 | if (p->current_fd>-1) { | |
| 116 | close(p->current_fd); | |
| 117 | p->current_fd=-1; | |
| 118 | } | |
| 119 | ||
| 120 | strncpy(p->current_file, file, 64); | |
| 121 | p->current_file_progress=0; | |
| 122 | ||
| 123 | /* does the file already exist? */ | |
| 124 | if (stat(file, &file_status)==0) { | |
| 125 | p->current_file_progress=file_status.st_size; | |
| 126 | p->total_transferred_bytes+=file_status.st_size; | |
| 127 | p->current_fd=open(file, O_WRONLY | O_APPEND); | |
| 128 | } else { | |
| 129 | #ifdef _WIN32 | |
| 130 | p->current_fd=open(file, O_WRONLY | O_CREAT); | |
| 131 | #else | |
| 132 | p->current_fd=open(file, O_WRONLY | O_CREAT, S_IRWXU); | |
| 133 | #endif | |
| 134 | } | |
| 135 | ||
| 136 | /* make sure we have a valid filehandle */ | |
| 137 | if (p->current_fd == -1) | |
| 138 | perror("couldn't open file: "); | |
| 139 | ||
| 140 | } | |
| 141 | ||
| 142 | void icq_FileSessionPrepareNextFile(icq_FileSession *p) | |
| 143 | { | |
| 144 | int i=0; | |
| 145 | char **files=p->files; | |
| 146 | ||
| 147 | p->current_file_num++; | |
| 148 | ||
| 149 | while(*files) { | |
| 150 | i++; | |
| 151 | if(i==p->current_file_num) | |
| 152 | break; | |
| 153 | else | |
| 154 | files++; | |
| 155 | } | |
| 156 | ||
| 157 | if(*files) { | |
| 158 | struct stat file_status; | |
| 159 | ||
| 160 | if (p->current_fd>-1) { | |
| 161 | close(p->current_fd); | |
| 162 | p->current_fd=-1; | |
| 163 | } | |
| 164 | ||
| 165 | if (stat(*files, &file_status)==0) { | |
| 166 | char *basename=*files; | |
| 167 | char *pos=strrchr(basename, '/'); | |
| 168 | if(pos) basename=pos+1; | |
| 169 | strncpy(p->current_file, basename, 64); | |
| 170 | p->current_file_progress=0; | |
| 171 | p->current_file_size=file_status.st_size; | |
| 172 | p->current_fd=open(*files, O_RDONLY); | |
| 173 | } | |
| 174 | ||
| 175 | /* make sure we have a valid filehandle */ | |
| 176 | if (p->current_fd == -1) | |
| 177 | perror("couldn't open file: "); | |
| 178 | } | |
| 179 | } | |
| 180 | ||
| 181 | void icq_FileSessionSendData(icq_FileSession *p) | |
| 182 | { | |
| 183 | /* for now just send a packet at a time */ | |
| 184 | char buffer[2048]; | |
| 185 | int count=read(p->current_fd, buffer, 2048); | |
| 186 | ||
| 187 | if(count>0) { | |
| 188 | icq_Packet *p2=icq_TCPCreateFile06Packet(count, buffer); | |
| 189 | icq_TCPLinkSend(p->tcplink, p2); | |
| 190 | p->total_transferred_bytes+=count; | |
| 191 | p->current_file_progress+=count; | |
| 192 | icq_FileSessionSetStatus(p, FILE_STATUS_SENDING); | |
|
1432
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
193 | |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
194 | invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_DATAPACKET, |
|
ab10a52f94a7
[gaim-migrate @ 1442]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1309
diff
changeset
|
195 | count, buffer); |
| 1152 | 196 | } |
| 197 | ||
| 198 | /* done transmitting if read returns less that 2048 bytes */ | |
| 199 | if(count<2048) | |
| 200 | icq_FileSessionClose(p); | |
| 201 | ||
| 202 | return; | |
| 203 | } | |
| 204 | ||
| 205 | /* public */ | |
| 206 | ||
| 207 | void icq_FileSessionSetSpeed(icq_FileSession *p, int speed) | |
| 208 | { | |
| 209 | icq_Packet *packet=icq_TCPCreateFile05Packet(speed); | |
| 210 | ||
| 211 | icq_TCPLinkSend(p->tcplink, packet); | |
| 212 | } | |
| 213 | ||
| 214 | void icq_FileSessionClose(icq_FileSession *p) | |
| 215 | { | |
| 216 | icq_TCPLink *plink=p->tcplink; | |
| 217 | ||
| 218 | /* TODO: handle closing already unallocated filesession? */ | |
| 219 | ||
| 220 | /* if we're attached to a tcplink, unattach so the link doesn't try | |
| 221 | * to close us, and then close the tcplink */ | |
| 222 | if (plink) | |
| 223 | { | |
| 224 | plink->session=0L; | |
| 225 | icq_TCPLinkClose(plink); | |
| 226 | } | |
| 227 | ||
| 228 | icq_FileSessionDelete(p); | |
| 229 | ||
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
230 | list_remove(p->icqlink->d->icq_FileSessions, p); |
| 1152 | 231 | } |
| 232 | ||
| 233 | void icq_FileSessionSetWorkingDir(icq_FileSession *p, const char *dir) | |
| 234 | { | |
| 235 | strncpy(p->working_dir, dir, 512); | |
| 236 | } | |
| 237 | ||
| 238 | void icq_FileSessionSetFiles(icq_FileSession *p, char **files) | |
| 239 | { | |
| 240 | p->files=files; | |
| 241 | } | |
| 242 |