Skip to content

Commit 9736701

Browse files
Proof of concept - share user file over existing botlink
1 parent 4c42116 commit 9736701

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

src/mod/share.mod/share.c

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <errno.h>
2828
#include "src/mod/module.h"
29+
#include <resolv.h>
2930

3031
#include <netinet/in.h>
3132
#include <arpa/inet.h>
@@ -81,6 +82,7 @@ static void dump_resync(int);
8182
static void q_resync(char *, struct chanset_t *);
8283
static void cancel_user_xfer(int, void *);
8384
static int private_globals_bitmask();
85+
static void finish_share(int);
8486

8587
#include "share.h"
8688

@@ -1224,6 +1226,49 @@ static void share_userfileq(int idx, char *par)
12241226
}
12251227
}
12261228

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+
12271272
/* us <ip> <port> <length>
12281273
*/
12291274
static void share_ufsend(int idx, char *par)
@@ -1428,6 +1473,7 @@ static botscmd_t C_share[] = {
14281473
{"sInv", "psj", (IntFunc) share_stick_invite},
14291474
{"u?", "", (IntFunc) share_userfileq},
14301475
{"un", "", (IntFunc) share_ufno},
1476+
{"ur", "", (IntFunc) share_ufsend2},
14311477
{"us", "", (IntFunc) share_ufsend},
14321478
{"uy", "", (IntFunc) share_ufyes},
14331479
{"v", "", (IntFunc) share_version},
@@ -1860,10 +1906,12 @@ static void finish_share(int idx)
18601906
struct chanset_t *chan;
18611907
int i, j = -1;
18621908

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);
18641911
if (!strcasecmp(dcc[i].nick, dcc[idx].host) &&
18651912
(dcc[i].type->flags & DCT_BOT))
18661913
j = i;
1914+
}
18671915
if (j == -1)
18681916
return;
18691917

@@ -2057,6 +2105,63 @@ static void start_sending_users(int idx)
20572105
return;
20582106
}
20592107

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+
20602165
if ((i = raw_dcc_send(share_file, "*users", "(users)")) > 0) {
20612166
unlink(share_file);
20622167
dprintf(idx, "s e %s\n", USERF_CANTSEND);
@@ -2143,7 +2248,10 @@ static void start_sending_users(int idx)
21432248
* for NFS setups. It's not worth the trouble.
21442249
*/
21452250
unlink(share_file);
2251+
printf("++ unlink share file\n");
21462252
}
2253+
printf("++ func end\n");
2254+
21472255
}
21482256

21492257
static void (*def_dcc_bot_kill) (int, void *) = 0;

src/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
*/
2828

2929
#define EGG_STRINGVER "1.9.5"
30-
#define EGG_NUMVER 1090506
31-
#define EGG_PATCH "accounttracking"
30+
#define EGG_NUMVER 1090508
31+
#define EGG_PATCH "sharemultiplex"

0 commit comments

Comments
 (0)