ban.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* vcc/plugins/ban/ban.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <vcc/vcc.h>
  22. #include <vcc/pretty.h>
  23. #include <vcc/plugin.h>
  24. #include <vcc/interfaces.h>
  25. #include <vcc/version.h>
  26. int do_cmd_ban(int argc, char **argv);
  27. int ban_init(void);
  28. struct cmdtable ban_cmdtab[] = {
  29. { "-ban", do_cmd_ban, "Ban control. " },
  30. { NULL, NULL, NULL }
  31. };
  32. struct plugin plg_ban = {
  33. .state = 0,
  34. .name = "ban",
  35. .descr = "Ban / unban somebody. ",
  36. .cmdtab = ban_cmdtab,
  37. .init = ban_init
  38. };
  39. struct klist_node banned_people = KLIST_NODE_INIT(&banned_people);
  40. #define this_plg (&plg_ban)
  41. static int ban_unban(int argc, char **argv) {
  42. char *name;
  43. struct klist_node *n;
  44. struct banlist *ban;
  45. switch (argc) {
  46. case 2:
  47. name = malloc(USRNAME_SIZE + 1);
  48. fprintf(stderr, _("Enter the user to ban: "));
  49. scanf("%s", name);
  50. break;
  51. case 3:
  52. name = argv[2];
  53. break;
  54. default:
  55. fprintf(stderr, "-ban -u [user-name]\n");
  56. return 1;
  57. }
  58. for (n = banned_people.next; n != &banned_people; n = n->next) {
  59. ban = banlist_of(n);
  60. if (!strcmp(ban->usrname, name)) {
  61. klist_del(&banned_people, n);
  62. free(ban->usrname);
  63. free(ban);
  64. break;
  65. }
  66. }
  67. fprintf(stderr, _("User not banned. \n"));
  68. if (argc == 2)
  69. free(name);
  70. return 0;
  71. }
  72. static int ban_ban(int argc, char **argv) {
  73. char *people_name, people_arr[USRNAME_SIZE];
  74. struct banlist *i;
  75. struct klist_node *n;
  76. switch (argc) {
  77. case 2:
  78. fprintf(stderr, _("Enter the user to ban: "));
  79. scanf("%s", people_arr);
  80. people_name = strdup(people_arr);
  81. break;
  82. case 3:
  83. people_name = strdup(argv[2]);
  84. break;
  85. default:
  86. fprintf(stderr, _("usage: -ban -b [user-name]\n"));
  87. return 1;
  88. }
  89. for (n = banned_people.next; n != &banned_people; n = n->next) {
  90. i = banlist_of(n);
  91. if (!strcmp(people_name, i->usrname)) {
  92. fprintf(stderr, _("User %s is already banned\n"), people_name);
  93. free(people_name);
  94. return 0;
  95. }
  96. }
  97. if (unlikely(!(i = malloc(sizeof(struct banlist))))) {
  98. fprintf(stderr, "*** malloc() failed\n");
  99. return 1;
  100. }
  101. i->usrname = people_name;
  102. klist_init(&i->node);
  103. klist_add(&banned_people, &i->node);
  104. return 0;
  105. }
  106. int do_cmd_ban(int argc, char **argv) {
  107. if (cmd_noptions > 1)
  108. goto bad_usage;
  109. if (cmd_options['b'])
  110. return ban_ban(argc, argv);
  111. else if (cmd_options['u'])
  112. return ban_unban(argc, argv);
  113. bad_usage:
  114. fprintf(stderr, "usage: -ban [-b|u] [user-name]\n");
  115. return 1;
  116. }
  117. char *ban_hook(struct hook_info *hi) {
  118. struct klist_node *n;
  119. struct banlist *ban;
  120. for (n = banned_people.next; n != &banned_people; n = n->next) {
  121. ban = banlist_of(n);
  122. if (ban->usrname && !strcmp(ban->usrname, hi->usr)) {
  123. printf("\nban: message from %s ignored. \n", hi->usr);
  124. return HOOK_HANDLED;
  125. }
  126. }
  127. return HOOK_IGNORE;
  128. }
  129. int ban_init(void) {
  130. register_hook(HOOK_RECV, ban_hook);
  131. return 0;
  132. }