app_userevent.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * UserEvent application -- send manager event
  5. *
  6. *
  7. * This program is free software, distributed under the terms of
  8. * the GNU General Public License
  9. */
  10. #include <asterisk/lock.h>
  11. #include <asterisk/file.h>
  12. #include <asterisk/logger.h>
  13. #include <asterisk/channel.h>
  14. #include <asterisk/pbx.h>
  15. #include <asterisk/module.h>
  16. #include <asterisk/manager.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. static char *tdesc = "Custom User Event Application";
  22. static char *app = "UserEvent";
  23. static char *synopsis = "Send an arbitrary event to the manager interface";
  24. static char *descrip =
  25. " UserEvent(eventname[|body]): Sends an arbitrary event to the\n"
  26. "manager interface, with an optional body representing additional\n"
  27. "arguments. The format of the event will be:\n"
  28. " Event: UserEvent<specified event name>\n"
  29. " Channel: <channel name>\n"
  30. " Uniqueid: <call uniqueid>\n"
  31. " [body]\n"
  32. "If the body is not specified, only Event, Channel, and Uniqueid fields\n"
  33. "will be present. Returns 0.";
  34. STANDARD_LOCAL_USER;
  35. LOCAL_USER_DECL;
  36. static int userevent_exec(struct ast_channel *chan, void *data)
  37. {
  38. struct localuser *u;
  39. char info[512];
  40. char eventname[512];
  41. char *eventbody;
  42. if (!data || !strlen(data)) {
  43. ast_log(LOG_WARNING, "UserEvent requires an argument (eventname|optional event body)\n");
  44. return -1;
  45. }
  46. strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
  47. snprintf(eventname, sizeof(eventname), "UserEvent%s", info);
  48. eventbody = strchr(eventname, '|');
  49. if (eventbody) {
  50. *eventbody = '\0';
  51. eventbody++;
  52. }
  53. LOCAL_USER_ADD(u);
  54. if(eventbody) {
  55. ast_log(LOG_DEBUG, "Sending user event: %s, %s\n", eventname, eventbody);
  56. manager_event(EVENT_FLAG_USER, eventname,
  57. "Channel: %s\r\nUniqueid: %s\r\n%s\r\n",
  58. chan->name, chan->uniqueid, eventbody);
  59. } else {
  60. ast_log(LOG_DEBUG, "Sending user event: %s\n", eventname);
  61. manager_event(EVENT_FLAG_USER, eventname,
  62. "Channel: %s\r\nUniqueid: %s\r\n", chan->name, chan->uniqueid);
  63. }
  64. LOCAL_USER_REMOVE(u);
  65. return 0;
  66. }
  67. int unload_module(void)
  68. {
  69. STANDARD_HANGUP_LOCALUSERS;
  70. return ast_unregister_application(app);
  71. }
  72. int load_module(void)
  73. {
  74. return ast_register_application(app, userevent_exec, synopsis, descrip);
  75. }
  76. char *description(void)
  77. {
  78. return tdesc;
  79. }
  80. int usecount(void)
  81. {
  82. int res;
  83. STANDARD_USECOUNT(res);
  84. return res;
  85. }
  86. char *key()
  87. {
  88. return ASTERISK_GPL_KEY;
  89. }