robot.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. { "lslcn", robot_lslcn, "List articles of LCTT. " },
  40. { NULL, NULL, NULL }
  41. };
  42. #define this_plg (&plg_robot)
  43. int robot_new_msg(struct vcc_request *req) {
  44. char *msg;
  45. if (ntohl(req->reqtype) != REQ_MSG_NEW)
  46. return do_bh(req);
  47. msg = req->msg;
  48. if (strncmp(msg, "robot, ", 7))
  49. return 0;
  50. do_robot_cmd(msg);
  51. return 0;
  52. }
  53. typedef int (* fptr_t) (char *);
  54. fptr_t execute_in(struct cmdtable *, char *, int);
  55. int do_robot_cmd(char *msg) {
  56. fptr_t f;
  57. int len;
  58. char *p;
  59. p = msg += 7;
  60. for (len = 0; *p && *p != '\n'; p++, len++)
  61. ;
  62. if (!(f = execute_in(commands, msg, len))) {
  63. send_msg("Are you calling me? I don't know what's your meaning. \n", "robot");
  64. return 0;
  65. }
  66. return f(msg);
  67. }