robot.c 1.8 KB

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