transfer.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /*
  2. * transfer.c -- part of transfer.mod
  3. *
  4. */
  5. /*
  6. * Small code snippets related to REGET/RESEND support were taken from
  7. * BitchX, copyright by panasync.
  8. */
  9. #define MODULE_NAME "transfer"
  10. #define MAKING_TRANSFER
  11. /* sigh sunos */
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include "src/mod/module.h"
  15. #include "src/mod/update.mod/update.h"
  16. #include "src/tandem.h"
  17. #include "src/users.h"
  18. #include "transfer.h"
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. static bind_table_t *BT_rcvd, *BT_sent, *BT_lost, *BT_tout;
  22. static Function *global = NULL, *update_funcs = NULL;
  23. static int copy_to_tmp = 1; /* Copy files to /tmp before transmitting? */
  24. static int wait_dcc_xfer = 40; /* Timeout time on DCC xfers */
  25. static int dcc_limit = 4; /* Maximum number of simultaneous file
  26. downloads allowed */
  27. static int dcc_block = 0; /* Size of one dcc block */
  28. static int quiet_reject = 1; /* Quietly reject dcc chat or sends from
  29. users without access? */
  30. /*
  31. * Prototypes
  32. */
  33. static void stats_add_dnload(struct userrec *, unsigned long);
  34. static void stats_add_upload(struct userrec *, unsigned long);
  35. static void wipe_tmp_filename(char *, int);
  36. static int at_limit(char *);
  37. static void dcc_get_pending(int, char *, int);
  38. static struct dcc_table DCC_SEND;
  39. static struct dcc_table DCC_GET;
  40. static struct dcc_table DCC_GET_PENDING;
  41. static fileq_t *fileq = NULL;
  42. /*
  43. * Misc functions
  44. */
  45. #undef MATCH
  46. #define MATCH (match+sofar)
  47. /* This function SHAMELESSLY :) pinched from match.c in the original
  48. * source, see that file for info about the author etc.
  49. */
  50. #define QUOTE '\\'
  51. #define WILDS '*'
  52. #define WILDQ '?'
  53. #define NOMATCH 0
  54. /*
  55. * wild_match_file(char *ma, char *na)
  56. *
  57. * Features: Forward, case-sensitive, ?, *
  58. * Best use: File mask matching, as it is case-sensitive
  59. */
  60. static int wild_match_file(register char *m, register char *n)
  61. {
  62. char *ma = m, *lsm = 0, *lsn = 0;
  63. int match = 1;
  64. register unsigned int sofar = 0;
  65. /* Take care of null strings (should never match) */
  66. if ((m == 0) || (n == 0) || (!*n))
  67. return NOMATCH;
  68. /* (!*m) test used to be here, too, but I got rid of it. After all, If
  69. * (!*n) was false, there must be a character in the name (the second
  70. * string), so if the mask is empty it is a non-match. Since the
  71. * algorithm handles this correctly without testing for it here and this
  72. * shouldn't be called with null masks anyway, it should be a bit faster
  73. * this way.
  74. */
  75. while (*n) {
  76. /* Used to test for (!*m) here, but this scheme seems to work better */
  77. switch (*m) {
  78. case 0:
  79. do
  80. m--; /* Search backwards */
  81. while ((m > ma) && (*m == '?')); /* For first non-? char */
  82. if ((m > ma) ? ((*m == '*') && (m[-1] != QUOTE)) : (*m == '*'))
  83. return MATCH; /* nonquoted * = match */
  84. break;
  85. case WILDS:
  86. do
  87. m++;
  88. while (*m == WILDS); /* Zap redundant wilds */
  89. lsm = m;
  90. lsn = n; /* Save * fallback spot */
  91. match += sofar;
  92. sofar = 0;
  93. continue; /* Save tally count */
  94. case WILDQ:
  95. m++;
  96. n++;
  97. continue; /* Match one char */
  98. case QUOTE:
  99. m++; /* Handle quoting */
  100. }
  101. if (*m == *n) { /* If matching */
  102. m++;
  103. n++;
  104. sofar++;
  105. continue; /* Tally the match */
  106. }
  107. if (lsm) { /* Try to fallback on * */
  108. n = ++lsn;
  109. m = lsm; /* Restore position */
  110. /* Used to test for (!*n) here but it wasn't necessary so it's gone */
  111. sofar = 0;
  112. continue; /* Next char, please */
  113. }
  114. return NOMATCH; /* No fallbacks=No match */
  115. }
  116. while (*m == WILDS)
  117. m++; /* Zap leftover *s */
  118. return (*m) ? NOMATCH : MATCH; /* End of both = match */
  119. }
  120. static void wipe_tmp_filename(char *fn, int idx)
  121. {
  122. int i, ok = 1;
  123. if (!copy_to_tmp)
  124. return;
  125. for (i = 0; i < dcc_total; i++)
  126. if (i != idx)
  127. if (dcc[i].type == &DCC_GET || dcc[i].type == &DCC_GET_PENDING)
  128. if (!strcmp(dcc[i].u.xfer->filename, fn)) {
  129. ok = 0;
  130. break;
  131. }
  132. if (ok)
  133. unlink(fn);
  134. }
  135. /* Return true if this user has >= the maximum number of file xfers allowed.
  136. */
  137. static int at_limit(char *nick)
  138. {
  139. int i, x = 0;
  140. for (i = 0; i < dcc_total; i++)
  141. if (dcc[i].type == &DCC_GET || dcc[i].type == &DCC_GET_PENDING)
  142. if (!egg_strcasecmp(dcc[i].nick, nick))
  143. x++;
  144. return (x >= dcc_limit);
  145. }
  146. /* Replaces all spaces with underscores (' ' -> '_'). The returned buffer
  147. * needs to be freed after use.
  148. */
  149. static char *replace_spaces(char *fn)
  150. {
  151. register char *ret, *p;
  152. p = ret = strdup(fn);
  153. while ((p = strchr(p, ' ')) != NULL)
  154. *p = '_';
  155. return ret;
  156. }
  157. static void check_sentrcvd(struct userrec *u, char *nick, char *path,
  158. bind_table_t *table)
  159. {
  160. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  161. char *hand = u ? u->handle : "*"; /* u might be NULL. */
  162. get_user_flagrec(u, &fr, NULL);
  163. check_bind(table, hand, &fr, u, nick, path);
  164. }
  165. static void check_toutlost(struct userrec *u, char *nick, char *path,
  166. unsigned long acked, unsigned long length,
  167. bind_table_t *table)
  168. {
  169. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  170. char *hand = u ? u->handle : "*"; /* u might be NULL. */
  171. get_user_flagrec(u, &fr, NULL);
  172. check_bind(table, hand, &fr, u, nick, path, acked, length);
  173. }
  174. /*
  175. * File queue functions
  176. */
  177. static void queue_file(char *dir, char *file, char *from, char *to)
  178. {
  179. fileq_t *q = fileq;
  180. fileq = (fileq_t *) malloc(sizeof(fileq_t));
  181. fileq->next = q;
  182. fileq->dir = strdup(dir);
  183. fileq->file = strdup(file);
  184. strcpy(fileq->nick, from);
  185. strcpy(fileq->to, to);
  186. }
  187. static void deq_this(fileq_t *this)
  188. {
  189. fileq_t *q = fileq, *last = NULL;
  190. while (q && q != this) {
  191. last = q;
  192. q = q->next;
  193. }
  194. if (!q)
  195. return; /* Bogus ptr */
  196. if (last)
  197. last->next = q->next;
  198. else
  199. fileq = q->next;
  200. free(q->dir);
  201. free(q->file);
  202. free(q);
  203. }
  204. /* Remove all files queued to a certain user.
  205. */
  206. static void flush_fileq(char *to)
  207. {
  208. fileq_t *q = fileq;
  209. int fnd = 1;
  210. while (fnd) {
  211. q = fileq;
  212. fnd = 0;
  213. while (q != NULL) {
  214. if (!egg_strcasecmp(q->to, to)) {
  215. deq_this(q);
  216. q = NULL;
  217. fnd = 1;
  218. }
  219. if (q != NULL)
  220. q = q->next;
  221. }
  222. }
  223. }
  224. static void send_next_file(char *to)
  225. {
  226. fileq_t *q, *this = NULL;
  227. char *s, *s1;
  228. int x;
  229. for (q = fileq; q; q = q->next)
  230. if (!egg_strcasecmp(q->to, to))
  231. this = q;
  232. if (this == NULL)
  233. return; /* None */
  234. /* Copy this file to /tmp */
  235. if (this->dir[0] == '*') { /* Absolute path */
  236. s = malloc(strlen(&this->dir[1]) + strlen(this->file) + 2);
  237. sprintf(s, "%s/%s", &this->dir[1], this->file);
  238. } else {
  239. char *p = strchr(this->dir, '*');
  240. if (p == NULL) { /* if it's messed up */
  241. send_next_file(to);
  242. return;
  243. }
  244. p++;
  245. s = malloc(strlen(p) + strlen(this->file) + 2);
  246. sprintf(s, "%s%s%s", p, p[0] ? "/" : "", this->file);
  247. strcpy(this->dir, &(p[atoi(this->dir)]));
  248. }
  249. if (copy_to_tmp) {
  250. s1 = malloc(strlen(tempdir) + strlen(this->file) + 1);
  251. sprintf(s1, "%s%s", tempdir, this->file);
  252. if (copyfile(s, s1) != 0) {
  253. putlog(LOG_FILES | LOG_MISC, "*",
  254. TRANSFER_COPY_FAILED,
  255. this->file, tempdir);
  256. dprintf(DP_HELP,
  257. TRANSFER_FILESYS_BROKEN,
  258. this->to);
  259. strcpy(s, this->to);
  260. flush_fileq(s);
  261. free(s1);
  262. free(s);
  263. return;
  264. }
  265. } else {
  266. s1 = strdup(s);
  267. }
  268. if (this->dir[0] == '*') {
  269. s = realloc(s, strlen(&this->dir[1]) + strlen(this->file) + 2);
  270. sprintf(s, "%s/%s", &this->dir[1], this->file);
  271. } else {
  272. s = realloc(s, strlen(this->dir) + strlen(this->file) + 2);
  273. sprintf(s, "%s%s%s", this->dir, this->dir[0] ? "/" : "", this->file);
  274. }
  275. x = raw_dcc_send(s1, this->to, this->nick, s);
  276. if (x == DCCSEND_OK) {
  277. if (egg_strcasecmp(this->to, this->nick))
  278. dprintf(DP_HELP, TRANSFER_FILE_ARRIVE, this->to,
  279. this->nick);
  280. deq_this(this);
  281. free(s);
  282. free(s1);
  283. return;
  284. }
  285. wipe_tmp_filename(s1, -1);
  286. if (x == DCCSEND_FULL) {
  287. putlog(LOG_FILES, "*",TRANSFER_LOG_CONFULL, s1, this->nick);
  288. dprintf(DP_HELP,
  289. TRANSFER_NOTICE_CONFULL,
  290. this->to);
  291. strcpy(s, this->to);
  292. flush_fileq(s);
  293. } else if (x == DCCSEND_NOSOCK) {
  294. putlog(LOG_FILES, "*", TRANSFER_LOG_SOCKERR, s1, this->nick);
  295. dprintf(DP_HELP, TRANSFER_NOTICE_SOCKERR,
  296. this->to);
  297. strcpy(s, this->to);
  298. flush_fileq(s);
  299. } else {
  300. if (x == DCCSEND_FEMPTY) {
  301. putlog(LOG_FILES, "*", TRANSFER_LOG_FILEEMPTY, this->file);
  302. dprintf(DP_HELP, TRANSFER_NOTICE_FILEEMPTY,
  303. this->to, this->file);
  304. }
  305. deq_this(this);
  306. }
  307. free(s);
  308. free(s1);
  309. return;
  310. }
  311. static void show_queued_files(int idx)
  312. {
  313. int i, cnt = 0, len;
  314. char spaces[] = " ";
  315. fileq_t *q;
  316. for (q = fileq; q; q = q->next) {
  317. if (!egg_strcasecmp(q->nick, dcc[idx].nick)) {
  318. if (!cnt) {
  319. spaces[HANDLEN - 9] = 0;
  320. dprintf(idx, TRANSFER_SEND_TO , spaces);
  321. dprintf(idx, TRANSFER_LINES , spaces);
  322. spaces[HANDLEN - 9] = ' ';
  323. }
  324. cnt++;
  325. spaces[len = HANDLEN - strlen(q->to)] = 0;
  326. if (q->dir[0] == '*')
  327. dprintf(idx, " %s%s %s/%s\n", q->to, spaces, &q->dir[1],
  328. q->file);
  329. else
  330. dprintf(idx, " %s%s /%s%s%s\n", q->to, spaces, q->dir,
  331. q->dir[0] ? "/" : "", q->file);
  332. spaces[len] = ' ';
  333. }
  334. }
  335. for (i = 0; i < dcc_total; i++) {
  336. if ((dcc[i].type == &DCC_GET_PENDING || dcc[i].type == &DCC_GET) &&
  337. (!egg_strcasecmp(dcc[i].nick, dcc[idx].nick) ||
  338. !egg_strcasecmp(dcc[i].u.xfer->from, dcc[idx].nick))) {
  339. char *nfn;
  340. if (!cnt) {
  341. spaces[HANDLEN - 9] = 0;
  342. dprintf(idx, TRANSFER_SEND_TO , spaces);
  343. dprintf(idx, TRANSFER_LINES , spaces);
  344. spaces[HANDLEN - 9] = ' ';
  345. }
  346. nfn = strrchr(dcc[i].u.xfer->origname, '/');
  347. if (nfn == NULL)
  348. nfn = dcc[i].u.xfer->origname;
  349. else
  350. nfn++;
  351. cnt++;
  352. spaces[len = HANDLEN - strlen(dcc[i].nick)] = 0;
  353. if (dcc[i].type == &DCC_GET_PENDING)
  354. dprintf(idx,TRANSFER_WAITING, dcc[i].nick, spaces,
  355. nfn);
  356. else
  357. dprintf(idx,TRANSFER_DONE, dcc[i].nick, spaces,
  358. nfn, (100.0 * ((float) dcc[i].status /
  359. (float) dcc[i].u.xfer->length)));
  360. spaces[len] = ' ';
  361. }
  362. }
  363. if (!cnt)
  364. dprintf(idx,TRANSFER_QUEUED_UP);
  365. else
  366. dprintf(idx,TRANSFER_TOTAL, cnt);
  367. }
  368. static void fileq_cancel(int idx, char *par)
  369. {
  370. int fnd = 1, matches = 0, atot = 0, i;
  371. fileq_t *q;
  372. char *s = NULL;
  373. while (fnd) {
  374. q = fileq;
  375. fnd = 0;
  376. while (q != NULL) {
  377. if (!egg_strcasecmp(dcc[idx].nick, q->nick)) {
  378. s = realloc(s, strlen(q->dir) + strlen(q->file) + 3);
  379. if (q->dir[0] == '*')
  380. sprintf(s, "%s/%s", &q->dir[1], q->file);
  381. else
  382. sprintf(s, "/%s%s%s", q->dir, q->dir[0] ? "/" : "", q->file);
  383. if (wild_match_file(par, s)) {
  384. dprintf(idx,TRANSFER_CANCELLED, s, q->to);
  385. fnd = 1;
  386. deq_this(q);
  387. q = NULL;
  388. matches++;
  389. }
  390. if (!fnd && wild_match_file(par, q->file)) {
  391. dprintf(idx,TRANSFER_CANCELLED, s, q->to);
  392. fnd = 1;
  393. deq_this(q);
  394. q = NULL;
  395. matches++;
  396. }
  397. }
  398. if (q != NULL)
  399. q = q->next;
  400. }
  401. }
  402. if (s)
  403. free(s);
  404. for (i = 0; i < dcc_total; i++) {
  405. if ((dcc[i].type == &DCC_GET_PENDING || dcc[i].type == &DCC_GET) &&
  406. (!egg_strcasecmp(dcc[i].nick, dcc[idx].nick) ||
  407. !egg_strcasecmp(dcc[i].u.xfer->from, dcc[idx].nick))) {
  408. char *nfn = strrchr(dcc[i].u.xfer->origname, '/');
  409. if (nfn == NULL)
  410. nfn = dcc[i].u.xfer->origname;
  411. else
  412. nfn++;
  413. if (wild_match_file(par, nfn)) {
  414. dprintf(idx,TRANSFER_ABORT_DCCSEND, nfn);
  415. if (egg_strcasecmp(dcc[i].nick, dcc[idx].nick))
  416. dprintf(DP_HELP, TRANSFER_NOTICE_ABORT,
  417. dcc[i].nick, nfn, dcc[idx].nick);
  418. if (dcc[i].type == &DCC_GET)
  419. putlog(LOG_FILES, "*",TRANSFER_DCC_CANCEL, nfn,
  420. dcc[i].nick, dcc[i].status, dcc[i].u.xfer->length);
  421. wipe_tmp_filename(dcc[i].u.xfer->filename, i);
  422. atot++;
  423. matches++;
  424. killsock(dcc[i].sock);
  425. lostdcc(i);
  426. }
  427. }
  428. }
  429. if (!matches)
  430. dprintf(idx,TRANSFER_NO_MATCHES);
  431. else
  432. dprintf(idx, TRANSFER_CANCELLED_FILE, matches, (matches != 1) ? "s" : "");
  433. for (i = 0; i < atot; i++)
  434. if (!at_limit(dcc[idx].nick))
  435. send_next_file(dcc[idx].nick);
  436. }
  437. /*
  438. * DCC routines
  439. */
  440. /* Instead of reading all data intended to go into the DCC block
  441. * in one go, we read it in PMAX_SIZE chunks, feed it to tputs and
  442. * continue until we get to know that the network buffer only
  443. * buffers the data instead of sending it.
  444. *
  445. * In that case, we delay further sending until we receive the
  446. * dcc outdone event.
  447. *
  448. * Note: To optimize buffer sizes, we default to PMAX_SIZE, but
  449. * allocate a smaller buffer for smaller pending_data sizes.
  450. */
  451. #define PMAX_SIZE 4096
  452. static unsigned long pump_file_to_sock(FILE *file, long sock,
  453. register unsigned long pending_data)
  454. {
  455. const unsigned long buf_len = pending_data >= PMAX_SIZE ?
  456. PMAX_SIZE : pending_data;
  457. char *bf = malloc(buf_len);
  458. register unsigned long actual_size;
  459. if (bf) {
  460. do {
  461. actual_size = pending_data >= buf_len ? buf_len : pending_data;
  462. fread(bf, actual_size, 1, file);
  463. tputs(sock, bf, actual_size);
  464. pending_data -= actual_size;
  465. } while (!sock_has_data(SOCK_DATA_OUTGOING, sock) && pending_data != 0);
  466. free(bf);
  467. }
  468. return pending_data;
  469. }
  470. static void eof_dcc_fork_send(int idx)
  471. {
  472. char s1[121];
  473. char *s2;
  474. fclose(dcc[idx].u.xfer->f);
  475. if (!strcmp(dcc[idx].nick, "*users")) {
  476. int x, y = 0;
  477. for (x = 0; x < dcc_total; x++)
  478. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  479. (dcc[x].type->flags & DCT_BOT)) {
  480. y = x;
  481. break;
  482. }
  483. if (y != 0) {
  484. dcc[y].status &= ~STAT_GETTING;
  485. dcc[y].status &= ~STAT_SHARE;
  486. }
  487. putlog(LOG_BOTS, "*", USERF_FAILEDXFER);
  488. unlink(dcc[idx].u.xfer->filename);
  489. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  490. int x, y = 0;
  491. for (x = 0; x < dcc_total; x++)
  492. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  493. (dcc[x].type->flags & DCT_BOT)) {
  494. y = x;
  495. break;
  496. }
  497. if (y != 0) {
  498. dcc[y].status &= ~STAT_GETTINGU;
  499. }
  500. putlog(LOG_BOTS, "*", "Failed binary transfer.");
  501. } else {
  502. neterror(s1);
  503. if (!quiet_reject)
  504. dprintf(DP_HELP, "NOTICE %s :%s (%s)\n", dcc[idx].nick,
  505. DCC_CONNECTFAILED1, s1);
  506. putlog(LOG_MISC, "*", "%s: SEND %s (%s!%s)", DCC_CONNECTFAILED2,
  507. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  508. putlog(LOG_MISC, "*", " (%s)", s1);
  509. s2 = malloc(strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  510. sprintf(s2, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  511. unlink(s2);
  512. free(s2);
  513. }
  514. killsock(dcc[idx].sock);
  515. lostdcc(idx);
  516. }
  517. static void eof_dcc_send(int idx)
  518. {
  519. int ok, j;
  520. char *ofn, *nfn, s[1024], *hand;
  521. struct userrec *u;
  522. fclose(dcc[idx].u.xfer->f);
  523. if (dcc[idx].u.xfer->length == dcc[idx].status) {
  524. int l;
  525. /* Success */
  526. ok = 0;
  527. if (!strcmp(dcc[idx].nick, "*users")) {
  528. module_entry *me = module_find("share", 0, 0);
  529. if (me && me->funcs) {
  530. Function f = me->funcs[SHARE_FINISH];
  531. (f) (idx);
  532. }
  533. killsock(dcc[idx].sock);
  534. lostdcc(idx);
  535. return;
  536. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  537. module_entry *me = module_find("update", 0, 0);
  538. if (me && me->funcs) {
  539. Function f = me->funcs[UPDATE_FINISH];
  540. (f) (idx);
  541. }
  542. killsock(dcc[idx].sock);
  543. lostdcc(idx);
  544. return;
  545. }
  546. putlog(LOG_FILES, "*", TRANSFER_COMPLETED_DCC,
  547. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  548. egg_snprintf(s, sizeof s, "%s!%s", dcc[idx].nick, dcc[idx].host);
  549. u = get_user_by_host(s);
  550. hand = u ? u->handle : "*";
  551. l = strlen(dcc[idx].u.xfer->filename);
  552. if (l > NAME_MAX) {
  553. /* The filename is to long... blow it off */
  554. putlog(LOG_FILES, "*",TRANSFER_FILENAME_TOOLONG , l);
  555. dprintf(DP_HELP, TRANSFER_NOTICE_FNTOOLONG,
  556. dcc[idx].nick, l);
  557. putlog(LOG_FILES, "*", TRANSFER_TOO_BAD );
  558. dprintf(DP_HELP, TRANSFER_NOTICE_TOOBAD,
  559. dcc[idx].nick);
  560. killsock(dcc[idx].sock);
  561. lostdcc(idx);
  562. return;
  563. }
  564. /* Move the file from /tmp */
  565. ofn = malloc(strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  566. nfn = malloc(strlen(dcc[idx].u.xfer->dir)
  567. + strlen(dcc[idx].u.xfer->origname) + 1);
  568. sprintf(ofn, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  569. sprintf(nfn, "%s%s", dcc[idx].u.xfer->dir, dcc[idx].u.xfer->origname);
  570. if (movefile(ofn, nfn))
  571. putlog(LOG_MISC | LOG_FILES, "*",
  572. TRANSFER_FAILED_MOVE, nfn, ofn);
  573. else {
  574. /* Add to file database */
  575. module_entry *fs = module_find("filesys", 0, 0);
  576. if (fs != NULL) {
  577. Function f = fs->funcs[FILESYS_ADDFILE];
  578. f(dcc[idx].u.xfer->dir, dcc[idx].u.xfer->origname, hand);
  579. }
  580. stats_add_upload(u, dcc[idx].u.xfer->length);
  581. check_sentrcvd(u, dcc[idx].nick, nfn, BT_rcvd);
  582. }
  583. free(ofn);
  584. free(nfn);
  585. for (j = 0; j < dcc_total; j++)
  586. if (!ok && (dcc[j].type->flags & (DCT_GETNOTES)) &&
  587. !egg_strcasecmp(dcc[j].nick, hand)) {
  588. ok = 1;
  589. dprintf(j,TRANSFER_THANKS);
  590. }
  591. if (!ok)
  592. dprintf(DP_HELP,TRANSFER_NOTICE_THANKS,
  593. dcc[idx].nick);
  594. killsock(dcc[idx].sock);
  595. lostdcc(idx);
  596. return;
  597. }
  598. /* Failure :( */
  599. if (!strcmp(dcc[idx].nick, "*users")) {
  600. int x, y = 0;
  601. for (x = 0; x < dcc_total; x++)
  602. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  603. (dcc[x].type->flags & DCT_BOT))
  604. y = x;
  605. if (y) {
  606. putlog(LOG_BOTS, "*",TRANSFER_USERFILE_LOST,
  607. dcc[y].nick);
  608. unlink(dcc[idx].u.xfer->filename);
  609. /* Drop that bot */
  610. dprintf(y, "bye\n");
  611. egg_snprintf(s, sizeof s,TRANSFER_USERFILE_DISCON,
  612. dcc[y].nick);
  613. botnet_send_unlinked(y, dcc[y].nick, s);
  614. chatout("*** %s\n", dcc[y].nick, s);
  615. if (y < idx) {
  616. int t = y;
  617. y = idx;
  618. idx = t;
  619. }
  620. if (y != idx) {
  621. killsock(dcc[y].sock);
  622. lostdcc(y);
  623. }
  624. killsock(dcc[idx].sock);
  625. lostdcc(idx);
  626. }
  627. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  628. int x, y = 0;
  629. for (x = 0; x < dcc_total; x++)
  630. if ((!egg_strcasecmp(dcc[x].nick, dcc[idx].host)) &&
  631. (dcc[x].type->flags & DCT_BOT))
  632. y = x;
  633. if (y) {
  634. putlog(LOG_BOTS, "*", "Lost binary transfer from %s; aborting.", dcc[y].nick);
  635. unlink(dcc[idx].u.xfer->filename);
  636. /* Drop that bot */
  637. // dprintf(y, "bye\n");
  638. // egg_snprintf(s, sizeof s,"Disconnected %s (aborted binary transfer)", dcc[y].nick);
  639. // botnet_send_unlinked(y, dcc[y].nick, s);
  640. // chatout("*** %s\n", dcc[y].nick, s);
  641. // if (y != idx) {
  642. // killsock(dcc[y].sock);
  643. // lostdcc(y);
  644. // }
  645. killsock(dcc[idx].sock);
  646. lostdcc(idx);
  647. }
  648. } else {
  649. putlog(LOG_FILES, "*",TRANSFER_LOST_DCCSEND,
  650. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host,
  651. dcc[idx].status, dcc[idx].u.xfer->length);
  652. ofn = malloc(strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  653. sprintf(ofn, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  654. unlink(ofn);
  655. free(ofn);
  656. killsock(dcc[idx].sock);
  657. lostdcc(idx);
  658. }
  659. }
  660. /* Determine byte order. Used for resend DCC startup packets.
  661. */
  662. static inline u_8bit_t byte_order_test(void)
  663. {
  664. u_16bit_t test = TRANSFER_REGET_PACKETID;
  665. if (*((u_8bit_t *)&test) == ((TRANSFER_REGET_PACKETID & 0xff00) >> 8))
  666. return 0;
  667. if (*((u_8bit_t *)&test) == (TRANSFER_REGET_PACKETID & 0x00ff))
  668. return 1;
  669. return 0;
  670. }
  671. /* Parse and handle resend DCC startup packets.
  672. */
  673. inline static void handle_resend_packet(int idx, transfer_reget *reget_data)
  674. {
  675. if (byte_order_test() != reget_data->byte_order) {
  676. /* The sender's byte order does not match our's so we need to switch the
  677. * bytes first, before we can make use of them.
  678. */
  679. reget_data->packet_id = ((reget_data->packet_id & 0x00ff) << 8) |
  680. ((reget_data->packet_id & 0xff00) >> 8);
  681. reget_data->byte_offset = ((reget_data->byte_offset & 0xff000000) >> 24) |
  682. ((reget_data->byte_offset & 0x00ff0000) >> 8) |
  683. ((reget_data->byte_offset & 0x0000ff00) << 8) |
  684. ((reget_data->byte_offset & 0x000000ff) << 24);
  685. }
  686. if (reget_data->packet_id != TRANSFER_REGET_PACKETID)
  687. putlog(LOG_FILES, "*", TRANSFER_REGET_PACKET,
  688. dcc[idx].nick, dcc[idx].u.xfer->origname);
  689. else
  690. dcc[idx].u.xfer->offset = reget_data->byte_offset;
  691. dcc[idx].u.xfer->type = XFER_RESEND;
  692. }
  693. /* Handles DCC packets the client sends us. As soon as the last sent dcc
  694. * block is fully acknowledged we send the next block.
  695. *
  696. * Note: The first received packet during reget is a special 8 bit packet
  697. * containing special information.
  698. */
  699. static void dcc_get(int idx, char *buf, int len)
  700. {
  701. char xnick[NICKLEN];
  702. unsigned char bbuf[4];
  703. unsigned long cmp, l;
  704. int w = len + dcc[idx].u.xfer->sofar, p = 0;
  705. dcc[idx].timeval = now; /* Mark as active */
  706. /* Add bytes to our buffer if we don't have a complete response yet.
  707. * This is either a 4 bit ack or the 8 bit reget packet.
  708. */
  709. if (w < 4 ||
  710. (w < 8 && dcc[idx].u.xfer->type == XFER_RESEND_PEND)) {
  711. my_memcpy(&(dcc[idx].u.xfer->buf[dcc[idx].u.xfer->sofar]), buf, len);
  712. dcc[idx].u.xfer->sofar += len;
  713. return;
  714. /* Waiting for the 8 bit reget packet? */
  715. } else if (dcc[idx].u.xfer->type == XFER_RESEND_PEND) {
  716. /* The 8 bit packet is complete now. Parse it. */
  717. if (w == 8) {
  718. transfer_reget reget_data;
  719. my_memcpy(&reget_data, dcc[idx].u.xfer->buf, dcc[idx].u.xfer->sofar);
  720. my_memcpy(&reget_data + dcc[idx].u.xfer->sofar, buf, len);
  721. handle_resend_packet(idx, &reget_data);
  722. cmp = dcc[idx].u.xfer->offset;
  723. } else
  724. return;
  725. /* Fall through! */
  726. /* No, only want 4 bit ack responses. */
  727. } else {
  728. /* Complete packet? */
  729. if (w == 4) {
  730. my_memcpy(bbuf, dcc[idx].u.xfer->buf, dcc[idx].u.xfer->sofar);
  731. my_memcpy(&(bbuf[dcc[idx].u.xfer->sofar]), buf, len);
  732. } else {
  733. p = ((w - 1) & ~3) - dcc[idx].u.xfer->sofar;
  734. w = w - ((w - 1) & ~3);
  735. if (w < 4) {
  736. my_memcpy(dcc[idx].u.xfer->buf, &(buf[p]), w);
  737. return;
  738. }
  739. my_memcpy(bbuf, &(buf[p]), w);
  740. }
  741. /* This is more compatible than ntohl for machines where an int
  742. * is more than 4 bytes:
  743. */
  744. cmp = ((unsigned int) (bbuf[0]) << 24) +
  745. ((unsigned int) (bbuf[1]) << 16) +
  746. ((unsigned int) (bbuf[2]) << 8) + bbuf[3];
  747. dcc[idx].u.xfer->acked = cmp;
  748. }
  749. dcc[idx].u.xfer->sofar = 0;
  750. if (cmp > dcc[idx].u.xfer->length && cmp > dcc[idx].status) {
  751. /* Attempt to resume, but file is not as long as requested... */
  752. putlog(LOG_FILES, "*",
  753. TRANSFER_BEHIND_FILEEND,
  754. dcc[idx].u.xfer->origname, dcc[idx].nick);
  755. } else if (cmp > dcc[idx].status) {
  756. /* Attempt to resume */
  757. if (!strcmp(dcc[idx].nick, "*users")) {
  758. putlog(LOG_BOTS, "*",TRANSFER_TRY_SKIP_AHEAD);
  759. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  760. putlog(LOG_BOTS, "*","!!! Trying to skip ahead on binary transfer");
  761. } else {
  762. fseek(dcc[idx].u.xfer->f, cmp, SEEK_SET);
  763. dcc[idx].status = cmp;
  764. putlog(LOG_FILES, "*",TRANSFER_RESUME_FILE,
  765. (int) (cmp / 1024), dcc[idx].u.xfer->origname,
  766. dcc[idx].nick);
  767. }
  768. } else {
  769. if (dcc[idx].u.xfer->ack_type == XFER_ACK_UNKNOWN) {
  770. if (cmp < dcc[idx].u.xfer->offset)
  771. /* If we don't start at the top of the file, some clients only tell
  772. * us the really received bytes (e.g. bitchx). This seems to be the
  773. * case here.
  774. */
  775. dcc[idx].u.xfer->ack_type = XFER_ACK_WITHOUT_OFFSET;
  776. else
  777. dcc[idx].u.xfer->ack_type = XFER_ACK_WITH_OFFSET;
  778. }
  779. if (dcc[idx].u.xfer->ack_type == XFER_ACK_WITHOUT_OFFSET)
  780. cmp += dcc[idx].u.xfer->offset;
  781. }
  782. if (cmp != dcc[idx].status)
  783. return;
  784. if (dcc[idx].status == dcc[idx].u.xfer->length) {
  785. /* Successful send, we are done */
  786. killsock(dcc[idx].sock);
  787. fclose(dcc[idx].u.xfer->f);
  788. if (!strcmp(dcc[idx].nick, "*users")) {
  789. module_entry *me = module_find("share", 0, 0);
  790. int x, y = 0;
  791. for (x = 0; x < dcc_total; x++)
  792. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  793. (dcc[x].type->flags & DCT_BOT))
  794. y = x;
  795. if (y != 0)
  796. dcc[y].status &= ~STAT_SENDING;
  797. putlog(LOG_BOTS, "*",TRANSFER_COMPLETED_USERFILE,
  798. dcc[y].nick);
  799. unlink(dcc[idx].u.xfer->filename);
  800. /* Any sharebot things that were queued: */
  801. if (me && me->funcs[SHARE_DUMP_RESYNC])
  802. ((me->funcs)[SHARE_DUMP_RESYNC]) (y);
  803. xnick[0] = 0;
  804. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  805. int x, y = 0;
  806. for (x = 0; x < dcc_total; x++)
  807. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  808. (dcc[x].type->flags & DCT_BOT))
  809. y = x;
  810. if (y != 0) {
  811. dcc[y].status &= ~STAT_SENDINGU;
  812. dcc[y].status |= STAT_UPDATED;
  813. }
  814. putlog(LOG_BOTS, "*", "Completed binary file send to %s",
  815. dcc[y].nick);
  816. xnick[0] = 0;
  817. #ifdef HUB
  818. bupdating = 0;
  819. #endif
  820. } else {
  821. module_entry *fs = module_find("filesys", 0, 0);
  822. struct userrec *u = get_user_by_handle(userlist,
  823. dcc[idx].u.xfer->from);
  824. check_sentrcvd(u, dcc[idx].nick,
  825. dcc[idx].u.xfer->dir, BT_sent);
  826. if (fs != NULL) {
  827. Function f = fs->funcs[FILESYS_INCRGOTS];
  828. f(dcc[idx].u.xfer->dir);
  829. }
  830. /* Download is credited to the user who requested it
  831. * (not the user who actually received it)
  832. */
  833. stats_add_dnload(u, dcc[idx].u.xfer->length);
  834. putlog(LOG_FILES, "*",TRANSFER_FINISHED_DCCSEND,
  835. dcc[idx].u.xfer->origname, dcc[idx].nick);
  836. wipe_tmp_filename(dcc[idx].u.xfer->filename, idx);
  837. strcpy((char *) xnick, dcc[idx].nick);
  838. }
  839. lostdcc(idx);
  840. /* Any to dequeue? */
  841. if (!at_limit(xnick))
  842. send_next_file(xnick);
  843. return;
  844. }
  845. /* Note: No fseek() needed here, because the file position is kept from
  846. * the last run.
  847. */
  848. l = dcc_block;
  849. if (l == 0 || dcc[idx].status + l > dcc[idx].u.xfer->length)
  850. l = dcc[idx].u.xfer->length - dcc[idx].status;
  851. dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
  852. dcc[idx].sock, l);
  853. dcc[idx].status += l;
  854. }
  855. static void eof_dcc_get(int idx)
  856. {
  857. char xnick[NICKLEN], s[1024];
  858. fclose(dcc[idx].u.xfer->f);
  859. if (!strcmp(dcc[idx].nick, "*users")) {
  860. int x, y = 0;
  861. for (x = 0; x < dcc_total; x++)
  862. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  863. (dcc[x].type->flags & DCT_BOT))
  864. y = x;
  865. putlog(LOG_BOTS, "*", TRANSFER_ABORT_USERFILE);
  866. /* Note: no need to unlink the xfer file, as it's already unlinked. */
  867. xnick[0] = 0;
  868. /* Drop that bot */
  869. dprintf(-dcc[y].sock, "bye\n");
  870. egg_snprintf(s, sizeof s, TRANSFER_USERFILE_DISCON,
  871. dcc[y].nick);
  872. botnet_send_unlinked(y, dcc[y].nick, s);
  873. chatout("*** %s\n", s);
  874. if (y != idx) {
  875. killsock(dcc[y].sock);
  876. lostdcc(y);
  877. }
  878. killsock(dcc[idx].sock);
  879. lostdcc(idx);
  880. return;
  881. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  882. int x, y = 0;
  883. for (x = 0; x < dcc_total; x++)
  884. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  885. (dcc[x].type->flags & DCT_BOT))
  886. y = x;
  887. putlog(LOG_BOTS, "*", "Lost binary transfer; aborting.");
  888. /* Note: no need to unlink the xfer file, as it's already unlinked. */
  889. xnick[0] = 0;
  890. /* Drop that bot */
  891. dcc[y].status &= ~STAT_SENDINGU;
  892. #ifdef HUB
  893. bupdating = 0;
  894. #endif
  895. // dprintf(-dcc[y].sock, "bye\n");
  896. // egg_snprintf(s, sizeof s, "Disconnected %s (aborted binary transfer)",
  897. // dcc[y].nick);
  898. // botnet_send_unlinked(y, dcc[y].nick, s);
  899. // chatout("*** %s\n", s);
  900. // if (y != idx) {
  901. // killsock(dcc[y].sock);
  902. // lostdcc(y);
  903. // }
  904. killsock(dcc[idx].sock);
  905. lostdcc(idx);
  906. return;
  907. } else {
  908. struct userrec *u;
  909. /* Call `lost' DCC trigger now.
  910. */
  911. egg_snprintf(s, sizeof s, "%s!%s", dcc[idx].nick, dcc[idx].host);
  912. u = get_user_by_host(s);
  913. check_toutlost(u, dcc[idx].nick, dcc[idx].u.xfer->dir,
  914. dcc[idx].u.xfer->acked, dcc[idx].u.xfer->length,
  915. BT_lost);
  916. putlog(LOG_FILES, "*",TRANSFER_LOST_DCCGET,
  917. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  918. wipe_tmp_filename(dcc[idx].u.xfer->filename, idx);
  919. strcpy(xnick, dcc[idx].nick);
  920. }
  921. killsock(dcc[idx].sock);
  922. lostdcc(idx);
  923. /* Send next queued file if there is one */
  924. if (!at_limit(xnick))
  925. send_next_file(xnick);
  926. }
  927. static void dcc_send(int idx, char *buf, int len)
  928. {
  929. char s[512], *b;
  930. unsigned long sent;
  931. fwrite(buf, len, 1, dcc[idx].u.xfer->f);
  932. dcc[idx].status += len;
  933. /* Put in network byte order */
  934. sent = dcc[idx].status;
  935. s[0] = (sent / (1 << 24));
  936. s[1] = (sent % (1 << 24)) / (1 << 16);
  937. s[2] = (sent % (1 << 16)) / (1 << 8);
  938. s[3] = (sent % (1 << 8));
  939. tputs(dcc[idx].sock, s, 4);
  940. dcc[idx].timeval = now;
  941. if (dcc[idx].status > dcc[idx].u.xfer->length &&
  942. dcc[idx].u.xfer->length > 0) {
  943. dprintf(DP_HELP,TRANSFER_BOGUS_FILE_LENGTH, dcc[idx].nick);
  944. putlog(LOG_FILES, "*",
  945. TRANSFER_FILE_TOO_LONG,
  946. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  947. fclose(dcc[idx].u.xfer->f);
  948. b = malloc(strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  949. sprintf(b, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  950. unlink(b);
  951. free(b);
  952. killsock(dcc[idx].sock);
  953. lostdcc(idx);
  954. }
  955. }
  956. static void transfer_get_timeout(int i)
  957. {
  958. char xx[1024];
  959. fclose(dcc[i].u.xfer->f);
  960. if (strcmp(dcc[i].nick, "*users") == 0) {
  961. int x, y = 0;
  962. for (x = 0; x < dcc_total; x++)
  963. if ((!egg_strcasecmp(dcc[x].nick, dcc[i].host)) &&
  964. (dcc[x].type->flags & DCT_BOT))
  965. y = x;
  966. if (y != 0) {
  967. dcc[y].status &= ~STAT_SENDING;
  968. dcc[y].status &= ~STAT_SHARE;
  969. }
  970. unlink(dcc[i].u.xfer->filename);
  971. putlog(LOG_BOTS, "*",TRANSFER_USERFILE_TIMEOUT);
  972. dprintf(y, "bye\n");
  973. egg_snprintf(xx, sizeof xx,TRANSFER_DICONNECT_TIMEOUT,
  974. dcc[y].nick);
  975. botnet_send_unlinked(y, dcc[y].nick, xx);
  976. chatout("*** %s\n", xx);
  977. if (y < i) {
  978. int t = y;
  979. y = i;
  980. i = t;
  981. }
  982. killsock(dcc[y].sock);
  983. lostdcc(y);
  984. xx[0] = 0;
  985. } else if (strcmp(dcc[i].nick, "*binary") == 0) {
  986. int x, y = 0;
  987. for (x = 0; x < dcc_total; x++)
  988. if ((!egg_strcasecmp(dcc[x].nick, dcc[i].host)) &&
  989. (dcc[x].type->flags & DCT_BOT))
  990. y = x;
  991. if (y != 0) {
  992. dcc[y].status &= ~STAT_SENDINGU;
  993. }
  994. putlog(LOG_BOTS, "*","Timeout on binary transfer.");
  995. dprintf(y, "bye\n");
  996. egg_snprintf(xx, sizeof xx,"Disconnected %s (timed-out binary transfer)",
  997. dcc[y].nick);
  998. botnet_send_unlinked(y, dcc[y].nick, xx);
  999. chatout("*** %s\n", xx);
  1000. if (y < i) {
  1001. int t = y;
  1002. y = i;
  1003. i = t;
  1004. }
  1005. killsock(dcc[y].sock);
  1006. lostdcc(y);
  1007. xx[0] = 0;
  1008. } else {
  1009. char *p;
  1010. struct userrec *u;
  1011. p = strrchr(dcc[i].u.xfer->origname, '/');
  1012. dprintf(DP_HELP, TRANSFER_NOTICE_TIMEOUT,
  1013. dcc[i].nick, p ? p + 1 : dcc[i].u.xfer->origname);
  1014. /* Call DCC `timeout' trigger now.
  1015. */
  1016. egg_snprintf(xx, sizeof xx, "%s!%s", dcc[i].nick, dcc[i].host);
  1017. u = get_user_by_host(xx);
  1018. check_toutlost(u, dcc[i].nick, dcc[i].u.xfer->dir,
  1019. dcc[i].u.xfer->acked, dcc[i].u.xfer->length, BT_tout);
  1020. putlog(LOG_FILES, "*",TRANSFER_DCC_GET_TIMEOUT,
  1021. p ? p + 1 : dcc[i].u.xfer->origname, dcc[i].nick, dcc[i].status,
  1022. dcc[i].u.xfer->length);
  1023. wipe_tmp_filename(dcc[i].u.xfer->filename, i);
  1024. strcpy(xx, dcc[i].nick);
  1025. }
  1026. killsock(dcc[i].sock);
  1027. lostdcc(i);
  1028. if (!at_limit(xx))
  1029. send_next_file(xx);
  1030. }
  1031. static void tout_dcc_send(int idx)
  1032. {
  1033. if (!strcmp(dcc[idx].nick, "*users")) {
  1034. int x, y = 0;
  1035. for (x = 0; x < dcc_total; x++)
  1036. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  1037. (dcc[x].type->flags & DCT_BOT))
  1038. y = x;
  1039. if (y != 0) {
  1040. dcc[y].status &= ~STAT_GETTING;
  1041. dcc[y].status &= ~STAT_SHARE;
  1042. }
  1043. unlink(dcc[idx].u.xfer->filename);
  1044. putlog(LOG_BOTS, "*", TRANSFER_USERFILE_TIMEOUT);
  1045. } else if (!strcmp(dcc[idx].nick, "*binary")) {
  1046. int x, y = 0;
  1047. for (x = 0; x < dcc_total; x++)
  1048. if (!egg_strcasecmp(dcc[x].nick, dcc[idx].host) &&
  1049. (dcc[x].type->flags & DCT_BOT))
  1050. y = x;
  1051. if (y != 0) {
  1052. dcc[y].status &= ~STAT_GETTINGU;
  1053. }
  1054. putlog(LOG_BOTS, "*", "Timeout on binary transfer.");
  1055. } else {
  1056. char *buf;
  1057. dprintf(DP_HELP,TRANSFER_NOTICE_TIMEOUT,
  1058. dcc[idx].nick, dcc[idx].u.xfer->origname);
  1059. putlog(LOG_FILES, "*",TRANSFER_DCC_SEND_TIMEOUT,
  1060. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].status,
  1061. dcc[idx].u.xfer->length);
  1062. buf = malloc(strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1);
  1063. sprintf(buf, "%s%s", tempdir, dcc[idx].u.xfer->filename);
  1064. unlink(buf);
  1065. free(buf);
  1066. }
  1067. killsock(dcc[idx].sock);
  1068. lostdcc(idx);
  1069. }
  1070. static void display_dcc_get(int idx, char *buf)
  1071. {
  1072. if (dcc[idx].status == dcc[idx].u.xfer->length)
  1073. sprintf(buf,TRANSFER_SEND, dcc[idx].u.xfer->acked,
  1074. dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
  1075. else
  1076. sprintf(buf,TRANSFER_SEND, dcc[idx].status,
  1077. dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
  1078. }
  1079. static void display_dcc_get_p(int idx, char *buf)
  1080. {
  1081. sprintf(buf,TRANSFER_SEND_WAITED, now - dcc[idx].timeval,
  1082. dcc[idx].u.xfer->origname);
  1083. }
  1084. static void display_dcc_send(int idx, char *buf)
  1085. {
  1086. sprintf(buf,TRANSFER_SEND, dcc[idx].status,
  1087. dcc[idx].u.xfer->length, dcc[idx].u.xfer->origname);
  1088. }
  1089. static void display_dcc_fork_send(int idx, char *buf)
  1090. {
  1091. sprintf(buf,TRANSFER_CONN_SEND);
  1092. }
  1093. static void kill_dcc_xfer(int idx, void *x)
  1094. {
  1095. register struct xfer_info *p = (struct xfer_info *) x;
  1096. if (p->filename)
  1097. free(p->filename);
  1098. /* We need to check if origname points to filename before
  1099. * attempting to free the memory.
  1100. */
  1101. if (p->origname && p->origname != p->filename)
  1102. free(p->origname);
  1103. free(x);
  1104. }
  1105. static void out_dcc_xfer(int idx, char *buf, void *x)
  1106. {
  1107. }
  1108. static void outdone_dcc_xfer(int idx)
  1109. {
  1110. if (dcc[idx].u.xfer->block_pending)
  1111. dcc[idx].u.xfer->block_pending =
  1112. pump_file_to_sock(dcc[idx].u.xfer->f, dcc[idx].sock,
  1113. dcc[idx].u.xfer->block_pending);
  1114. }
  1115. static struct dcc_table DCC_SEND =
  1116. {
  1117. "SEND",
  1118. DCT_FILETRAN | DCT_FILESEND | DCT_VALIDIDX,
  1119. eof_dcc_send,
  1120. dcc_send,
  1121. &wait_dcc_xfer,
  1122. tout_dcc_send,
  1123. display_dcc_send,
  1124. kill_dcc_xfer,
  1125. out_dcc_xfer
  1126. };
  1127. static void dcc_fork_send(int idx, char *x, int y);
  1128. static struct dcc_table DCC_FORK_SEND =
  1129. {
  1130. "FORK_SEND",
  1131. DCT_FILETRAN | DCT_FORKTYPE | DCT_FILESEND | DCT_VALIDIDX,
  1132. eof_dcc_fork_send,
  1133. dcc_fork_send,
  1134. &wait_dcc_xfer,
  1135. eof_dcc_fork_send,
  1136. display_dcc_fork_send,
  1137. kill_dcc_xfer,
  1138. out_dcc_xfer
  1139. };
  1140. static void dcc_fork_send(int idx, char *x, int y)
  1141. {
  1142. char s1[121];
  1143. if (dcc[idx].type != &DCC_FORK_SEND)
  1144. return;
  1145. dcc[idx].type = &DCC_SEND;
  1146. dcc[idx].u.xfer->start_time = now;
  1147. egg_snprintf(s1, sizeof s1, "%s!%s", dcc[idx].nick, dcc[idx].host);
  1148. if (strcmp(dcc[idx].nick, "*users") && strcmp(dcc[idx].nick, "*binary"))
  1149. putlog(LOG_MISC, "*", TRANSFER_DCC_CONN, dcc[idx].u.xfer->origname, s1);
  1150. }
  1151. static struct dcc_table DCC_GET =
  1152. {
  1153. "GET",
  1154. DCT_FILETRAN | DCT_VALIDIDX,
  1155. eof_dcc_get,
  1156. dcc_get,
  1157. &wait_dcc_xfer,
  1158. transfer_get_timeout,
  1159. display_dcc_get,
  1160. kill_dcc_xfer,
  1161. out_dcc_xfer,
  1162. outdone_dcc_xfer
  1163. };
  1164. static struct dcc_table DCC_GET_PENDING =
  1165. {
  1166. "GET_PENDING",
  1167. DCT_FILETRAN | DCT_VALIDIDX,
  1168. eof_dcc_get,
  1169. dcc_get_pending,
  1170. &wait_dcc_xfer,
  1171. transfer_get_timeout,
  1172. display_dcc_get_p,
  1173. kill_dcc_xfer,
  1174. out_dcc_xfer
  1175. };
  1176. static void dcc_get_pending(int idx, char *buf, int len)
  1177. {
  1178. unsigned long ip;
  1179. unsigned short port;
  1180. int i;
  1181. char s[UHOSTLEN];
  1182. i = answer(dcc[idx].sock, s, &ip, &port, 1);
  1183. killsock(dcc[idx].sock);
  1184. dcc[idx].sock = i;
  1185. dcc[idx].addr = ip;
  1186. dcc[idx].port = (int) port;
  1187. if (dcc[idx].sock == -1) {
  1188. neterror(s);
  1189. dprintf(DP_HELP, TRANSFER_NOTICE_BAD_CONN, dcc[idx].nick, s);
  1190. putlog(LOG_FILES, "*", TRANSFER_LOG_BAD_CONN,
  1191. dcc[idx].u.xfer->origname, dcc[idx].nick, dcc[idx].host);
  1192. fclose(dcc[idx].u.xfer->f);
  1193. lostdcc(idx);
  1194. return;
  1195. }
  1196. dcc[idx].type = &DCC_GET;
  1197. dcc[idx].u.xfer->ack_type = XFER_ACK_UNKNOWN;
  1198. /*
  1199. * Note: The file was already opened and dcc[idx].u.xfer->f may be
  1200. * used immediately. Leave it opened until the file transfer
  1201. * is complete.
  1202. */
  1203. /* Are we resuming? */
  1204. if (dcc[idx].u.xfer->type == XFER_RESUME_PEND) {
  1205. int l;
  1206. if (dcc_block == 0 || dcc[idx].u.xfer->length < dcc_block) {
  1207. l = dcc[idx].u.xfer->length - dcc[idx].u.xfer->offset;
  1208. dcc[idx].status = dcc[idx].u.xfer->length;
  1209. } else {
  1210. l = dcc_block;
  1211. dcc[idx].status = dcc[idx].u.xfer->offset + dcc_block;
  1212. }
  1213. /* Seek forward ... */
  1214. fseek(dcc[idx].u.xfer->f, dcc[idx].u.xfer->offset, SEEK_SET);
  1215. dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
  1216. dcc[idx].sock, l);
  1217. dcc[idx].u.xfer->type = XFER_RESUME;
  1218. } else {
  1219. dcc[idx].u.xfer->offset = 0;
  1220. /* If we're resending the data, wait for the client's response first,
  1221. * before sending anything ourself.
  1222. */
  1223. if (dcc[idx].u.xfer->type != XFER_RESEND_PEND) {
  1224. if (dcc_block == 0 || dcc[idx].u.xfer->length < dcc_block)
  1225. dcc[idx].status = dcc[idx].u.xfer->length;
  1226. else
  1227. dcc[idx].status = dcc_block;
  1228. dcc[idx].u.xfer->block_pending = pump_file_to_sock(dcc[idx].u.xfer->f,
  1229. dcc[idx].sock,
  1230. dcc[idx].status);
  1231. } else
  1232. dcc[idx].status = 0;
  1233. }
  1234. dcc[idx].timeval = dcc[idx].u.xfer->start_time = now;
  1235. }
  1236. /* Starts a new DCC SEND or DCC RESEND connection to `nick', transferring
  1237. * `filename' from `dir'.
  1238. *
  1239. * Use raw_dcc_resend() and raw_dcc_send() instead of this function.
  1240. */
  1241. static int raw_dcc_resend_send(char *filename, char *nick, char *from,
  1242. char *dir, int resend)
  1243. {
  1244. int zz, port, i;
  1245. char *nfn, *buf = NULL;
  1246. long dccfilesize;
  1247. FILE *f, *dccfile;
  1248. sdprintf("raw_dcc_resend_send()");
  1249. zz = (-1);
  1250. dccfile = fopen(filename,"r");
  1251. fseek(dccfile, 0, SEEK_END);
  1252. dccfilesize = ftell(dccfile);
  1253. fclose(dccfile);
  1254. /* File empty?! */
  1255. if (dccfilesize == 0)
  1256. return DCCSEND_FEMPTY;
  1257. if (reserved_port_min > 0 && reserved_port_min < reserved_port_max) {
  1258. for (port = reserved_port_min; port <= reserved_port_max; port++)
  1259. #ifdef USE_IPV6
  1260. if ((zz = open_listen_by_af(&port, AF_INET)) != -1) /* no idea how we want to handle this -poptix 02/03/03 */
  1261. #else
  1262. if ((zz = open_listen(&port)) != -1)
  1263. #endif /* USE_IPV6 */
  1264. break;
  1265. } else {
  1266. port = reserved_port_min;
  1267. #ifdef USE_IPV6
  1268. zz = open_listen_by_af(&port, AF_INET);
  1269. #else
  1270. zz = open_listen(&port);
  1271. #endif /* USE_IPV6 */
  1272. }
  1273. if (zz == (-1))
  1274. return DCCSEND_NOSOCK;
  1275. nfn = strrchr(dir, '/');
  1276. if (nfn == NULL)
  1277. nfn = dir;
  1278. else
  1279. nfn++;
  1280. f = fopen(filename, "r");
  1281. if (!f)
  1282. return DCCSEND_BADFN;
  1283. if ((i = new_dcc(&DCC_GET_PENDING, sizeof(struct xfer_info))) == -1)
  1284. return DCCSEND_FULL;
  1285. dcc[i].sock = zz;
  1286. dcc[i].addr = (IP) (-559026163);
  1287. dcc[i].port = port;
  1288. strcpy(dcc[i].nick, nick);
  1289. strcpy(dcc[i].host, "irc");
  1290. dcc[i].u.xfer->filename = calloc(1, strlen(filename) + 1);
  1291. strcpy(dcc[i].u.xfer->filename, filename);
  1292. if (strchr(nfn, ' '))
  1293. nfn = buf = replace_spaces(nfn);
  1294. dcc[i].u.xfer->origname = calloc(1, strlen(nfn) + 1);
  1295. strcpy(dcc[i].u.xfer->origname, nfn);
  1296. strncpyz(dcc[i].u.xfer->from, from, NICKLEN);
  1297. strncpyz(dcc[i].u.xfer->dir, dir, DIRLEN);
  1298. dcc[i].u.xfer->length = dccfilesize;
  1299. dcc[i].timeval = now;
  1300. dcc[i].u.xfer->f = f;
  1301. dcc[i].u.xfer->type = resend ? XFER_RESEND_PEND : XFER_SEND;
  1302. if (nick[0] != '*') {
  1303. dprintf(DP_HELP, "PRIVMSG %s :\001DCC %sSEND %s %lu %d %lu\001\n", nick,
  1304. resend ? "RE" : "", nfn,
  1305. iptolong(natip[0] ? (IP) inet_addr(natip) : getmyip()), port,
  1306. dccfilesize);
  1307. putlog(LOG_FILES, "*",TRANSFER_BEGIN_DCC, resend ? TRANSFER_RE : "",
  1308. nfn, nick);
  1309. }
  1310. if (buf)
  1311. free(buf);
  1312. return DCCSEND_OK;
  1313. }
  1314. /* Starts a DCC RESEND connection.
  1315. */
  1316. static int raw_dcc_resend(char *filename, char *nick, char *from, char *dir)
  1317. {
  1318. return raw_dcc_resend_send(filename, nick, from, dir, 1);
  1319. }
  1320. /* Starts a DCC_SEND connection.
  1321. */
  1322. static int raw_dcc_send(char *filename, char *nick, char *from, char *dir)
  1323. {
  1324. return raw_dcc_resend_send(filename, nick, from, dir, 0);
  1325. }
  1326. /*
  1327. * fstat functions
  1328. */
  1329. static int fstat_unpack(struct userrec *u, struct user_entry *e)
  1330. {
  1331. char *par, *arg;
  1332. struct filesys_stats *fs;
  1333. fs = malloc(sizeof(struct filesys_stats));
  1334. egg_bzero(fs, sizeof(struct filesys_stats));
  1335. par = e->u.list->extra;
  1336. arg = newsplit(&par);
  1337. if (arg[0])
  1338. fs->uploads = atoi(arg);
  1339. arg = newsplit(&par);
  1340. if (arg[0])
  1341. fs->upload_ks = atoi(arg);
  1342. arg = newsplit(&par);
  1343. if (arg[0])
  1344. fs->dnloads = atoi(arg);
  1345. arg = newsplit(&par);
  1346. if (arg[0])
  1347. fs->dnload_ks = atoi(arg);
  1348. list_type_kill(e->u.list);
  1349. e->u.extra = fs;
  1350. return 1;
  1351. }
  1352. static int fstat_pack(struct userrec *u, struct user_entry *e)
  1353. {
  1354. register struct filesys_stats *fs;
  1355. struct list_type *l = malloc(sizeof(struct list_type));
  1356. fs = e->u.extra;
  1357. l->extra = malloc(41);
  1358. egg_snprintf(l->extra, 41, "%09u %09u %09u %09u",
  1359. fs->uploads, fs->upload_ks, fs->dnloads, fs->dnload_ks);
  1360. l->next = NULL;
  1361. e->u.list = l;
  1362. free(fs);
  1363. return 1;
  1364. }
  1365. static int fstat_write_userfile(FILE *f, struct userrec *u,
  1366. struct user_entry *e)
  1367. {
  1368. register struct filesys_stats *fs;
  1369. fs = e->u.extra;
  1370. if (lfprintf(f, "--FSTAT %09u %09u %09u %09u\n",
  1371. fs->uploads, fs->upload_ks,
  1372. fs->dnloads, fs->dnload_ks) == EOF)
  1373. return 0;
  1374. return 1;
  1375. }
  1376. static int fstat_set(struct userrec *u, struct user_entry *e, void *buf)
  1377. {
  1378. register struct filesys_stats *fs = buf;
  1379. if (e->u.extra != fs) {
  1380. if (e->u.extra)
  1381. free(e->u.extra);
  1382. e->u.extra = fs;
  1383. } else if (!fs) /* e->u.extra == NULL && fs == NULL */
  1384. return 1;
  1385. if (!noshare && !(u->flags & (USER_BOT | USER_UNSHARED))) {
  1386. if (fs)
  1387. /* Don't check here for something like
  1388. * ofs->uploads != fs->uploads || ofs->upload_ks != fs->upload_ks ||
  1389. * ofs->dnloads != fs->dnloads || ofs->dnload_ks != fs->dnload_ks
  1390. * someone could do:
  1391. * e->u.extra->uploads = 12345;
  1392. * fs = malloc(sizeof(struct filesys_stats));
  1393. * memcpy (...e->u.extra...fs...);
  1394. * set_user(&USERENTRY_FSTAT, u, fs);
  1395. * then we wouldn't detect here that something's changed...
  1396. * --rtc
  1397. */
  1398. shareout (NULL, "ch fstat %09u %09u %09u %09u\n",
  1399. fs->uploads, fs->upload_ks, fs->dnloads, fs->dnload_ks);
  1400. else
  1401. shareout (NULL, "ch fstat r\n");
  1402. }
  1403. return 1;
  1404. }
  1405. static int fstat_tcl_get(Tcl_Interp *irp, struct userrec *u,
  1406. struct user_entry *e, int argc, char **argv)
  1407. {
  1408. register struct filesys_stats *fs;
  1409. char d[50];
  1410. BADARGS(3, 4, " handle FSTAT ?u/d?");
  1411. fs = e->u.extra;
  1412. if (argc == 3)
  1413. egg_snprintf(d, sizeof d, "%u %u %u %u", fs->uploads, fs->upload_ks,
  1414. fs->dnloads, fs->dnload_ks);
  1415. else
  1416. switch (argv[3][0]) {
  1417. case 'u':
  1418. egg_snprintf(d, sizeof d, "%u %u", fs->uploads, fs->upload_ks);
  1419. break;
  1420. case 'd':
  1421. egg_snprintf(d, sizeof d, "%u %u", fs->dnloads, fs->dnload_ks);
  1422. break;
  1423. }
  1424. Tcl_AppendResult(irp, d, NULL);
  1425. return TCL_OK;
  1426. }
  1427. static int fstat_kill(struct user_entry *e)
  1428. {
  1429. if (e->u.extra)
  1430. free(e->u.extra);
  1431. free(e);
  1432. return 1;
  1433. }
  1434. static void fstat_display(int idx, struct user_entry *e, struct userrec *u)
  1435. {
  1436. struct filesys_stats *fs;
  1437. fs = e->u.extra;
  1438. dprintf(idx, " FILES: %u download%s (%luk), %u upload%s (%luk)\n",
  1439. fs->dnloads, (fs->dnloads == 1) ? "" : "s", fs->dnload_ks,
  1440. fs->uploads, (fs->uploads == 1) ? "" : "s", fs->upload_ks);
  1441. }
  1442. static int fstat_gotshare(struct userrec *u, struct user_entry *e,
  1443. char *par, int idx);
  1444. static int fstat_dupuser(struct userrec *u, struct userrec *o,
  1445. struct user_entry *e);
  1446. static void stats_add_dnload(struct userrec *u, unsigned long bytes);
  1447. static void stats_add_upload(struct userrec *u, unsigned long bytes);
  1448. static int fstat_tcl_set(Tcl_Interp *irp, struct userrec *u,
  1449. struct user_entry *e, int argc, char **argv);
  1450. static struct user_entry_type USERENTRY_FSTAT =
  1451. {
  1452. NULL,
  1453. fstat_gotshare,
  1454. fstat_dupuser,
  1455. fstat_unpack,
  1456. fstat_pack,
  1457. fstat_write_userfile,
  1458. fstat_kill,
  1459. NULL,
  1460. fstat_set,
  1461. fstat_tcl_get,
  1462. fstat_tcl_set,
  1463. fstat_display,
  1464. "FSTAT"
  1465. };
  1466. static int fstat_gotshare(struct userrec *u, struct user_entry *e,
  1467. char *par, int idx)
  1468. {
  1469. char *p;
  1470. struct filesys_stats *fs;
  1471. noshare = 1;
  1472. switch (par[0]) {
  1473. case 'u':
  1474. case 'd':
  1475. /* No stats_add_up/dnload here, it's already been sent... --rtc */
  1476. break;
  1477. case 'r':
  1478. set_user (&USERENTRY_FSTAT, u, NULL);
  1479. break;
  1480. default:
  1481. if (!(fs = e->u.extra)) {
  1482. fs = malloc(sizeof(struct filesys_stats));
  1483. egg_bzero(fs, sizeof(struct filesys_stats));
  1484. }
  1485. p = newsplit (&par);
  1486. if (p[0])
  1487. fs->uploads = atoi (p);
  1488. p = newsplit (&par);
  1489. if (p[0])
  1490. fs->upload_ks = atoi (p);
  1491. p = newsplit (&par);
  1492. if (p[0])
  1493. fs->dnloads = atoi (p);
  1494. p = newsplit (&par);
  1495. if (p[0])
  1496. fs->dnload_ks = atoi (p);
  1497. set_user(&USERENTRY_FSTAT, u, fs);
  1498. break;
  1499. }
  1500. noshare = 0;
  1501. return 1;
  1502. }
  1503. static int fstat_dupuser(struct userrec *u, struct userrec *o,
  1504. struct user_entry *e)
  1505. {
  1506. struct filesys_stats *fs;
  1507. if (e->u.extra) {
  1508. fs = malloc(sizeof(struct filesys_stats));
  1509. my_memcpy(fs, e->u.extra, sizeof(struct filesys_stats));
  1510. return set_user(&USERENTRY_FSTAT, u, fs);
  1511. }
  1512. return 0;
  1513. }
  1514. static void stats_add_dnload(struct userrec *u, unsigned long bytes)
  1515. {
  1516. struct user_entry *ue;
  1517. register struct filesys_stats *fs;
  1518. if (u) {
  1519. if (!(ue = find_user_entry (&USERENTRY_FSTAT, u)) ||
  1520. !(fs = ue->u.extra)) {
  1521. fs = malloc(sizeof(struct filesys_stats));
  1522. egg_bzero(fs, sizeof(struct filesys_stats));
  1523. }
  1524. fs->dnloads++;
  1525. fs->dnload_ks += ((bytes + 512) / 1024);
  1526. set_user(&USERENTRY_FSTAT, u, fs);
  1527. /* No shareout here, set_user already sends info... --rtc */
  1528. }
  1529. }
  1530. static void stats_add_upload(struct userrec *u, unsigned long bytes)
  1531. {
  1532. struct user_entry *ue;
  1533. register struct filesys_stats *fs;
  1534. if (u) {
  1535. if (!(ue = find_user_entry (&USERENTRY_FSTAT, u)) ||
  1536. !(fs = ue->u.extra)) {
  1537. fs = malloc(sizeof(struct filesys_stats));
  1538. egg_bzero(fs, sizeof(struct filesys_stats));
  1539. }
  1540. fs->uploads++;
  1541. fs->upload_ks += ((bytes + 512) / 1024);
  1542. set_user(&USERENTRY_FSTAT, u, fs);
  1543. /* No shareout here, set_user already sends info... --rtc */
  1544. }
  1545. }
  1546. static int fstat_tcl_set(Tcl_Interp *irp, struct userrec *u,
  1547. struct user_entry *e, int argc, char **argv)
  1548. {
  1549. register struct filesys_stats *fs;
  1550. int f = 0, k = 0;
  1551. BADARGS(4, 6, " handle FSTAT u/d ?files ?ks??");
  1552. if (argc > 4)
  1553. f = atoi(argv[4]);
  1554. if (argc > 5)
  1555. k = atoi(argv[5]);
  1556. switch (argv[3][0]) {
  1557. case 'u':
  1558. case 'd':
  1559. if (!(fs = e->u.extra)) {
  1560. fs = malloc(sizeof(struct filesys_stats));
  1561. egg_bzero(fs, sizeof(struct filesys_stats));
  1562. }
  1563. switch (argv[3][0]) {
  1564. case 'u':
  1565. fs->uploads = f;
  1566. fs->upload_ks = k;
  1567. break;
  1568. case 'd':
  1569. fs->dnloads = f;
  1570. fs->dnload_ks = k;
  1571. break;
  1572. }
  1573. set_user (&USERENTRY_FSTAT, u, fs);
  1574. break;
  1575. case 'r':
  1576. set_user (&USERENTRY_FSTAT, u, NULL);
  1577. break;
  1578. }
  1579. return TCL_OK;
  1580. }
  1581. /*
  1582. * CTCP functions
  1583. */
  1584. /* This handles DCC RESUME requests.
  1585. */
  1586. static int ctcp_DCC_RESUME(char *nick, char *from, char *handle,
  1587. char *object, char *keyword, char *text)
  1588. {
  1589. char *action, *fn, buf[512], *msg = buf;
  1590. int i, port;
  1591. unsigned long offset;
  1592. strcpy(msg, text);
  1593. action = newsplit(&msg);
  1594. if (egg_strcasecmp(action, "RESUME"))
  1595. return 0;
  1596. fn = newsplit(&msg);
  1597. port = atoi(newsplit(&msg));
  1598. offset = my_atoul(newsplit(&msg));
  1599. /* Search for existing SEND */
  1600. for (i = 0; i < dcc_total; i++)
  1601. if ((dcc[i].type == &DCC_GET_PENDING) &&
  1602. (!rfc_casecmp(dcc[i].nick, nick)) && (dcc[i].port == port))
  1603. break;
  1604. /* No matching transfer found? */
  1605. if (i == dcc_total)
  1606. return 0;
  1607. if (dcc[i].u.xfer->length <= offset) {
  1608. char *p = strrchr(dcc[i].u.xfer->origname, '/');
  1609. dprintf(DP_HELP,TRANSFER_DCC_IGNORED,
  1610. nick, p ? p + 1 : dcc[i].u.xfer->origname);
  1611. return 0;
  1612. }
  1613. dcc[i].u.xfer->type = XFER_RESUME_PEND;
  1614. dcc[i].u.xfer->offset = offset;
  1615. dprintf(DP_HELP, "PRIVMSG %s :\001DCC ACCEPT %s %d %u\001\n", nick,
  1616. fn, port, offset);
  1617. /* Now we wait for the client to connect. */
  1618. return 1;
  1619. }
  1620. static cmd_t transfer_ctcps[] =
  1621. {
  1622. {"DCC", "", ctcp_DCC_RESUME, "transfer:DCC"},
  1623. {NULL, NULL, NULL, NULL}
  1624. };
  1625. /* Add our CTCP bindings if the server module is loaded. */
  1626. static int server_transfer_setup(char *mod)
  1627. {
  1628. add_builtins("ctcp", transfer_ctcps);
  1629. return 1;
  1630. }
  1631. static cmd_t transfer_load[] =
  1632. {
  1633. {"server", "", server_transfer_setup, NULL},
  1634. {NULL, "", NULL, NULL}
  1635. };
  1636. /*
  1637. * Module functions
  1638. */
  1639. static void transfer_report(int idx, int details)
  1640. {
  1641. if (details) {
  1642. dprintf(idx,TRANSFER_STAT_BLOCK,
  1643. dcc_block, (dcc_block == 0) ? " (turbo dcc)" : "", dcc_limit);
  1644. }
  1645. }
  1646. EXPORT_SCOPE char *transfer_start();
  1647. static Function transfer_table[] =
  1648. {
  1649. (Function) transfer_start,
  1650. (Function) NULL,
  1651. (Function) 0,
  1652. (Function) transfer_report,
  1653. /* 4- 7 */
  1654. (Function) & DCC_FORK_SEND, /* struct dcc_table */
  1655. (Function) at_limit,
  1656. (Function) & copy_to_tmp, /* int */
  1657. (Function) fileq_cancel,
  1658. /* 8 - 11 */
  1659. (Function) queue_file,
  1660. (Function) raw_dcc_send,
  1661. (Function) show_queued_files,
  1662. (Function) wild_match_file,
  1663. /* 12 - 15 */
  1664. (Function) wipe_tmp_filename,
  1665. (Function) & DCC_GET, /* struct dcc_table */
  1666. (Function) 0,
  1667. (Function) 0,
  1668. /* 16 - 19 */
  1669. (Function) & USERENTRY_FSTAT, /* struct user_entry_type */
  1670. (Function) & quiet_reject, /* int */
  1671. (Function) raw_dcc_resend,
  1672. (Function) 0,
  1673. /* 20 - 23 */
  1674. (Function) 0,
  1675. };
  1676. char *transfer_start(Function *global_funcs)
  1677. {
  1678. global = global_funcs;
  1679. fileq = NULL;
  1680. module_register(MODULE_NAME, transfer_table, 2, 2);
  1681. if (!(update_funcs = module_depend(MODULE_NAME, "update", 0, 0))) {
  1682. module_undepend(MODULE_NAME);
  1683. return "This module requires update module 0.0 or later.";
  1684. }
  1685. add_builtins("load", transfer_load);
  1686. server_transfer_setup(NULL);
  1687. BT_rcvd = bind_table_add("rcvd", 3, "Uss", MATCH_MASK, BIND_STACKABLE);
  1688. BT_sent = bind_table_add("sent", 3, "Uss", MATCH_MASK, BIND_STACKABLE);
  1689. BT_lost = bind_table_add("lost", 5, "Ussii", MATCH_MASK, BIND_STACKABLE);
  1690. BT_tout = bind_table_add("tout", 5, "Ussii", MATCH_MASK, BIND_STACKABLE);
  1691. USERENTRY_FSTAT.get = def_get;
  1692. add_entry_type(&USERENTRY_FSTAT);
  1693. return NULL;
  1694. }