tclmisc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifdef HAVE_UNAME
  11. #include <sys/utsname.h>
  12. #endif
  13. extern p_tcl_bind_list bind_table_list;
  14. extern tcl_timer_t *timer, *utimer;
  15. extern struct dcc_t *dcc;
  16. extern char origbotname[], botnetnick[], quit_msg[];
  17. extern struct userrec *userlist;
  18. extern time_t now;
  19. extern module_entry *module_list;
  20. extern int timesync;
  21. extern Tcl_Interp *interp;
  22. int expmem_tclmisc()
  23. {
  24. int tot = 0;
  25. return tot;
  26. }
  27. static int tcl_putlog STDVAR
  28. {
  29. char logtext[501];
  30. BADARGS(2, 2, " text");
  31. strncpyz(logtext, argv[1], sizeof logtext);
  32. putlog(LOG_MISC, "*", "%s", logtext);
  33. return TCL_OK;
  34. }
  35. static int tcl_unixtime STDVAR
  36. {
  37. char s[11];
  38. BADARGS(1, 1, "");
  39. egg_snprintf(s, sizeof s, "%lu", (unsigned long) now);
  40. Tcl_AppendResult(irp, s, NULL);
  41. return TCL_OK;
  42. }
  43. static int tcl_ctime STDVAR
  44. {
  45. time_t tt;
  46. char s[25];
  47. BADARGS(2, 2, " unixtime");
  48. tt = (time_t) atol(argv[1]);
  49. strncpyz(s, ctime(&tt), sizeof s);
  50. Tcl_AppendResult(irp, s, NULL);
  51. return TCL_OK;
  52. }
  53. static int tcl_strftime STDVAR
  54. {
  55. char buf[512];
  56. struct tm *tm1;
  57. time_t t;
  58. BADARGS(2, 3, " format ?time?");
  59. if (argc == 3)
  60. t = atol(argv[2]);
  61. else
  62. t = now;
  63. tm1 = localtime(&t);
  64. if (egg_strftime(buf, sizeof(buf) - 1, argv[1], tm1)) {
  65. Tcl_AppendResult(irp, buf, NULL);
  66. return TCL_OK;
  67. }
  68. Tcl_AppendResult(irp, " error with strftime", NULL);
  69. return TCL_ERROR;
  70. }
  71. static int tcl_myip STDVAR
  72. {
  73. char s[16];
  74. BADARGS(1, 1, "");
  75. egg_snprintf(s, sizeof s, "%lu", iptolong(getmyip()));
  76. Tcl_AppendResult(irp, s, NULL);
  77. return TCL_OK;
  78. }
  79. static int tcl_rand STDVAR
  80. {
  81. unsigned long x;
  82. char s[11];
  83. BADARGS(2, 2, " limit");
  84. if (atol(argv[1]) <= 0) {
  85. Tcl_AppendResult(irp, "random limit must be greater than zero", NULL);
  86. return TCL_ERROR;
  87. }
  88. x = random() % (unsigned long) (atol(argv[1]));
  89. egg_snprintf(s, sizeof s, "%lu", x);
  90. Tcl_AppendResult(irp, s, NULL);
  91. return TCL_OK;
  92. }
  93. static int tcl_timesync STDVAR
  94. {
  95. char buf[50];
  96. egg_snprintf(buf, sizeof buf, "%li %li %d", (now+timesync), now, timesync);
  97. Tcl_AppendResult(irp, buf, NULL);
  98. return TCL_OK;
  99. }
  100. static int tcl_die STDVAR
  101. {
  102. char s[1024];
  103. BADARGS(1, 2, " ?reason?");
  104. if (argc == 2) {
  105. egg_snprintf(s, sizeof s, "BOT SHUTDOWN (%s)", argv[1]);
  106. strncpyz(quit_msg, argv[1], 1024);
  107. } else {
  108. strncpyz(s, "BOT SHUTDOWN (No reason)", sizeof s);
  109. quit_msg[0] = 0;
  110. }
  111. kill_bot(s, quit_msg[0] ? quit_msg : "EXIT");
  112. return TCL_OK;
  113. }
  114. tcl_cmds tclmisc_cmds[] =
  115. {
  116. {"putlog", tcl_putlog},
  117. {"unixtime", tcl_unixtime},
  118. {"strftime", tcl_strftime},
  119. {"ctime", tcl_ctime},
  120. {"myip", tcl_myip},
  121. {"rand", tcl_rand},
  122. {"timesync", tcl_timesync},
  123. {"exit", tcl_die},
  124. {"die", tcl_die},
  125. {NULL, NULL}
  126. };