robot.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. int do_bh(struct vcc_request *);
  26. int send_msg(char *, char *);
  27. int robot_lslcn(char *s);
  28. int do_robot_cmd(char *s);
  29. struct cmdtable robot_cmdtab[] = {
  30. { NULL, NULL, NULL }
  31. };
  32. struct plugin plg_robot = {
  33. .state = 0,
  34. .name = "robot",
  35. .descr = "The robot plugin. ",
  36. .cmdtab = robot_cmdtab
  37. };
  38. static struct cmdtable commands[] = {
  39. { NULL, NULL, NULL }
  40. };
  41. #define this_plg (&plg_robot)
  42. int robot_new_msg(struct vcc_request *req) {
  43. char *msg;
  44. if (ntohl(req->reqtype) != REQ_MSG_NEW)
  45. return do_bh(req);
  46. msg = req->msg;
  47. if (strncmp(msg, "robot, ", 7))
  48. return 0;
  49. do_robot_cmd(msg);
  50. return 0;
  51. }
  52. typedef int (* fptr_t) (char *);
  53. fptr_t execute_in(struct cmdtable *, char *, int);
  54. int do_robot_cmd(char *msg) {
  55. fptr_t f;
  56. int len;
  57. char *p;
  58. p = msg += 7;
  59. for (len = 0; *p && *p != '\n'; p++, len++)
  60. ;
  61. if (!(f = execute_in(commands, msg, len))) {
  62. send_msg("Are you calling me? I don't know what's your meaning. \n", "robot");
  63. return 0;
  64. }
  65. return f(msg);
  66. }