Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 <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
131static const char app[] = "Pickup";
132static const char app2[] = "PickupChan";
133
135 /*! Channel attempting to pickup a call. */
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
143static 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
164static 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 */
186static 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. */
226static 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 */
243static 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_log(LOG_NOTICE, "%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
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
274static 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);
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 */
298static 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
314static 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_log(LOG_NOTICE, "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() */
332static 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 {
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 */
379static 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 */
401static 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
418 OPT_PICKUPCHAN_PARTIAL = (1 << 0), /* Channel name is a partial name. */
419};
420
424
425/* application entry point for PickupChan() */
426static int pickupchan_exec(struct ast_channel *chan, const char *data)
427{
428 char *pickup = NULL;
429 char *parse = ast_strdupa(data);
431 AST_APP_ARG(channel);
433 AST_APP_ARG(other); /* Any remining unused arguments */
434 );
435 struct ast_flags opts;
436
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
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
478static int unload_module(void)
479{
480 int res;
481
484
485 return res;
486}
487
488static int load_module(void)
489{
490 int res;
491
494
495 return res;
496}
497
498AST_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.
char * strsep(char **str, const char *delims)
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
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:1305
struct ast_channel_iterator * ast_channel_iterator_destroy(struct ast_channel_iterator *i)
Destroy a channel iterator.
Definition: channel.c:1387
#define ast_channel_lock(chan)
Definition: channel.h:2970
struct ast_channel * ast_channel_iterator_next(struct ast_channel_iterator *i)
Get the next channel for a channel iterator.
Definition: channel.c:1449
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:1395
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3006
#define ast_channel_unlock(chan)
Definition: channel.h:2971
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
#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:80
int ast_do_pickup(struct ast_channel *chan, struct ast_channel *target)
Pickup a call target.
Definition: pickup.c:304
struct ast_channel * ast_pickup_find_by_group(struct ast_channel *chan)
Find a pickup channel target by group.
Definition: pickup.c:136
#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