plugins.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* vcc/plugins.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 <ctype.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 <klist.h>
  33. #include <vcc/vcc.h>
  34. #include <vcc/plugin.h>
  35. #include <vcc/version.h>
  36. #include <vcc/pretty.h>
  37. #include <vcc/interfaces.h>
  38. extern struct plugin plg_hello, plg_robot, plg_m4, plg_script, plg_hist,
  39. plg_cqd, plg_beep, plg_ban, plg_vcr, plg_emerg;
  40. struct plugin *plugins[] = {
  41. &plg_hello, &plg_robot, &plg_m4, &plg_script, &plg_hist,
  42. &plg_cqd, &plg_beep, &plg_ban, &plg_vcr, &plg_emerg,
  43. (struct plugin *) NULL
  44. };
  45. int plg_pls(int, char **);
  46. int plg_pins(int, char **);
  47. int plg_prem(int, char **);
  48. int do_cmd_plg(int argc, char **argv) {
  49. if (cmd_noptions > 1)
  50. goto bad_usage;
  51. if (cmd_options['l'])
  52. return plg_pls(argc, argv);
  53. else if (cmd_options['i'])
  54. return plg_pins(argc, argv);
  55. else if (cmd_options['d'])
  56. return plg_prem(argc, argv);
  57. bad_usage:
  58. fprintf(stderr, _("usage: -plg -l|i|d\n"));
  59. return 1;
  60. }
  61. int plg_pls(int argc, char **argv) {
  62. int i;
  63. struct plugin *p;
  64. (void) argv;
  65. if (argc != 2) {
  66. fprintf(stderr, _("usage: -pls\n"));
  67. return 1;
  68. }
  69. for (i = 0; (p = plugins[i]); i++)
  70. pretty_cmd_help(p->name, p->descr);
  71. return 0;
  72. }
  73. struct plugin *find_plg(char *s) {
  74. int i;
  75. struct plugin *p;
  76. for (i = 0; (p = plugins[i]); i++)
  77. if (!strcmp(s, p->name))
  78. return p;
  79. return NULL;
  80. }
  81. int plg_insert(struct plugin *plg) {
  82. plg->state = PLG_INSERTED;
  83. return 0;
  84. }
  85. int plg_remove(struct plugin *plg) {
  86. plg->state = 0;
  87. return 0;
  88. }
  89. int insert_plugin(char *s) {
  90. struct plugin *plg;
  91. if (!(plg = find_plg(s)))
  92. return 1;
  93. plg_insert(plg);
  94. return plg->init ? plg->init() : 0;
  95. }
  96. int plg_prem(int argc, char **argv) {
  97. struct plugin *p;
  98. char name[MSG_SIZE];
  99. char *s;
  100. switch (argc) {
  101. case 2:
  102. fprintf(stderr, _("plugin: "));
  103. scanf("%s", name);
  104. s = name;
  105. break;
  106. case 3:
  107. s = argv[2];
  108. break;
  109. default:
  110. fprintf(stderr, _("usage: -prem [plugin-name]\n"));
  111. break;
  112. }
  113. if (!(p = find_plg(s))) {
  114. fprintf(stderr, _("*** plugin not found. \n"));
  115. return 1;
  116. }
  117. if (p->state != PLG_INSERTED) {
  118. fprintf(stderr, _("*** plugin didn't insert. \n"));
  119. return 1;
  120. }
  121. if (p->exit)
  122. p->exit();
  123. return plg_remove(p);
  124. }
  125. int plg_pins(int argc, char **argv) {
  126. struct plugin *p;
  127. char name[MSG_SIZE];
  128. char *s;
  129. switch (argc) {
  130. case 2:
  131. fprintf(stderr, _("plugin: "));
  132. scanf("%s", name);
  133. s = name;
  134. break;
  135. case 3:
  136. s = argv[2];
  137. break;
  138. default:
  139. fprintf(stderr, _("usage: -pins [plugin-name]\n"));
  140. return 1;
  141. }
  142. if (!(p = find_plg(s))) {
  143. fprintf(stderr, _("*** plugin not found. \n"));
  144. return 1;
  145. }
  146. plg_insert(p);
  147. if (p->init)
  148. p->init();
  149. return 0;
  150. }