1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
*** xx/apps/app_pickup2.c 1970-01-01 08:00:00.000000000 +0800
--- asterisk-1.4.22/apps/app_pickup2.c 2009-01-19 17:44:07.483909506 +0900
***************
*** 0 ****
--- 1,279 ----
+ /*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * Pickup, channel independent call pickup
+ *
+ * Copyright (C) 2005-2007, Thorsten Knabe <ast@thorsten-knabe.de>
+ *
+ * Copyright (C) 2004, Junghanns.NET GmbH
+ *
+ * Klaus-Peter Junghanns <kpj@junghanns.net>
+ *
+ * Copyright (C) 2004, Florian Overkamp <florian@obsimref.com>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+ /*** MODULEINFO
+ <defaultenabled>yes</defaultenabled>
+ ***/
+
+ #include "asterisk.h"
+
+ ASTERISK_FILE_VERSION(__FILE__, "$Revision: 2 $")
+
+ #include <stdlib.h>
+ #include <unistd.h>
+ #include <string.h>
+ #include <stdio.h>
+ #include <signal.h>
+ #include <pthread.h>
+ #include "asterisk/lock.h"
+ #include "asterisk/file.h"
+ #include "asterisk/logger.h"
+ #include "asterisk/channel.h"
+ #include "asterisk/pbx.h"
+ #include "asterisk/module.h"
+ #include "asterisk/musiconhold.h"
+ #include "asterisk/features.h"
+ #include "asterisk/options.h"
+
+ static char *app = "PickUp2";
+ static char *synopsis = "PickUp ringing channel.";
+ static char *descrip =
+ " PickUp2(Technology/resource[&Technology2/resource2&...][|<option>]):\n"
+ "Matches the list of prefixes in the parameter list against channels in\n"
+ "state RINGING. If a match is found the channel is picked up and\n"
+ "PICKUP_CHANNEL is set to the picked up channel name. If no matching\n"
+ "channel is found PICKUP_CHANNEL is empty.\n"
+ "Possible options:\n"
+ "B: match on channel name of bridged channel.\n";
+
+ static char *app2 = "PickDown2";
+ static char *synopsis2 = "Hangup ringing channel.";
+ static char *descrip2 =
+ " PickDown2(Technology/resource[&Technology2/resource2&...][|<option>]):\n"
+ "Matches the list of prefixes in the parameter list against channels in\n"
+ "state RINGING. If a match is found the channel is hung up and\n"
+ "PICKDOWN_CHANNEL is set to the hung up channel name. If no matching\n"
+ "channel is found PICKDOWN_CHANNEL is empty.\n"
+ "Possible options:\n"
+ "B: match on channel name of bridged channel.\n";
+
+ static char *app3 = "Steal2";
+ static char *synopsis3 = "Steal a connected channel.";
+
+ static char *descrip3 =
+ " Steal2(Technology/resource[&Technology2/resource2&...][|<option>]):\n"
+ "Matches the list of prefixes in the parameter list against channels in\n"
+ "state UP. If a match is found the channel is stolen and\n"
+ "STEAL_CHANNEL is set to the stolen channel name. If no matching\n"
+ "channel is found STEAL_CHANNEL is empty.\n"
+ "Possible options:\n"
+ "B: match on channel name of bridged channel.\n";
+
+ /* Find channel matching given pattern and state, skipping our own channel.
+ * Returns locked channel, which has to be unlocked using ast_mutex_unlock().
+ * Returns NULL when no matching channel is found.
+ */
+ static struct ast_channel *find_matching_channel(struct ast_channel *chan,
+ void *pattern, int chanstate)
+ {
+ struct ast_channel *cur;
+ char *pat = "";
+ char *next_pat = NULL;
+ char *option = "";
+ struct ast_channel *bridged;
+
+ /* copy original pattern or use empty pattern if no pattern has been given*/
+ if (pattern) {
+ pat = alloca(strlen(pattern) + 1);
+ strcpy(pat, pattern);
+ }
+ for (option = pat; *option; option++) {
+ if (*option == '|') {
+ *option = '\0';
+ option++;
+ break;
+ }
+ }
+ ast_verbose(VERBOSE_PREFIX_4
+ "find_matching_channel: pattern='%s' option='%s' state=%d\n",
+ (char *)pat, option, chanstate);
+
+ /* match on bridged channel name */
+ if (!strcmp("B", option)) {
+ /* Iterate over each part of the pattern */
+ while (pat) {
+ /* find pattern for next iteration,
+ * terminate current pattern */
+ for (next_pat = pat;
+ *next_pat && *next_pat != '&'; next_pat++);
+ if (*next_pat == '&') {
+ *next_pat = 0;
+ next_pat++;
+ } else
+ next_pat = NULL;
+ /* Iterate over all channels */
+ cur = ast_channel_walk_locked(NULL);
+ while (cur) {
+ bridged = ast_bridged_channel(cur);
+ if (bridged) {
+ ast_verbose(VERBOSE_PREFIX_4
+ "find_matching_channel: trying channel='%s' bridged='%s' "
+ "state=%d pattern='%s'\n",
+ cur->name, bridged->name, cur->_state, pat);
+ if ((cur != chan) && (bridged != chan) &&
+ (cur->_state == chanstate) &&
+ !strncmp(pat, bridged->name, strlen(pat))) {
+ ast_verbose(VERBOSE_PREFIX_4
+ "find_matching_channel: "
+ "found channel='%s' bridged='%s'\n",
+ cur->name, bridged->name);
+ return(cur);
+ }
+ }
+ ast_mutex_unlock(&cur->lock);
+ cur = ast_channel_walk_locked(cur);
+ }
+ pat = next_pat;
+ }
+ } else {
+ /* Iterate over each part of the pattern */
+ while (pat) {
+ /* find pattern for next iteration,
+ * terminate current pattern */
+ for (next_pat = pat;
+ *next_pat && *next_pat != '&'; next_pat++);
+ if (*next_pat == '&') {
+ *next_pat = 0;
+ next_pat++;
+ } else
+ next_pat = NULL;
+ /* Iterate over all channels */
+ cur = ast_channel_walk_locked(NULL);
+ while (cur) {
+ ast_verbose(VERBOSE_PREFIX_4
+ "find_matching_channel: trying channel='%s' "
+ "state=%d pattern='%s'\n",
+ cur->name, cur->_state, pat);
+ if ((cur != chan) &&
+ (cur->_state == chanstate) &&
+ !strncmp(pat, cur->name, strlen(pat))) {
+ ast_verbose(VERBOSE_PREFIX_4
+ "find_matching_channel: "
+ "found channel='%s'\n",
+ cur->name);
+ return(cur);
+ }
+ ast_mutex_unlock(&cur->lock);
+ cur = ast_channel_walk_locked(cur);
+ }
+ pat = next_pat;
+ }
+ }
+ return(NULL);
+ }
+
+ static int pickup_channel(struct ast_channel *chan, void *pattern)
+ {
+ int ret = 0;
+ struct ast_module_user *u;
+ struct ast_channel *cur;
+ u = ast_module_user_add(chan);
+ cur = find_matching_channel(chan, pattern, AST_STATE_RINGING);
+ if (cur) {
+ ast_verbose(VERBOSE_PREFIX_3
+ "Channel %s picked up ringing channel %s\n",
+ chan->name, cur->name);
+ pbx_builtin_setvar_helper(chan, "PICKUP_CHANNEL", cur->name);
+ if (chan->_state != AST_STATE_UP) {
+ ast_answer(chan);
+ }
+ if (ast_channel_masquerade(cur, chan)) {
+ ast_log(LOG_ERROR, "unable to masquerade\n");
+ ret = -1;
+ }
+ ast_mutex_unlock(&cur->lock);
+ ast_mutex_unlock(&chan->lock);
+ } else {
+ pbx_builtin_setvar_helper(chan, "PICKUP_CHANNEL", "");
+ }
+ ast_module_user_remove(u);
+ return(ret);
+ }
+
+ static int pickdown_channel(struct ast_channel *chan, void *pattern)
+ {
+ int ret = 0;
+ struct ast_module_user *u;
+ struct ast_channel *cur;
+ u = ast_module_user_add(chan);
+ cur = find_matching_channel(chan, pattern, AST_STATE_RINGING);
+ if (cur) {
+ ast_verbose(VERBOSE_PREFIX_3
+ "Channel %s hung up ringing channel %s\n",
+ chan->name, cur->name);
+ pbx_builtin_setvar_helper(chan, "PICKDOWN_CHANNEL", cur->name);
+ ast_softhangup_nolock(cur, AST_SOFTHANGUP_DEV);
+ ast_mutex_unlock(&cur->lock);
+ } else {
+ pbx_builtin_setvar_helper(chan, "PICKDOWN_CHANNEL", "");
+ }
+ ast_module_user_remove(u);
+ return(ret);
+ }
+
+ static int steal_channel(struct ast_channel *chan, void *pattern)
+ {
+ int ret = 0;
+ struct ast_module_user *u;
+ struct ast_channel *cur;
+ u = ast_module_user_add(chan);
+ cur = find_matching_channel(chan, pattern, AST_STATE_UP);
+ if (cur) {
+ ast_verbose(VERBOSE_PREFIX_3
+ "Channel %s stole channel %s\n",
+ chan->name, cur->name);
+ pbx_builtin_setvar_helper(chan, "STEAL_CHANNEL", cur->name);
+ if (chan->_state != AST_STATE_UP) {
+ ast_answer(chan);
+ }
+ if (cur->_bridge) {
+ if (!ast_mutex_lock(&cur->_bridge->lock)) {
+ ast_moh_stop(cur->_bridge);
+ ast_mutex_unlock(&cur->_bridge->lock);
+ }
+ }
+
+ if (ast_channel_masquerade(cur, chan)) {
+ ast_log(LOG_ERROR, "unable to masquerade\n");
+ ret = -1;
+ }
+ ast_mutex_unlock(&cur->lock);
+ ast_mutex_unlock(&chan->lock);
+ } else {
+ pbx_builtin_setvar_helper(chan, "STEAL_CHANNEL", "");
+ }
+ ast_module_user_remove(u);
+ return(ret);
+ }
+
+ static int unload_module(void)
+ {
+ ast_module_user_hangup_all();
+ ast_unregister_application(app3);
+ ast_unregister_application(app2);
+ return ast_unregister_application(app);
+ }
+
+ static int load_module(void)
+ {
+ ast_register_application(app3, steal_channel, synopsis3, descrip3);
+ ast_register_application(app2, pickdown_channel, synopsis2, descrip2);
+ return ast_register_application(app, pickup_channel, synopsis, descrip);
+ }
+
+ AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY,
+ "PickUp2, PickDown2, Steal2 Application");
|