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