core_binds.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * core_binds.c -- handles:
  3. *
  4. * binds for the CORE
  5. *
  6. */
  7. #include "common.h"
  8. #include "dccutil.h"
  9. #include "auth.h"
  10. #include "core_binds.h"
  11. #include "userrec.h"
  12. #include "main.h"
  13. #include "settings.h"
  14. #include "set.h"
  15. #include "users.h"
  16. #include "misc.h"
  17. #include "tclhash.h"
  18. #include "dcc.h"
  19. extern cmd_t C_dcc[];
  20. static bind_table_t *BT_away = NULL, *BT_dcc = NULL;
  21. static bind_table_t *BT_note = NULL;
  22. static bind_table_t *BT_bot = NULL, *BT_nkch = NULL, *BT_chon = NULL;
  23. static bind_table_t *BT_time = NULL;
  24. void core_binds_init()
  25. {
  26. BT_away = bind_table_add("away", 3, "sis", MATCH_MASK, BIND_STACKABLE);
  27. BT_bot = bind_table_add("bot", 3, "sss", MATCH_EXACT, 0);
  28. BT_chon = bind_table_add("chon", 2, "si", MATCH_MASK | MATCH_FLAGS, BIND_STACKABLE);
  29. BT_dcc = bind_table_add("dcc", 2, "is", MATCH_PARTIAL | MATCH_FLAGS, 0);
  30. egg_bzero(&cmdlist, 500);
  31. add_builtins("dcc", C_dcc);
  32. BT_nkch = bind_table_add("nkch", 2, "ss", MATCH_MASK, BIND_STACKABLE);
  33. BT_note = bind_table_add("note", 3 , "sss", MATCH_EXACT, 0);
  34. BT_time = bind_table_add("time", 5, "iiiii", MATCH_MASK, BIND_STACKABLE);
  35. }
  36. bool check_aliases(int idx, const char *cmd, const char *args)
  37. {
  38. char *a = NULL, *p = NULL, *aliasp = NULL, *aliasdup = NULL, *argsp = NULL, *argsdup = NULL;
  39. bool found = 0;
  40. aliasp = aliasdup = strdup(alias);
  41. argsp = argsdup = strdup(args);
  42. while ((a = strsep(&aliasdup, ","))) { //a = entire alias "alias cmd params"
  43. p = newsplit(&a); //p = alias cmd //a = rcmd params
  44. if (!egg_strcasecmp(p, cmd)) { //a match on the cmd we were given!
  45. p = newsplit(&a); //p = rcmd //a = params
  46. if (!egg_strcasecmp(cmd, p)) {
  47. putlog(LOG_WARN, "*", "Loop detected in alias '%s'", p);
  48. free(argsp);
  49. return 0;
  50. }
  51. char *myargs = NULL, *pass = NULL;
  52. size_t size = 0;
  53. found = 1;
  54. size = strlen(a) + 1 + strlen(argsdup) + 1 + 2;
  55. myargs = (char *) calloc(1, size);
  56. if (has_cmd_pass(p)) {
  57. pass = newsplit(&argsdup);
  58. simple_snprintf(myargs, size, "%s %s %s", pass, a, argsdup);
  59. } else
  60. simple_snprintf(myargs, size, "%s %s", a, argsdup);
  61. check_bind_dcc(p, idx, myargs);
  62. if (myargs)
  63. free(myargs);
  64. break;
  65. }
  66. }
  67. free(aliasp);
  68. free(argsp);
  69. return found;
  70. }
  71. void check_bind_dcc(const char *cmd, int idx, const char *text)
  72. {
  73. real_check_bind_dcc(cmd, idx, text, NULL);
  74. }
  75. void real_check_bind_dcc(const char *cmd, int idx, const char *text, Auth *auth)
  76. {
  77. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  78. bind_entry_t *entry = NULL;
  79. bind_table_t *table = NULL;
  80. char *args = strdup(text);
  81. get_user_flagrec(dcc[idx].user, &fr, dcc[idx].u.chat->con_chan);
  82. table = bind_table_lookup("dcc");
  83. for (entry = table->entries; entry && entry->next; entry = entry->next) {
  84. if (!egg_strcasecmp(cmd, entry->mask)) {
  85. if (has_cmd_pass(cmd)) {
  86. if (flagrec_ok(&entry->user_flags, &fr)) {
  87. char *p = NULL, work[1024] = "", pass[128] = "";
  88. p = strchr(args, ' ');
  89. if (p)
  90. *p = 0;
  91. strlcpy(pass, args, sizeof(pass));
  92. if (check_cmd_pass(cmd, pass)) {
  93. if (p)
  94. *p = ' ';
  95. strlcpy(work, args, sizeof(work));
  96. p = work;
  97. newsplit(&p);
  98. strcpy(args, p);
  99. } else {
  100. dprintf(idx, "Invalid command password.\nUse: $b%scommand <password> [arguments]$b\n", settings.dcc_prefix);
  101. putlog(LOG_CMDS, "*", "$ #%s# %s %s", dcc[idx].nick, cmd, args);
  102. putlog(LOG_MISC, "*", "%s attempted %s%s with missing or incorrect command password", dcc[idx].nick, settings.dcc_prefix, cmd);
  103. free(args);
  104. return;
  105. }
  106. } else {
  107. putlog(LOG_CMDS, "*", "! #%s# %s %s", dcc[idx].nick, cmd, args);
  108. dprintf(idx, "What? You need '%shelp'\n", settings.dcc_prefix);
  109. free(args);
  110. return;
  111. }
  112. }
  113. break;
  114. }
  115. }
  116. if (entry && auth) {
  117. if (!(entry->cflags & AUTH))
  118. return;
  119. }
  120. int hits = 0;
  121. check_bind_hits(BT_dcc, cmd, &fr, &hits, idx, args);
  122. if (hits != 1)
  123. putlog(LOG_CMDS, "*", "! #%s# %s %s", dcc[idx].nick, cmd, args);
  124. if (hits == 0) {
  125. if (!check_aliases(idx, cmd, args))
  126. dprintf(idx, "What? You need '%shelp'\n", settings.dcc_prefix);
  127. } else if (hits > 1)
  128. dprintf(idx, "Ambiguous command.\n");
  129. free(args);
  130. }
  131. void check_bind_bot(const char *nick, const char *code, const char *param)
  132. {
  133. char *mynick = NULL, *myparam = NULL, *p1 = NULL, *p2 = NULL;
  134. mynick = p1 = strdup(nick);
  135. myparam = p2 = strdup(param);
  136. check_bind(BT_bot, code, NULL, mynick, code, myparam);
  137. free(p1);
  138. free(p2);
  139. }
  140. void check_bind_chon(char *hand, int idx)
  141. {
  142. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  143. struct userrec *u = NULL;
  144. u = get_user_by_handle(userlist, hand);
  145. touch_laston(u, "partyline", now);
  146. get_user_flagrec(u, &fr, NULL);
  147. check_bind(BT_chon, hand, &fr, hand, idx);
  148. }
  149. void check_bind_chof(char *hand, int idx)
  150. {
  151. struct userrec *u = NULL;
  152. u = get_user_by_handle(userlist, hand);
  153. touch_laston(u, "partyline", now);
  154. }
  155. void check_bind_nkch(const char *ohand, const char *nhand)
  156. {
  157. check_bind(BT_nkch, ohand, NULL, ohand, nhand);
  158. }
  159. int check_bind_note(const char *from, const char *to, const char *text)
  160. {
  161. return check_bind(BT_note, to, NULL, from, to, text);
  162. }
  163. void check_bind_away(const char *bot, int idx, const char *msg)
  164. {
  165. check_bind(BT_away, bot, NULL, bot, idx, msg);
  166. }
  167. void check_bind_time(struct tm *tm)
  168. {
  169. char full[32] = "";
  170. egg_snprintf(full, sizeof(full), "%02d %02d %02d %02d %04d", tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
  171. check_bind(BT_time, full, NULL, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
  172. }