Debian Codegraph @

apps/app_directed_pickup.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005, Joshua Colp
5 *
6 * Joshua Colp <[email protected]>
7 *
8 * Portions merged from app_pickupchan, which was
9 * Copyright (C) 2008, Gary Cook
10 *
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
20 */
21
22 /*! \file
23 *
24 * \brief Directed Call Pickup Support
25 *
26 * \author Joshua Colp <[email protected]>
27 * \author Gary Cook
28 *
29 * \ingroup applications
30 */
31
32 /*** MODULEINFO
33 <support_level>core</support_level>
34 ***/
35
36 #include "asterisk.h"
37
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/app.h"
44 #include "asterisk/pickup.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/callerid.h"
47
48 #define PICKUPMARK "PICKUPMARK"
49
50 /*** DOCUMENTATION
51 <application name="Pickup" language="en_US">
52 <since>
53 <version>1.2.0</version>
54 </since>
55 <synopsis>
56 Directed extension call pickup.
57 </synopsis>
58 <syntax>
59 <parameter name="targets" argsep="&amp;">
60 <argument name="extension" argsep="@" required="true">
61 <para>Specification of the pickup target.</para>
62 <argument name="extension" required="true"/>
63 <argument name="context" />
64 </argument>
65 <argument name="extension2" argsep="@" multiple="true">
66 <para>Additional specifications of pickup targets.</para>
67 <argument name="extension2" required="true"/>
68 <argument name="context2"/>
69 </argument>
70 </parameter>
71 </syntax>
72 <description>
73 <para>This application can pickup a specified ringing channel. The channel
74 to pickup can be specified in the following ways.</para>
75 <para>1) If no <replaceable>extension</replaceable> targets are specified,
76 the application will pickup a channel matching the pickup group of the
77 requesting channel.</para>
78 <para>2) If the <replaceable>extension</replaceable> is specified with a
79 <replaceable>context</replaceable> of the special string
80 <literal>PICKUPMARK</literal> (for example 10@PICKUPMARK), the application
81 will pickup a channel which has defined the channel variable
82 <variable>PICKUPMARK</variable> with the same value as
83 <replaceable>extension</replaceable> (in this example,
84 <literal>10</literal>).</para>
85 <para>3) If the <replaceable>extension</replaceable> is specified
86 with or without a <replaceable>context</replaceable>, the channel with a
87 matching <replaceable>extension</replaceable> and <replaceable>context</replaceable>
88 will be picked up. If no <replaceable>context</replaceable> is specified,
89 the current context will be used.</para>
90 <note><para>The <replaceable>extension</replaceable> is typically set on
91 matching channels by the dial application that created the channel. The
92 <replaceable>context</replaceable> is set on matching channels by the
93 channel driver for the device.</para></note>
94 </description>
95 </application>
96 <application name="PickupChan" language="en_US">
97 <since>
98 <version>1.6.0</version>
99 </since>
100 <synopsis>
101 Pickup a ringing channel.
102 </synopsis>
103 <syntax >
104 <parameter name="channel" argsep="&amp;" required="true">
105 <argument name="channel" required="true" />
106 <argument name="channel2" required="false" multiple="true" />
107 <para>List of channel names or channel uniqueids to pickup if ringing.
108 For example, a channel name could be <literal>SIP/bob</literal> or
109 <literal>SIP/bob-00000000</literal> to find
110 <literal>SIP/bob-00000000</literal>.
111 </para>
112 </parameter>
113 <parameter name="options" required="false">
114 <optionlist>
115 <option name="p">
116 <para>Supplied channel names are prefixes. For example,
117 <literal>SIP/bob</literal> will match
118 <literal>SIP/bob-00000000</literal> and
119 <literal>SIP/bobby-00000000</literal>.
120 </para>
121 </option>
122 </optionlist>
123 </parameter>
124 </syntax>
125 <description>
126 <para>Pickup a specified <replaceable>channel</replaceable> if ringing.</para>
127 </description>
128 </application>
129 ***/
130
131 static const char app[] = "Pickup";
132 static const char app2[] = "PickupChan";
133
134 struct pickup_by_name_args {
135 /*! Channel attempting to pickup a call. */
136 struct ast_channel *chan;
137 /*! Channel uniqueid or partial channel name to match. */
138 const char *name;
139 /*! Length of partial channel name to match. */
140 size_t len;
141 };
142
143 static int find_by_name(void *obj, void *arg, void *data, int flags)
144 {
145 struct ast_channel *target = obj;/*!< Potential pickup target */
146 struct pickup_by_name_args *args = data;
147
148 if (args->chan == target) {
149 /* The channel attempting to pickup a call cannot pickup itself. */
150 return 0;
151 }
152
153 ast_channel_lock(target);
154 if (!strncasecmp(ast_channel_name(target), args->name, args->len)
155 && ast_can_pickup(target)) {
156 /* Return with the channel still locked on purpose */
157 return CMP_MATCH | CMP_STOP;
158 }
159 ast_channel_unlock(target);
160
161 return 0;
162 }
163
164 static int find_by_uniqueid(void *obj, void *arg, void *data, int flags)
165 {
166 struct ast_channel *target = obj;/*!< Potential pickup target */
167 struct pickup_by_name_args *args = data;
168
169 if (args->chan == target) {
170 /* The channel attempting to pickup a call cannot pickup itself. */
171 return 0;
172 }
173
174 ast_channel_lock(target);
175 if (!strcasecmp(ast_channel_uniqueid(target), args->name)
176 && ast_can_pickup(target)) {
177 /* Return with the channel still locked on purpose */
178 return CMP_MATCH | CMP_STOP;
179 }
180 ast_channel_unlock(target);
181
182 return 0;
183 }
184
185 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
186 static struct ast_channel *find_by_channel(struct ast_channel *chan, const char *channame)
187 {
188 struct ast_channel *target;
189 char *chkchan;
190 struct pickup_by_name_args pickup_args;
191
192 pickup_args.chan = chan;
193
194 if (strchr(channame, '-')) {
195 /*
196 * Use the given channel name string as-is. This allows a full channel
197 * name with a typical sequence number to be used as well as still
198 * allowing the odd partial channel name that has a '-' in it to still
199 * work, i.e. Local/bob@en-phone.
200 */
201 pickup_args.len = strlen(channame);
202 pickup_args.name = channame;
203 } else {
204 /*
205 * Append a '-' for the comparison so we check the channel name less
206 * a sequence number, i.e Find SIP/bob- and not SIP/bobby.
207 */
208 pickup_args.len = strlen(channame) + 1;
209 chkchan = ast_alloca(pickup_args.len + 1);
210 strcpy(chkchan, channame);/* Safe */
211 strcat(chkchan, "-");
212 pickup_args.name = chkchan;
213 }
214 target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
215 if (target) {
216 return target;
217 }
218
219 /* Now try a search for uniqueid. */
220 pickup_args.name = channame;
221 pickup_args.len = 0;
222 return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
223 }
224
225 /*! \brief Attempt to pick up named channel. */
226 static int pickup_by_channel(struct ast_channel *chan, const char *name)
227 {
228 int res = -1;
229 struct ast_channel *target;/*!< Potential pickup target */
230
231 /* The found channel is already locked. */
232 target = find_by_channel(chan, name);
233 if (target) {
234 res = ast_do_pickup(chan, target);
235 ast_channel_unlock(target);
236 target = ast_channel_unref(target);
237 }
238
239 return res;
240 }
241
242 /* Attempt to pick up specified extension with context */
243 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
244 {
245 struct ast_channel *target = NULL;/*!< Potential pickup target */
246 struct ast_channel_iterator *iter;
247 int res = -1;
248
249 if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
250 return -1;
251 }
252
253 while ((target = ast_channel_iterator_next(iter))) {
254 ast_channel_lock(target);
255 if ((chan != target) && ast_can_pickup(target)) {
256 ast_verb(3, "%s pickup by %s\n", ast_channel_name(target), ast_channel_name(chan));
257 break;
258 }
259 ast_channel_unlock(target);
260 target = ast_channel_unref(target);
261 }
262
263 ast_channel_iterator_destroy(iter);
264
265 if (target) {
266 res = ast_do_pickup(chan, target);
267 ast_channel_unlock(target);
268 target = ast_channel_unref(target);
269 }
270
271 return res;
272 }
273
274 static int find_by_mark(void *obj, void *arg, void *data, int flags)
275 {
276 struct ast_channel *target = obj;/*!< Potential pickup target */
277 struct ast_channel *chan = arg;
278 const char *mark = data;
279 const char *tmp;
280
281 if (chan == target) {
282 /* The channel attempting to pickup a call cannot pickup itself. */
283 return 0;
284 }
285
286 ast_channel_lock(target);
287 tmp = pbx_builtin_getvar_helper(target, PICKUPMARK);
288 if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
289 /* Return with the channel still locked on purpose */
290 return CMP_MATCH | CMP_STOP;
291 }
292 ast_channel_unlock(target);
293
294 return 0;
295 }
296
297 /* Attempt to pick up specified mark */
298 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
299 {
300 struct ast_channel *target;/*!< Potential pickup target */
301 int res = -1;
302
303 /* The found channel is already locked. */
304 target = ast_channel_callback(find_by_mark, chan, (char *) mark, 0);
305 if (target) {
306 res = ast_do_pickup(chan, target);
307 ast_channel_unlock(target);
308 target = ast_channel_unref(target);
309 }
310
311 return res;
312 }
313
314 static int pickup_by_group(struct ast_channel *chan)
315 {
316 struct ast_channel *target;/*!< Potential pickup target */
317 int res = -1;
318
319 /* The found channel is already locked. */
320 target = ast_pickup_find_by_group(chan);
321 if (target) {
322 ast_verb(3, "pickup %s attempt by %s\n", ast_channel_name(target), ast_channel_name(chan));
323 res = ast_do_pickup(chan, target);
324 ast_channel_unlock(target);
325 target = ast_channel_unref(target);
326 }
327
328 return res;
329 }
330
331 /* application entry point for Pickup() */
332 static int pickup_exec(struct ast_channel *chan, const char *data)
333 {
334 char *parse;
335 char *exten;
336 char *context;
337
338 if (ast_strlen_zero(data)) {
339 return pickup_by_group(chan) ? 0 : -1;
340 }
341
342 /* Parse extension (and context if there) */
343 parse = ast_strdupa(data);
344 for (;;) {
345 if (ast_strlen_zero(parse)) {
346 break;
347 }
348 exten = strsep(&parse, "&");
349 if (ast_strlen_zero(exten)) {
350 continue;
351 }
352
353 context = strchr(exten, '@');
354 if (context) {
355 *context++ = '\0';
356 }
357 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
358 if (!pickup_by_mark(chan, exten)) {
359 /* Pickup successful. Stop the dialplan this channel is a zombie. */
360 return -1;
361 }
362 } else {
363 if (ast_strlen_zero(context)) {
364 context = (char *) ast_channel_context(chan);
365 }
366 if (!pickup_by_exten(chan, exten, context)) {
367 /* Pickup successful. Stop the dialplan this channel is a zombie. */
368 return -1;
369 }
370 }
371 ast_log(LOG_NOTICE, "No target channel found for %s@%s.\n", exten, context);
372 }
373
374 /* Pickup failed. Keep going in the dialplan. */
375 return 0;
376 }
377
378 /* Find channel for pick up specified by partial channel name */
379 static struct ast_channel *find_by_part(struct ast_channel *chan, const char *part)
380 {
381 struct ast_channel *target;
382 struct pickup_by_name_args pickup_args;
383
384 pickup_args.chan = chan;
385
386 /* Try a partial channel name search. */
387 pickup_args.name = part;
388 pickup_args.len = strlen(part);
389 target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
390 if (target) {
391 return target;
392 }
393
394 /* Now try a search for uniqueid. */
395 pickup_args.name = part;
396 pickup_args.len = 0;
397 return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
398 }
399
400 /* Attempt to pick up specified by partial channel name */
401 static int pickup_by_part(struct ast_channel *chan, const char *part)
402 {
403 struct ast_channel *target;/*!< Potential pickup target */
404 int res = -1;
405
406 /* The found channel is already locked. */
407 target = find_by_part(chan, part);
408 if (target) {
409 res = ast_do_pickup(chan, target);
410 ast_channel_unlock(target);
411 target = ast_channel_unref(target);
412 }
413
414 return res;
415 }
416
417 enum OPT_PICKUPCHAN_FLAGS {
418 OPT_PICKUPCHAN_PARTIAL = (1 << 0), /* Channel name is a partial name. */
419 };
420
421 AST_APP_OPTIONS(pickupchan_opts, BEGIN_OPTIONS
422 AST_APP_OPTION('p', OPT_PICKUPCHAN_PARTIAL),
423 END_OPTIONS);
424
425 /* application entry point for PickupChan() */
426 static int pickupchan_exec(struct ast_channel *chan, const char *data)
427 {
428 char *pickup = NULL;
429 char *parse = ast_strdupa(data);
430 AST_DECLARE_APP_ARGS(args,
431 AST_APP_ARG(channel);
432 AST_APP_ARG(options);
433 AST_APP_ARG(other); /* Any remining unused arguments */
434 );
435 struct ast_flags opts;
436
437 AST_STANDARD_APP_ARGS(args, parse);
438
439 if (ast_strlen_zero(args.channel)) {
440 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
441 /* Pickup failed. Keep going in the dialplan. */
442 return 0;
443 }
444 if (ast_app_parse_options(pickupchan_opts, &opts, NULL, args.options)) {
445 /*
446 * General invalid option syntax.
447 * Pickup failed. Keep going in the dialplan.
448 */
449 return 0;
450 }
451
452 /* Parse channel */
453 for (;;) {
454 if (ast_strlen_zero(args.channel)) {
455 break;
456 }
457 pickup = strsep(&args.channel, "&");
458 if (ast_strlen_zero(pickup)) {
459 continue;
460 }
461
462 if (ast_test_flag(&opts, OPT_PICKUPCHAN_PARTIAL)) {
463 if (!pickup_by_part(chan, pickup)) {
464 /* Pickup successful. Stop the dialplan this channel is a zombie. */
465 return -1;
466 }
467 } else if (!pickup_by_channel(chan, pickup)) {
468 /* Pickup successful. Stop the dialplan this channel is a zombie. */
469 return -1;
470 }
471 ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
472 }
473
474 /* Pickup failed. Keep going in the dialplan. */
475 return 0;
476 }
477
478 static int unload_module(void)
479 {
480 int res;
481
482 res = ast_unregister_application(app);
483 res |= ast_unregister_application(app2);
484
485 return res;
486 }
487
488 static int load_module(void)
489 {
490 int res;
491
492 res = ast_register_application_xml(app, pickup_exec);
493 res |= ast_register_application_xml(app2, pickupchan_exec);
494
495 return res;
496 }
497
498 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");