pbx_spool.c 11 KB

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