app_striplsd.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Skeleton application
  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 <sys/types.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/lock.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. static char *tdesc = "Strip trailing digits";
  25. static char *descrip =
  26. " StripLSD(count): Strips the trailing 'count' digits from the channel's\n"
  27. "associated extension. For example, the number 5551212 when stripped with a\n"
  28. "count of 4 would be changed to 555. This app always returns 0, and the PBX\n"
  29. "will continue processing at the next priority for the *new* extension.\n"
  30. " So, for example, if priority 3 of 5551212 is StripLSD 4, the next step\n"
  31. "executed will be priority 4 of 555. If you switch into an extension which\n"
  32. "has no first step, the PBX will treat it as though the user dialed an\n"
  33. "invalid extension.\n";
  34. static char *app = "StripLSD";
  35. static char *synopsis = "Strip Least Significant Digits";
  36. STANDARD_LOCAL_USER;
  37. LOCAL_USER_DECL;
  38. static int striplsd_exec(struct ast_channel *chan, void *data)
  39. {
  40. char newexten[AST_MAX_EXTENSION] = "";
  41. int maxbytes = 0;
  42. int stripcount = 0;
  43. int extlen = strlen(chan->exten);
  44. maxbytes = sizeof(newexten) - 1;
  45. if (data) {
  46. stripcount = atoi(data);
  47. }
  48. if (!stripcount) {
  49. ast_log(LOG_DEBUG, "Ignoring, since number of digits to strip is 0\n");
  50. return 0;
  51. }
  52. if (extlen > stripcount) {
  53. if (extlen - stripcount <= maxbytes) {
  54. maxbytes = extlen - stripcount;
  55. }
  56. strncpy(newexten, chan->exten, maxbytes);
  57. }
  58. strncpy(chan->exten, newexten, sizeof(chan->exten)-1);
  59. return 0;
  60. }
  61. int unload_module(void)
  62. {
  63. STANDARD_HANGUP_LOCALUSERS;
  64. return ast_unregister_application(app);
  65. }
  66. int load_module(void)
  67. {
  68. return ast_register_application(app, striplsd_exec, synopsis, descrip);
  69. }
  70. char *description(void)
  71. {
  72. return tdesc;
  73. }
  74. int usecount(void)
  75. {
  76. int res;
  77. STANDARD_USECOUNT(res);
  78. return res;
  79. }
  80. char *key()
  81. {
  82. return ASTERISK_GPL_KEY;
  83. }