tcldcc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * tcldcc.c -- handles:
  3. * Tcl stubs for the dcc commands
  4. *
  5. */
  6. #include "main.h"
  7. #include "tandem.h"
  8. #include "modules.h"
  9. #include <sys/stat.h>
  10. extern Tcl_Interp *interp;
  11. extern tcl_timer_t *timer,
  12. *utimer;
  13. extern struct dcc_t *dcc;
  14. extern int dcc_total, backgrd, parties,
  15. do_restart, remote_boots, max_dcc, hub, leaf;
  16. extern char botnetnick[], *binname;
  17. extern party_t *party;
  18. extern tand_t *tandbot;
  19. extern time_t now;
  20. /* Traffic stuff. */
  21. extern unsigned long otraffic_irc, otraffic_irc_today, itraffic_irc, itraffic_irc_today, otraffic_bn, otraffic_bn_today, itraffic_bn, itraffic_bn_today, otraffic_dcc, otraffic_dcc_today, itraffic_dcc, itraffic_dcc_today, otraffic_trans, otraffic_trans_today, itraffic_trans, itraffic_trans_today, otraffic_unknown, otraffic_unknown_today, itraffic_unknown, itraffic_unknown_today;
  22. int enable_simul = 0;
  23. int expmem_tcldcc(void)
  24. {
  25. int tot = 0;
  26. return tot;
  27. }
  28. /***********************************************************************/
  29. static int tcl_putdcc STDVAR
  30. {
  31. int i, j;
  32. BADARGS(3, 3, " idx text");
  33. i = atoi(argv[1]);
  34. j = findidx(i);
  35. if (j < 0) {
  36. Tcl_AppendResult(irp, "invalid idx", NULL);
  37. return TCL_ERROR;
  38. }
  39. dumplots(-i, "", argv[2]);
  40. return TCL_OK;
  41. }
  42. static int tcl_hand2idx STDVAR
  43. {
  44. int i;
  45. char s[11];
  46. BADARGS(2, 2, " nickname");
  47. for (i = 0; i < dcc_total; i++)
  48. if ((dcc[i].type->flags & DCT_SIMUL) &&
  49. !egg_strcasecmp(argv[1], dcc[i].nick)) {
  50. egg_snprintf(s, sizeof s, "%ld", dcc[i].sock);
  51. Tcl_AppendResult(irp, s, NULL);
  52. return TCL_OK;
  53. }
  54. Tcl_AppendResult(irp, "-1", NULL);
  55. return TCL_OK;
  56. }
  57. static int tcl_valididx STDVAR
  58. {
  59. int idx;
  60. BADARGS(2, 2, " idx");
  61. idx = findidx(atoi(argv[1]));
  62. if (idx < 0 || !(dcc[idx].type->flags & DCT_VALIDIDX))
  63. Tcl_AppendResult(irp, "0", NULL);
  64. else
  65. Tcl_AppendResult(irp, "1", NULL);
  66. return TCL_OK;
  67. }
  68. static int tcl_killdcc STDVAR
  69. {
  70. int idx;
  71. BADARGS(2, 3, " idx ?reason?");
  72. idx = findidx(atoi(argv[1]));
  73. if (idx < 0) {
  74. Tcl_AppendResult(irp, "invalid idx", NULL);
  75. return TCL_ERROR;
  76. }
  77. /* Don't kill terminal socket */
  78. if ((dcc[idx].sock == STDOUT) && !backgrd)
  79. return TCL_OK;
  80. /* Make sure 'whom' info is updated for other bots */
  81. if (dcc[idx].type->flags & DCT_CHAT) {
  82. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s has left the %s%s%s\n",
  83. dcc[idx].nick, dcc[idx].u.chat ? "channel" : "partyline",
  84. argc == 3 ? ": " : "", argc == 3 ? argv[2] : "");
  85. botnet_send_part_idx(idx, argc == 3 ? argv[2] : "");
  86. if ((dcc[idx].u.chat->channel >= 0) &&
  87. (dcc[idx].u.chat->channel < GLOBAL_CHANS))
  88. check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
  89. dcc[idx].u.chat->channel);
  90. check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
  91. /* Notice is sent to the party line, the script can add a reason. */
  92. }
  93. killsock(dcc[idx].sock);
  94. lostdcc(idx);
  95. return TCL_OK;
  96. }
  97. static int tcl_putbot STDVAR
  98. {
  99. int i;
  100. char msg[SGRAB-110];
  101. BADARGS(3, 3, " botnick message");
  102. i = nextbot(argv[1]);
  103. if (i < 0) {
  104. Tcl_AppendResult(irp, "bot is not in the botnet", NULL);
  105. return TCL_ERROR;
  106. }
  107. strncpyz(msg, argv[2], sizeof msg);
  108. botnet_send_zapf(i, botnetnick, argv[1], msg);
  109. return TCL_OK;
  110. }
  111. static int tcl_putallbots STDVAR
  112. {
  113. char msg[SGRAB-110];
  114. BADARGS(2, 2, " message");
  115. strncpyz(msg, argv[1], sizeof msg);
  116. botnet_send_zapf_broad(-1, botnetnick, NULL, msg);
  117. return TCL_OK;
  118. }
  119. static int tcl_idx2hand STDVAR
  120. {
  121. int idx;
  122. BADARGS(2, 2, " idx");
  123. idx = findidx(atoi(argv[1]));
  124. if (idx < 0) {
  125. Tcl_AppendResult(irp, "invalid idx", NULL);
  126. return TCL_ERROR;
  127. }
  128. Tcl_AppendResult(irp, dcc[idx].nick, NULL);
  129. return TCL_OK;
  130. }
  131. static int tcl_islinked STDVAR
  132. {
  133. int i;
  134. BADARGS(2, 2, " bot");
  135. i = nextbot(argv[1]);
  136. if (i < 0)
  137. Tcl_AppendResult(irp, "0", NULL);
  138. else
  139. Tcl_AppendResult(irp, "1", NULL);
  140. return TCL_OK;
  141. }
  142. static int tcl_randstring STDVAR
  143. {
  144. int length = atoi(argv[1]);
  145. char s[length+1];
  146. BADARGS(2, 2, " length");
  147. if (length) {
  148. make_rand_str(s,length);
  149. Tcl_AppendResult(irp, s, NULL);
  150. } else
  151. Tcl_AppendResult(irp, "", NULL);
  152. return TCL_OK;
  153. }
  154. static int tcl_binname STDVAR
  155. {
  156. Tcl_AppendResult(irp, binname, NULL);
  157. return TCL_OK;
  158. }
  159. static int tcl_bots STDVAR
  160. {
  161. tand_t *bot;
  162. BADARGS(1, 1, "");
  163. for (bot = tandbot; bot; bot = bot->next)
  164. Tcl_AppendElement(irp, bot->bot);
  165. return TCL_OK;
  166. }
  167. static int tcl_botlist STDVAR
  168. {
  169. tand_t *bot;
  170. char *p;
  171. char sh[2], string[20];
  172. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  173. CONST char *list[4];
  174. #else
  175. char *list[4];
  176. #endif
  177. BADARGS(1, 1, "");
  178. sh[1] = 0;
  179. list[3] = sh;
  180. list[2] = string;
  181. for (bot = tandbot; bot; bot = bot->next) {
  182. list[0] = bot->bot;
  183. list[1] = (bot->uplink == (tand_t *) 1) ? botnetnick : bot->uplink->bot;
  184. strncpyz(string, int_to_base10(bot->ver), sizeof string);
  185. sh[0] = bot->share;
  186. p = Tcl_Merge(4, list);
  187. Tcl_AppendElement(irp, p);
  188. Tcl_Free((char *) p);
  189. }
  190. return TCL_OK;
  191. }
  192. static int tcl_link STDVAR
  193. {
  194. int x, i;
  195. char bot[HANDLEN + 1], bot2[HANDLEN + 1];
  196. BADARGS(2, 3, " ?via-bot? bot");
  197. strncpyz(bot, argv[1], sizeof bot);
  198. if (argc == 3) {
  199. x = 1;
  200. strncpyz(bot2, argv[2], sizeof bot2);
  201. i = nextbot(bot);
  202. if (i < 0)
  203. x = 0;
  204. else
  205. botnet_send_link(i, botnetnick, bot, bot2);
  206. } else
  207. x = botlink("", -2, bot);
  208. egg_snprintf(bot, sizeof bot, "%d", x);
  209. Tcl_AppendResult(irp, bot, NULL);
  210. return TCL_OK;
  211. }
  212. static int tcl_unlink STDVAR
  213. {
  214. int i, x;
  215. char bot[HANDLEN + 1];
  216. BADARGS(2, 3, " bot ?comment?");
  217. strncpyz(bot, argv[1], sizeof bot);
  218. i = nextbot(bot);
  219. if (i < 0)
  220. x = 0;
  221. else {
  222. x = 1;
  223. if (!egg_strcasecmp(bot, dcc[i].nick))
  224. x = botunlink(-2, bot, argv[2]);
  225. else
  226. botnet_send_unlink(i, botnetnick, lastbot(bot), bot, argv[2]);
  227. }
  228. egg_snprintf(bot, sizeof bot, "%d", x);
  229. Tcl_AppendResult(irp, bot, NULL);
  230. return TCL_OK;
  231. }
  232. static int tcl_rehash STDVAR
  233. {
  234. BADARGS(1, 1, " ");
  235. #ifdef HUB
  236. write_userfile(-1);
  237. #endif
  238. putlog(LOG_MISC, "*", USERF_REHASHING);
  239. do_restart = -2;
  240. return TCL_OK;
  241. }
  242. static int tcl_restart STDVAR
  243. {
  244. BADARGS(1, 1, " ");
  245. if (!backgrd) {
  246. Tcl_AppendResult(interp, "You can't restart a -n bot", NULL);
  247. return TCL_ERROR;
  248. }
  249. #ifdef HUB
  250. write_userfile(-1);
  251. #endif
  252. putlog(LOG_MISC, "*", MISC_RESTARTING);
  253. wipe_timers(interp, &utimer);
  254. wipe_timers(interp, &timer);
  255. do_restart = -1;
  256. return TCL_OK;
  257. }
  258. tcl_cmds tcldcc_cmds[] =
  259. {
  260. {"binname", tcl_binname},
  261. {"putidx", tcl_putdcc},
  262. {"hand2idx", tcl_hand2idx},
  263. {"valididx", tcl_valididx},
  264. {"killdcc", tcl_killdcc},
  265. {"putbot", tcl_putbot},
  266. {"putallbots", tcl_putallbots},
  267. {"idx2hand", tcl_idx2hand},
  268. {"bots", tcl_bots},
  269. {"botlist", tcl_botlist},
  270. {"islinked", tcl_islinked},
  271. {"randstring", tcl_randstring},
  272. {"link", tcl_link},
  273. {"unlink", tcl_unlink},
  274. {"rehash", tcl_rehash},
  275. {"restart", tcl_restart},
  276. {NULL, NULL}
  277. };