Add a CONFBRIDGE_RESULT channel variable to discern why a channel left a ConfBridge.
[asterisk/asterisk.git] / apps / confbridge / conf_state_multi_marked.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012, Terry Wilson
5  *
6  * Terry Wilson <twilson@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  * Please follow coding guidelines
19  * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
20  */
21
22 /*! \file
23  *
24  * \brief Confbridge state handling for the MULTI_MARKED state
25  *
26  * \author\verbatim Terry Wilson <twilson@digium.com> \endverbatim
27  *
28  * \ingroup applications
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/linkedlists.h"
38 #include "include/confbridge.h"
39 #include "asterisk/musiconhold.h"
40 #include "include/conf_state.h"
41 #include "asterisk/pbx.h"
42
43 static void join_active(struct confbridge_user *user);
44 static void join_marked(struct confbridge_user *user);
45 static void leave_active(struct confbridge_user *user);
46 static void leave_marked(struct confbridge_user *user);
47 static void transition_to_marked(struct confbridge_user *user);
48
49 static struct confbridge_state STATE_MULTI_MARKED = {
50         .name = "MULTI_MARKED",
51         .join_unmarked = join_active,
52         .join_waitmarked = join_active,
53         .join_marked = join_marked,
54         .leave_unmarked = leave_active,
55         .leave_waitmarked = leave_active,
56         .leave_marked = leave_marked,
57         .entry = transition_to_marked,
58 };
59 struct confbridge_state *CONF_STATE_MULTI_MARKED = &STATE_MULTI_MARKED;
60
61 static void join_active(struct confbridge_user *user)
62 {
63         conf_add_user_active(user->conference, user);
64         conf_update_user_mute(user);
65 }
66
67 static void join_marked(struct confbridge_user *user)
68 {
69         conf_add_user_marked(user->conference, user);
70         conf_update_user_mute(user);
71 }
72
73 static void leave_active(struct confbridge_user *user)
74 {
75         conf_remove_user_active(user->conference, user);
76         if (user->conference->activeusers == 1) {
77                 conf_change_state(user, CONF_STATE_SINGLE_MARKED);
78         }
79 }
80
81 static void leave_marked(struct confbridge_user *user)
82 {
83         struct confbridge_user *user_iter;
84
85         conf_remove_user_marked(user->conference, user);
86
87         if (user->conference->markedusers == 0) {
88                 /* Play back the audio prompt saying the leader has left the conference */
89                 if (!ast_test_flag(&user->u_profile, USER_OPT_QUIET)) {
90                         ao2_unlock(user->conference);
91                         ast_autoservice_start(user->chan);
92                         play_sound_file(user->conference,
93                                 conf_get_sound(CONF_SOUND_LEADER_HAS_LEFT, user->b_profile.sounds));
94                         ast_autoservice_stop(user->chan);
95                         ao2_lock(user->conference);
96                 }
97
98                 AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->active_list, user_iter, list) {
99                         /* Kick ENDMARKED user_iters */
100                         if (ast_test_flag(&user_iter->u_profile, USER_OPT_ENDMARKED)) {
101                                 if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
102                                         && !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
103                                         AST_LIST_REMOVE_CURRENT(list);
104                                         user_iter->conference->activeusers--;
105                                         AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
106                                         user_iter->conference->waitingusers++;
107                                 }
108                                 user_iter->kicked = 1;
109                                 pbx_builtin_setvar_helper(user_iter->chan, "CONFBRIDGE_RESULT", "ENDMARKED");
110                                 ast_bridge_remove(user_iter->conference->bridge, user_iter->chan);
111                         } else if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
112                                 && !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
113                                 AST_LIST_REMOVE_CURRENT(list);
114                                 user_iter->conference->activeusers--;
115                                 AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
116                                 user_iter->conference->waitingusers++;
117
118                                 /* Handle moh of user_iter if necessary */
119                                 if (ast_test_flag(&user_iter->u_profile, USER_OPT_MUSICONHOLD)) {
120                                         conf_moh_start(user_iter);
121                                 }
122                                 conf_update_user_mute(user_iter);
123                         }
124                 }
125                 AST_LIST_TRAVERSE_SAFE_END;
126         }
127
128         switch (user->conference->activeusers) {
129         case 0:
130                 /* Implies markedusers == 0 */
131                 switch (user->conference->waitingusers) {
132                 case 0:
133                         conf_change_state(user, CONF_STATE_EMPTY);
134                         break;
135                 default:
136                         conf_change_state(user, CONF_STATE_INACTIVE);
137                         break;
138                 }
139                 break;
140         case 1:
141                 switch (user->conference->markedusers) {
142                 case 0:
143                         conf_change_state(user, CONF_STATE_SINGLE);
144                         break;
145                 case 1:
146                         /* XXX I seem to remember doing this for a reason, but right now it escapes me
147                          * how we could possibly ever have a waiting user while we have a marked user */
148                         switch (user->conference->waitingusers) {
149                         case 0:
150                                 conf_change_state(user, CONF_STATE_SINGLE_MARKED);
151                                 break;
152                         case 1:
153                                 break; /* Stay in marked */
154                         }
155                         break;
156                 }
157                 break;
158         default:
159                 switch (user->conference->markedusers) {
160                 case 0:
161                         conf_change_state(user, CONF_STATE_MULTI);
162                         break;
163                 default:
164                         break; /* Stay in marked */
165                 }
166         }
167 }
168
169 static void transition_to_marked(struct confbridge_user *user)
170 {
171         struct confbridge_user *user_iter;
172
173         /* Play the audio file stating they are going to be placed into the conference */
174         if (user->conference->markedusers == 1 && ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
175                 conf_handle_first_marked_common(user);
176         }
177
178         /* Move all waiting users to active, stopping MOH and unmuting if necessary */
179         AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->waiting_list, user_iter, list) {
180                 AST_LIST_REMOVE_CURRENT(list);
181                 user->conference->waitingusers--;
182                 AST_LIST_INSERT_TAIL(&user->conference->active_list, user_iter, list);
183                 user->conference->activeusers++;
184                 if (user_iter->playing_moh) {
185                         conf_moh_stop(user_iter);
186                 }
187                 conf_update_user_mute(user_iter);
188         }
189         AST_LIST_TRAVERSE_SAFE_END;
190 }