pbx_spool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Full-featured outgoing call spool support
  21. *
  22. */
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <time.h>
  26. #include <utime.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <dirent.h>
  30. #include <string.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/lock.h"
  37. #include "asterisk/file.h"
  38. #include "asterisk/logger.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/callerid.h"
  41. #include "asterisk/pbx.h"
  42. #include "asterisk/module.h"
  43. #include "asterisk/options.h"
  44. #include "asterisk/utils.h"
  45. /*
  46. * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
  47. * The spool file contains a header
  48. */
  49. static char *tdesc = "Outgoing Spool Support";
  50. static char qdir[255];
  51. struct outgoing {
  52. char fn[256];
  53. /* Current number of retries */
  54. int retries;
  55. /* Maximum number of retries permitted */
  56. int maxretries;
  57. /* How long to wait between retries (in seconds) */
  58. int retrytime;
  59. /* How long to wait for an answer */
  60. int waittime;
  61. /* PID which is currently calling */
  62. int callingpid;
  63. /* What to connect to outgoing */
  64. char tech[256];
  65. char dest[256];
  66. /* If application */
  67. char app[256];
  68. char data[256];
  69. /* If extension/context/priority */
  70. char exten[256];
  71. char context[256];
  72. int priority;
  73. /* CallerID Information */
  74. char cid_num[256];
  75. char cid_name[256];
  76. /* Variables and Functions */
  77. struct ast_variable *vars;
  78. /* Maximum length of call */
  79. int maxlen;
  80. };
  81. static void init_outgoing(struct outgoing *o)
  82. {
  83. memset(o, 0, sizeof(struct outgoing));
  84. o->priority = 1;
  85. o->retrytime = 300;
  86. o->waittime = 45;
  87. }
  88. static void free_outgoing(struct outgoing *o)
  89. {
  90. free(o);
  91. }
  92. static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
  93. {
  94. char buf[256];
  95. char *c, *c2;
  96. int lineno = 0;
  97. struct ast_variable *var;
  98. while(fgets(buf, sizeof(buf), f)) {
  99. lineno++;
  100. /* Trim comments */
  101. c = buf;
  102. while ((c = strchr(c, '#'))) {
  103. if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
  104. *c = '\0';
  105. else
  106. c++;
  107. }
  108. c = buf;
  109. while ((c = strchr(c, ';'))) {
  110. if ((c > buf) && (c[-1] == '\\')) {
  111. memmove(c - 1, c, strlen(c) + 1);
  112. c++;
  113. } else {
  114. *c = '\0';
  115. break;
  116. }
  117. }
  118. /* Trim trailing white space */
  119. while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33)
  120. buf[strlen(buf) - 1] = '\0';
  121. if (!ast_strlen_zero(buf)) {
  122. c = strchr(buf, ':');
  123. if (c) {
  124. *c = '\0';
  125. c++;
  126. while ((*c) && (*c < 33))
  127. c++;
  128. #if 0
  129. printf("'%s' is '%s' at line %d\n", buf, c, lineno);
  130. #endif
  131. if (!strcasecmp(buf, "channel")) {
  132. strncpy(o->tech, c, sizeof(o->tech) - 1);
  133. if ((c2 = strchr(o->tech, '/'))) {
  134. *c2 = '\0';
  135. c2++;
  136. strncpy(o->dest, c2, sizeof(o->dest) - 1);
  137. } else {
  138. ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
  139. o->tech[0] = '\0';
  140. }
  141. } else if (!strcasecmp(buf, "callerid")) {
  142. ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
  143. } else if (!strcasecmp(buf, "application")) {
  144. strncpy(o->app, c, sizeof(o->app) - 1);
  145. } else if (!strcasecmp(buf, "data")) {
  146. strncpy(o->data, c, sizeof(o->data) - 1);
  147. } else if (!strcasecmp(buf, "maxretries")) {
  148. if (sscanf(c, "%d", &o->maxretries) != 1) {
  149. ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
  150. o->maxretries = 0;
  151. }
  152. } else if (!strcasecmp(buf, "context")) {
  153. strncpy(o->context, c, sizeof(o->context) - 1);
  154. } else if (!strcasecmp(buf, "extension")) {
  155. strncpy(o->exten, c, sizeof(o->exten) - 1);
  156. } else if (!strcasecmp(buf, "priority")) {
  157. if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
  158. ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
  159. o->priority = 1;
  160. }
  161. } else if (!strcasecmp(buf, "retrytime")) {
  162. if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
  163. ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
  164. o->retrytime = 300;
  165. }
  166. } else if (!strcasecmp(buf, "waittime")) {
  167. if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
  168. ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
  169. o->waittime = 45;
  170. }
  171. } else if (!strcasecmp(buf, "retry")) {
  172. o->retries++;
  173. } else if (!strcasecmp(buf, "startretry")) {
  174. if (sscanf(c, "%d", &o->callingpid) != 1) {
  175. ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
  176. o->callingpid = 0;
  177. }
  178. } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
  179. o->callingpid = 0;
  180. o->retries++;
  181. } else if (!strcasecmp(buf, "delayedretry")) {
  182. } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) {
  183. c2 = c;
  184. strsep(&c2, "=");
  185. if (c2) {
  186. var = ast_variable_new(c, c2);
  187. if (var) {
  188. var->next = o->vars;
  189. o->vars = var;
  190. }
  191. } else {
  192. ast_log(LOG_WARNING, "Malformed Set: argument! Should be Set: Variable=value\n");
  193. }
  194. } else if (!strcasecmp(buf, "account")) {
  195. var = ast_variable_new("CDR(accountcode|r)", c);
  196. if (var) {
  197. var->next = o->vars;
  198. o->vars = var;
  199. }
  200. } else {
  201. ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
  202. }
  203. } else
  204. ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
  205. }
  206. }
  207. strncpy(o->fn, fn, sizeof(o->fn) - 1);
  208. if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
  209. ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. static void safe_append(struct outgoing *o, time_t now, char *s)
  215. {
  216. int fd;
  217. FILE *f;
  218. struct utimbuf tbuf;
  219. fd = open(o->fn, O_WRONLY|O_APPEND);
  220. if (fd > -1) {
  221. f = fdopen(fd, "a");
  222. if (f) {
  223. fprintf(f, "%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
  224. fclose(f);
  225. } else
  226. close(fd);
  227. /* Update the file time */
  228. tbuf.actime = now;
  229. tbuf.modtime = now + o->retrytime;
  230. if (utime(o->fn, &tbuf))
  231. ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
  232. }
  233. }
  234. static void *attempt_thread(void *data)
  235. {
  236. struct outgoing *o = data;
  237. int res, reason;
  238. if (!ast_strlen_zero(o->app)) {
  239. if (option_verbose > 2)
  240. ast_verbose(VERBOSE_PREFIX_3 "Attempting call on %s/%s for application %s(%s) (Retry %d)\n", o->tech, o->dest, o->app, o->data, o->retries);
  241. res = ast_pbx_outgoing_app(o->tech, AST_FORMAT_SLINEAR, o->dest, o->waittime * 1000, o->app, o->data, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, NULL);
  242. } else {
  243. if (option_verbose > 2)
  244. ast_verbose(VERBOSE_PREFIX_3 "Attempting call on %s/%s for %s@%s:%d (Retry %d)\n", o->tech, o->dest, o->exten, o->context,o->priority, o->retries);
  245. res = ast_pbx_outgoing_exten(o->tech, AST_FORMAT_SLINEAR, o->dest, o->waittime * 1000, o->context, o->exten, o->priority, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, NULL);
  246. }
  247. if (res) {
  248. ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
  249. if (o->retries >= o->maxretries + 1) {
  250. /* Max retries exceeded */
  251. ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt%s\n", o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : "");
  252. unlink(o->fn);
  253. } else {
  254. /* Notate that the call is still active */
  255. safe_append(o, time(NULL), "EndRetry");
  256. }
  257. } else {
  258. ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
  259. ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
  260. unlink(o->fn);
  261. }
  262. free_outgoing(o);
  263. return NULL;
  264. }
  265. static void launch_service(struct outgoing *o)
  266. {
  267. pthread_t t;
  268. pthread_attr_t attr;
  269. pthread_attr_init(&attr);
  270. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  271. if (ast_pthread_create(&t,&attr,attempt_thread, o) == -1) {
  272. ast_log(LOG_WARNING, "Unable to create thread :(\n");
  273. free_outgoing(o);
  274. }
  275. }
  276. static int scan_service(char *fn, time_t now, time_t atime)
  277. {
  278. struct outgoing *o;
  279. FILE *f;
  280. o = malloc(sizeof(struct outgoing));
  281. if (o) {
  282. init_outgoing(o);
  283. f = fopen(fn, "r+");
  284. if (f) {
  285. if (!apply_outgoing(o, fn, f)) {
  286. #if 0
  287. printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
  288. #endif
  289. fclose(f);
  290. if (o->retries <= o->maxretries) {
  291. if (o->callingpid && (o->callingpid == ast_mainpid)) {
  292. safe_append(o, time(NULL), "DelayedRetry");
  293. ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
  294. } else {
  295. /* Increment retries */
  296. o->retries++;
  297. /* If someone else was calling, they're presumably gone now
  298. so abort their retry and continue as we were... */
  299. if (o->callingpid)
  300. safe_append(o, time(NULL), "AbortRetry");
  301. safe_append(o, now, "StartRetry");
  302. launch_service(o);
  303. }
  304. now += o->retrytime;
  305. return now;
  306. } else {
  307. ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt%s\n", o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : "");
  308. free_outgoing(o);
  309. unlink(fn);
  310. return 0;
  311. }
  312. } else {
  313. free_outgoing(o);
  314. ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
  315. fclose(f);
  316. unlink(fn);
  317. }
  318. } else {
  319. free_outgoing(o);
  320. ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
  321. unlink(fn);
  322. }
  323. } else
  324. ast_log(LOG_WARNING, "Out of memory :(\n");
  325. return -1;
  326. }
  327. static void *scan_thread(void *unused)
  328. {
  329. struct stat st;
  330. DIR *dir;
  331. struct dirent *de;
  332. char fn[256];
  333. int res;
  334. time_t last = 0, next = 0, now;
  335. for(;;) {
  336. /* Wait a sec */
  337. sleep(1);
  338. time(&now);
  339. if (!stat(qdir, &st)) {
  340. if ((st.st_mtime != last) || (next && (now > next))) {
  341. #if 0
  342. printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
  343. printf("Ooh, something changed / timeout\n");
  344. #endif
  345. next = 0;
  346. last = st.st_mtime;
  347. dir = opendir(qdir);
  348. if (dir) {
  349. while((de = readdir(dir))) {
  350. snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
  351. if (!stat(fn, &st)) {
  352. if (S_ISREG(st.st_mode)) {
  353. if (st.st_mtime <= now) {
  354. res = scan_service(fn, now, st.st_atime);
  355. if (res > 0) {
  356. /* Update next service time */
  357. if (!next || (res < next)) {
  358. next = res;
  359. }
  360. } else if (res)
  361. ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
  362. } else {
  363. /* Update "next" update if necessary */
  364. if (!next || (st.st_mtime < next))
  365. next = st.st_mtime;
  366. }
  367. }
  368. } else
  369. ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
  370. }
  371. closedir(dir);
  372. } else
  373. ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
  374. }
  375. } else
  376. ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
  377. }
  378. return NULL;
  379. }
  380. int unload_module(void)
  381. {
  382. return -1;
  383. }
  384. int load_module(void)
  385. {
  386. pthread_t thread;
  387. pthread_attr_t attr;
  388. snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
  389. if (mkdir(qdir, 0700) && (errno != EEXIST)) {
  390. ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
  391. return 0;
  392. }
  393. pthread_attr_init(&attr);
  394. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  395. if (ast_pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
  396. ast_log(LOG_WARNING, "Unable to create thread :(\n");
  397. return -1;
  398. }
  399. return 0;
  400. }
  401. char *description(void)
  402. {
  403. return tdesc;
  404. }
  405. int usecount(void)
  406. {
  407. return 1;
  408. }
  409. char *key()
  410. {
  411. return ASTERISK_GPL_KEY;
  412. }