pbx_spool.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <pthread.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(!feof(f)) {
  83. fgets(buf, sizeof(buf), f);
  84. lineno++;
  85. if (!feof(f)) {
  86. /* Trim comments */
  87. c = buf;
  88. while ((c = strchr(c, '#'))) {
  89. if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
  90. *c = '\0';
  91. else
  92. c++;
  93. }
  94. c = strchr(buf, ';');
  95. if (c)
  96. *c = '\0';
  97. /* Trim trailing white space */
  98. while(strlen(buf) && buf[strlen(buf) - 1] < 33)
  99. buf[strlen(buf) - 1] = '\0';
  100. if (strlen(buf)) {
  101. c = strchr(buf, ':');
  102. if (c) {
  103. *c = '\0';
  104. c++;
  105. while(*c < 33)
  106. c++;
  107. #if 0
  108. printf("'%s' is '%s' at line %d\n", buf, c, lineno);
  109. #endif
  110. if (!strcasecmp(buf, "channel")) {
  111. strncpy(o->tech, c, sizeof(o->tech) - 1);
  112. if ((c2 = strchr(o->tech, '/'))) {
  113. *c2 = '\0';
  114. c2++;
  115. strncpy(o->dest, c2, sizeof(o->dest) - 1);
  116. } else {
  117. ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
  118. strcpy(o->tech, "");
  119. }
  120. } else if (!strcasecmp(buf, "callerid")) {
  121. strncpy(o->callerid, c, sizeof(o->callerid) - 1);
  122. } else if (!strcasecmp(buf, "application")) {
  123. strncpy(o->app, c, sizeof(o->app) - 1);
  124. } else if (!strcasecmp(buf, "data")) {
  125. strncpy(o->data, c, sizeof(o->data) - 1);
  126. } else if (!strcasecmp(buf, "maxretries")) {
  127. if (sscanf(c, "%d", &o->maxretries) != 1) {
  128. ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
  129. o->maxretries = 0;
  130. }
  131. } else if (!strcasecmp(buf, "context")) {
  132. strncpy(o->context, c, sizeof(o->context) - 1);
  133. } else if (!strcasecmp(buf, "extension")) {
  134. strncpy(o->exten, c, sizeof(o->exten) - 1);
  135. } else if (!strcasecmp(buf, "priority")) {
  136. if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
  137. ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
  138. o->priority = 1;
  139. }
  140. } else if (!strcasecmp(buf, "retrytime")) {
  141. if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
  142. ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
  143. o->retrytime = 300;
  144. }
  145. } else if (!strcasecmp(buf, "waittime")) {
  146. if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
  147. ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
  148. o->waittime = 45;
  149. }
  150. } else if (!strcasecmp(buf, "retry")) {
  151. o->retries++;
  152. } else if (!strcasecmp(buf, "startretry")) {
  153. if (sscanf(c, "%d", &o->callingpid) != 1) {
  154. ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
  155. o->callingpid = 0;
  156. }
  157. } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
  158. o->callingpid = 0;
  159. o->retries++;
  160. } else if (!strcasecmp(buf, "delayedretry")) {
  161. } else if (!strcasecmp(buf, "setvar")) { /* JDG variable support */
  162. strncat(o->variable, c, sizeof(o->variable) - strlen(o->variable) - 1);
  163. strncat(o->variable, "|", sizeof(o->variable) - strlen(o->variable) - 1);
  164. } else if (!strcasecmp(buf, "account")) {
  165. strncpy(o->account, c, sizeof(o->account) - 1);
  166. } else {
  167. ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
  168. }
  169. } else
  170. ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
  171. }
  172. }
  173. }
  174. strncpy(o->fn, fn, sizeof(o->fn) - 1);
  175. /* Check sanity of times */
  176. if (o->retrytime < o->waittime + 5)
  177. o->retrytime = o->waittime + 5;
  178. if (!strlen(o->tech) || !strlen(o->dest) || (!strlen(o->app) && !strlen(o->exten))) {
  179. ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. static void safe_append(struct outgoing *o, time_t now, char *s)
  185. {
  186. int fd;
  187. FILE *f;
  188. struct utimbuf tbuf;
  189. fd = open(o->fn, O_WRONLY|O_APPEND);
  190. if (fd > -1) {
  191. f = fdopen(fd, "a");
  192. if (f) {
  193. fprintf(f, "%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
  194. fclose(f);
  195. } else
  196. close(fd);
  197. /* Update the file time */
  198. tbuf.actime = now;
  199. tbuf.modtime = now + o->retrytime;
  200. if (utime(o->fn, &tbuf))
  201. ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
  202. }
  203. }
  204. static void *attempt_thread(void *data)
  205. {
  206. struct outgoing *o = data;
  207. int res, reason;
  208. if (strlen(o->app)) {
  209. if (option_verbose > 2)
  210. 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);
  211. 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);
  212. } else {
  213. if (option_verbose > 2)
  214. 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);
  215. 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);
  216. }
  217. if (res) {
  218. ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
  219. if (o->retries >= o->maxretries + 1) {
  220. /* Max retries exceeded */
  221. ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
  222. unlink(o->fn);
  223. } else {
  224. /* Notate that the call is still active */
  225. safe_append(o, time(NULL), "EndRetry");
  226. }
  227. } else {
  228. ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
  229. ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
  230. unlink(o->fn);
  231. }
  232. free(o);
  233. return NULL;
  234. }
  235. static void launch_service(struct outgoing *o)
  236. {
  237. pthread_t t;
  238. pthread_attr_t attr;
  239. pthread_attr_init(&attr);
  240. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  241. if (pthread_create(&t,&attr,attempt_thread, o) == -1) {
  242. ast_log(LOG_WARNING, "Unable to create thread :(\n");
  243. free(o);
  244. }
  245. }
  246. static int scan_service(char *fn, time_t now, time_t atime)
  247. {
  248. struct outgoing *o;
  249. FILE *f;
  250. o = malloc(sizeof(struct outgoing));
  251. if (o) {
  252. init_outgoing(o);
  253. f = fopen(fn, "r+");
  254. if (f) {
  255. if (!apply_outgoing(o, fn, f)) {
  256. #if 0
  257. printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
  258. #endif
  259. fclose(f);
  260. if (o->retries <= o->maxretries) {
  261. if (o->callingpid && (o->callingpid == ast_mainpid)) {
  262. safe_append(o, time(NULL), "DelayedRetry");
  263. ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
  264. } else {
  265. /* Increment retries */
  266. o->retries++;
  267. /* If someone else was calling, they're presumably gone now
  268. so abort their retry and continue as we were... */
  269. if (o->callingpid)
  270. safe_append(o, time(NULL), "AbortRetry");
  271. safe_append(o, now, "StartRetry");
  272. launch_service(o);
  273. }
  274. now += o->retrytime;
  275. return now;
  276. } else {
  277. ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
  278. free(o);
  279. unlink(fn);
  280. return 0;
  281. }
  282. } else {
  283. free(o);
  284. ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
  285. fclose(f);
  286. unlink(fn);
  287. }
  288. } else {
  289. free(o);
  290. ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
  291. unlink(fn);
  292. }
  293. } else
  294. ast_log(LOG_WARNING, "Out of memory :(\n");
  295. return -1;
  296. }
  297. static void *scan_thread(void *unused)
  298. {
  299. struct stat st;
  300. DIR *dir;
  301. struct dirent *de;
  302. char fn[256];
  303. int res;
  304. time_t last = 0, next = 0, now;
  305. for(;;) {
  306. /* Wait a sec */
  307. sleep(1);
  308. time(&now);
  309. if (!stat(qdir, &st)) {
  310. if ((st.st_mtime != last) || (next && (now > next))) {
  311. #if 0
  312. printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
  313. printf("Ooh, something changed / timeout\n");
  314. #endif
  315. next = 0;
  316. last = st.st_mtime;
  317. dir = opendir(qdir);
  318. if (dir) {
  319. while((de = readdir(dir))) {
  320. snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
  321. if (!stat(fn, &st)) {
  322. if (S_ISREG(st.st_mode)) {
  323. if (st.st_mtime <= now) {
  324. res = scan_service(fn, now, st.st_atime);
  325. if (res > 0) {
  326. /* Update next service time */
  327. if (!next || (res < next)) {
  328. next = res;
  329. }
  330. } else if (res)
  331. ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
  332. } else {
  333. /* Update "next" update if necessary */
  334. if (!next || (st.st_mtime < next))
  335. next = st.st_mtime;
  336. }
  337. }
  338. } else
  339. ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
  340. }
  341. closedir(dir);
  342. } else
  343. ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
  344. }
  345. } else
  346. ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
  347. }
  348. return NULL;
  349. }
  350. int unload_module(void)
  351. {
  352. return -1;
  353. }
  354. int load_module(void)
  355. {
  356. pthread_t thread;
  357. pthread_attr_t attr;
  358. snprintf((char *)qdir,sizeof(qdir)-1,"%s/%s",(char *)ast_config_AST_SPOOL_DIR,"outgoing");
  359. if (mkdir(qdir, 0700) && (errno != EEXIST)) {
  360. ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
  361. return 0;
  362. }
  363. pthread_attr_init(&attr);
  364. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  365. if (pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
  366. ast_log(LOG_WARNING, "Unable to create thread :(\n");
  367. return -1;
  368. }
  369. return 0;
  370. }
  371. char *description(void)
  372. {
  373. return tdesc;
  374. }
  375. int usecount(void)
  376. {
  377. return 1;
  378. }
  379. char *key()
  380. {
  381. return ASTERISK_GPL_KEY;
  382. }