transfer.c 48 KB

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