Massive cleanups to applications for LOCAL_USER handling and some other things.
[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 || ast_strlen_zero(data)) {
65                 ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
66                 return 0;
67         }
68         
69         LOCAL_USER_ADD(u);
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                 strncpy(name, c->name, sizeof(name)-1);
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->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         LOCAL_USER_REMOVE(u);
101
102         return 0;
103 }
104
105 int unload_module(void)
106 {
107         int res;
108
109         res = ast_unregister_application(app);
110
111         STANDARD_HANGUP_LOCALUSERS;
112
113         return res;     
114 }
115
116 int load_module(void)
117 {
118         return ast_register_application(app, softhangup_exec, synopsis, desc);
119 }
120
121 char *description(void)
122 {
123         return tdesc;
124 }
125
126 int usecount(void)
127 {
128         int res;
129         STANDARD_USECOUNT(res);
130         return res;
131 }
132
133 char *key()
134 {
135         return ASTERISK_GPL_KEY;
136 }