Merge the dialplan_aesthetics branch. Most of this patch simply converts applications
[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 <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/app.h"
45
46 static char *synopsis = "Soft Hangup Application";
47
48 static char *desc = "SoftHangup(Technology/resource[,options])\n"
49 "Hangs up the requested channel.  If there are no channels to hangup,\n"
50 "the application will report it.\n"
51 "- 'options' may contain the following letter:\n"
52 "     'a' : hang up all channels on a specified device instead of a single resource\n";
53
54 static char *app = "SoftHangup";
55
56 enum {
57         OPTION_ALL = (1 << 0),
58 };
59
60 AST_APP_OPTIONS(app_opts,{
61         AST_APP_OPTION('a', OPTION_ALL),
62 });
63
64 static int softhangup_exec(struct ast_channel *chan, void *data)
65 {
66         struct ast_channel *c = NULL;
67         char *cut, *opts[0];
68         char name[AST_CHANNEL_NAME] = "", *parse;
69         struct ast_flags flags;
70         int lenmatch;
71         AST_DECLARE_APP_ARGS(args,
72                 AST_APP_ARG(channel);
73                 AST_APP_ARG(options);
74         );
75         
76         if (ast_strlen_zero(data)) {
77                 ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
78                 return 0;
79         }
80
81         parse = ast_strdupa(data);
82         AST_STANDARD_APP_ARGS(args, parse);
83
84         if (args.argc == 2)
85                 ast_app_parse_options(app_opts, &flags, opts, args.options);
86         lenmatch = strlen(args.channel);
87
88         for (c = ast_walk_channel_by_name_prefix_locked(NULL, args.channel, lenmatch);
89                  c;
90                  c = ast_walk_channel_by_name_prefix_locked(c, args.channel, lenmatch)) {
91                 ast_copy_string(name, c->name, sizeof(name));
92                 if (ast_test_flag(&flags, OPTION_ALL)) {
93                         /* CAPI is set up like CAPI[foo/bar]/clcnt */ 
94                         if (!strcmp(c->tech->type, "CAPI")) 
95                                 cut = strrchr(name, '/');
96                         /* Basically everything else is Foo/Bar-Z */
97                         else
98                                 cut = strchr(name, '-');
99                         /* Get rid of what we've cut */
100                         if (cut)
101                                 *cut = 0;
102                 }
103                 if (!strcasecmp(name, args.channel)) {
104                         ast_log(LOG_WARNING, "Soft hanging %s up.\n", c->name);
105                         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
106                         if (!ast_test_flag(&flags, OPTION_ALL)) {
107                                 ast_mutex_unlock(&c->lock);
108                                 break;
109                         }
110                 }
111                 ast_mutex_unlock(&c->lock);
112         }
113
114         return 0;
115 }
116
117 static int unload_module(void)
118 {
119         return ast_unregister_application(app);
120 }
121
122 static int load_module(void)
123 {
124         return ast_register_application(app, softhangup_exec, synopsis, desc);
125 }
126
127 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hangs up the requested channel");