app_setcidname.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * App to set callerid
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/translate.h>
  20. #include <asterisk/image.h>
  21. #include <asterisk/callerid.h>
  22. #include <asterisk/utils.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. static char *tdesc = "Set CallerID Name";
  26. static char *app = "SetCIDName";
  27. static char *synopsis = "Set CallerID Name";
  28. static char *descrip =
  29. " SetCIDName(cname[|a]): Set Caller*ID Name on a call to a new\n"
  30. "value, while preserving the original Caller*ID number. This is\n"
  31. "useful for providing additional information to the called\n"
  32. "party. Sets ANI as well if a flag is used. Always returns 0\n";
  33. STANDARD_LOCAL_USER;
  34. LOCAL_USER_DECL;
  35. static int setcallerid_exec(struct ast_channel *chan, void *data)
  36. {
  37. int res = 0;
  38. char tmp[256] = "";
  39. char oldcid[256] = "", *l, *n;
  40. char newcid[256] = "";
  41. struct localuser *u;
  42. char *opt;
  43. int anitoo = 0;
  44. if (data)
  45. strncpy(tmp, (char *)data, sizeof(tmp) - 1);
  46. opt = strchr(tmp, '|');
  47. if (opt) {
  48. *opt = '\0';
  49. opt++;
  50. if (*opt == 'a')
  51. anitoo = 1;
  52. }
  53. LOCAL_USER_ADD(u);
  54. if (chan->callerid) {
  55. strncpy(oldcid, chan->callerid, sizeof(oldcid) - 1);
  56. ast_callerid_parse(oldcid, &n, &l);
  57. n = tmp;
  58. if (!ast_strlen_zero(n)) {
  59. if (l && !ast_strlen_zero(l))
  60. snprintf(newcid, sizeof(newcid), "\"%s\" <%s>", n, l);
  61. else
  62. strncpy(newcid, tmp, sizeof(newcid) - 1);
  63. } else if (l && !ast_strlen_zero(l)) {
  64. strncpy(newcid, l, sizeof(newcid) - 1);
  65. }
  66. } else
  67. strncpy(newcid, tmp, sizeof(newcid) - 1);
  68. ast_set_callerid(chan, !ast_strlen_zero(newcid) ? newcid : NULL, anitoo);
  69. LOCAL_USER_REMOVE(u);
  70. return res;
  71. }
  72. int unload_module(void)
  73. {
  74. STANDARD_HANGUP_LOCALUSERS;
  75. return ast_unregister_application(app);
  76. }
  77. int load_module(void)
  78. {
  79. return ast_register_application(app, setcallerid_exec, synopsis, descrip);
  80. }
  81. char *description(void)
  82. {
  83. return tdesc;
  84. }
  85. int usecount(void)
  86. {
  87. int res;
  88. STANDARD_USECOUNT(res);
  89. return res;
  90. }
  91. char *key()
  92. {
  93. return ASTERISK_GPL_KEY;
  94. }