7af8525604cbc9a343c7cd2c909785480ba36e89
[asterisk/asterisk.git] / apps / app_softhangup.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief SoftHangup application
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/file.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36 #include "asterisk/lock.h"
37 #include "asterisk/app.h"
38
39 static char *synopsis = "Soft Hangup Application";
40
41 static char *desc = "  SoftHangup(Technology/resource[,options]):\n"
42 "Hangs up the requested channel.  If there are no channels to hangup,\n"
43 "the application will report it.\n"
44 "  Options:\n"
45 "     'a'  - hang up all channels on a specified device instead of a single resource\n";
46
47 static char *app = "SoftHangup";
48
49 enum {
50         OPTION_ALL = (1 << 0),
51 };
52
53 AST_APP_OPTIONS(app_opts,{
54         AST_APP_OPTION('a', OPTION_ALL),
55 });
56
57 static int softhangup_exec(struct ast_channel *chan, void *data)
58 {
59         struct ast_channel *c = NULL;
60         char *cut, *opts[0];
61         char name[AST_CHANNEL_NAME] = "", *parse;
62         struct ast_flags flags;
63         int lenmatch;
64         AST_DECLARE_APP_ARGS(args,
65                 AST_APP_ARG(channel);
66                 AST_APP_ARG(options);
67         );
68         
69         if (ast_strlen_zero(data)) {
70                 ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
71                 return 0;
72         }
73
74         parse = ast_strdupa(data);
75         AST_STANDARD_APP_ARGS(args, parse);
76
77         if (args.argc == 2)
78                 ast_app_parse_options(app_opts, &flags, opts, args.options);
79         lenmatch = strlen(args.channel);
80
81         for (c = ast_walk_channel_by_name_prefix_locked(NULL, args.channel, lenmatch);
82                  c;
83                  c = ast_walk_channel_by_name_prefix_locked(c, args.channel, lenmatch)) {
84                 ast_copy_string(name, c->name, sizeof(name));
85                 if (ast_test_flag(&flags, OPTION_ALL)) {
86                         /* CAPI is set up like CAPI[foo/bar]/clcnt */ 
87                         if (!strcmp(c->tech->type, "CAPI")) 
88                                 cut = strrchr(name, '/');
89                         /* Basically everything else is Foo/Bar-Z */
90                         else
91                                 cut = strchr(name, '-');
92                         /* Get rid of what we've cut */
93                         if (cut)
94                                 *cut = 0;
95                 }
96                 if (!strcasecmp(name, args.channel)) {
97                         ast_log(LOG_WARNING, "Soft hanging %s up.\n", c->name);
98                         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
99                         if (!ast_test_flag(&flags, OPTION_ALL)) {
100                                 ast_channel_unlock(c);
101                                 break;
102                         }
103                 }
104                 ast_channel_unlock(c);
105         }
106
107         return 0;
108 }
109
110 static int unload_module(void)
111 {
112         return ast_unregister_application(app);
113 }
114
115 static int load_module(void)
116 {
117         return ast_register_application(app, softhangup_exec, synopsis, desc);
118 }
119
120 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hangs up the requested channel");