app_lookupcidname.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * App to set callerid name from database, based on directory number
  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/options.h>
  17. #include <asterisk/channel.h>
  18. #include <asterisk/pbx.h>
  19. #include <asterisk/module.h>
  20. #include <asterisk/translate.h>
  21. #include <asterisk/image.h>
  22. #include <asterisk/callerid.h>
  23. #include <asterisk/astdb.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. static char *tdesc = "Look up CallerID Name from local database";
  27. static char *app = "LookupCIDName";
  28. static char *synopsis = "Look up CallerID Name from local database";
  29. static char *descrip =
  30. " LookupCIDName: Looks up the Caller*ID number on the active\n"
  31. "channel in the Asterisk database (family 'cidname') and sets the\n"
  32. "Caller*ID name. Does nothing if no Caller*ID was received on the\n"
  33. "channel. This is useful if you do not subscribe to Caller*ID\n"
  34. "name delivery, or if you want to change the names on some incoming\n"
  35. "calls. Always returns 0.\n";
  36. STANDARD_LOCAL_USER;
  37. LOCAL_USER_DECL;
  38. static int
  39. lookupcidname_exec (struct ast_channel *chan, void *data)
  40. {
  41. char old_cid[144] = "", *num, *name;
  42. char new_cid[144];
  43. char dbname[64];
  44. char shrunknum[64] = "";
  45. struct localuser *u;
  46. LOCAL_USER_ADD (u);
  47. if (chan->callerid)
  48. {
  49. strncpy (old_cid, chan->callerid, sizeof (old_cid) - 1);
  50. ast_callerid_parse (old_cid, &name, &num); /* this destroys the original string */
  51. if (num) /* It's possible to get an empty number */
  52. strncpy (shrunknum, num, sizeof (shrunknum) - 1);
  53. else
  54. num = shrunknum;
  55. ast_shrink_phone_number (shrunknum);
  56. if (!ast_db_get ("cidname", shrunknum, dbname, sizeof (dbname)))
  57. {
  58. snprintf (new_cid, sizeof (new_cid), "\"%s\" <%s>", dbname, num);
  59. ast_set_callerid (chan, new_cid, 0);
  60. if (option_verbose > 2)
  61. ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID to %s\n",
  62. new_cid);
  63. }
  64. }
  65. LOCAL_USER_REMOVE (u);
  66. return 0;
  67. }
  68. int
  69. unload_module (void)
  70. {
  71. STANDARD_HANGUP_LOCALUSERS;
  72. return ast_unregister_application (app);
  73. }
  74. int
  75. load_module (void)
  76. {
  77. return ast_register_application (app, lookupcidname_exec, synopsis,
  78. descrip);
  79. }
  80. char *
  81. description (void)
  82. {
  83. return tdesc;
  84. }
  85. int
  86. usecount (void)
  87. {
  88. int res;
  89. STANDARD_USECOUNT (res);
  90. return res;
  91. }
  92. char *
  93. key ()
  94. {
  95. return ASTERISK_GPL_KEY;
  96. }