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