Merged revisions 46200 via svnmerge from
[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
45 static char *synopsis = "Soft Hangup Application";
46
47 static char *desc = "  SoftHangup(Technology/resource|options)\n"
48 "Hangs up the requested channel.  If there are no channels to hangup,\n"
49 "the application will report it.\n"
50 "- 'options' may contain the following letter:\n"
51 "     'a' : hang up all channels on a specified device instead of a single resource\n";
52
53 static char *app = "SoftHangup";
54
55
56 static int softhangup_exec(struct ast_channel *chan, void *data)
57 {
58         struct ast_module_user *u;
59         struct ast_channel *c=NULL;
60         char *options, *cut, *cdata, *match;
61         char name[AST_CHANNEL_NAME] = "";
62         int all = 0;
63         
64         if (ast_strlen_zero(data)) {
65                 ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
66                 return 0;
67         }
68         
69         u = ast_module_user_add(chan);
70
71         cdata = ast_strdupa(data);
72         match = strsep(&cdata, "|");
73         options = strsep(&cdata, "|");
74         all = options && strchr(options,'a');
75         c = ast_channel_walk_locked(NULL);
76         while (c) {
77                 ast_copy_string(name, c->name, sizeof(name));
78                 ast_mutex_unlock(&c->lock);
79                 /* XXX watch out, i think it is wrong to access c-> after unlocking! */
80                 if (all) {
81                         /* CAPI is set up like CAPI[foo/bar]/clcnt */ 
82                         if (!strcmp(c->tech->type, "CAPI")) 
83                                 cut = strrchr(name,'/');
84                         /* Basically everything else is Foo/Bar-Z */
85                         else
86                                 cut = strchr(name,'-');
87                         /* Get rid of what we've cut */
88                         if (cut)
89                                 *cut = 0;
90                 }
91                 if (!strcasecmp(name, match)) {
92                         ast_log(LOG_WARNING, "Soft hanging %s up.\n",c->name);
93                         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
94                         if(!all)
95                                 break;
96                 }
97                 c = ast_channel_walk_locked(c);
98         }
99         
100         ast_module_user_remove(u);
101
102         return 0;
103 }
104
105 static int unload_module(void)
106 {
107         int res;
108
109         res = ast_unregister_application(app);
110
111         ast_module_user_hangup_all();
112
113         return res;     
114 }
115
116 static int load_module(void)
117 {
118         return ast_register_application(app, softhangup_exec, synopsis, desc);
119 }
120
121 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hangs up the requested channel");