hooks.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* vcc/hooks.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 <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <locale.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <poll.h>
  31. #include <signal.h>
  32. #include <getopt.h>
  33. #include <libintl.h>
  34. #include <limits.h>
  35. #include <klist.h>
  36. #include <vcc/vcc.h>
  37. #include <vcc/version.h>
  38. #include <vcc/robot.h>
  39. #include <vcc/plugin.h>
  40. #include <vcc/pretty.h>
  41. #include <vcc/hooks.h>
  42. #include <vcc/defserv.h>
  43. #include <vcc/config.h>
  44. #include <vcc/interfaces.h>
  45. struct klist_node hooks = KLIST_NODE_INIT(&hooks);
  46. struct hook *register_hook(int type, hookfn_t hookfn) {
  47. struct hook *hook;
  48. if (unlikely(!(hook = malloc(sizeof(struct hook))))) {
  49. fprintf(stderr, "*** malloc() failed\n");
  50. return NULL;
  51. }
  52. klist_init(&hook->node);
  53. hook->type = type;
  54. hook->hook = hookfn;
  55. klist_add(&hooks, &hook->node);
  56. return hook;
  57. }
  58. int unregister_hook(struct hook *hook) {
  59. klist_del(&hooks, &hook->node);
  60. free(hook);
  61. return 0;
  62. }
  63. int do_hook(int type, char *s, char *usrname, int sid) {
  64. struct hook_info hi;
  65. struct klist_node *n;
  66. struct hook *hook;
  67. char *p;
  68. int i;
  69. hi.type = type;
  70. hi.msg = s;
  71. hi.usr = usrname;
  72. hi.sid = sid;
  73. for (n = hooks.next; n != &hooks; n = n->next) {
  74. hook = hook_of(n);
  75. if (hook->type == type) {
  76. /* so the order is quiet important */
  77. if (unlikely(!(p = hook->hook(&hi))))
  78. fprintf(stderr, "*** do_hook: one of hooks returned NULL. abandon... \n");
  79. if (i)
  80. free(hi.msg);
  81. hi.msg = p;
  82. i++;
  83. }
  84. }
  85. return 0;
  86. }
  87. int do_hook_recv(char *s, char *usrname, int sid) {
  88. return do_hook(HOOK_RECV, s, usrname, sid);
  89. }
  90. int do_hook_send(char *s) {
  91. return do_hook(HOOK_SEND, s, usrname, current_sid);
  92. }