|
26 | 26 |
|
27 | 27 | #include <errno.h> |
28 | 28 | #include "src/mod/module.h" |
| 29 | +#include <resolv.h> |
29 | 30 |
|
30 | 31 | #include <netinet/in.h> |
31 | 32 | #include <arpa/inet.h> |
@@ -81,6 +82,7 @@ static void dump_resync(int); |
81 | 82 | static void q_resync(char *, struct chanset_t *); |
82 | 83 | static void cancel_user_xfer(int, void *); |
83 | 84 | static int private_globals_bitmask(); |
| 85 | +static void finish_share(int); |
84 | 86 |
|
85 | 87 | #include "share.h" |
86 | 88 |
|
@@ -1224,6 +1226,49 @@ static void share_userfileq(int idx, char *par) |
1224 | 1226 | } |
1225 | 1227 | } |
1226 | 1228 |
|
| 1229 | +/* New user file share mechanism via multiplex over already existing bot link |
| 1230 | + * ur <len> |
| 1231 | + */ |
| 1232 | +static void share_ufsend2(int idx, char *par) |
| 1233 | +{ |
| 1234 | + int len; |
| 1235 | + unsigned char *buf; |
| 1236 | + ssize_t i; |
| 1237 | + |
| 1238 | + len = strlen(par); |
| 1239 | + debug2("share: share_ufsend2(): start: par = >%s< len = %i", par, len); |
| 1240 | + buf = nmalloc(len); |
| 1241 | + if ((len = b64_pton(par, buf, len)) < 0) { |
| 1242 | + putlog(LOG_BOTS, "*", "Share: share_ufsend2(): error b64_pton()"); |
| 1243 | + return; |
| 1244 | + } |
| 1245 | + debug1("share: share_ufsend2(): len = %i", len); |
| 1246 | + char template[] = "/tmp/shareXXXXXXXX"; |
| 1247 | + int fd; |
| 1248 | + if ((fd = mkstemp(template)) < 0) { |
| 1249 | + putlog(LOG_BOTS, "*", "Share: share_ufsend2(): error mkstemp(): %s", strerror(errno)); |
| 1250 | + return; |
| 1251 | + } |
| 1252 | + if ((i = write(fd, buf, len)) != len) { |
| 1253 | + if (i < 0) |
| 1254 | + putlog(LOG_MISC, "*", "Share error: share_ufsend2(): write(): %s", strerror(errno)); |
| 1255 | + else |
| 1256 | + putlog(LOG_MISC, "*", "Share error: share_ufsend2(): write(): Wrote %ld bytes instead of %i bytes.", (long) i, len); |
| 1257 | + return; |
| 1258 | + } |
| 1259 | + close(fd); |
| 1260 | + dcc[idx].u.xfer->filename = template; |
| 1261 | + /* The new method has got no extra dcc[] for the file transfer, |
| 1262 | + * so temporarely alter the dcc[] we habe to make share_finish() happy |
| 1263 | + */ |
| 1264 | + char host[UHOSTLEN]; |
| 1265 | + strcpy(host, dcc[idx].host); |
| 1266 | + strcpy(dcc[idx].host, dcc[idx].nick); |
| 1267 | + finish_share(idx); |
| 1268 | + strcpy(dcc[idx].host, host); |
| 1269 | + debug0("Share: share_ufsend2(): end"); |
| 1270 | +} |
| 1271 | + |
1227 | 1272 | /* us <ip> <port> <length> |
1228 | 1273 | */ |
1229 | 1274 | static void share_ufsend(int idx, char *par) |
@@ -1428,6 +1473,7 @@ static botscmd_t C_share[] = { |
1428 | 1473 | {"sInv", "psj", (IntFunc) share_stick_invite}, |
1429 | 1474 | {"u?", "", (IntFunc) share_userfileq}, |
1430 | 1475 | {"un", "", (IntFunc) share_ufno}, |
| 1476 | + {"ur", "", (IntFunc) share_ufsend2}, |
1431 | 1477 | {"us", "", (IntFunc) share_ufsend}, |
1432 | 1478 | {"uy", "", (IntFunc) share_ufyes}, |
1433 | 1479 | {"v", "", (IntFunc) share_version}, |
@@ -1860,10 +1906,12 @@ static void finish_share(int idx) |
1860 | 1906 | struct chanset_t *chan; |
1861 | 1907 | int i, j = -1; |
1862 | 1908 |
|
1863 | | - for (i = 0; i < dcc_total; i++) |
| 1909 | + for (i = 0; i < dcc_total; i++) { |
| 1910 | + printf("%i %i %s %s %i %i\n", idx, i, dcc[i].nick, dcc[idx].host, dcc[i].type->flags, DCT_BOT); |
1864 | 1911 | if (!strcasecmp(dcc[i].nick, dcc[idx].host) && |
1865 | 1912 | (dcc[i].type->flags & DCT_BOT)) |
1866 | 1913 | j = i; |
| 1914 | + } |
1867 | 1915 | if (j == -1) |
1868 | 1916 | return; |
1869 | 1917 |
|
@@ -2057,6 +2105,63 @@ static void start_sending_users(int idx) |
2057 | 2105 | return; |
2058 | 2106 | } |
2059 | 2107 |
|
| 2108 | + if (dcc[idx].u.bot->numver >= 1090508) { |
| 2109 | + debug0("share: start_sending_users(): multiplex: start"); |
| 2110 | + FILE * f = tmpfile(); |
| 2111 | + if (!f) { |
| 2112 | + debug1("share: start_sending_users(): multiplex: tmpfile(): error: %s", strerror(errno)); |
| 2113 | + unlink(share_file); |
| 2114 | + return; |
| 2115 | + } |
| 2116 | + if (copyfilef(share_file, f)) { |
| 2117 | + unlink(share_file); |
| 2118 | + fclose(f); |
| 2119 | + return; |
| 2120 | + } |
| 2121 | + unlink(share_file); |
| 2122 | + if (fseeko(f, 0, SEEK_END) < 0) { |
| 2123 | + debug1("share: start_sending_users(): multiplex: fseeko(): error: %s", strerror(errno)); |
| 2124 | + return; |
| 2125 | + } |
| 2126 | + //dcc[idx].u.xfer->length = ftello(f); |
| 2127 | + //rewind(f); |
| 2128 | + //dcc[idx].u.xfer->f = f; |
| 2129 | + // TODO: ggf. start time in millisecs via gettimeofday() or friends um statt dcc[idx].u.xfer->start_time ein debug mit elapsed time for share userfile zu loggen |
| 2130 | + // TODO: new_tbuf(dcc[idx].nick); |
| 2131 | + |
| 2132 | + /* multiplexing means, we just send the userfile via tputs() as a normal botlink message */ |
| 2133 | + /* TODO: |
| 2134 | + * alloc buffer suffixsize + filesize |
| 2135 | + * read file into buffer |
| 2136 | + * put suffix in |
| 2137 | + */ |
| 2138 | + /* Also we dont need to do pump_file_to_sock(), fread(EVERYTHING) will return short only in case of hard error, not to make us loop */ |
| 2139 | + off_t len = ftello(f); |
| 2140 | + char *buf = nmalloc(len + 5 + (((len + 2) / 3) << 2) + 2); /* filesize + "s ur " + b64 + "\n\0" */ |
| 2141 | + rewind(f); |
| 2142 | + if (fread(buf, 1, len, f) < len) { |
| 2143 | + debug1("share: start_sending_users(): multiplex: fread(): error: %s", ferror(f) ? strerror(errno) : "*unknown*"); |
| 2144 | + return; |
| 2145 | + } |
| 2146 | + fclose(f); |
| 2147 | + int len2; |
| 2148 | + /* TODO: irgendwo hier muesen noch die feature funcs gecalled werden, wie zum bbsp. compression */ |
| 2149 | + if ((len2 = b64_ntop((uint8_t *) buf, len, buf + len + 5, (sizeof buf) - len - 5)) == -1) { |
| 2150 | + putlog(LOG_SERV, "*", "share: start_sending_users(): multiplex: error: could not base64 encode"); |
| 2151 | + nfree(buf); |
| 2152 | + return; |
| 2153 | + } |
| 2154 | + memcpy(buf + len, "s ur ", 5); |
| 2155 | + strcpy(buf + len + 5 + len2, "\n"); |
| 2156 | + updatebot(-1, dcc[idx].nick, '+', 0); |
| 2157 | + dcc[idx].status |= STAT_SENDING; |
| 2158 | + tputs(dcc[idx].sock, buf + len, 5 + len2 + 2); |
| 2159 | + nfree(buf); |
| 2160 | + new_tbuf(dcc[idx].nick); |
| 2161 | + debug2("share: start_sending_users(): multiplex: end %lu %i", len, len2); |
| 2162 | + return; |
| 2163 | + } |
| 2164 | + |
2060 | 2165 | if ((i = raw_dcc_send(share_file, "*users", "(users)")) > 0) { |
2061 | 2166 | unlink(share_file); |
2062 | 2167 | dprintf(idx, "s e %s\n", USERF_CANTSEND); |
@@ -2143,7 +2248,10 @@ static void start_sending_users(int idx) |
2143 | 2248 | * for NFS setups. It's not worth the trouble. |
2144 | 2249 | */ |
2145 | 2250 | unlink(share_file); |
| 2251 | + printf("++ unlink share file\n"); |
2146 | 2252 | } |
| 2253 | + printf("++ func end\n"); |
| 2254 | + |
2147 | 2255 | } |
2148 | 2256 |
|
2149 | 2257 | static void (*def_dcc_bot_kill) (int, void *) = 0; |
|
0 commit comments