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