robot.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* vcc/plugins/robot/robot.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 <arpa/inet.h>
  21. #include <vcc/vcc.h>
  22. #include <vcc/pretty.h>
  23. #include <vcc/plugin.h>
  24. #include <vcc/version.h>
  25. #include <vcc/interfaces.h>
  26. static int robot_plg_init(void);
  27. static char *do_robot_cmd(char *s);
  28. struct cmdtable robot_cmdtab[] = {
  29. { NULL, NULL, NULL }
  30. };
  31. struct plugin plg_robot = {
  32. .state = 0,
  33. .name = "robot",
  34. .descr = "The robot plugin. ",
  35. .cmdtab = robot_cmdtab,
  36. .init = robot_plg_init,
  37. };
  38. static struct cmdtable commands[] = {
  39. { NULL, NULL, NULL }
  40. };
  41. #define this_plg (&plg_robot)
  42. char *robot_hook(struct hook_info *hi) {
  43. if (strncmp(hi->msg, "robot, ", 7))
  44. return 0;
  45. return do_robot_cmd(hi->msg);
  46. }
  47. typedef int (* fptr_t) (char *);
  48. fptr_t execute_in(struct cmdtable *, char *, int);
  49. static char *do_robot_cmd(char *msg) {
  50. fptr_t f;
  51. int len;
  52. char *p;
  53. p = msg += 7;
  54. for (len = 0; *p && *p != '\n'; p++, len++)
  55. ;
  56. if (!(f = execute_in(commands, msg, len))) {
  57. send_msg("Are you calling me? I don't know what's your meaning. \n", "robot");
  58. return 0;
  59. }
  60. return f(msg) ? HOOK_IGNORE : HOOK_FAILED;
  61. }
  62. static int robot_plg_init(void) {
  63. register_hook(HOOK_RECV, robot_hook);
  64. return 0;
  65. }