Move OSP* applications static documentation to XML.
[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 /*** DOCUMENTATION
40         <application name="SoftHangup" language="en_US">
41                 <synopsis>
42                         Hangs up the requested channel.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="Technology/Resource" required="true" />
46                         <parameter name="options">
47                                 <optionlist>
48                                         <option name="a">
49                                                 <para>Hang up all channels on a specified device instead of a single resource</para>
50                                         </option>
51                                 </optionlist>
52                         </parameter>
53                 </syntax>       
54                 <description>
55                         <para>Hangs up the requested channel.  If there are no channels to 
56                         hangup, the application will report it.</para>
57                 </description>
58         </application>
59
60  ***/
61
62 static char *app = "SoftHangup";
63
64 enum {
65         OPTION_ALL = (1 << 0),
66 };
67
68 AST_APP_OPTIONS(app_opts,{
69         AST_APP_OPTION('a', OPTION_ALL),
70 });
71
72 static int softhangup_exec(struct ast_channel *chan, const char *data)
73 {
74         struct ast_channel *c = NULL;
75         char *cut, *opts[0];
76         char name[AST_CHANNEL_NAME] = "", *parse;
77         struct ast_flags flags;
78         int lenmatch;
79         AST_DECLARE_APP_ARGS(args,
80                 AST_APP_ARG(channel);
81                 AST_APP_ARG(options);
82         );
83         struct ast_channel_iterator *iter;
84         
85         if (ast_strlen_zero(data)) {
86                 ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
87                 return 0;
88         }
89
90         parse = ast_strdupa(data);
91         AST_STANDARD_APP_ARGS(args, parse);
92
93         if (args.argc == 2)
94                 ast_app_parse_options(app_opts, &flags, opts, args.options);
95         lenmatch = strlen(args.channel);
96
97         if (!(iter = ast_channel_iterator_by_name_new(0, args.channel, lenmatch))) {
98                 return -1;
99         }
100
101         while ((c = ast_channel_iterator_next(iter))) {
102                 ast_channel_lock(c);
103                 ast_copy_string(name, c->name, sizeof(name));
104                 if (ast_test_flag(&flags, OPTION_ALL)) {
105                         /* CAPI is set up like CAPI[foo/bar]/clcnt */ 
106                         if (!strcmp(c->tech->type, "CAPI")) 
107                                 cut = strrchr(name, '/');
108                         /* Basically everything else is Foo/Bar-Z */
109                         else
110                                 cut = strchr(name, '-');
111                         /* Get rid of what we've cut */
112                         if (cut)
113                                 *cut = 0;
114                 }
115                 if (!strcasecmp(name, args.channel)) {
116                         ast_log(LOG_WARNING, "Soft hanging %s up.\n", c->name);
117                         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
118                         if (!ast_test_flag(&flags, OPTION_ALL)) {
119                                 ast_channel_unlock(c);
120                                 c = ast_channel_unref(c);
121                                 break;
122                         }
123                 }
124                 ast_channel_unlock(c);
125                 c = ast_channel_unref(c);
126         }
127
128         ast_channel_iterator_destroy(iter);
129
130         return 0;
131 }
132
133 static int unload_module(void)
134 {
135         return ast_unregister_application(app);
136 }
137
138 static int load_module(void)
139 {
140         return ast_register_application_xml(app, softhangup_exec);
141 }
142
143 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hangs up the requested channel");