app_followme.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * A full-featured Find-Me/Follow-Me Application
  5. *
  6. * Copyright (C) 2005-2006, BJ Weschke All Rights Reserved.
  7. *
  8. * BJ Weschke <bweschke@btwtech.com>
  9. *
  10. * This code is released by the author with no restrictions on usage.
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. */
  19. /*! \file
  20. *
  21. * \brief Find-Me Follow-Me application
  22. *
  23. * \author BJ Weschke <bweschke@btwtech.com>
  24. *
  25. * \ingroup applications
  26. */
  27. /*! \li \ref app_followme.c uses the configuration file \ref followme.conf
  28. * \addtogroup configuration_file Configuration Files
  29. */
  30. /*!
  31. * \page followme.conf followme.conf
  32. * \verbinclude followme.conf.sample
  33. */
  34. /*** MODULEINFO
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <signal.h>
  40. #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
  41. #include "asterisk/lock.h"
  42. #include "asterisk/file.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/pbx.h"
  45. #include "asterisk/module.h"
  46. #include "asterisk/translate.h"
  47. #include "asterisk/say.h"
  48. #include "asterisk/features.h"
  49. #include "asterisk/musiconhold.h"
  50. #include "asterisk/cli.h"
  51. #include "asterisk/manager.h"
  52. #include "asterisk/config.h"
  53. #include "asterisk/utils.h"
  54. #include "asterisk/causes.h"
  55. #include "asterisk/astdb.h"
  56. #include "asterisk/dsp.h"
  57. #include "asterisk/app.h"
  58. #include "asterisk/stasis_channels.h"
  59. #include "asterisk/max_forwards.h"
  60. #define REC_FORMAT "sln"
  61. /*** DOCUMENTATION
  62. <application name="FollowMe" language="en_US">
  63. <synopsis>
  64. Find-Me/Follow-Me application.
  65. </synopsis>
  66. <syntax>
  67. <parameter name="followmeid" required="true" />
  68. <parameter name="options">
  69. <optionlist>
  70. <option name="a">
  71. <para>Record the caller's name so it can be announced to the
  72. callee on each step.</para>
  73. </option>
  74. <option name="B" argsep="^">
  75. <para>Before initiating the outgoing call(s), Gosub to the specified
  76. location using the current channel.</para>
  77. <argument name="context" required="false" />
  78. <argument name="exten" required="false" />
  79. <argument name="priority" required="true" hasparams="optional" argsep="^">
  80. <argument name="arg1" multiple="true" required="true" />
  81. <argument name="argN" />
  82. </argument>
  83. </option>
  84. <option name="b" argsep="^">
  85. <para>Before initiating an outgoing call, Gosub to the specified
  86. location using the newly created channel. The Gosub will be
  87. executed for each destination channel.</para>
  88. <argument name="context" required="false" />
  89. <argument name="exten" required="false" />
  90. <argument name="priority" required="true" hasparams="optional" argsep="^">
  91. <argument name="arg1" multiple="true" required="true" />
  92. <argument name="argN" />
  93. </argument>
  94. </option>
  95. <option name="d">
  96. <para>Disable the 'Please hold while we try to connect your call' announcement.</para>
  97. </option>
  98. <option name="I">
  99. <para>Asterisk will ignore any connected line update requests
  100. it may receive on this dial attempt.</para>
  101. </option>
  102. <option name="l">
  103. <para>Disable local call optimization so that applications with
  104. audio hooks between the local bridge don't get dropped when the
  105. calls get joined directly.</para>
  106. </option>
  107. <option name="N">
  108. <para>Don't answer the incoming call until we're ready to
  109. connect the caller or give up.</para>
  110. <note>
  111. <para>This option is ignored if the call is already answered.</para>
  112. </note>
  113. <note>
  114. <para>If the call is not already answered, the 'a' and 's'
  115. options are ignored while the 'd' option is implicitly enabled.</para>
  116. </note>
  117. </option>
  118. <option name="n">
  119. <para>Playback the unreachable status message if we've run out
  120. of steps or the callee has elected not to be reachable.</para>
  121. </option>
  122. <option name="s">
  123. <para>Playback the incoming status message prior to starting
  124. the follow-me step(s)</para>
  125. </option>
  126. </optionlist>
  127. </parameter>
  128. </syntax>
  129. <description>
  130. <para>This application performs Find-Me/Follow-Me functionality for the caller
  131. as defined in the profile matching the <replaceable>followmeid</replaceable> parameter in
  132. <filename>followme.conf</filename>. If the specified <replaceable>followmeid</replaceable>
  133. profile doesn't exist in <filename>followme.conf</filename>, execution will be returned
  134. to the dialplan and call execution will continue at the next priority.</para>
  135. <para>Returns -1 on hangup.</para>
  136. </description>
  137. </application>
  138. ***/
  139. static char *app = "FollowMe";
  140. /*! Maximum accept/decline DTMF string plus terminator. */
  141. #define MAX_YN_STRING 20
  142. /*! \brief Number structure */
  143. struct number {
  144. char number[512]; /*!< Phone Number(s) and/or Extension(s) */
  145. long timeout; /*!< Dial Timeout, if used. */
  146. int order; /*!< The order to dial in */
  147. AST_LIST_ENTRY(number) entry; /*!< Next Number record */
  148. };
  149. /*! \brief Data structure for followme scripts */
  150. struct call_followme {
  151. ast_mutex_t lock;
  152. char name[AST_MAX_EXTENSION]; /*!< Name - FollowMeID */
  153. char moh[MAX_MUSICCLASS]; /*!< Music On Hold Class to be used */
  154. char context[AST_MAX_CONTEXT]; /*!< Context to dial from */
  155. unsigned int active; /*!< Profile is active (1), or disabled (0). */
  156. int realtime; /*!< Cached from realtime */
  157. char takecall[MAX_YN_STRING]; /*!< Digit mapping to take a call */
  158. char nextindp[MAX_YN_STRING]; /*!< Digit mapping to decline a call */
  159. char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
  160. char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
  161. char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
  162. char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
  163. char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
  164. char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
  165. char connprompt[PATH_MAX]; /*!< Sound prompt name and path */
  166. AST_LIST_HEAD_NOLOCK(numbers, number) numbers; /*!< Head of the list of follow-me numbers */
  167. AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
  168. AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
  169. AST_LIST_ENTRY(call_followme) entry; /*!< Next Follow-Me record */
  170. };
  171. struct fm_args {
  172. char *mohclass;
  173. AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
  174. /*! Gosub app arguments for outgoing calls. NULL if not supplied. */
  175. const char *predial_callee;
  176. /*! Accumulated connected line information from inbound call. */
  177. struct ast_party_connected_line connected_in;
  178. /*! Accumulated connected line information from outbound call. */
  179. struct ast_party_connected_line connected_out;
  180. /*! TRUE if connected line information from inbound call changed. */
  181. unsigned int pending_in_connected_update:1;
  182. /*! TRUE if connected line information from outbound call is available. */
  183. unsigned int pending_out_connected_update:1;
  184. /*! TRUE if caller has a pending hold request for the winning call. */
  185. unsigned int pending_hold:1;
  186. /*! Music On Hold Class suggested by caller hold for winning call. */
  187. char suggested_moh[MAX_MUSICCLASS];
  188. char context[AST_MAX_CONTEXT];
  189. char namerecloc[PATH_MAX];
  190. char takecall[MAX_YN_STRING]; /*!< Digit mapping to take a call */
  191. char nextindp[MAX_YN_STRING]; /*!< Digit mapping to decline a call */
  192. char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
  193. char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
  194. char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
  195. char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
  196. char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
  197. char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
  198. char connprompt[PATH_MAX]; /*!< Sound prompt name and path */
  199. struct ast_flags followmeflags;
  200. };
  201. struct findme_user {
  202. struct ast_channel *ochan;
  203. /*! Accumulated connected line information from outgoing call. */
  204. struct ast_party_connected_line connected;
  205. long digts;
  206. int ynidx;
  207. int state;
  208. char dialarg[256];
  209. /*! Collected digits to accept/decline the call. */
  210. char yn[MAX_YN_STRING];
  211. /*! TRUE if the outgoing call is answered. */
  212. unsigned int answered:1;
  213. /*! TRUE if connected line information is available. */
  214. unsigned int pending_connected_update:1;
  215. AST_LIST_ENTRY(findme_user) entry;
  216. };
  217. enum {
  218. FOLLOWMEFLAG_STATUSMSG = (1 << 0),
  219. FOLLOWMEFLAG_RECORDNAME = (1 << 1),
  220. FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2),
  221. FOLLOWMEFLAG_DISABLEHOLDPROMPT = (1 << 3),
  222. FOLLOWMEFLAG_NOANSWER = (1 << 4),
  223. FOLLOWMEFLAG_DISABLEOPTIMIZATION = (1 << 5),
  224. FOLLOWMEFLAG_IGNORE_CONNECTEDLINE = (1 << 6),
  225. FOLLOWMEFLAG_PREDIAL_CALLER = (1 << 7),
  226. FOLLOWMEFLAG_PREDIAL_CALLEE = (1 << 8),
  227. };
  228. enum {
  229. FOLLOWMEFLAG_ARG_PREDIAL_CALLER,
  230. FOLLOWMEFLAG_ARG_PREDIAL_CALLEE,
  231. /* note: this entry _MUST_ be the last one in the enum */
  232. FOLLOWMEFLAG_ARG_ARRAY_SIZE
  233. };
  234. AST_APP_OPTIONS(followme_opts, {
  235. AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME),
  236. AST_APP_OPTION_ARG('B', FOLLOWMEFLAG_PREDIAL_CALLER, FOLLOWMEFLAG_ARG_PREDIAL_CALLER),
  237. AST_APP_OPTION_ARG('b', FOLLOWMEFLAG_PREDIAL_CALLEE, FOLLOWMEFLAG_ARG_PREDIAL_CALLEE),
  238. AST_APP_OPTION('d', FOLLOWMEFLAG_DISABLEHOLDPROMPT),
  239. AST_APP_OPTION('I', FOLLOWMEFLAG_IGNORE_CONNECTEDLINE),
  240. AST_APP_OPTION('l', FOLLOWMEFLAG_DISABLEOPTIMIZATION),
  241. AST_APP_OPTION('N', FOLLOWMEFLAG_NOANSWER),
  242. AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG),
  243. AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG),
  244. });
  245. static const char *featuredigittostr;
  246. static int featuredigittimeout = 5000; /*!< Feature Digit Timeout */
  247. static const char *defaultmoh = "default"; /*!< Default Music-On-Hold Class */
  248. static char takecall[MAX_YN_STRING] = "1";
  249. static char nextindp[MAX_YN_STRING] = "2";
  250. static char callfromprompt[PATH_MAX] = "followme/call-from";
  251. static char norecordingprompt[PATH_MAX] = "followme/no-recording";
  252. static char optionsprompt[PATH_MAX] = "followme/options";
  253. static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
  254. static char statusprompt[PATH_MAX] = "followme/status";
  255. static char sorryprompt[PATH_MAX] = "followme/sorry";
  256. static char connprompt[PATH_MAX] = "";
  257. static AST_RWLIST_HEAD_STATIC(followmes, call_followme);
  258. AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
  259. static void free_numbers(struct call_followme *f)
  260. {
  261. /* Free numbers attached to the profile */
  262. struct number *prev;
  263. while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
  264. /* Free the number */
  265. ast_free(prev);
  266. AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
  267. while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
  268. /* Free the blacklisted number */
  269. ast_free(prev);
  270. AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
  271. while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
  272. /* Free the whitelisted number */
  273. ast_free(prev);
  274. AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
  275. }
  276. /*! \brief Allocate and initialize followme profile */
  277. static struct call_followme *alloc_profile(const char *fmname)
  278. {
  279. struct call_followme *f;
  280. if (!(f = ast_calloc(1, sizeof(*f))))
  281. return NULL;
  282. ast_mutex_init(&f->lock);
  283. ast_copy_string(f->name, fmname, sizeof(f->name));
  284. AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
  285. AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
  286. AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
  287. return f;
  288. }
  289. static void init_profile(struct call_followme *f, int activate)
  290. {
  291. f->context[0] = '\0';
  292. ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
  293. ast_copy_string(f->takecall, takecall, sizeof(f->takecall));
  294. ast_copy_string(f->nextindp, nextindp, sizeof(f->nextindp));
  295. ast_copy_string(f->callfromprompt, callfromprompt, sizeof(f->callfromprompt));
  296. ast_copy_string(f->norecordingprompt, norecordingprompt, sizeof(f->norecordingprompt));
  297. ast_copy_string(f->optionsprompt, optionsprompt, sizeof(f->optionsprompt));
  298. ast_copy_string(f->plsholdprompt, plsholdprompt, sizeof(f->plsholdprompt));
  299. ast_copy_string(f->statusprompt, statusprompt, sizeof(f->statusprompt));
  300. ast_copy_string(f->sorryprompt, sorryprompt, sizeof(f->sorryprompt));
  301. ast_copy_string(f->connprompt, connprompt, sizeof(f->connprompt));
  302. if (activate) {
  303. f->active = 1;
  304. }
  305. }
  306. /*! \brief Set parameter in profile from configuration file */
  307. static void profile_set_param(struct call_followme *f, const char *param, const char *val, int linenum, int failunknown)
  308. {
  309. if (!strcasecmp(param, "musicclass") || !strcasecmp(param, "musiconhold") || !strcasecmp(param, "music"))
  310. ast_copy_string(f->moh, val, sizeof(f->moh));
  311. else if (!strcasecmp(param, "context"))
  312. ast_copy_string(f->context, val, sizeof(f->context));
  313. else if (!strcasecmp(param, "takecall"))
  314. ast_copy_string(f->takecall, val, sizeof(f->takecall));
  315. else if (!strcasecmp(param, "declinecall"))
  316. ast_copy_string(f->nextindp, val, sizeof(f->nextindp));
  317. else if (!strcasecmp(param, "call-from-prompt") || !strcasecmp(param, "call_from_prompt"))
  318. ast_copy_string(f->callfromprompt, val, sizeof(f->callfromprompt));
  319. else if (!strcasecmp(param, "followme-norecording-prompt") || !strcasecmp(param, "norecording_prompt"))
  320. ast_copy_string(f->norecordingprompt, val, sizeof(f->norecordingprompt));
  321. else if (!strcasecmp(param, "followme-options-prompt") || !strcasecmp(param, "options_prompt"))
  322. ast_copy_string(f->optionsprompt, val, sizeof(f->optionsprompt));
  323. else if (!strcasecmp(param, "followme-pls-hold-prompt") || !strcasecmp(param, "pls_hold_prompt"))
  324. ast_copy_string(f->plsholdprompt, val, sizeof(f->plsholdprompt));
  325. else if (!strcasecmp(param, "followme-status-prompt") || !strcasecmp(param, "status_prompt"))
  326. ast_copy_string(f->statusprompt, val, sizeof(f->statusprompt));
  327. else if (!strcasecmp(param, "followme-sorry-prompt") || !strcasecmp(param, "sorry_prompt"))
  328. ast_copy_string(f->sorryprompt, val, sizeof(f->sorryprompt));
  329. else if (!strcasecmp(param, "followme-connecting-prompt") || !strcasecmp(param, "connecting_prompt")) {
  330. ast_copy_string(f->connprompt, val, sizeof(f->connprompt));
  331. } else if (failunknown) {
  332. if (linenum >= 0)
  333. ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
  334. else
  335. ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
  336. }
  337. }
  338. /*! \brief Add a new number */
  339. static struct number *create_followme_number(const char *number, int timeout, int numorder)
  340. {
  341. struct number *cur;
  342. char *buf = ast_strdupa(number);
  343. char *tmp;
  344. if (!(cur = ast_calloc(1, sizeof(*cur))))
  345. return NULL;
  346. cur->timeout = timeout;
  347. if ((tmp = strchr(buf, ',')))
  348. *tmp = '\0';
  349. ast_copy_string(cur->number, buf, sizeof(cur->number));
  350. cur->order = numorder;
  351. ast_debug(1, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
  352. return cur;
  353. }
  354. /*! \brief Reload followme application module */
  355. static int reload_followme(int reload)
  356. {
  357. struct call_followme *f;
  358. struct ast_config *cfg;
  359. char *cat = NULL, *tmp;
  360. struct ast_variable *var;
  361. struct number *cur, *nm;
  362. char *numberstr;
  363. int timeout;
  364. int numorder;
  365. const char *takecallstr;
  366. const char *declinecallstr;
  367. const char *tmpstr;
  368. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  369. if (!(cfg = ast_config_load("followme.conf", config_flags))) {
  370. ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
  371. return 0;
  372. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  373. return 0;
  374. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  375. ast_log(LOG_ERROR, "Config file followme.conf is in an invalid format. Aborting.\n");
  376. return 0;
  377. }
  378. AST_RWLIST_WRLOCK(&followmes);
  379. /* Reset Global Var Values */
  380. featuredigittimeout = 5000;
  381. /* Mark all profiles as inactive for the moment */
  382. AST_RWLIST_TRAVERSE(&followmes, f, entry) {
  383. f->active = 0;
  384. }
  385. featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
  386. if (!ast_strlen_zero(featuredigittostr)) {
  387. if (!sscanf(featuredigittostr, "%30d", &featuredigittimeout))
  388. featuredigittimeout = 5000;
  389. }
  390. if ((takecallstr = ast_variable_retrieve(cfg, "general", "takecall")) && !ast_strlen_zero(takecallstr)) {
  391. ast_copy_string(takecall, takecallstr, sizeof(takecall));
  392. }
  393. if ((declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall")) && !ast_strlen_zero(declinecallstr)) {
  394. ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
  395. }
  396. if ((tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt")) && !ast_strlen_zero(tmpstr)) {
  397. ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
  398. } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "call_from_prompt")) && !ast_strlen_zero(tmpstr)) {
  399. ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
  400. }
  401. if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt")) && !ast_strlen_zero(tmpstr)) {
  402. ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
  403. } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording_prompt")) && !ast_strlen_zero(tmpstr)) {
  404. ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
  405. }
  406. if ((tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt")) && !ast_strlen_zero(tmpstr)) {
  407. ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
  408. } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "options_prompt")) && !ast_strlen_zero(tmpstr)) {
  409. ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
  410. }
  411. if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt")) && !ast_strlen_zero(tmpstr)) {
  412. ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
  413. } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls_hold_prompt")) && !ast_strlen_zero(tmpstr)) {
  414. ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
  415. }
  416. if ((tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt")) && !ast_strlen_zero(tmpstr)) {
  417. ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
  418. } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "status_prompt")) && !ast_strlen_zero(tmpstr)) {
  419. ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
  420. }
  421. if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt")) && !ast_strlen_zero(tmpstr)) {
  422. ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
  423. } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry_prompt")) && !ast_strlen_zero(tmpstr)) {
  424. ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
  425. }
  426. if ((tmpstr = ast_variable_retrieve(cfg, "general", "connecting-prompt")) && !ast_strlen_zero(tmpstr)) {
  427. ast_copy_string(connprompt, tmpstr, sizeof(connprompt));
  428. } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "connecting_prompt")) && !ast_strlen_zero(tmpstr)) {
  429. ast_copy_string(connprompt, tmpstr, sizeof(connprompt));
  430. }
  431. /* Chug through config file */
  432. while ((cat = ast_category_browse(cfg, cat))) {
  433. int new = 0;
  434. if (!strcasecmp(cat, "general"))
  435. continue;
  436. /* Look for an existing one */
  437. AST_LIST_TRAVERSE(&followmes, f, entry) {
  438. if (!strcasecmp(f->name, cat))
  439. break;
  440. }
  441. ast_debug(1, "New profile %s.\n", cat);
  442. if (!f) {
  443. /* Make one then */
  444. f = alloc_profile(cat);
  445. new = 1;
  446. }
  447. /* Totally fail if we fail to find/create an entry */
  448. if (!f)
  449. continue;
  450. if (!new)
  451. ast_mutex_lock(&f->lock);
  452. /* Re-initialize the profile */
  453. init_profile(f, 1);
  454. free_numbers(f);
  455. var = ast_variable_browse(cfg, cat);
  456. while (var) {
  457. if (!strcasecmp(var->name, "number")) {
  458. int idx = 0;
  459. /* Add a new number */
  460. numberstr = ast_strdupa(var->value);
  461. if ((tmp = strchr(numberstr, ','))) {
  462. *tmp++ = '\0';
  463. timeout = atoi(tmp);
  464. if (timeout < 0) {
  465. timeout = 25;
  466. }
  467. if ((tmp = strchr(tmp, ','))) {
  468. *tmp++ = '\0';
  469. numorder = atoi(tmp);
  470. if (numorder < 0)
  471. numorder = 0;
  472. } else
  473. numorder = 0;
  474. } else {
  475. timeout = 25;
  476. numorder = 0;
  477. }
  478. if (!numorder) {
  479. idx = 1;
  480. AST_LIST_TRAVERSE(&f->numbers, nm, entry)
  481. idx++;
  482. numorder = idx;
  483. }
  484. cur = create_followme_number(numberstr, timeout, numorder);
  485. if (cur) {
  486. AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
  487. }
  488. } else {
  489. profile_set_param(f, var->name, var->value, var->lineno, 1);
  490. ast_debug(2, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
  491. }
  492. var = var->next;
  493. } /* End while(var) loop */
  494. if (!new)
  495. ast_mutex_unlock(&f->lock);
  496. else
  497. AST_RWLIST_INSERT_HEAD(&followmes, f, entry);
  498. }
  499. ast_config_destroy(cfg);
  500. AST_RWLIST_UNLOCK(&followmes);
  501. return 1;
  502. }
  503. static void publish_dial_end_event(struct ast_channel *in, struct findme_user_listptr *findme_user_list, struct ast_channel *exception, const char *status)
  504. {
  505. struct findme_user *tmpuser;
  506. AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
  507. if (tmpuser->ochan && tmpuser->ochan != exception) {
  508. ast_channel_publish_dial(in, tmpuser->ochan, NULL, status);
  509. }
  510. }
  511. }
  512. static void clear_caller(struct findme_user *tmpuser)
  513. {
  514. struct ast_channel *outbound;
  515. if (!tmpuser->ochan) {
  516. /* Call already cleared. */
  517. return;
  518. }
  519. outbound = tmpuser->ochan;
  520. ast_hangup(outbound);
  521. tmpuser->ochan = NULL;
  522. }
  523. static void clear_unanswered_calls(struct findme_user_listptr *findme_user_list)
  524. {
  525. struct findme_user *tmpuser;
  526. AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
  527. if (!tmpuser->answered) {
  528. clear_caller(tmpuser);
  529. }
  530. }
  531. }
  532. static void destroy_calling_node(struct findme_user *node)
  533. {
  534. clear_caller(node);
  535. ast_party_connected_line_free(&node->connected);
  536. ast_free(node);
  537. }
  538. static void destroy_calling_tree(struct findme_user_listptr *findme_user_list)
  539. {
  540. struct findme_user *fmuser;
  541. while ((fmuser = AST_LIST_REMOVE_HEAD(findme_user_list, entry))) {
  542. destroy_calling_node(fmuser);
  543. }
  544. }
  545. static struct ast_channel *wait_for_winner(struct findme_user_listptr *findme_user_list, struct number *nm, struct ast_channel *caller, struct fm_args *tpargs)
  546. {
  547. struct ast_party_connected_line connected;
  548. struct ast_channel *watchers[256];
  549. int pos;
  550. struct ast_channel *winner;
  551. struct ast_frame *f;
  552. struct findme_user *tmpuser;
  553. int to = 0;
  554. int livechannels;
  555. int tmpto;
  556. long totalwait = 0, wtd = 0, towas = 0;
  557. char *callfromname;
  558. char *pressbuttonname;
  559. /* ------------ wait_for_winner_channel start --------------- */
  560. callfromname = ast_strdupa(tpargs->callfromprompt);
  561. pressbuttonname = ast_strdupa(tpargs->optionsprompt);
  562. totalwait = nm->timeout * 1000;
  563. for (;;) {
  564. to = 1000;
  565. pos = 1;
  566. livechannels = 0;
  567. watchers[0] = caller;
  568. winner = NULL;
  569. AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
  570. if (!tmpuser->ochan) {
  571. continue;
  572. }
  573. if (tmpuser->state == 3) {
  574. tmpuser->digts += (towas - wtd);
  575. }
  576. if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
  577. ast_verb(3, "<%s> We've been waiting for digits longer than we should have.\n",
  578. ast_channel_name(tmpuser->ochan));
  579. if (!ast_strlen_zero(tpargs->namerecloc)) {
  580. tmpuser->state = 1;
  581. tmpuser->digts = 0;
  582. if (!ast_streamfile(tmpuser->ochan, callfromname, ast_channel_language(tmpuser->ochan))) {
  583. ast_sched_runq(ast_channel_sched(tmpuser->ochan));
  584. } else {
  585. ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
  586. clear_caller(tmpuser);
  587. continue;
  588. }
  589. } else {
  590. tmpuser->state = 2;
  591. tmpuser->digts = 0;
  592. if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, ast_channel_language(tmpuser->ochan)))
  593. ast_sched_runq(ast_channel_sched(tmpuser->ochan));
  594. else {
  595. ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
  596. clear_caller(tmpuser);
  597. continue;
  598. }
  599. }
  600. }
  601. if (ast_channel_stream(tmpuser->ochan)) {
  602. ast_sched_runq(ast_channel_sched(tmpuser->ochan));
  603. tmpto = ast_sched_wait(ast_channel_sched(tmpuser->ochan));
  604. if (tmpto > 0 && tmpto < to)
  605. to = tmpto;
  606. else if (tmpto < 0 && !ast_channel_timingfunc(tmpuser->ochan)) {
  607. ast_stopstream(tmpuser->ochan);
  608. switch (tmpuser->state) {
  609. case 1:
  610. ast_verb(3, "<%s> Playback of the call-from file appears to be done.\n",
  611. ast_channel_name(tmpuser->ochan));
  612. if (!ast_streamfile(tmpuser->ochan, tpargs->namerecloc, ast_channel_language(tmpuser->ochan))) {
  613. tmpuser->state = 2;
  614. } else {
  615. ast_log(LOG_NOTICE, "<%s> Unable to playback %s. Maybe the caller didn't record their name?\n",
  616. ast_channel_name(tmpuser->ochan), tpargs->namerecloc);
  617. memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
  618. tmpuser->ynidx = 0;
  619. if (!ast_streamfile(tmpuser->ochan, pressbuttonname, ast_channel_language(tmpuser->ochan)))
  620. tmpuser->state = 3;
  621. else {
  622. ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
  623. clear_caller(tmpuser);
  624. continue;
  625. }
  626. }
  627. break;
  628. case 2:
  629. ast_verb(3, "<%s> Playback of name file appears to be done.\n",
  630. ast_channel_name(tmpuser->ochan));
  631. memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
  632. tmpuser->ynidx = 0;
  633. if (!ast_streamfile(tmpuser->ochan, pressbuttonname, ast_channel_language(tmpuser->ochan))) {
  634. tmpuser->state = 3;
  635. } else {
  636. clear_caller(tmpuser);
  637. continue;
  638. }
  639. break;
  640. case 3:
  641. ast_verb(3, "<%s> Playback of the next step file appears to be done.\n",
  642. ast_channel_name(tmpuser->ochan));
  643. tmpuser->digts = 0;
  644. break;
  645. default:
  646. break;
  647. }
  648. }
  649. }
  650. watchers[pos++] = tmpuser->ochan;
  651. livechannels++;
  652. }
  653. if (!livechannels) {
  654. ast_verb(3, "No live channels left for this step.\n");
  655. return NULL;
  656. }
  657. tmpto = to;
  658. if (to < 0) {
  659. to = 1000;
  660. tmpto = 1000;
  661. }
  662. towas = to;
  663. winner = ast_waitfor_n(watchers, pos, &to);
  664. tmpto -= to;
  665. totalwait -= tmpto;
  666. wtd = to;
  667. if (totalwait <= 0) {
  668. ast_verb(3, "We've hit our timeout for this step. Dropping unanswered calls and starting the next step.\n");
  669. clear_unanswered_calls(findme_user_list);
  670. return NULL;
  671. }
  672. if (winner) {
  673. /* Need to find out which channel this is */
  674. if (winner != caller) {
  675. /* The winner is an outgoing channel. */
  676. AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
  677. if (tmpuser->ochan == winner) {
  678. break;
  679. }
  680. }
  681. } else {
  682. tmpuser = NULL;
  683. }
  684. f = ast_read(winner);
  685. if (f) {
  686. if (f->frametype == AST_FRAME_CONTROL) {
  687. switch (f->subclass.integer) {
  688. case AST_CONTROL_HANGUP:
  689. ast_verb(3, "%s received a hangup frame.\n", ast_channel_name(winner));
  690. if (f->data.uint32) {
  691. ast_channel_hangupcause_set(winner, f->data.uint32);
  692. }
  693. if (!tmpuser) {
  694. ast_verb(3, "The calling channel hungup. Need to drop everyone.\n");
  695. publish_dial_end_event(caller, findme_user_list, NULL, "CANCEL");
  696. ast_frfree(f);
  697. return NULL;
  698. }
  699. clear_caller(tmpuser);
  700. break;
  701. case AST_CONTROL_ANSWER:
  702. if (!tmpuser) {
  703. /* The caller answered? We want an outgoing channel to answer. */
  704. break;
  705. }
  706. ast_verb(3, "%s answered %s\n", ast_channel_name(winner), ast_channel_name(caller));
  707. ast_channel_publish_dial(caller, winner, NULL, "ANSWER");
  708. publish_dial_end_event(caller, findme_user_list, winner, "CANCEL");
  709. tmpuser->answered = 1;
  710. /* If call has been answered, then the eventual hangup is likely to be normal hangup */
  711. ast_channel_hangupcause_set(winner, AST_CAUSE_NORMAL_CLEARING);
  712. ast_channel_hangupcause_set(caller, AST_CAUSE_NORMAL_CLEARING);
  713. ast_verb(3, "Starting playback of %s\n", callfromname);
  714. if (!ast_strlen_zero(tpargs->namerecloc)) {
  715. if (!ast_streamfile(winner, callfromname, ast_channel_language(winner))) {
  716. ast_sched_runq(ast_channel_sched(winner));
  717. tmpuser->state = 1;
  718. } else {
  719. ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
  720. clear_caller(tmpuser);
  721. }
  722. } else {
  723. tmpuser->state = 2;
  724. if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, ast_channel_language(tmpuser->ochan)))
  725. ast_sched_runq(ast_channel_sched(tmpuser->ochan));
  726. else {
  727. ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
  728. clear_caller(tmpuser);
  729. }
  730. }
  731. break;
  732. case AST_CONTROL_BUSY:
  733. ast_verb(3, "%s is busy\n", ast_channel_name(winner));
  734. if (tmpuser) {
  735. /* Outbound call was busy. Drop it. */
  736. ast_channel_publish_dial(caller, winner, NULL, "BUSY");
  737. clear_caller(tmpuser);
  738. }
  739. break;
  740. case AST_CONTROL_CONGESTION:
  741. ast_verb(3, "%s is circuit-busy\n", ast_channel_name(winner));
  742. if (tmpuser) {
  743. /* Outbound call was congested. Drop it. */
  744. ast_channel_publish_dial(caller, winner, NULL, "CONGESTION");
  745. clear_caller(tmpuser);
  746. }
  747. break;
  748. case AST_CONTROL_RINGING:
  749. ast_verb(3, "%s is ringing\n", ast_channel_name(winner));
  750. break;
  751. case AST_CONTROL_PROGRESS:
  752. ast_verb(3, "%s is making progress\n", ast_channel_name(winner));
  753. break;
  754. case AST_CONTROL_VIDUPDATE:
  755. ast_verb(3, "%s requested a video update\n", ast_channel_name(winner));
  756. break;
  757. case AST_CONTROL_SRCUPDATE:
  758. ast_verb(3, "%s requested a source update\n", ast_channel_name(winner));
  759. break;
  760. case AST_CONTROL_PROCEEDING:
  761. ast_verb(3, "%s is proceeding\n", ast_channel_name(winner));
  762. break;
  763. case AST_CONTROL_HOLD:
  764. ast_verb(3, "%s placed call on hold\n", ast_channel_name(winner));
  765. if (!tmpuser) {
  766. /* Caller placed outgoing calls on hold. */
  767. tpargs->pending_hold = 1;
  768. if (f->data.ptr) {
  769. ast_copy_string(tpargs->suggested_moh, f->data.ptr,
  770. sizeof(tpargs->suggested_moh));
  771. } else {
  772. tpargs->suggested_moh[0] = '\0';
  773. }
  774. } else {
  775. /*
  776. * Outgoing call placed caller on hold.
  777. *
  778. * Ignore because the outgoing call should not be able to place
  779. * the caller on hold until after they are bridged.
  780. */
  781. }
  782. break;
  783. case AST_CONTROL_UNHOLD:
  784. ast_verb(3, "%s removed call from hold\n", ast_channel_name(winner));
  785. if (!tmpuser) {
  786. /* Caller removed outgoing calls from hold. */
  787. tpargs->pending_hold = 0;
  788. } else {
  789. /*
  790. * Outgoing call removed caller from hold.
  791. *
  792. * Ignore because the outgoing call should not be able to place
  793. * the caller on hold until after they are bridged.
  794. */
  795. }
  796. break;
  797. case AST_CONTROL_OFFHOOK:
  798. case AST_CONTROL_FLASH:
  799. /* Ignore going off hook and flash */
  800. break;
  801. case AST_CONTROL_CONNECTED_LINE:
  802. if (ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_IGNORE_CONNECTEDLINE)) {
  803. ast_verb(3, "Connected line update from %s prevented.\n",
  804. ast_channel_name(winner));
  805. break;
  806. }
  807. if (!tmpuser) {
  808. /*
  809. * Hold connected line update from caller until we have a
  810. * winner.
  811. */
  812. ast_verb(3,
  813. "%s connected line has changed. Saving it until we have a winner.\n",
  814. ast_channel_name(winner));
  815. ast_party_connected_line_set_init(&connected, &tpargs->connected_in);
  816. if (!ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected)) {
  817. ast_party_connected_line_set(&tpargs->connected_in,
  818. &connected, NULL);
  819. tpargs->pending_in_connected_update = 1;
  820. }
  821. ast_party_connected_line_free(&connected);
  822. } else {
  823. ast_verb(3,
  824. "%s connected line has changed. Saving it until answer.\n",
  825. ast_channel_name(winner));
  826. ast_party_connected_line_set_init(&connected, &tmpuser->connected);
  827. if (!ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected)) {
  828. ast_party_connected_line_set(&tmpuser->connected,
  829. &connected, NULL);
  830. tmpuser->pending_connected_update = 1;
  831. }
  832. ast_party_connected_line_free(&connected);
  833. }
  834. break;
  835. case AST_CONTROL_REDIRECTING:
  836. /*
  837. * Ignore because we are masking the FollowMe search progress to
  838. * the caller.
  839. */
  840. break;
  841. case AST_CONTROL_PVT_CAUSE_CODE:
  842. ast_indicate_data(caller, f->subclass.integer, f->data.ptr, f->datalen);
  843. break;
  844. case -1:
  845. ast_verb(3, "%s stopped sounds\n", ast_channel_name(winner));
  846. break;
  847. default:
  848. ast_debug(1, "Dunno what to do with control type %d from %s\n",
  849. f->subclass.integer, ast_channel_name(winner));
  850. break;
  851. }
  852. }
  853. if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
  854. int cmp_len;
  855. if (ast_channel_stream(winner))
  856. ast_stopstream(winner);
  857. tmpuser->digts = 0;
  858. ast_debug(1, "DTMF received: %c\n", (char) f->subclass.integer);
  859. if (tmpuser->ynidx < ARRAY_LEN(tmpuser->yn) - 1) {
  860. tmpuser->yn[tmpuser->ynidx++] = f->subclass.integer;
  861. } else {
  862. /* Discard oldest digit. */
  863. memmove(tmpuser->yn, tmpuser->yn + 1,
  864. sizeof(tmpuser->yn) - 2 * sizeof(tmpuser->yn[0]));
  865. tmpuser->yn[ARRAY_LEN(tmpuser->yn) - 2] = f->subclass.integer;
  866. }
  867. ast_debug(1, "DTMF string: %s\n", tmpuser->yn);
  868. cmp_len = strlen(tpargs->takecall);
  869. if (cmp_len <= tmpuser->ynidx
  870. && !strcmp(tmpuser->yn + (tmpuser->ynidx - cmp_len), tpargs->takecall)) {
  871. ast_debug(1, "Match to take the call!\n");
  872. ast_frfree(f);
  873. return tmpuser->ochan;
  874. }
  875. cmp_len = strlen(tpargs->nextindp);
  876. if (cmp_len <= tmpuser->ynidx
  877. && !strcmp(tmpuser->yn + (tmpuser->ynidx - cmp_len), tpargs->nextindp)) {
  878. ast_debug(1, "Declined to take the call.\n");
  879. clear_caller(tmpuser);
  880. }
  881. }
  882. ast_frfree(f);
  883. } else {
  884. ast_debug(1, "we didn't get a frame. hanging up.\n");
  885. if (!tmpuser) {
  886. /* Caller hung up. */
  887. ast_verb(3, "The calling channel hungup. Need to drop everyone.\n");
  888. return NULL;
  889. }
  890. /* Outgoing channel hung up. */
  891. ast_channel_publish_dial(caller, winner, NULL, "NOANSWER");
  892. clear_caller(tmpuser);
  893. }
  894. } else {
  895. ast_debug(1, "timed out waiting for action\n");
  896. }
  897. }
  898. /* Unreachable. */
  899. }
  900. /*!
  901. * \internal
  902. * \brief Find an extension willing to take the call.
  903. *
  904. * \param tpargs Active Followme config.
  905. * \param caller Channel initiating the outgoing calls.
  906. *
  907. * \retval winner Winning outgoing call.
  908. * \retval NULL if could not find someone to take the call.
  909. */
  910. static struct ast_channel *findmeexec(struct fm_args *tpargs, struct ast_channel *caller)
  911. {
  912. struct number *nm;
  913. struct ast_channel *winner = NULL;
  914. char num[512];
  915. int dg, idx;
  916. char *rest, *number;
  917. struct findme_user *tmpuser;
  918. struct findme_user *fmuser;
  919. struct findme_user_listptr findme_user_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  920. struct findme_user_listptr new_user_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  921. for (idx = 1; !ast_check_hangup(caller); ++idx) {
  922. /* Find next followme numbers to dial. */
  923. AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry) {
  924. if (nm->order == idx) {
  925. break;
  926. }
  927. }
  928. if (!nm) {
  929. ast_verb(3, "No more steps left.\n");
  930. break;
  931. }
  932. ast_debug(2, "Number(s) %s timeout %ld\n", nm->number, nm->timeout);
  933. /*
  934. * Put all active outgoing channels into autoservice.
  935. *
  936. * This needs to be done because ast_exists_extension() may put
  937. * the caller into autoservice.
  938. */
  939. AST_LIST_TRAVERSE(&findme_user_list, tmpuser, entry) {
  940. if (tmpuser->ochan) {
  941. ast_autoservice_start(tmpuser->ochan);
  942. }
  943. }
  944. /* Create all new outgoing calls */
  945. ast_copy_string(num, nm->number, sizeof(num));
  946. for (number = num; number; number = rest) {
  947. struct ast_channel *outbound;
  948. rest = strchr(number, '&');
  949. if (rest) {
  950. *rest++ = 0;
  951. }
  952. /* We check if the extension exists, before creating the ast_channel struct */
  953. if (!ast_exists_extension(caller, tpargs->context, number, 1, S_COR(ast_channel_caller(caller)->id.number.valid, ast_channel_caller(caller)->id.number.str, NULL))) {
  954. ast_log(LOG_ERROR, "Extension '%s@%s' doesn't exist\n", number, tpargs->context);
  955. continue;
  956. }
  957. tmpuser = ast_calloc(1, sizeof(*tmpuser));
  958. if (!tmpuser) {
  959. continue;
  960. }
  961. if (ast_strlen_zero(tpargs->context)) {
  962. snprintf(tmpuser->dialarg, sizeof(tmpuser->dialarg), "%s%s",
  963. number,
  964. ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_DISABLEOPTIMIZATION)
  965. ? "/n" : "/m");
  966. } else {
  967. snprintf(tmpuser->dialarg, sizeof(tmpuser->dialarg), "%s@%s%s",
  968. number, tpargs->context,
  969. ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_DISABLEOPTIMIZATION)
  970. ? "/n" : "/m");
  971. }
  972. outbound = ast_request("Local", ast_channel_nativeformats(caller), NULL, caller,
  973. tmpuser->dialarg, &dg);
  974. if (!outbound) {
  975. ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n",
  976. tmpuser->dialarg, ast_cause2str(dg));
  977. ast_free(tmpuser);
  978. continue;
  979. }
  980. ast_channel_lock_both(caller, outbound);
  981. ast_connected_line_copy_from_caller(ast_channel_connected(outbound), ast_channel_caller(caller));
  982. ast_channel_inherit_variables(caller, outbound);
  983. ast_channel_datastore_inherit(caller, outbound);
  984. ast_max_forwards_decrement(outbound);
  985. ast_channel_language_set(outbound, ast_channel_language(caller));
  986. ast_channel_req_accountcodes(outbound, caller, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
  987. ast_channel_musicclass_set(outbound, ast_channel_musicclass(caller));
  988. ast_channel_unlock(outbound);
  989. ast_channel_unlock(caller);
  990. tmpuser->ochan = outbound;
  991. tmpuser->state = 0;
  992. AST_LIST_INSERT_TAIL(&new_user_list, tmpuser, entry);
  993. }
  994. /*
  995. * PREDIAL: Run gosub on all of the new callee channels
  996. *
  997. * We run the callee predial before ast_call() in case the user
  998. * wishes to do something on the newly created channels before
  999. * the channel does anything important.
  1000. */
  1001. if (tpargs->predial_callee && !AST_LIST_EMPTY(&new_user_list)) {
  1002. /* Put caller into autoservice. */
  1003. ast_autoservice_start(caller);
  1004. /* Run predial on all new outgoing calls. */
  1005. AST_LIST_TRAVERSE(&new_user_list, tmpuser, entry) {
  1006. ast_pre_call(tmpuser->ochan, tpargs->predial_callee);
  1007. }
  1008. /* Take caller out of autoservice. */
  1009. if (ast_autoservice_stop(caller)) {
  1010. /*
  1011. * Caller hungup.
  1012. *
  1013. * Destoy all new outgoing calls.
  1014. */
  1015. while ((tmpuser = AST_LIST_REMOVE_HEAD(&new_user_list, entry))) {
  1016. destroy_calling_node(tmpuser);
  1017. }
  1018. /* Take all active outgoing channels out of autoservice. */
  1019. AST_LIST_TRAVERSE(&findme_user_list, tmpuser, entry) {
  1020. if (tmpuser->ochan) {
  1021. ast_autoservice_stop(tmpuser->ochan);
  1022. }
  1023. }
  1024. break;
  1025. }
  1026. }
  1027. /* Start all new outgoing calls */
  1028. AST_LIST_TRAVERSE_SAFE_BEGIN(&new_user_list, tmpuser, entry) {
  1029. ast_verb(3, "calling Local/%s\n", tmpuser->dialarg);
  1030. if (ast_call(tmpuser->ochan, tmpuser->dialarg, 0)) {
  1031. ast_verb(3, "couldn't reach at this number.\n");
  1032. AST_LIST_REMOVE_CURRENT(entry);
  1033. /* Destroy this failed new outgoing call. */
  1034. destroy_calling_node(tmpuser);
  1035. continue;
  1036. }
  1037. ast_channel_publish_dial(caller, tmpuser->ochan, tmpuser->dialarg, NULL);
  1038. }
  1039. AST_LIST_TRAVERSE_SAFE_END;
  1040. /* Take all active outgoing channels out of autoservice. */
  1041. AST_LIST_TRAVERSE_SAFE_BEGIN(&findme_user_list, tmpuser, entry) {
  1042. if (tmpuser->ochan && ast_autoservice_stop(tmpuser->ochan)) {
  1043. /* Existing outgoing call hungup. */
  1044. AST_LIST_REMOVE_CURRENT(entry);
  1045. destroy_calling_node(tmpuser);
  1046. }
  1047. }
  1048. AST_LIST_TRAVERSE_SAFE_END;
  1049. if (AST_LIST_EMPTY(&new_user_list)) {
  1050. /* No new channels remain at this order level. If there were any at all. */
  1051. continue;
  1052. }
  1053. /* Add new outgoing channels to the findme list. */
  1054. AST_LIST_APPEND_LIST(&findme_user_list, &new_user_list, entry);
  1055. winner = wait_for_winner(&findme_user_list, nm, caller, tpargs);
  1056. if (!winner) {
  1057. /* Remove all dead outgoing nodes. */
  1058. AST_LIST_TRAVERSE_SAFE_BEGIN(&findme_user_list, tmpuser, entry) {
  1059. if (!tmpuser->ochan) {
  1060. AST_LIST_REMOVE_CURRENT(entry);
  1061. destroy_calling_node(tmpuser);
  1062. }
  1063. }
  1064. AST_LIST_TRAVERSE_SAFE_END;
  1065. continue;
  1066. }
  1067. /* Destroy losing calls up to the winner. The rest will be destroyed later. */
  1068. while ((fmuser = AST_LIST_REMOVE_HEAD(&findme_user_list, entry))) {
  1069. if (fmuser->ochan == winner) {
  1070. /*
  1071. * Pass any connected line info up.
  1072. *
  1073. * NOTE: This code must be in line with destroy_calling_node().
  1074. */
  1075. tpargs->connected_out = fmuser->connected;
  1076. tpargs->pending_out_connected_update = fmuser->pending_connected_update;
  1077. ast_free(fmuser);
  1078. break;
  1079. } else {
  1080. /* Destroy losing call. */
  1081. destroy_calling_node(fmuser);
  1082. }
  1083. }
  1084. break;
  1085. }
  1086. destroy_calling_tree(&findme_user_list);
  1087. return winner;
  1088. }
  1089. static struct call_followme *find_realtime(const char *name)
  1090. {
  1091. struct ast_variable *var;
  1092. struct ast_variable *v;
  1093. struct ast_config *cfg;
  1094. const char *catg;
  1095. struct call_followme *new_follower;
  1096. struct ast_str *str;
  1097. str = ast_str_create(16);
  1098. if (!str) {
  1099. return NULL;
  1100. }
  1101. var = ast_load_realtime("followme", "name", name, SENTINEL);
  1102. if (!var) {
  1103. ast_free(str);
  1104. return NULL;
  1105. }
  1106. if (!(new_follower = alloc_profile(name))) {
  1107. ast_variables_destroy(var);
  1108. ast_free(str);
  1109. return NULL;
  1110. }
  1111. init_profile(new_follower, 0);
  1112. for (v = var; v; v = v->next) {
  1113. if (!strcasecmp(v->name, "active")) {
  1114. if (ast_false(v->value)) {
  1115. ast_mutex_destroy(&new_follower->lock);
  1116. ast_free(new_follower);
  1117. ast_variables_destroy(var);
  1118. ast_free(str);
  1119. return NULL;
  1120. }
  1121. } else {
  1122. profile_set_param(new_follower, v->name, v->value, 0, 0);
  1123. }
  1124. }
  1125. ast_variables_destroy(var);
  1126. new_follower->realtime = 1;
  1127. /* Load numbers */
  1128. cfg = ast_load_realtime_multientry("followme_numbers", "ordinal LIKE", "%", "name",
  1129. name, SENTINEL);
  1130. if (!cfg) {
  1131. ast_mutex_destroy(&new_follower->lock);
  1132. ast_free(new_follower);
  1133. ast_free(str);
  1134. return NULL;
  1135. }
  1136. for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
  1137. const char *numstr;
  1138. const char *timeoutstr;
  1139. const char *ordstr;
  1140. int timeout;
  1141. struct number *cur;
  1142. if (!(numstr = ast_variable_retrieve(cfg, catg, "phonenumber"))) {
  1143. continue;
  1144. }
  1145. if (!(timeoutstr = ast_variable_retrieve(cfg, catg, "timeout"))
  1146. || sscanf(timeoutstr, "%30d", &timeout) != 1
  1147. || timeout < 1) {
  1148. timeout = 25;
  1149. }
  1150. /* This one has to exist; it was part of the query */
  1151. ordstr = ast_variable_retrieve(cfg, catg, "ordinal");
  1152. ast_str_set(&str, 0, "%s", numstr);
  1153. if ((cur = create_followme_number(ast_str_buffer(str), timeout, atoi(ordstr)))) {
  1154. AST_LIST_INSERT_TAIL(&new_follower->numbers, cur, entry);
  1155. }
  1156. }
  1157. ast_config_destroy(cfg);
  1158. ast_free(str);
  1159. return new_follower;
  1160. }
  1161. static void end_bridge_callback(void *data)
  1162. {
  1163. char buf[80];
  1164. time_t end;
  1165. struct ast_channel *chan = data;
  1166. time(&end);
  1167. ast_channel_lock(chan);
  1168. snprintf(buf, sizeof(buf), "%d", ast_channel_get_up_time(chan));
  1169. pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
  1170. snprintf(buf, sizeof(buf), "%d", ast_channel_get_duration(chan));
  1171. pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
  1172. ast_channel_unlock(chan);
  1173. }
  1174. static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator)
  1175. {
  1176. bconfig->end_bridge_callback_data = originator;
  1177. }
  1178. static int app_exec(struct ast_channel *chan, const char *data)
  1179. {
  1180. struct fm_args *targs;
  1181. struct ast_bridge_config config;
  1182. struct call_followme *f;
  1183. struct number *nm, *newnm;
  1184. int res = 0;
  1185. char *argstr;
  1186. struct ast_channel *caller;
  1187. struct ast_channel *outbound;
  1188. AST_DECLARE_APP_ARGS(args,
  1189. AST_APP_ARG(followmeid);
  1190. AST_APP_ARG(options);
  1191. );
  1192. char *opt_args[FOLLOWMEFLAG_ARG_ARRAY_SIZE];
  1193. int max_forwards;
  1194. if (ast_strlen_zero(data)) {
  1195. ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
  1196. return -1;
  1197. }
  1198. ast_channel_lock(chan);
  1199. max_forwards = ast_max_forwards_get(chan);
  1200. ast_channel_unlock(chan);
  1201. if (max_forwards <= 0) {
  1202. ast_log(LOG_WARNING, "Unable to execute FollowMe on channel %s. Max forwards exceeded\n",
  1203. ast_channel_name(chan));
  1204. return -1;
  1205. }
  1206. argstr = ast_strdupa((char *) data);
  1207. AST_STANDARD_APP_ARGS(args, argstr);
  1208. if (ast_strlen_zero(args.followmeid)) {
  1209. ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
  1210. return -1;
  1211. }
  1212. targs = ast_calloc(1, sizeof(*targs));
  1213. if (!targs) {
  1214. return -1;
  1215. }
  1216. AST_RWLIST_RDLOCK(&followmes);
  1217. AST_RWLIST_TRAVERSE(&followmes, f, entry) {
  1218. if (!strcasecmp(f->name, args.followmeid) && (f->active))
  1219. break;
  1220. }
  1221. AST_RWLIST_UNLOCK(&followmes);
  1222. ast_debug(1, "New profile %s.\n", args.followmeid);
  1223. if (!f) {
  1224. f = find_realtime(args.followmeid);
  1225. }
  1226. if (!f) {
  1227. ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
  1228. ast_free(targs);
  1229. return 0;
  1230. }
  1231. /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
  1232. if (args.options) {
  1233. ast_app_parse_options(followme_opts, &targs->followmeflags, opt_args, args.options);
  1234. }
  1235. /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
  1236. ast_mutex_lock(&f->lock);
  1237. targs->mohclass = ast_strdupa(f->moh);
  1238. ast_copy_string(targs->context, f->context, sizeof(targs->context));
  1239. ast_copy_string(targs->takecall, f->takecall, sizeof(targs->takecall));
  1240. ast_copy_string(targs->nextindp, f->nextindp, sizeof(targs->nextindp));
  1241. ast_copy_string(targs->callfromprompt, f->callfromprompt, sizeof(targs->callfromprompt));
  1242. ast_copy_string(targs->norecordingprompt, f->norecordingprompt, sizeof(targs->norecordingprompt));
  1243. ast_copy_string(targs->optionsprompt, f->optionsprompt, sizeof(targs->optionsprompt));
  1244. ast_copy_string(targs->plsholdprompt, f->plsholdprompt, sizeof(targs->plsholdprompt));
  1245. ast_copy_string(targs->statusprompt, f->statusprompt, sizeof(targs->statusprompt));
  1246. ast_copy_string(targs->sorryprompt, f->sorryprompt, sizeof(targs->sorryprompt));
  1247. ast_copy_string(targs->connprompt, f->connprompt, sizeof(targs->connprompt));
  1248. /* Copy the numbers we're going to use into another list in case the master list should get modified
  1249. (and locked) while we're trying to do a follow-me */
  1250. AST_LIST_HEAD_INIT_NOLOCK(&targs->cnumbers);
  1251. AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
  1252. newnm = create_followme_number(nm->number, nm->timeout, nm->order);
  1253. if (newnm) {
  1254. AST_LIST_INSERT_TAIL(&targs->cnumbers, newnm, entry);
  1255. }
  1256. }
  1257. ast_mutex_unlock(&f->lock);
  1258. /* PREDIAL: Preprocess any callee gosub arguments. */
  1259. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_PREDIAL_CALLEE)
  1260. && !ast_strlen_zero(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLEE])) {
  1261. ast_replace_subargument_delimiter(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLEE]);
  1262. targs->predial_callee =
  1263. ast_app_expand_sub_args(chan, opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLEE]);
  1264. }
  1265. /* PREDIAL: Run gosub on the caller's channel */
  1266. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_PREDIAL_CALLER)
  1267. && !ast_strlen_zero(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLER])) {
  1268. ast_replace_subargument_delimiter(opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLER]);
  1269. ast_app_exec_sub(NULL, chan, opt_args[FOLLOWMEFLAG_ARG_PREDIAL_CALLER], 0);
  1270. }
  1271. /* Forget the 'N' option if the call is already up. */
  1272. if (ast_channel_state(chan) == AST_STATE_UP) {
  1273. ast_clear_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER);
  1274. }
  1275. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
  1276. ast_indicate(chan, AST_CONTROL_RINGING);
  1277. } else {
  1278. /* Answer the call */
  1279. if (ast_channel_state(chan) != AST_STATE_UP) {
  1280. ast_answer(chan);
  1281. }
  1282. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_STATUSMSG)) {
  1283. ast_stream_and_wait(chan, targs->statusprompt, "");
  1284. }
  1285. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_RECORDNAME)) {
  1286. int duration = 5;
  1287. snprintf(targs->namerecloc, sizeof(targs->namerecloc), "%s/followme.%s",
  1288. ast_config_AST_SPOOL_DIR, ast_channel_uniqueid(chan));
  1289. if (ast_play_and_record(chan, "vm-rec-name", targs->namerecloc, 5, REC_FORMAT, &duration,
  1290. NULL, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE), 0, NULL) < 0) {
  1291. goto outrun;
  1292. }
  1293. if (!ast_fileexists(targs->namerecloc, NULL, ast_channel_language(chan))) {
  1294. targs->namerecloc[0] = '\0';
  1295. }
  1296. }
  1297. if (!ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_DISABLEHOLDPROMPT)) {
  1298. if (ast_streamfile(chan, targs->plsholdprompt, ast_channel_language(chan))) {
  1299. goto outrun;
  1300. }
  1301. if (ast_waitstream(chan, "") < 0)
  1302. goto outrun;
  1303. }
  1304. ast_moh_start(chan, targs->mohclass, NULL);
  1305. }
  1306. ast_channel_lock(chan);
  1307. ast_connected_line_copy_from_caller(&targs->connected_in, ast_channel_caller(chan));
  1308. ast_channel_unlock(chan);
  1309. outbound = findmeexec(targs, chan);
  1310. if (!outbound) {
  1311. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
  1312. if (ast_channel_state(chan) != AST_STATE_UP) {
  1313. ast_answer(chan);
  1314. }
  1315. } else {
  1316. ast_moh_stop(chan);
  1317. }
  1318. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG)) {
  1319. ast_stream_and_wait(chan, targs->sorryprompt, "");
  1320. }
  1321. res = 0;
  1322. } else {
  1323. caller = chan;
  1324. /* Play "connecting" message to the winner, if configured. */
  1325. if (!ast_strlen_zero(targs->connprompt)) {
  1326. ast_autoservice_start(caller);
  1327. ast_stream_and_wait(outbound, targs->connprompt, "");
  1328. ast_autoservice_stop(caller);
  1329. }
  1330. /* Bridge the two channels. */
  1331. memset(&config, 0, sizeof(config));
  1332. ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
  1333. ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
  1334. ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
  1335. config.end_bridge_callback = end_bridge_callback;
  1336. config.end_bridge_callback_data = chan;
  1337. config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
  1338. /* Update connected line to caller if available. */
  1339. if (targs->pending_out_connected_update) {
  1340. if (ast_channel_connected_line_sub(outbound, caller, &targs->connected_out, 0) &&
  1341. ast_channel_connected_line_macro(outbound, caller, &targs->connected_out, 1, 0)) {
  1342. ast_channel_update_connected_line(caller, &targs->connected_out, NULL);
  1343. }
  1344. }
  1345. if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
  1346. if (ast_channel_state(caller) != AST_STATE_UP) {
  1347. ast_answer(caller);
  1348. }
  1349. } else {
  1350. ast_moh_stop(caller);
  1351. }
  1352. /* Be sure no generators are left on it */
  1353. ast_deactivate_generator(caller);
  1354. /* Make sure channels are compatible */
  1355. res = ast_channel_make_compatible(caller, outbound);
  1356. if (res < 0) {
  1357. ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", ast_channel_name(caller), ast_channel_name(outbound));
  1358. ast_autoservice_chan_hangup_peer(caller, outbound);
  1359. goto outrun;
  1360. }
  1361. /* Update connected line to winner if changed. */
  1362. if (targs->pending_in_connected_update) {
  1363. if (ast_channel_connected_line_sub(caller, outbound, &targs->connected_in, 0) &&
  1364. ast_channel_connected_line_macro(caller, outbound, &targs->connected_in, 0, 0)) {
  1365. ast_channel_update_connected_line(outbound, &targs->connected_in, NULL);
  1366. }
  1367. }
  1368. /* Put winner on hold if caller requested. */
  1369. if (targs->pending_hold) {
  1370. if (ast_strlen_zero(targs->suggested_moh)) {
  1371. ast_indicate_data(outbound, AST_CONTROL_HOLD, NULL, 0);
  1372. } else {
  1373. ast_indicate_data(outbound, AST_CONTROL_HOLD,
  1374. targs->suggested_moh, strlen(targs->suggested_moh) + 1);
  1375. }
  1376. }
  1377. res = ast_bridge_call(caller, outbound, &config);
  1378. }
  1379. outrun:
  1380. while ((nm = AST_LIST_REMOVE_HEAD(&targs->cnumbers, entry))) {
  1381. ast_free(nm);
  1382. }
  1383. if (!ast_strlen_zero(targs->namerecloc)) {
  1384. int ret;
  1385. char fn[PATH_MAX + sizeof(REC_FORMAT)];
  1386. snprintf(fn, sizeof(fn), "%s.%s", targs->namerecloc,
  1387. REC_FORMAT);
  1388. ret = unlink(fn);
  1389. if (ret != 0) {
  1390. ast_log(LOG_NOTICE, "Failed to delete recorded name file %s: %d (%s)\n",
  1391. fn, errno, strerror(errno));
  1392. } else {
  1393. ast_debug(2, "deleted recorded prompt %s.\n", fn);
  1394. }
  1395. }
  1396. ast_free((char *) targs->predial_callee);
  1397. ast_party_connected_line_free(&targs->connected_in);
  1398. ast_party_connected_line_free(&targs->connected_out);
  1399. ast_free(targs);
  1400. if (f->realtime) {
  1401. /* Not in list */
  1402. free_numbers(f);
  1403. ast_free(f);
  1404. }
  1405. return res;
  1406. }
  1407. static int unload_module(void)
  1408. {
  1409. struct call_followme *f;
  1410. ast_unregister_application(app);
  1411. /* Free Memory. Yeah! I'm free! */
  1412. AST_RWLIST_WRLOCK(&followmes);
  1413. while ((f = AST_RWLIST_REMOVE_HEAD(&followmes, entry))) {
  1414. free_numbers(f);
  1415. ast_free(f);
  1416. }
  1417. AST_RWLIST_UNLOCK(&followmes);
  1418. return 0;
  1419. }
  1420. /*!
  1421. * \brief Load the module
  1422. *
  1423. * Module loading including tests for configuration or dependencies.
  1424. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1425. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1426. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1427. * configuration file or other non-critical problem return
  1428. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1429. */
  1430. static int load_module(void)
  1431. {
  1432. if(!reload_followme(0))
  1433. return AST_MODULE_LOAD_DECLINE;
  1434. return ast_register_application_xml(app, app_exec);
  1435. }
  1436. static int reload(void)
  1437. {
  1438. reload_followme(1);
  1439. return 0;
  1440. }
  1441. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
  1442. .support_level = AST_MODULE_SUPPORT_CORE,
  1443. .load = load_module,
  1444. .unload = unload_module,
  1445. .reload = reload,
  1446. );