Asterisk - The Open Source Telephony Project GIT-master-d5a0626
app_directed_pickup.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005, Joshua Colp
5 *
6 * Joshua Colp <jcolp@digium.com>
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 <jcolp@digium.com>
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 <synopsis>
53 Directed extension call pickup.
54 </synopsis>
55 <syntax>
56 <parameter name="targets" argsep="&amp;">
57 <argument name="extension" argsep="@" required="true">
58 <para>Specification of the pickup target.</para>
59 <argument name="extension" required="true"/>
60 <argument name="context" />
61 </argument>
62 <argument name="extension2" argsep="@" multiple="true">
63 <para>Additional specifications of pickup targets.</para>
64 <argument name="extension2" required="true"/>
65 <argument name="context2"/>
66 </argument>
67 </parameter>
68 </syntax>
69 <description>
70 <para>This application can pickup a specified ringing channel. The channel
71 to pickup can be specified in the following ways.</para>
72 <para>1) If no <replaceable>extension</replaceable> targets are specified,
73 the application will pickup a channel matching the pickup group of the
74 requesting channel.</para>
75 <para>2) If the <replaceable>extension</replaceable> is specified with a
76 <replaceable>context</replaceable> of the special string
77 <literal>PICKUPMARK</literal> (for example 10@PICKUPMARK), the application
78 will pickup a channel which has defined the channel variable
79 <variable>PICKUPMARK</variable> with the same value as
80 <replaceable>extension</replaceable> (in this example,
81 <literal>10</literal>).</para>
82 <para>3) If the <replaceable>extension</replaceable> is specified
83 with or without a <replaceable>context</replaceable>, the channel with a
84 matching <replaceable>extension</replaceable> and <replaceable>context</replaceable>
85 will be picked up. If no <replaceable>context</replaceable> is specified,
86 the current context will be used.</para>
87 <note><para>The <replaceable>extension</replaceable> is typically set on
88 matching channels by the dial application that created the channel. The
89 <replaceable>context</replaceable> is set on matching channels by the
90 channel driver for the device.</para></note>
91 </description>
92 </application>
93 <application name="PickupChan" language="en_US">
94 <synopsis>
95 Pickup a ringing channel.
96 </synopsis>
97 <syntax >
98 <parameter name="channel" argsep="&amp;" required="true">
99 <argument name="channel" required="true" />
100 <argument name="channel2" required="false" multiple="true" />
101 <para>List of channel names or channel uniqueids to pickup if ringing.
102 For example, a channel name could be <literal>SIP/bob</literal> or
103 <literal>SIP/bob-00000000</literal> to find
104 <literal>SIP/bob-00000000</literal>.
105 </para>
106 </parameter>
107 <parameter name="options" required="false">
108 <optionlist>
109 <option name="p">
110 <para>Supplied channel names are prefixes. For example,
111 <literal>SIP/bob</literal> will match
112 <literal>SIP/bob-00000000</literal> and
113 <literal>SIP/bobby-00000000</literal>.
114 </para>
115 </option>
116 </optionlist>
117 </parameter>
118 </syntax>
119 <description>
120 <para>Pickup a specified <replaceable>channel</replaceable> if ringing.</para>
121 </description>
122 </application>
123 ***/
124
125static const char app[] = "Pickup";
126static const char app2[] = "PickupChan";
127
129 /*! Channel attempting to pickup a call. */
131 /*! Channel uniqueid or partial channel name to match. */
132 const char *name;
133 /*! Length of partial channel name to match. */
134 size_t len;
135};
136
137static int find_by_name(void *obj, void *arg, void *data, int flags)
138{
139 struct ast_channel *target = obj;/*!< Potential pickup target */
140 struct pickup_by_name_args *args = data;
141
142 if (args->chan == target) {
143 /* The channel attempting to pickup a call cannot pickup itself. */
144 return 0;
145 }
146
147 ast_channel_lock(target);
148 if (!strncasecmp(ast_channel_name(target), args->name, args->len)
149 && ast_can_pickup(target)) {
150 /* Return with the channel still locked on purpose */
151 return CMP_MATCH | CMP_STOP;
152 }
153 ast_channel_unlock(target);
154
155 return 0;
156}
157
158static int find_by_uniqueid(void *obj, void *arg, void *data, int flags)
159{
160 struct ast_channel *target = obj;/*!< Potential pickup target */
161 struct pickup_by_name_args *args = data;
162
163 if (args->chan == target) {
164 /* The channel attempting to pickup a call cannot pickup itself. */
165 return 0;
166 }
167
168 ast_channel_lock(target);
169 if (!strcasecmp(ast_channel_uniqueid(target), args->name)
170 && ast_can_pickup(target)) {
171 /* Return with the channel still locked on purpose */
172 return CMP_MATCH | CMP_STOP;
173 }
174 ast_channel_unlock(target);
175
176 return 0;
177}
178
179/*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
180static struct ast_channel *find_by_channel(struct ast_channel *chan, const char *channame)
181{
182 struct ast_channel *target;
183 char *chkchan;
184 struct pickup_by_name_args pickup_args;
185
186 pickup_args.chan = chan;
187
188 if (strchr(channame, '-')) {
189 /*
190 * Use the given channel name string as-is. This allows a full channel
191 * name with a typical sequence number to be used as well as still
192 * allowing the odd partial channel name that has a '-' in it to still
193 * work, i.e. Local/bob@en-phone.
194 */
195 pickup_args.len = strlen(channame);
196 pickup_args.name = channame;
197 } else {
198 /*
199 * Append a '-' for the comparison so we check the channel name less
200 * a sequence number, i.e Find SIP/bob- and not SIP/bobby.
201 */
202 pickup_args.len = strlen(channame) + 1;
203 chkchan = ast_alloca(pickup_args.len + 1);
204 strcpy(chkchan, channame);/* Safe */
205 strcat(chkchan, "-");
206 pickup_args.name = chkchan;
207 }
208 target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
209 if (target) {
210 return target;
211 }
212
213 /* Now try a search for uniqueid. */
214 pickup_args.name = channame;
215 pickup_args.len = 0;
216 return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
217}
218
219/*! \brief Attempt to pick up named channel. */
220static int pickup_by_channel(struct ast_channel *chan, const char *name)
221{
222 int res = -1;
223 struct ast_channel *target;/*!< Potential pickup target */
224
225 /* The found channel is already locked. */
226 target = find_by_channel(chan, name);
227 if (target) {
228 res = ast_do_pickup(chan, target);
229 ast_channel_unlock(target);
230 target = ast_channel_unref(target);
231 }
232
233 return res;
234}
235
236/* Attempt to pick up specified extension with context */
237static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
238{
239 struct ast_channel *target = NULL;/*!< Potential pickup target */
240 struct ast_channel_iterator *iter;
241 int res = -1;
242
243 if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
244 return -1;
245 }
246
247 while ((target = ast_channel_iterator_next(iter))) {
248 ast_channel_lock(target);
249 if ((chan != target) && ast_can_pickup(target)) {
250 ast_log(LOG_NOTICE, "%s pickup by %s\n", ast_channel_name(target), ast_channel_name(chan));
251 break;
252 }
253 ast_channel_unlock(target);
254 target = ast_channel_unref(target);
255 }
256
258
259 if (target) {
260 res = ast_do_pickup(chan, target);
261 ast_channel_unlock(target);
262 target = ast_channel_unref(target);
263 }
264
265 return res;
266}
267
268static int find_by_mark(void *obj, void *arg, void *data, int flags)
269{
270 struct ast_channel *target = obj;/*!< Potential pickup target */
271 struct ast_channel *chan = arg;
272 const char *mark = data;
273 const char *tmp;
274
275 if (chan == target) {
276 /* The channel attempting to pickup a call cannot pickup itself. */
277 return 0;
278 }
279
280 ast_channel_lock(target);
282 if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
283 /* Return with the channel still locked on purpose */
284 return CMP_MATCH | CMP_STOP;
285 }
286 ast_channel_unlock(target);
287
288 return 0;
289}
290
291/* Attempt to pick up specified mark */
292static int pickup_by_mark(struct ast_channel *chan, const char *mark)
293{
294 struct ast_channel *target;/*!< Potential pickup target */
295 int res = -1;
296
297 /* The found channel is already locked. */
298 target = ast_channel_callback(find_by_mark, chan, (char *) mark, 0);
299 if (target) {
300 res = ast_do_pickup(chan, target);
301 ast_channel_unlock(target);
302 target = ast_channel_unref(target);
303 }
304
305 return res;
306}
307
308static int pickup_by_group(struct ast_channel *chan)
309{
310 struct ast_channel *target;/*!< Potential pickup target */
311 int res = -1;
312
313 /* The found channel is already locked. */
314 target = ast_pickup_find_by_group(chan);
315 if (target) {
316 ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", ast_channel_name(target), ast_channel_name(chan));
317 res = ast_do_pickup(chan, target);
318 ast_channel_unlock(target);
319 target = ast_channel_unref(target);
320 }
321
322 return res;
323}
324
325/* application entry point for Pickup() */
326static int pickup_exec(struct ast_channel *chan, const char *data)
327{
328 char *parse;
329 char *exten;
330 char *context;
331
332 if (ast_strlen_zero(data)) {
333 return pickup_by_group(chan) ? 0 : -1;
334 }
335
336 /* Parse extension (and context if there) */
337 parse = ast_strdupa(data);
338 for (;;) {
339 if (ast_strlen_zero(parse)) {
340 break;
341 }
342 exten = strsep(&parse, "&");
343 if (ast_strlen_zero(exten)) {
344 continue;
345 }
346
347 context = strchr(exten, '@');
348 if (context) {
349 *context++ = '\0';
350 }
351 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
352 if (!pickup_by_mark(chan, exten)) {
353 /* Pickup successful. Stop the dialplan this channel is a zombie. */
354 return -1;
355 }
356 } else {
358 context = (char *) ast_channel_context(chan);
359 }
360 if (!pickup_by_exten(chan, exten, context)) {
361 /* Pickup successful. Stop the dialplan this channel is a zombie. */
362 return -1;
363 }
364 }
365 ast_log(LOG_NOTICE, "No target channel found for %s@%s.\n", exten, context);
366 }
367
368 /* Pickup failed. Keep going in the dialplan. */
369 return 0;
370}
371
372/* Find channel for pick up specified by partial channel name */
373static struct ast_channel *find_by_part(struct ast_channel *chan, const char *part)
374{
375 struct ast_channel *target;
376 struct pickup_by_name_args pickup_args;
377
378 pickup_args.chan = chan;
379
380 /* Try a partial channel name search. */
381 pickup_args.name = part;
382 pickup_args.len = strlen(part);
383 target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
384 if (target) {
385 return target;
386 }
387
388 /* Now try a search for uniqueid. */
389 pickup_args.name = part;
390 pickup_args.len = 0;
391 return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
392}
393
394/* Attempt to pick up specified by partial channel name */
395static int pickup_by_part(struct ast_channel *chan, const char *part)
396{
397 struct ast_channel *target;/*!< Potential pickup target */
398 int res = -1;
399
400 /* The found channel is already locked. */
401 target = find_by_part(chan, part);
402 if (target) {
403 res = ast_do_pickup(chan, target);
404 ast_channel_unlock(target);
405 target = ast_channel_unref(target);
406 }
407
408 return res;
409}
410
412 OPT_PICKUPCHAN_PARTIAL = (1 << 0), /* Channel name is a partial name. */
413};
414
418
419/* application entry point for PickupChan() */
420static int pickupchan_exec(struct ast_channel *chan, const char *data)
421{
422 char *pickup = NULL;
423 char *parse = ast_strdupa(data);
425 AST_APP_ARG(channel);
427 AST_APP_ARG(other); /* Any remining unused arguments */
428 );
429 struct ast_flags opts;
430
432
433 if (ast_strlen_zero(args.channel)) {
434 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
435 /* Pickup failed. Keep going in the dialplan. */
436 return 0;
437 }
438 if (ast_app_parse_options(pickupchan_opts, &opts, NULL, args.options)) {
439 /*
440 * General invalid option syntax.
441 * Pickup failed. Keep going in the dialplan.
442 */
443 return 0;
444 }
445
446 /* Parse channel */
447 for (;;) {
448 if (ast_strlen_zero(args.channel)) {
449 break;
450 }
451 pickup = strsep(&args.channel, "&");
452 if (ast_strlen_zero(pickup)) {
453 continue;
454 }
455
457 if (!pickup_by_part(chan, pickup)) {
458 /* Pickup successful. Stop the dialplan this channel is a zombie. */
459 return -1;
460 }
461 } else if (!pickup_by_channel(chan, pickup)) {
462 /* Pickup successful. Stop the dialplan this channel is a zombie. */
463 return -1;
464 }
465 ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
466 }
467
468 /* Pickup failed. Keep going in the dialplan. */
469 return 0;
470}
471
472static int unload_module(void)
473{
474 int res;
475
478
479 return res;
480}
481
482static int load_module(void)
483{
484 int res;
485
488
489 return res;
490}
491
492AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");
static int pickup_by_part(struct ast_channel *chan, const char *part)
static const char app[]
static int find_by_name(void *obj, void *arg, void *data, int flags)
static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
static int pickupchan_exec(struct ast_channel *chan, const char *data)
static const struct ast_app_option pickupchan_opts[128]
#define PICKUPMARK
static int pickup_by_mark(struct ast_channel *chan, const char *mark)
static int pickup_by_channel(struct ast_channel *chan, const char *name)
Attempt to pick up named channel.
static int pickup_exec(struct ast_channel *chan, const char *data)
static const char app2[]
OPT_PICKUPCHAN_FLAGS
@ OPT_PICKUPCHAN_PARTIAL
static struct ast_channel * find_by_part(struct ast_channel *chan, const char *part)
static int pickup_by_group(struct ast_channel *chan)
static int load_module(void)
static int find_by_mark(void *obj, void *arg, void *data, int flags)
static int unload_module(void)
static int find_by_uniqueid(void *obj, void *arg, void *data, int flags)
static struct ast_channel * find_by_channel(struct ast_channel *chan, const char *channame)
Helper Function to walk through ALL channels checking NAME and STATE.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
@ CMP_MATCH
Definition: astobj2.h:1027
@ CMP_STOP
Definition: astobj2.h:1028
static int tmp()
Definition: bt_open.c:389
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_channel * ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg, void *data, int ao2_flags)
Call a function with every active channel.
Definition: channel.c:1297
struct ast_channel_iterator * ast_channel_iterator_destroy(struct ast_channel_iterator *i)
Destroy a channel iterator.
Definition: channel.c:1379
#define ast_channel_lock(chan)
Definition: channel.h:2968
struct ast_channel * ast_channel_iterator_next(struct ast_channel_iterator *i)
Get the next channel for a channel iterator.
Definition: channel.c:1441
const char * ast_channel_uniqueid(const struct ast_channel *chan)
const char * ast_channel_context(const struct ast_channel *chan)
struct ast_channel_iterator * ast_channel_iterator_by_exten_new(const char *exten, const char *context)
Create a new channel iterator based on extension.
Definition: channel.c:1387
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3004
#define ast_channel_unlock(chan)
Definition: channel.h:2969
Generic File Format Support. Should be included by clients of the file handling routines....
static const char name[]
Definition: format_mp3.c:68
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define END_OPTIONS
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define BEGIN_OPTIONS
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3066
char * strsep(char **str, const char *delims)
#define LOG_NOTICE
#define LOG_WARNING
Asterisk locking-related definitions:
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
Call Pickup API.
int ast_can_pickup(struct ast_channel *chan)
Test if a channel can be picked up.
Definition: pickup.c:77
int ast_do_pickup(struct ast_channel *chan, struct ast_channel *target)
Pickup a call target.
Definition: pickup.c:301
struct ast_channel * ast_pickup_find_by_group(struct ast_channel *chan)
Find a pickup channel target by group.
Definition: pickup.c:133
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
char exten[AST_MAX_EXTENSION]
const char * data
struct ast_flags flags
Structure used to handle boolean flags.
Definition: utils.h:199
struct ast_channel * chan
const char * args
static struct test_options options
#define ast_test_flag(p, flag)
Definition: utils.h:63