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