issue #5605
[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  * \ingroup applications
24  */
25
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/file.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41
42 static char *synopsis = "Soft Hangup Application";
43
44 static char *tdesc = "Hangs up the requested channel";
45
46 static char *desc = "  SoftHangup(Technology/resource|options)\n"
47 "Hangs up the requested channel.  Always returns 0\n"
48 "- 'options' may contain the following letter:\n"
49 "     'a' : hang up all channels on a specified device instead of a single resource\n";
50
51 static char *app = "SoftHangup";
52
53 STANDARD_LOCAL_USER;
54
55 LOCAL_USER_DECL;
56
57 static int softhangup_exec(struct ast_channel *chan, void *data)
58 {
59         struct localuser *u;
60         struct ast_channel *c=NULL;
61         char *options, *cut, *cdata, *match;
62         char name[AST_CHANNEL_NAME] = "";
63         int all = 0;
64         
65         if (ast_strlen_zero(data)) {
66                 ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
67                 return 0;
68         }
69         
70         LOCAL_USER_ADD(u);
71
72         cdata = ast_strdupa(data);
73         match = strsep(&cdata, "|");
74         options = strsep(&cdata, "|");
75         all = options && strchr(options,'a');
76         c = ast_channel_walk_locked(NULL);
77         while (c) {
78                 strncpy(name, c->name, sizeof(name)-1);
79                 ast_mutex_unlock(&c->lock);
80                 /* XXX watch out, i think it is wrong to access c-> after unlocking! */
81                 if (all) {
82                         /* CAPI is set up like CAPI[foo/bar]/clcnt */ 
83                         if (!strcmp(c->type,"CAPI")) 
84                                 cut = strrchr(name,'/');
85                         /* Basically everything else is Foo/Bar-Z */
86                         else
87                                 cut = strchr(name,'-');
88                         /* Get rid of what we've cut */
89                         if (cut)
90                                 *cut = 0;
91                 }
92                 if (!strcasecmp(name, match)) {
93                         ast_log(LOG_WARNING, "Soft hanging %s up.\n",c->name);
94                         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
95                         if(!all)
96                                 break;
97                 }
98                 c = ast_channel_walk_locked(c);
99         }
100         
101         LOCAL_USER_REMOVE(u);
102
103         return 0;
104 }
105
106 int unload_module(void)
107 {
108         int res;
109
110         res = ast_unregister_application(app);
111
112         STANDARD_HANGUP_LOCALUSERS;
113
114         return res;     
115 }
116
117 int load_module(void)
118 {
119         return ast_register_application(app, softhangup_exec, synopsis, desc);
120 }
121
122 char *description(void)
123 {
124         return tdesc;
125 }
126
127 int usecount(void)
128 {
129         int res;
130         STANDARD_USECOUNT(res);
131         return res;
132 }
133
134 char *key()
135 {
136         return ASTERISK_GPL_KEY;
137 }