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