Applications no longer need to call ast_module_user_add and ast_module_user_remove...
[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  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/lock.h"
44
45 static char *synopsis = "Soft Hangup Application";
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
56 static int softhangup_exec(struct ast_channel *chan, void *data)
57 {
58         struct ast_channel *c=NULL;
59         char *options, *cut, *cdata, *match;
60         char name[AST_CHANNEL_NAME] = "";
61         int all = 0;
62         
63         if (ast_strlen_zero(data)) {
64                 ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
65                 return 0;
66         }
67         
68         cdata = ast_strdupa(data);
69         match = strsep(&cdata, "|");
70         options = strsep(&cdata, "|");
71         all = options && strchr(options,'a');
72         c = ast_channel_walk_locked(NULL);
73         while (c) {
74                 ast_copy_string(name, c->name, sizeof(name));
75                 ast_mutex_unlock(&c->lock);
76                 /* XXX watch out, i think it is wrong to access c-> after unlocking! */
77                 if (all) {
78                         /* CAPI is set up like CAPI[foo/bar]/clcnt */ 
79                         if (!strcmp(c->tech->type, "CAPI")) 
80                                 cut = strrchr(name,'/');
81                         /* Basically everything else is Foo/Bar-Z */
82                         else
83                                 cut = strchr(name,'-');
84                         /* Get rid of what we've cut */
85                         if (cut)
86                                 *cut = 0;
87                 }
88                 if (!strcasecmp(name, match)) {
89                         ast_log(LOG_WARNING, "Soft hanging %s up.\n",c->name);
90                         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
91                         if(!all)
92                                 break;
93                 }
94                 c = ast_channel_walk_locked(c);
95         }
96
97         return 0;
98 }
99
100 static int unload_module(void)
101 {
102         return ast_unregister_application(app);
103 }
104
105 static int load_module(void)
106 {
107         return ast_register_application(app, softhangup_exec, synopsis, desc);
108 }
109
110 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hangs up the requested channel");