Fix crash in PresenceState AMI action when specifying an invalid provider
[asterisk/asterisk.git] / main / presencestate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011-2012, Digium, Inc.
5  *
6  * David Vossel <dvossel@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 Presence state management
22  */
23
24 /*** MODULEINFO
25         <support_level>core</support_level>
26  ***/
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/_private.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/lock.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/presencestate.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/app.h"
39 #include "asterisk/event.h"
40
41 /*! \brief Device state strings for printing */
42 static const struct {
43         const char *string;
44         enum ast_presence_state state;
45
46 } state2string[] = {
47         { "not_set", AST_PRESENCE_NOT_SET},
48         { "unavailable", AST_PRESENCE_UNAVAILABLE },
49         { "available", AST_PRESENCE_AVAILABLE},
50         { "away", AST_PRESENCE_AWAY},
51         { "xa", AST_PRESENCE_XA},
52         { "chat", AST_PRESENCE_CHAT},
53         { "dnd", AST_PRESENCE_DND},
54 };
55
56 /*! \brief Flag for the queue */
57 static ast_cond_t change_pending;
58
59 struct state_change {
60         AST_LIST_ENTRY(state_change) list;
61         char provider[1];
62 };
63
64 /*! \brief  A presence state provider */
65 struct presence_state_provider {
66         char label[40];
67         ast_presence_state_prov_cb_type callback;
68         AST_RWLIST_ENTRY(presence_state_provider) list;
69 };
70
71 /*! \brief A list of providers */
72 static AST_RWLIST_HEAD_STATIC(presence_state_providers, presence_state_provider);
73
74 /*! \brief The state change queue. State changes are queued
75         for processing by a separate thread */
76 static AST_LIST_HEAD_STATIC(state_changes, state_change);
77
78 /*! \brief The presence state change notification thread */
79 static pthread_t change_thread = AST_PTHREADT_NULL;
80
81 const char *ast_presence_state2str(enum ast_presence_state state)
82 {
83         int i;
84         for (i = 0; i < ARRAY_LEN(state2string); i++) {
85                 if (state == state2string[i].state) {
86                         return state2string[i].string;
87                 }
88         }
89         return "";
90 }
91
92 enum ast_presence_state ast_presence_state_val(const char *val)
93 {
94         int i;
95         for (i = 0; i < ARRAY_LEN(state2string); i++) {
96                 if (!strcasecmp(val, state2string[i].string)) {
97                         return state2string[i].state;
98                 }
99         }
100         return AST_PRESENCE_INVALID;
101 }
102
103 static enum ast_presence_state presence_state_cached(const char *presence_provider, char **subtype, char **message)
104 {
105         enum ast_presence_state res = AST_PRESENCE_INVALID;
106         struct ast_event *event;
107         const char *_subtype;
108         const char *_message;
109
110         event = ast_event_get_cached(AST_EVENT_PRESENCE_STATE,
111                 AST_EVENT_IE_PRESENCE_PROVIDER, AST_EVENT_IE_PLTYPE_STR, presence_provider,
112                 AST_EVENT_IE_END);
113
114         if (!event) {
115                 return res;
116         }
117
118         res = ast_event_get_ie_uint(event, AST_EVENT_IE_PRESENCE_STATE);
119         _subtype = ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_SUBTYPE);
120         _message = ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_MESSAGE);
121
122         *subtype = !ast_strlen_zero(_subtype) ? ast_strdup(_subtype) : NULL;
123         *message = !ast_strlen_zero(_message) ? ast_strdup(_message) : NULL;
124         ast_event_destroy(event);
125
126         return res;
127 }
128
129 static enum ast_presence_state ast_presence_state_helper(const char *presence_provider, char **subtype, char **message, int check_cache)
130 {
131         struct presence_state_provider *provider;
132         char *address;
133         char *label = ast_strdupa(presence_provider);
134         int res = AST_PRESENCE_INVALID;
135
136         if (check_cache) {
137                 res = presence_state_cached(presence_provider, subtype, message);
138                 if (res != AST_PRESENCE_INVALID) {
139                         return res;
140                 }
141         }
142
143         if ((address = strchr(label, ':'))) {
144                 *address = '\0';
145                 address++;
146         } else {
147                 ast_log(LOG_WARNING, "No label found for presence state provider: %s\n", presence_provider);
148                 return res;
149         }
150
151         AST_RWLIST_RDLOCK(&presence_state_providers);
152         AST_RWLIST_TRAVERSE(&presence_state_providers, provider, list) {
153                 ast_debug(5, "Checking provider %s with %s\n", provider->label, label);
154
155                 if (!strcasecmp(provider->label, label)) {
156                         res = provider->callback(address, subtype, message);
157                         break;
158                 }
159         }
160         AST_RWLIST_UNLOCK(&presence_state_providers);
161
162         if (!provider) {
163                 ast_log(LOG_WARNING, "No provider found for label %s\n", label);
164         }
165
166         return res;
167 }
168
169 enum ast_presence_state ast_presence_state(const char *presence_provider, char **subtype, char **message)
170 {
171         return ast_presence_state_helper(presence_provider, subtype, message, 1);
172 }
173
174 enum ast_presence_state ast_presence_state_nocache(const char *presence_provider, char **subtype, char **message)
175 {
176         return ast_presence_state_helper(presence_provider, subtype, message, 0);
177 }
178
179 int ast_presence_state_prov_add(const char *label, ast_presence_state_prov_cb_type callback)
180 {
181         struct presence_state_provider *provider;
182
183         if (!callback || !(provider = ast_calloc(1, sizeof(*provider)))) {
184                 return -1;
185         }
186
187         provider->callback = callback;
188         ast_copy_string(provider->label, label, sizeof(provider->label));
189
190         AST_RWLIST_WRLOCK(&presence_state_providers);
191         AST_RWLIST_INSERT_HEAD(&presence_state_providers, provider, list);
192         AST_RWLIST_UNLOCK(&presence_state_providers);
193
194         return 0;
195 }
196 int ast_presence_state_prov_del(const char *label)
197 {
198         struct presence_state_provider *provider;
199         int res = -1;
200
201         AST_RWLIST_WRLOCK(&presence_state_providers);
202         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&presence_state_providers, provider, list) {
203                 if (!strcasecmp(provider->label, label)) {
204                         AST_RWLIST_REMOVE_CURRENT(list);
205                         ast_free(provider);
206                         res = 0;
207                         break;
208                 }
209         }
210         AST_RWLIST_TRAVERSE_SAFE_END;
211         AST_RWLIST_UNLOCK(&presence_state_providers);
212
213         return res;
214 }
215
216 static void presence_state_event(const char *provider,
217                 enum ast_presence_state state,
218                 const char *subtype,
219                 const char *message)
220 {
221         struct ast_event *event;
222
223         if (!(event = ast_event_new(AST_EVENT_PRESENCE_STATE,
224                         AST_EVENT_IE_PRESENCE_PROVIDER, AST_EVENT_IE_PLTYPE_STR, provider,
225                         AST_EVENT_IE_PRESENCE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
226                         AST_EVENT_IE_PRESENCE_SUBTYPE, AST_EVENT_IE_PLTYPE_STR, S_OR(subtype, ""),
227                         AST_EVENT_IE_PRESENCE_MESSAGE, AST_EVENT_IE_PLTYPE_STR, S_OR(message, ""),
228                         AST_EVENT_IE_END))) {
229                 return;
230         }
231
232         ast_event_queue_and_cache(event);
233 }
234
235 static void do_presence_state_change(const char *provider)
236 {
237         char *subtype = NULL;
238         char *message = NULL;
239         enum ast_presence_state state;
240
241         state = ast_presence_state_helper(provider, &subtype, &message, 0);
242
243         if (state < 0) {
244                 return;
245         }
246
247         presence_state_event(provider, state, subtype, message);
248         ast_free(subtype);
249         ast_free(message);
250 }
251
252 int ast_presence_state_changed_literal(enum ast_presence_state state,
253                 const char *subtype,
254                 const char *message,
255                 const char *presence_provider)
256 {
257         struct state_change *change;
258
259         if (state != AST_PRESENCE_NOT_SET) {
260                 presence_state_event(presence_provider, state, subtype, message);
261         } else if ((change_thread == AST_PTHREADT_NULL) ||
262                 !(change = ast_calloc(1, sizeof(*change) + strlen(presence_provider)))) {
263                 do_presence_state_change(presence_provider);
264         } else {
265                 strcpy(change->provider, presence_provider);
266                 AST_LIST_LOCK(&state_changes);
267                 AST_LIST_INSERT_TAIL(&state_changes, change, list);
268                 ast_cond_signal(&change_pending);
269                 AST_LIST_UNLOCK(&state_changes);
270         }
271
272         return 0;
273 }
274
275 int ast_presence_state_changed(enum ast_presence_state state,
276                 const char *subtype,
277                 const char *message,
278                 const char *fmt, ...)
279 {
280         char buf[AST_MAX_EXTENSION];
281         va_list ap;
282
283         va_start(ap, fmt);
284         vsnprintf(buf, sizeof(buf), fmt, ap);
285         va_end(ap);
286
287         return ast_presence_state_changed_literal(state, subtype, message, buf);
288 }
289
290 /*! \brief Go through the presence state change queue and update changes in the presence state thread */
291 static void *do_presence_changes(void *data)
292 {
293         struct state_change *next, *current;
294
295         for (;;) {
296                 /* This basically pops off any state change entries, resets the list back to NULL, unlocks, and processes each state change */
297                 AST_LIST_LOCK(&state_changes);
298                 if (AST_LIST_EMPTY(&state_changes))
299                         ast_cond_wait(&change_pending, &state_changes.lock);
300                 next = AST_LIST_FIRST(&state_changes);
301                 AST_LIST_HEAD_INIT_NOLOCK(&state_changes);
302                 AST_LIST_UNLOCK(&state_changes);
303
304                 /* Process each state change */
305                 while ((current = next)) {
306                         next = AST_LIST_NEXT(current, list);
307                         do_presence_state_change(current->provider);
308                         ast_free(current);
309                 }
310         }
311
312         return NULL;
313 }
314
315 int ast_presence_state_engine_init(void)
316 {
317         ast_cond_init(&change_pending, NULL);
318         if (ast_pthread_create_background(&change_thread, NULL, do_presence_changes, NULL) < 0) {
319                 ast_log(LOG_ERROR, "Unable to start presence state change thread.\n");
320                 return -1;
321         }
322
323         return 0;
324 }
325