app_setcdruserfield.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Applictions connected with CDR engine
  5. *
  6. * Copyright (C) 2003, Digium
  7. *
  8. * Justin Huff <jjhuff@mspin.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <sys/types.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/cdr.h>
  16. #include <asterisk/module.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/logger.h>
  19. #include <asterisk/config.h>
  20. #include <asterisk/manager.h>
  21. #include <asterisk/utils.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. static char *tdesc = "CDR user field apps";
  25. static char *setcdruserfield_descrip =
  26. "[Synopsis]\n"
  27. "SetCDRUserField(value)\n\n"
  28. "[Description]\n"
  29. "SetCDRUserField(value): Set the CDR 'user field' to value\n"
  30. " The Call Data Record (CDR) user field is an extra field you\n"
  31. " can use for data not stored anywhere else in the record.\n"
  32. " CDR records can be used for billing or storing other arbitrary data\n"
  33. " (I.E. telephone survey responses)\n"
  34. " Also see AppendCDRUserField().\n"
  35. " Always returns 0\n";
  36. static char *setcdruserfield_app = "SetCDRUserField";
  37. static char *setcdruserfield_synopsis = "Set the CDR user field";
  38. static char *appendcdruserfield_descrip =
  39. "[Synopsis]\n"
  40. "AppendCDRUserField(value)\n\n"
  41. "[Description]\n"
  42. "AppendCDRUserField(value): Append value to the CDR user field\n"
  43. " The Call Data Record (CDR) user field is an extra field you\n"
  44. " can use for data not stored anywhere else in the record.\n"
  45. " CDR records can be used for billing or storing other arbitrary data\n"
  46. " (I.E. telephone survey responses)\n"
  47. " Also see SetCDRUserField().\n"
  48. " Always returns 0\n";
  49. static char *appendcdruserfield_app = "AppendCDRUserField";
  50. static char *appendcdruserfield_synopsis = "Append to the CDR user field";
  51. STANDARD_LOCAL_USER;
  52. LOCAL_USER_DECL;
  53. static int action_setcdruserfield(struct mansession *s, struct message *m)
  54. {
  55. struct ast_channel *c = NULL;
  56. char *userfield = astman_get_header(m, "UserField");
  57. char *channel = astman_get_header(m, "Channel");
  58. char *append = astman_get_header(m, "Append");
  59. if (ast_strlen_zero(channel)) {
  60. astman_send_error(s, m, "No Channel specified");
  61. return 0;
  62. }
  63. if (ast_strlen_zero(userfield)) {
  64. astman_send_error(s, m, "No UserField specified");
  65. return 0;
  66. }
  67. c = ast_channel_walk_locked(NULL);
  68. while (c) {
  69. if (!strcasecmp(c->name, channel))
  70. break;
  71. ast_mutex_unlock(&c->lock);
  72. c = ast_channel_walk_locked(c);
  73. }
  74. if (!c) {
  75. astman_send_error(s, m, "No such channel");
  76. return 0;
  77. }
  78. if (ast_true(append))
  79. ast_cdr_appenduserfield(c, userfield);
  80. else
  81. ast_cdr_setuserfield(c, userfield);
  82. ast_mutex_unlock(&c->lock);
  83. astman_send_ack(s, m, "CDR Userfield Set");
  84. return 0;
  85. }
  86. static int setcdruserfield_exec(struct ast_channel *chan, void *data)
  87. {
  88. struct localuser *u;
  89. int res = 0;
  90. LOCAL_USER_ADD(u)
  91. if (chan->cdr && data)
  92. {
  93. ast_cdr_setuserfield(chan, (char*)data);
  94. }
  95. LOCAL_USER_REMOVE(u);
  96. return res;
  97. }
  98. static int appendcdruserfield_exec(struct ast_channel *chan, void *data)
  99. {
  100. struct localuser *u;
  101. int res = 0;
  102. LOCAL_USER_ADD(u)
  103. if (chan->cdr && data)
  104. {
  105. ast_cdr_appenduserfield(chan, (char*)data);
  106. }
  107. LOCAL_USER_REMOVE(u);
  108. return res;
  109. }
  110. int unload_module(void)
  111. {
  112. int res;
  113. STANDARD_HANGUP_LOCALUSERS;
  114. res = ast_unregister_application(setcdruserfield_app);
  115. res |= ast_unregister_application(appendcdruserfield_app);
  116. ast_manager_unregister("SetCDRUserField");
  117. return res;
  118. }
  119. int load_module(void)
  120. {
  121. int res;
  122. res = ast_register_application(setcdruserfield_app, setcdruserfield_exec, setcdruserfield_synopsis, setcdruserfield_descrip);
  123. res |= ast_register_application(appendcdruserfield_app, appendcdruserfield_exec, appendcdruserfield_synopsis, appendcdruserfield_descrip);
  124. ast_manager_register("SetCDRUserField", EVENT_FLAG_CALL, action_setcdruserfield, "Set the CDR UserField");
  125. return res;
  126. }
  127. char *description(void)
  128. {
  129. return tdesc;
  130. }
  131. int usecount(void)
  132. {
  133. int res;
  134. STANDARD_USECOUNT(res);
  135. return res;
  136. }
  137. char *key()
  138. {
  139. return ASTERISK_GPL_KEY;
  140. }