tclmisc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * tclmisc.c -- handles:
  3. * Tcl stubs for everything else
  4. *
  5. */
  6. #include <sys/stat.h>
  7. #include "main.h"
  8. #include "modules.h"
  9. #include "tandem.h"
  10. #include "md5/md5.h"
  11. #ifdef HAVE_UNAME
  12. #include <sys/utsname.h>
  13. #endif
  14. extern p_tcl_bind_list bind_table_list;
  15. extern tcl_timer_t *timer, *utimer;
  16. extern struct dcc_t *dcc;
  17. extern char origbotname[], botnetnick[], quit_msg[];
  18. extern struct userrec *userlist;
  19. extern time_t now;
  20. extern module_entry *module_list;
  21. extern int max_logs, timesync;
  22. extern log_t *logs;
  23. extern Tcl_Interp *interp;
  24. int expmem_tclmisc()
  25. {
  26. int i, tot = 0;
  27. for (i = 0; i < max_logs; i++)
  28. if (logs[i].filename != NULL) {
  29. tot += strlen(logs[i].filename) + 1;
  30. tot += strlen(logs[i].chname) + 1;
  31. }
  32. return tot;
  33. }
  34. /*
  35. * Logging
  36. */
  37. /* logfile [<modes> <channel> <filename>] */
  38. #ifdef HUB
  39. static int tcl_logfile STDVAR
  40. {
  41. int i;
  42. char s[151];
  43. BADARGS(1, 4, " ?logModes channel logFile?");
  44. if (argc == 1) {
  45. /* They just want a list of the logfiles and modes */
  46. for (i = 0; i < max_logs; i++)
  47. if (logs[i].filename != NULL) {
  48. strcpy(s, masktype(logs[i].mask));
  49. strcat(s, " ");
  50. strcat(s, logs[i].chname);
  51. strcat(s, " ");
  52. strcat(s, logs[i].filename);
  53. Tcl_AppendElement(interp, s);
  54. }
  55. return TCL_OK;
  56. }
  57. BADARGS(4, 4, " ?logModes channel logFile?");
  58. for (i = 0; i < max_logs; i++)
  59. if ((logs[i].filename != NULL) && (!strcmp(logs[i].filename, argv[3]))) {
  60. logs[i].flags &= ~LF_EXPIRING;
  61. logs[i].mask = logmodes(argv[1]);
  62. nfree(logs[i].chname);
  63. logs[i].chname = NULL;
  64. if (!logs[i].mask) {
  65. /* ending logfile */
  66. nfree(logs[i].filename);
  67. logs[i].filename = NULL;
  68. if (logs[i].f != NULL) {
  69. fclose(logs[i].f);
  70. logs[i].f = NULL;
  71. }
  72. logs[i].flags = 0;
  73. } else {
  74. logs[i].chname = (char *) nmalloc(strlen(argv[2]) + 1);
  75. strcpy(logs[i].chname, argv[2]);
  76. }
  77. Tcl_AppendResult(interp, argv[3], NULL);
  78. return TCL_OK;
  79. }
  80. /* Do not add logfiles without any flags to log ++rtc */
  81. if (!logmodes (argv [1])) {
  82. Tcl_AppendResult (interp, "can't remove \"", argv[3],
  83. "\" from list: no such logfile", NULL);
  84. return TCL_ERROR;
  85. }
  86. for (i = 0; i < max_logs; i++)
  87. if (logs[i].filename == NULL) {
  88. logs[i].flags = 0;
  89. logs[i].mask = logmodes(argv[1]);
  90. logs[i].filename = (char *) nmalloc(strlen(argv[3]) + 1);
  91. strcpy(logs[i].filename, argv[3]);
  92. logs[i].chname = (char *) nmalloc(strlen(argv[2]) + 1);
  93. strcpy(logs[i].chname, argv[2]);
  94. Tcl_AppendResult(interp, argv[3], NULL);
  95. return TCL_OK;
  96. }
  97. Tcl_AppendResult(interp, "reached max # of logfiles", NULL);
  98. return TCL_ERROR;
  99. }
  100. #endif
  101. static int tcl_putlog STDVAR
  102. {
  103. char logtext[501];
  104. BADARGS(2, 2, " text");
  105. strncpyz(logtext, argv[1], sizeof logtext);
  106. putlog(LOG_MISC, "*", "%s", logtext);
  107. return TCL_OK;
  108. }
  109. static int tcl_putlogl STDVAR
  110. {
  111. char logtext[501];
  112. BADARGS(2, 2, " text");
  113. strncpyz(logtext, argv[1], sizeof logtext);
  114. putlog(LOG_MISC, "@", "%s", logtext);
  115. return TCL_OK;
  116. }
  117. static int tcl_putcmdlog STDVAR
  118. {
  119. char logtext[501];
  120. BADARGS(2, 2, " text");
  121. strncpyz(logtext, argv[1], sizeof logtext);
  122. putlog(LOG_CMDS, "*", "%s", logtext);
  123. return TCL_OK;
  124. }
  125. static int tcl_putcmdlogl STDVAR
  126. {
  127. char logtext[501];
  128. BADARGS(2, 2, " text");
  129. strncpyz(logtext, argv[1], sizeof logtext);
  130. putlog(LOG_CMDS, "@", "%s", logtext);
  131. return TCL_OK;
  132. }
  133. static int tcl_putxferlog STDVAR
  134. {
  135. char logtext[501];
  136. BADARGS(2, 2, " text");
  137. strncpyz(logtext, argv[1], sizeof logtext);
  138. putlog(LOG_FILES, "*", "%s", logtext);
  139. return TCL_OK;
  140. }
  141. static int tcl_putloglev STDVAR
  142. {
  143. int lev = 0;
  144. char logtext[501];
  145. BADARGS(4, 4, " level channel text");
  146. lev = logmodes(argv[1]);
  147. if (!lev) {
  148. Tcl_AppendResult(irp, "No valid log-level given", NULL);
  149. return TCL_ERROR;
  150. }
  151. strncpyz(logtext, argv[3], sizeof logtext);
  152. putlog(lev, argv[2], "%s", logtext);
  153. return TCL_OK;
  154. }
  155. static int tcl_binds STDVAR
  156. {
  157. tcl_bind_list_t *tl, *tl_kind;
  158. tcl_bind_mask_t *tm;
  159. tcl_cmd_t *tc;
  160. char *g, flg[100], hits[11];
  161. int matching = 0;
  162. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  163. CONST char *list[5];
  164. #else
  165. char *list[5];
  166. #endif
  167. BADARGS(1, 2, " ?type/mask?");
  168. if (argv[1])
  169. tl_kind = find_bind_table(argv[1]);
  170. else
  171. tl_kind = NULL;
  172. if (!tl_kind && argv[1])
  173. matching = 1;
  174. for (tl = tl_kind ? tl_kind : bind_table_list; tl;
  175. tl = tl_kind ? 0 : tl->next) {
  176. if (tl->flags & HT_DELETED)
  177. continue;
  178. for (tm = tl->first; tm; tm = tm->next) {
  179. if (tm->flags & TBM_DELETED)
  180. continue;
  181. for (tc = tm->first; tc; tc = tc->next) {
  182. if (tc->attributes & TC_DELETED)
  183. continue;
  184. if (matching &&
  185. !wild_match_per(argv[1], tl->name) &&
  186. !wild_match_per(argv[1], tm->mask) &&
  187. !wild_match_per(argv[1], tc->func_name))
  188. continue;
  189. build_flags(flg, &(tc->flags), NULL);
  190. egg_snprintf(hits, sizeof hits, "%i", (int) tc->hits);
  191. list[0] = tl->name;
  192. list[1] = flg;
  193. list[2] = tm->mask;
  194. list[3] = hits;
  195. list[4] = tc->func_name;
  196. g = Tcl_Merge(5, list);
  197. Tcl_AppendElement(irp, g);
  198. Tcl_Free((char *) g);
  199. }
  200. }
  201. }
  202. return TCL_OK;
  203. }
  204. static int tcl_timer STDVAR
  205. {
  206. unsigned long x;
  207. char s[16];
  208. BADARGS(3, 3, " minutes command");
  209. if (atoi(argv[1]) < 0) {
  210. Tcl_AppendResult(irp, "time value must be positive", NULL);
  211. return TCL_ERROR;
  212. }
  213. if (argv[2][0] != '#') {
  214. x = add_timer(&timer, atoi(argv[1]), argv[2], 0L);
  215. egg_snprintf(s, sizeof s, "timer%lu", x);
  216. Tcl_AppendResult(irp, s, NULL);
  217. }
  218. return TCL_OK;
  219. }
  220. static int tcl_ischanhub STDVAR
  221. {
  222. #ifdef HUB
  223. Tcl_AppendResult(irp, "0", NULL);
  224. return TCL_OK;
  225. #endif
  226. if (ischanhub())
  227. Tcl_AppendResult(irp, "1", NULL);
  228. else
  229. Tcl_AppendResult(irp, "0", NULL);
  230. return TCL_OK;
  231. }
  232. static int tcl_utimer STDVAR
  233. {
  234. unsigned long x;
  235. char s[16];
  236. BADARGS(3, 3, " seconds command");
  237. if (atoi(argv[1]) < 0) {
  238. Tcl_AppendResult(irp, "time value must be positive", NULL);
  239. return TCL_ERROR;
  240. }
  241. if (argv[2][0] != '#') {
  242. x = add_timer(&utimer, atoi(argv[1]), argv[2], 0L);
  243. egg_snprintf(s, sizeof s, "timer%lu", x);
  244. Tcl_AppendResult(irp, s, NULL);
  245. }
  246. return TCL_OK;
  247. }
  248. static int tcl_killtimer STDVAR
  249. {
  250. BADARGS(2, 2, " timerID");
  251. if (strncmp(argv[1], "timer", 5)) {
  252. Tcl_AppendResult(irp, "argument is not a timerID", NULL);
  253. return TCL_ERROR;
  254. }
  255. if (remove_timer(&timer, atol(&argv[1][5])))
  256. return TCL_OK;
  257. Tcl_AppendResult(irp, "invalid timerID", NULL);
  258. return TCL_ERROR;
  259. }
  260. static int tcl_killutimer STDVAR
  261. {
  262. BADARGS(2, 2, " timerID");
  263. if (strncmp(argv[1], "timer", 5)) {
  264. Tcl_AppendResult(irp, "argument is not a timerID", NULL);
  265. return TCL_ERROR;
  266. }
  267. if (remove_timer(&utimer, atol(&argv[1][5])))
  268. return TCL_OK;
  269. Tcl_AppendResult(irp, "invalid timerID", NULL);
  270. return TCL_ERROR;
  271. }
  272. static int tcl_timers STDVAR
  273. {
  274. BADARGS(1, 1, "");
  275. list_timers(irp, timer);
  276. return TCL_OK;
  277. }
  278. static int tcl_utimers STDVAR
  279. {
  280. BADARGS(1, 1, "");
  281. list_timers(irp, utimer);
  282. return TCL_OK;
  283. }
  284. static int tcl_duration STDVAR
  285. {
  286. char s[70];
  287. unsigned long sec, tmp;
  288. BADARGS(2, 2, " seconds");
  289. if (atol(argv[1]) <= 0) {
  290. Tcl_AppendResult(irp, "0 seconds", NULL);
  291. return TCL_OK;
  292. }
  293. sec = atol(argv[1]);
  294. s[0] = 0;
  295. if (sec >= 31536000) {
  296. tmp = (sec / 31536000);
  297. sprintf(s, "%lu year%s ", tmp, (tmp == 1) ? "" : "s");
  298. sec -= (tmp * 31536000);
  299. }
  300. if (sec >= 604800) {
  301. tmp = (sec / 604800);
  302. sprintf(&s[strlen(s)], "%lu week%s ", tmp, (tmp == 1) ? "" : "s");
  303. sec -= (tmp * 604800);
  304. }
  305. if (sec >= 86400) {
  306. tmp = (sec / 86400);
  307. sprintf(&s[strlen(s)], "%lu day%s ", tmp, (tmp == 1) ? "" : "s");
  308. sec -= (tmp * 86400);
  309. }
  310. if (sec >= 3600) {
  311. tmp = (sec / 3600);
  312. sprintf(&s[strlen(s)], "%lu hour%s ", tmp, (tmp == 1) ? "" : "s");
  313. sec -= (tmp * 3600);
  314. }
  315. if (sec >= 60) {
  316. tmp = (sec / 60);
  317. sprintf(&s[strlen(s)], "%lu minute%s ", tmp, (tmp == 1) ? "" : "s");
  318. sec -= (tmp * 60);
  319. }
  320. if (sec > 0) {
  321. tmp = (sec);
  322. sprintf(&s[strlen(s)], "%lu second%s", tmp, (tmp == 1) ? "" : "s");
  323. }
  324. if (strlen(s) > 0 && s[strlen(s) - 1] == ' ')
  325. s[strlen(s) - 1] = 0;
  326. Tcl_AppendResult(irp, s, NULL);
  327. return TCL_OK;
  328. }
  329. static int tcl_unixtime STDVAR
  330. {
  331. char s[11];
  332. BADARGS(1, 1, "");
  333. egg_snprintf(s, sizeof s, "%lu", (unsigned long) now);
  334. Tcl_AppendResult(irp, s, NULL);
  335. return TCL_OK;
  336. }
  337. static int tcl_ctime STDVAR
  338. {
  339. time_t tt;
  340. char s[25];
  341. BADARGS(2, 2, " unixtime");
  342. tt = (time_t) atol(argv[1]);
  343. strncpyz(s, ctime(&tt), sizeof s);
  344. Tcl_AppendResult(irp, s, NULL);
  345. return TCL_OK;
  346. }
  347. static int tcl_strftime STDVAR
  348. {
  349. char buf[512];
  350. struct tm *tm1;
  351. time_t t;
  352. BADARGS(2, 3, " format ?time?");
  353. if (argc == 3)
  354. t = atol(argv[2]);
  355. else
  356. t = now;
  357. tm1 = localtime(&t);
  358. if (egg_strftime(buf, sizeof(buf) - 1, argv[1], tm1)) {
  359. Tcl_AppendResult(irp, buf, NULL);
  360. return TCL_OK;
  361. }
  362. Tcl_AppendResult(irp, " error with strftime", NULL);
  363. return TCL_ERROR;
  364. }
  365. static int tcl_myip STDVAR
  366. {
  367. char s[16];
  368. BADARGS(1, 1, "");
  369. egg_snprintf(s, sizeof s, "%lu", iptolong(getmyip()));
  370. Tcl_AppendResult(irp, s, NULL);
  371. return TCL_OK;
  372. }
  373. static int tcl_rand STDVAR
  374. {
  375. unsigned long x;
  376. char s[11];
  377. BADARGS(2, 2, " limit");
  378. if (atol(argv[1]) <= 0) {
  379. Tcl_AppendResult(irp, "random limit must be greater than zero", NULL);
  380. return TCL_ERROR;
  381. }
  382. x = random() % (unsigned long) (atol(argv[1]));
  383. egg_snprintf(s, sizeof s, "%lu", x);
  384. Tcl_AppendResult(irp, s, NULL);
  385. return TCL_OK;
  386. }
  387. static int tcl_sendnote STDVAR
  388. {
  389. char s[5], from[NOTENAMELEN + 1], to[NOTENAMELEN + 1], msg[451];
  390. BADARGS(4, 4, " from to message");
  391. strncpyz(from, argv[1], sizeof from);
  392. strncpyz(to, argv[2], sizeof to);
  393. strncpyz(msg, argv[3], sizeof msg);
  394. egg_snprintf(s, sizeof s, "%d", add_note(to, from, msg, -1, 0));
  395. Tcl_AppendResult(irp, s, NULL);
  396. return TCL_OK;
  397. }
  398. #ifdef HUB
  399. static int tcl_backup STDVAR
  400. {
  401. BADARGS(1, 1, "");
  402. call_hook(HOOK_BACKUP);
  403. return TCL_OK;
  404. }
  405. #endif
  406. static int tcl_timesync STDVAR
  407. {
  408. char buf[50];
  409. egg_snprintf(buf, sizeof buf, "%li %li %d", (now+timesync), now, timesync);
  410. Tcl_AppendResult(irp, buf, NULL);
  411. return TCL_OK;
  412. }
  413. static int tcl_die STDVAR
  414. {
  415. char s[1024];
  416. BADARGS(1, 2, " ?reason?");
  417. if (argc == 2) {
  418. egg_snprintf(s, sizeof s, "BOT SHUTDOWN (%s)", argv[1]);
  419. strncpyz(quit_msg, argv[1], 1024);
  420. } else {
  421. strncpyz(s, "BOT SHUTDOWN (No reason)", sizeof s);
  422. quit_msg[0] = 0;
  423. }
  424. kill_bot(s, quit_msg[0] ? quit_msg : "EXIT");
  425. return TCL_OK;
  426. }
  427. static int tcl_loadmodule STDVAR
  428. {
  429. const char *p;
  430. BADARGS(2, 2, " module-name");
  431. p = module_load(argv[1]);
  432. if (p && strcmp(p, MOD_ALREADYLOAD) && !strcmp(argv[0], "loadmodule"))
  433. putlog(LOG_MISC, "*", "%s %s: %s", MOD_CANTLOADMOD, argv[1], p);
  434. Tcl_AppendResult(irp, p, NULL);
  435. return TCL_OK;
  436. }
  437. static int tcl_unloadmodule STDVAR
  438. {
  439. BADARGS(2, 2, " module-name");
  440. Tcl_AppendResult(irp, module_unload(argv[1], botnetnick), NULL);
  441. return TCL_OK;
  442. }
  443. static int tcl_unames STDVAR
  444. {
  445. char *unix_n, *vers_n;
  446. #ifdef HAVE_UNAME
  447. struct utsname un;
  448. if (uname(&un) < 0) {
  449. #endif
  450. unix_n = "*unkown*";
  451. vers_n = "";
  452. #ifdef HAVE_UNAME
  453. } else {
  454. unix_n = un.sysname;
  455. vers_n = un.release;
  456. }
  457. #endif
  458. Tcl_AppendResult(irp, unix_n, " ", vers_n, NULL);
  459. return TCL_OK;
  460. }
  461. static int tcl_callevent STDVAR
  462. {
  463. BADARGS(2, 2, " event");
  464. check_tcl_event(argv[1]);
  465. return TCL_OK;
  466. }
  467. #if (TCL_MAJOR_VERSION >= 8)
  468. static int tcl_md5(cd, irp, objc, objv)
  469. ClientData cd;
  470. Tcl_Interp *irp;
  471. int objc;
  472. Tcl_Obj *CONST objv[];
  473. {
  474. #else
  475. static int tcl_md5 STDVAR
  476. {
  477. #endif
  478. MD5_CTX md5context;
  479. char digest_string[33], *string;
  480. unsigned char digest[16];
  481. int i, len;
  482. #if (TCL_MAJOR_VERSION >= 8)
  483. if (objc != 2) {
  484. Tcl_WrongNumArgs(irp, 1, objv, "string");
  485. return TCL_ERROR;
  486. }
  487. #if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION < 1)
  488. string = Tcl_GetStringFromObj(objv[1], &len);
  489. #else
  490. string = Tcl_GetByteArrayFromObj(objv[1], &len);
  491. #endif
  492. #else
  493. BADARGS(2, 2, " string");
  494. string = argv[1];
  495. len = strlen(argv[1]);
  496. #endif
  497. MD5_Init(&md5context);
  498. MD5_Update(&md5context, (unsigned char *)string, len);
  499. MD5_Final(digest, &md5context);
  500. for(i=0; i<16; i++)
  501. sprintf(digest_string + (i*2), "%.2x", digest[i]);
  502. Tcl_AppendResult(irp, digest_string, NULL);
  503. return TCL_OK;
  504. }
  505. tcl_cmds tclmisc_objcmds[] =
  506. {
  507. #if (TCL_MAJOR_VERSION >= 8)
  508. {"md5", tcl_md5},
  509. #endif
  510. {NULL, NULL}
  511. };
  512. tcl_cmds tclmisc_cmds[] =
  513. {
  514. #ifdef HUB
  515. {"logfile", tcl_logfile},
  516. #endif
  517. {"putlog", tcl_putlog},
  518. {"putlogl", tcl_putlogl},
  519. {"putcmdlog", tcl_putcmdlog},
  520. {"putcmdlogl", tcl_putcmdlogl},
  521. {"putxferlog", tcl_putxferlog},
  522. {"putloglev", tcl_putloglev},
  523. {"timer", tcl_timer},
  524. {"utimer", tcl_utimer},
  525. {"killtimer", tcl_killtimer},
  526. {"killutimer", tcl_killutimer},
  527. {"timers", tcl_timers},
  528. {"utimers", tcl_utimers},
  529. {"unixtime", tcl_unixtime},
  530. {"strftime", tcl_strftime},
  531. {"ctime", tcl_ctime},
  532. {"myip", tcl_myip},
  533. {"rand", tcl_rand},
  534. {"sendnote", tcl_sendnote},
  535. #ifdef HUB
  536. {"backup", tcl_backup},
  537. #endif
  538. {"ischanhub", tcl_ischanhub},
  539. {"timesync", tcl_timesync},
  540. {"exit", tcl_die},
  541. {"die", tcl_die},
  542. {"unames", tcl_unames},
  543. {"unloadmodule", tcl_unloadmodule},
  544. {"loadmodule", tcl_loadmodule},
  545. {"checkmodule", tcl_loadmodule},
  546. {"duration", tcl_duration},
  547. #if (TCL_MAJOR_VERSION < 8)
  548. {"md5", tcl_md5},
  549. #endif
  550. {"binds", tcl_binds},
  551. {"callevent", tcl_callevent},
  552. {NULL, NULL}
  553. };