app_substring.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/pbx.h>
  20. #include <asterisk/lock.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. static char *tdesc = "(Deprecated) Save substring digits in a given variable";
  26. static char *descrip =
  27. " (Deprecated, use ${variable:a:b} instead)\n"
  28. "\n"
  29. " SubString(variable=string_of_digits|count1|count2): Assigns the substring\n"
  30. "of string_of_digits to a given variable. Parameter count1 may be positive\n"
  31. "or negative. If it's positive then we skip the first count1 digits from the\n"
  32. "left. If it's negative, we move count1 digits counting from the end of\n"
  33. "the string to the left. Parameter count2 implies how many digits we are\n"
  34. "taking from the point that count1 placed us. If count2 is negative, then\n"
  35. "that many digits are omitted from the end.\n"
  36. "For example:\n"
  37. "exten => _NXXXXXX,1,SubString,test=2564286161|0|3\n"
  38. "assigns the area code (3 first digits) to variable test.\n"
  39. "exten => _NXXXXXX,1,SubString,test=2564286161|-7|7\n"
  40. "assigns the last 7 digits to variable test.\n"
  41. "exten => _NXXXXXX,1,SubString,test=2564286161|0|-4\n"
  42. "assigns all but the last 4 digits to variable test.\n"
  43. "If there are no parameters it'll return with -1.\n"
  44. "If there wrong parameters it go on and return with 0\n";
  45. static char *app = "SubString";
  46. static char *synopsis = "(Deprecated) Save substring digits in a given variable";
  47. STANDARD_LOCAL_USER;
  48. LOCAL_USER_DECL;
  49. static int substring_exec(struct ast_channel *chan, void *data)
  50. {
  51. char newexten[AST_MAX_EXTENSION] = "";
  52. char *count1, *count2;
  53. char *first, *second, *stringp;
  54. stringp=alloca(strlen(data)+1);
  55. ast_log(LOG_WARNING, "The use of Substring application is deprecated. Please use ${variable:a:b} instead\n");
  56. strncpy(stringp,data,strlen(data));
  57. if (strchr(stringp,'|')&&strchr(stringp,'=')) {
  58. int icount1,icount2;
  59. first=strsep(&stringp,"=");
  60. second=strsep(&stringp,"|");
  61. count1=strsep(&stringp,"|");
  62. count2=strsep(&stringp,"\0");
  63. if (!first || !second || !count1 || !count2) {
  64. ast_log(LOG_DEBUG, "Ignoring, since there is no argument: variable or string or count1 or count2\n");
  65. return 0;
  66. }
  67. icount1=atoi(count1);
  68. icount2=atoi(count2);
  69. if (icount2<0) {
  70. icount2 = icount2 + strlen(second);
  71. }
  72. if (abs(icount1)>strlen(second)) {
  73. ast_log(LOG_WARNING, "Limiting count1 parameter because it exceeds the length of the string\n");
  74. if (icount1>=0)
  75. icount1=strlen(second);
  76. else
  77. icount1=0;
  78. }
  79. if ((icount1<0 && icount2>-icount1) || (icount1>=0 && icount1+icount2>strlen(second))) {
  80. ast_log(LOG_WARNING, "Limiting count2 parameter because it exceeds the length of the string\n");
  81. if (icount1>=0)
  82. icount2=strlen(second)-icount1;
  83. else
  84. icount2=strlen(second)+icount1;
  85. }
  86. if (first&&second) {
  87. if (icount1>=0)
  88. strncpy(newexten,second+icount1,icount2);
  89. else
  90. strncpy(newexten,second+strlen(second)+icount1,icount2);
  91. pbx_builtin_setvar_helper(chan,first,newexten);
  92. }
  93. } else {
  94. ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
  95. }
  96. return 0;
  97. }
  98. int unload_module(void)
  99. {
  100. STANDARD_HANGUP_LOCALUSERS;
  101. return ast_unregister_application(app);
  102. }
  103. int load_module(void)
  104. {
  105. return ast_register_application(app, substring_exec, synopsis, descrip);
  106. }
  107. char *description(void)
  108. {
  109. return tdesc;
  110. }
  111. int usecount(void)
  112. {
  113. int res;
  114. STANDARD_USECOUNT(res);
  115. return res;
  116. }
  117. char *key()
  118. {
  119. return ASTERISK_GPL_KEY;
  120. }