51df75098242efb4dddfce97a524c0c0d0bb6fb5
[asterisk/asterisk.git] / funcs / func_presencestate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011, 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 Custom presence provider
22  * \ingroup functions
23  */
24
25 /*** MODULEINFO
26         <support_level>core</support_level>
27  ***/
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/presencestate.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/astdb.h"
41 #include "asterisk/app.h"
42 #ifdef TEST_FRAMEWORK
43 #include "asterisk/test.h"
44 #include "asterisk/event.h"
45 #include <semaphore.h>
46 #endif
47
48 /*** DOCUMENTATION
49         <function name="PRESENCE_STATE" language="en_US">
50                 <synopsis>
51                         Get or Set a presence state.
52                 </synopsis>
53                 <syntax>
54                         <parameter name="provider" required="true">
55                           <para>The provider of the presence, such as <literal>CustomPresence</literal></para>
56                         </parameter>
57                         <parameter name="field" required="true">
58                           <para>Which field of the presence state information is wanted.</para>
59                           <optionlist>
60                                 <option name="value">
61                                   <para>The current presence, such as <literal>away</literal></para>
62                                 </option>
63                                 <option name="subtype">
64                                   <para>Further information about the current presence</para>
65                                 </option>
66                             <option name="message">
67                                   <para>A custom message that may indicate further details about the presence</para>
68                                 </option>
69                           </optionlist>
70                         </parameter>
71                         <parameter name="options" required="false">
72                           <optionlist>
73                             <option name="e">
74                                   <para>Base-64 encode the data.</para>
75                                 </option>
76                           </optionlist>
77                         </parameter>
78                 </syntax>
79                 <description>
80                         <para>The PRESENCE_STATE function can be used to retrieve the presence from any
81                         presence provider. For example:</para>
82                         <para>NoOp(SIP/mypeer has presence ${PRESENCE_STATE(SIP/mypeer,value)})</para>
83                         <para>NoOp(Conference number 1234 has presence message ${PRESENCE_STATE(MeetMe:1234,message)})</para>
84                         <para>The PRESENCE_STATE function can also be used to set custom presence state from
85                         the dialplan.  The <literal>CustomPresence:</literal> prefix must be used. For example:</para>
86                         <para>Set(PRESENCE_STATE(CustomPresence:lamp1)=away,temporary,Out to lunch)</para>
87                         <para>Set(PRESENCE_STATE(CustomPresence:lamp2)=dnd,,Trying to get work done)</para>
88                         <para>You can subscribe to the status of a custom presence state using a hint in
89                         the dialplan:</para>
90                         <para>exten => 1234,hint,CustomPresence:lamp1</para>
91                         <para>The possible values for both uses of this function are:</para>
92                         <para>not_set | unavailable | available | away | xa | chat | dnd</para>
93                 </description>
94         </function>
95  ***/
96
97
98 static const char astdb_family[] = "CustomPresence";
99
100 static int presence_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
101 {
102         int state;
103         char *message = NULL;
104         char *subtype = NULL;
105         char *parse;
106         int base64encode = 0;
107         AST_DECLARE_APP_ARGS(args,
108                 AST_APP_ARG(provider);
109                 AST_APP_ARG(field);
110                 AST_APP_ARG(options);
111         );
112
113         if (ast_strlen_zero(data)) {
114                 ast_log(LOG_WARNING, "PRESENCE_STATE reading requires an argument \n");
115                 return -1;
116         }
117
118         parse = ast_strdupa(data);
119
120         AST_STANDARD_APP_ARGS(args, parse);
121
122         if (ast_strlen_zero(args.provider) || ast_strlen_zero(args.field)) {
123                 ast_log(LOG_WARNING, "PRESENCE_STATE reading requires both presence provider and presence field arguments. \n");
124                 return -1;
125         }
126
127         state = ast_presence_state_nocache(args.provider, &subtype, &message);
128         if (state == AST_PRESENCE_INVALID) {
129                 ast_log(LOG_WARNING, "PRESENCE_STATE unknown \n");
130                 return -1;
131         }
132
133         if (!(ast_strlen_zero(args.options)) && (strchr(args.options, 'e'))) {
134                 base64encode = 1;
135         }
136
137         if (!ast_strlen_zero(subtype) && !strcasecmp(args.field, "subtype")) {
138                 if (base64encode) {
139                         ast_base64encode(buf, (unsigned char *) subtype, strlen(subtype), len);
140                 } else {
141                         ast_copy_string(buf, subtype, len);
142                 }
143         } else if (!ast_strlen_zero(message) && !strcasecmp(args.field, "message")) {
144                 if (base64encode) {
145                         ast_base64encode(buf, (unsigned char *) message, strlen(message), len);
146                 } else {
147                         ast_copy_string(buf, message, len);
148                 }
149
150         } else if (!strcasecmp(args.field, "value")) {
151                 ast_copy_string(buf, ast_presence_state2str(state), len);
152         }
153
154         ast_free(message);
155         ast_free(subtype);
156
157         return 0;
158 }
159
160 static int parse_data(char *data, enum ast_presence_state *state, char **subtype, char **message, char **options)
161 {
162         char *state_str;
163
164         /* data syntax is state,subtype,message,options */
165         *subtype = "";
166         *message = "";
167         *options = "";
168
169         state_str = strsep(&data, ",");
170         if (ast_strlen_zero(state_str)) {
171                 return -1; /* state is required */
172         }
173
174         *state = ast_presence_state_val(state_str);
175
176         /* not a valid state */
177         if (*state == AST_PRESENCE_INVALID) {
178                 ast_log(LOG_WARNING, "Unknown presence state value %s\n", state_str);
179                 return -1;
180         }
181
182         if (!(*subtype = strsep(&data,","))) {
183                 *subtype = "";
184                 return 0;
185         }
186
187         if (!(*message = strsep(&data, ","))) {
188                 *message = "";
189                 return 0;
190         }
191
192         if (!(*options = strsep(&data, ","))) {
193                 *options = "";
194                 return 0;
195         }
196
197         if (!ast_strlen_zero(*options) && !(strchr(*options, 'e'))) {
198                 ast_log(LOG_NOTICE, "Invalid options  '%s'\n", *options);
199                 return -1;
200         }
201
202         return 0;
203 }
204
205 static int presence_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
206 {
207         size_t len = strlen("CustomPresence:");
208         char *tmp = data;
209         char *args = ast_strdupa(value);
210         enum ast_presence_state state;
211         char *options, *message, *subtype;
212
213         if (strncasecmp(data, "CustomPresence:", len)) {
214                 ast_log(LOG_WARNING, "The PRESENCE_STATE function can only set CustomPresence: presence providers.\n");
215                 return -1;
216         }
217         data += len;
218         if (ast_strlen_zero(data)) {
219                 ast_log(LOG_WARNING, "PRESENCE_STATE function called with no custom device name!\n");
220                 return -1;
221         }
222
223         if (parse_data(args, &state, &subtype, &message, &options)) {
224                 ast_log(LOG_WARNING, "Invalid arguments to PRESENCE_STATE\n");
225                 return -1;
226         }
227
228         ast_db_put(astdb_family, data, value);
229
230         ast_presence_state_changed_literal(state, subtype, message, tmp);
231
232         return 0;
233 }
234
235 static enum ast_presence_state custom_presence_callback(const char *data, char **subtype, char **message)
236 {
237         char buf[1301] = "";
238         enum ast_presence_state state;
239         char *_options;
240         char *_message;
241         char *_subtype;
242
243         ast_db_get(astdb_family, data, buf, sizeof(buf));
244
245         if (parse_data(buf, &state, &_subtype, &_message, &_options)) {
246                 return AST_PRESENCE_INVALID;
247         }
248
249         if ((strchr(_options, 'e'))) {
250                 char tmp[1301];
251                 if (ast_strlen_zero(_subtype)) {
252                         *subtype = NULL;
253                 } else {
254                         memset(tmp, 0, sizeof(tmp));
255                         ast_base64decode((unsigned char *) tmp, _subtype, sizeof(tmp) - 1);
256                         *subtype = ast_strdup(tmp);
257                 }
258
259                 if (ast_strlen_zero(_message)) {
260                         *message = NULL;
261                 } else {
262                         memset(tmp, 0, sizeof(tmp));
263                         ast_base64decode((unsigned char *) tmp, _message, sizeof(tmp) - 1);
264                         *message = ast_strdup(tmp);
265                 }
266         } else {
267                 *subtype = ast_strlen_zero(_subtype) ? NULL : ast_strdup(_subtype);
268                 *message = ast_strlen_zero(_message) ? NULL : ast_strdup(_message);
269         }
270         return state;
271 }
272
273 static struct ast_custom_function presence_function = {
274         .name = "PRESENCE_STATE",
275         .read = presence_read,
276         .write = presence_write,
277 };
278
279 static char *handle_cli_presencestate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
280 {
281         struct ast_db_entry *db_entry, *db_tree;
282
283         switch (cmd) {
284         case CLI_INIT:
285                 e->command = "presencestate list";
286                 e->usage =
287                         "Usage: presencestate list\n"
288                         "       List all custom presence states that have been set by using\n"
289                         "       the PRESENCE_STATE dialplan function.\n";
290                 return NULL;
291         case CLI_GENERATE:
292                 return NULL;
293         }
294
295         if (a->argc != e->args) {
296                 return CLI_SHOWUSAGE;
297         }
298
299         ast_cli(a->fd, "\n"
300                 "---------------------------------------------------------------------\n"
301                 "--- Custom Presence States ------------------------------------------\n"
302                 "---------------------------------------------------------------------\n"
303                 "---\n");
304
305         db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
306         if (!db_entry) {
307                 ast_cli(a->fd, "No custom presence states defined\n");
308                 return CLI_SUCCESS;
309         }
310         for (; db_entry; db_entry = db_entry->next) {
311                 const char *object_name = strrchr(db_entry->key, '/') + 1;
312                 char state_info[1301];
313                 enum ast_presence_state state;
314                 char *subtype;
315                 char *message;
316                 char *options;
317
318                 ast_copy_string(state_info, db_entry->data, sizeof(state_info));
319                 if (parse_data(state_info, &state, &subtype, &message, &options)) {
320                         ast_log(LOG_WARNING, "Invalid CustomPresence entry %s encountered\n", db_entry->data);
321                         continue;
322                 }
323
324                 if (object_name <= (const char *) 1) {
325                         continue;
326                 }
327                 ast_cli(a->fd, "--- Name: 'CustomPresence:%s'\n"
328                                        "    --- State: '%s'\n"
329                                            "    --- Subtype: '%s'\n"
330                                            "    --- Message: '%s'\n"
331                                            "    --- Base64 Encoded: '%s'\n"
332                                "---\n",
333                                            object_name,
334                                            ast_presence_state2str(state),
335                                            subtype,
336                                            message,
337                                            AST_CLI_YESNO(strchr(options, 'e')));
338         }
339         ast_db_freetree(db_tree);
340         db_tree = NULL;
341
342         ast_cli(a->fd,
343                 "---------------------------------------------------------------------\n"
344                 "---------------------------------------------------------------------\n"
345                 "\n");
346
347         return CLI_SUCCESS;
348 }
349
350 static char *handle_cli_presencestate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
351 {
352     size_t len;
353         const char *dev, *state, *full_dev;
354         enum ast_presence_state state_val;
355         char *message;
356         char *subtype;
357         char *options;
358         char *args;
359
360         switch (cmd) {
361         case CLI_INIT:
362                 e->command = "presencestate change";
363                 e->usage =
364                         "Usage: presencestate change <entity> <state>[,<subtype>[,message[,options]]]\n"
365                         "       Change a custom presence to a new state.\n"
366                         "       The possible values for the state are:\n"
367                         "NOT_SET | UNAVAILABLE | AVAILABLE | AWAY | XA | CHAT | DND\n"
368                         "Optionally, a custom subtype and message may be provided, along with any options\n"
369                         "accepted by func_presencestate. If the subtype or message provided contain spaces,\n"
370                         "be sure to enclose the data in quotation marks (\"\")\n"
371                         "\n"
372                         "Examples:\n"
373                         "       presencestate change CustomPresence:mystate1 AWAY\n"
374                         "       presencestate change CustomPresence:mystate1 AVAILABLE\n"
375                         "       presencestate change CustomPresence:mystate1 \"Away,upstairs,eating lunch\"\n"
376                         "       \n";
377                 return NULL;
378         case CLI_GENERATE:
379         {
380                 static const char * const cmds[] = { "NOT_SET", "UNAVAILABLE", "AVAILABLE", "AWAY",
381                                                      "XA", "CHAT", "DND", NULL };
382
383                 if (a->pos == e->args + 1) {
384                         return ast_cli_complete(a->word, cmds, a->n);
385                 }
386
387                 return NULL;
388         }
389         }
390
391         if (a->argc != e->args + 2) {
392                 return CLI_SHOWUSAGE;
393         }
394
395         len = strlen("CustomPresence:");
396         full_dev = dev = a->argv[e->args];
397         state = a->argv[e->args + 1];
398
399         if (strncasecmp(dev, "CustomPresence:", len)) {
400                 ast_cli(a->fd, "The presencestate command can only be used to set 'CustomPresence:' presence state!\n");
401                 return CLI_FAILURE;
402         }
403
404         dev += len;
405         if (ast_strlen_zero(dev)) {
406                 return CLI_SHOWUSAGE;
407         }
408
409         args = ast_strdupa(state);
410         if (parse_data(args, &state_val, &subtype, &message, &options)) {
411                 return CLI_SHOWUSAGE;
412         }
413
414         if (state_val == AST_PRESENCE_NOT_SET) {
415                 return CLI_SHOWUSAGE;
416         }
417
418         ast_cli(a->fd, "Changing %s to %s\n", dev, args);
419
420         ast_db_put(astdb_family, dev, state);
421
422         ast_presence_state_changed_literal(state_val, subtype, message, full_dev);
423
424         return CLI_SUCCESS;
425 }
426
427 static struct ast_cli_entry cli_funcpresencestate[] = {
428         AST_CLI_DEFINE(handle_cli_presencestate_list, "List currently know custom presence states"),
429         AST_CLI_DEFINE(handle_cli_presencestate_change, "Change a custom presence state"),
430 };
431
432 #ifdef TEST_FRAMEWORK
433
434 struct test_string {
435         char *parse_string;
436         struct {
437                 int value;
438                 const char *subtype;
439                 const char *message;
440                 const char *options; 
441         } outputs;
442 };
443
444 AST_TEST_DEFINE(test_valid_parse_data)
445 {
446         int i;
447         enum ast_presence_state state;
448         char *subtype;
449         char *message;
450         char *options;
451         enum ast_test_result_state res = AST_TEST_PASS;
452         
453         struct test_string tests [] = {
454                 { "away",
455                         { AST_PRESENCE_AWAY,
456                                 "",
457                                 "",
458                                 ""
459                         }
460                 },
461                 { "not_set",
462                         { AST_PRESENCE_NOT_SET,
463                                 "",
464                                 "",
465                                 ""
466                         }
467                 },
468                 { "unavailable",
469                         { AST_PRESENCE_UNAVAILABLE,
470                                 "",
471                                 "",
472                                 ""
473                         }
474                 },
475                 { "available",
476                         { AST_PRESENCE_AVAILABLE,
477                                 "",
478                                 "",
479                                 ""
480                         }
481                 },
482                 { "xa",
483                         { AST_PRESENCE_XA,
484                                 "",
485                                 "",
486                                 ""
487                         }
488                 },
489                 { "chat",
490                         { AST_PRESENCE_CHAT,
491                                 "",
492                                 "",
493                                 ""
494                         }
495                 },
496                 { "dnd",
497                         { AST_PRESENCE_DND,
498                                 "",
499                                 "",
500                                 ""
501                         }
502                 },
503                 { "away,down the hall",
504                         { AST_PRESENCE_AWAY,
505                                 "down the hall",
506                                 "",
507                                 ""
508                         }
509                 },
510                 { "away,down the hall,Quarterly financial meeting",
511                         { AST_PRESENCE_AWAY,
512                                 "down the hall",
513                                 "Quarterly financial meeting",
514                                 ""
515                         }
516                 },
517                 { "away,,Quarterly financial meeting",
518                         { AST_PRESENCE_AWAY,
519                                 "",
520                                 "Quarterly financial meeting",
521                                 ""
522                         }
523                 },
524                 { "away,,,e",
525                         { AST_PRESENCE_AWAY,
526                                 "",
527                                 "",
528                                 "e",
529                         }
530                 },
531                 { "away,down the hall,,e",
532                         { AST_PRESENCE_AWAY,
533                                 "down the hall",
534                                 "",
535                                 "e"
536                         }
537                 },
538                 { "away,down the hall,Quarterly financial meeting,e",
539                         { AST_PRESENCE_AWAY,
540                                 "down the hall",
541                                 "Quarterly financial meeting",
542                                 "e"
543                         }
544                 },
545                 { "away,,Quarterly financial meeting,e",
546                         { AST_PRESENCE_AWAY,
547                                 "",
548                                 "Quarterly financial meeting",
549                                 "e"
550                         }
551                 }
552         };
553
554         switch (cmd) {
555         case TEST_INIT:
556                 info->name = "parse_valid_presence_data";
557                 info->category = "/funcs/func_presence/";
558                 info->summary = "PRESENCESTATE parsing test";
559                 info->description =
560                         "Ensure that parsing function accepts proper values, and gives proper outputs";
561                 return AST_TEST_NOT_RUN;
562         case TEST_EXECUTE:
563                 break;
564         }
565
566         for (i = 0; i < ARRAY_LEN(tests); ++i) {
567                 int parse_result;
568                 char *parse_string = ast_strdup(tests[i].parse_string);
569                 if (!parse_string) {
570                         res = AST_TEST_FAIL;
571                         break;
572                 }
573                 parse_result = parse_data(parse_string, &state, &subtype, &message, &options);
574                 if (parse_result == -1) {
575                         res = AST_TEST_FAIL;
576                         ast_free(parse_string);
577                         break;
578                 }
579                 if (tests[i].outputs.value != state ||
580                                 strcmp(tests[i].outputs.subtype, subtype) ||
581                                 strcmp(tests[i].outputs.message, message) ||
582                                 strcmp(tests[i].outputs.options, options)) {
583                         res = AST_TEST_FAIL;
584                         ast_free(parse_string);
585                         break;
586                 }
587                 ast_free(parse_string);
588         }
589
590         return res;
591 }
592
593 AST_TEST_DEFINE(test_invalid_parse_data)
594 {
595         int i;
596         enum ast_presence_state state;
597         char *subtype;
598         char *message;
599         char *options;
600         enum ast_test_result_state res = AST_TEST_PASS;
601
602         char *tests[] = {
603                 "",
604                 "bored",
605                 "away,,,i",
606                 /* XXX The following actually is parsed correctly. Should that
607                  * be changed?
608                  * "away,,,,e",
609                  */
610         };
611
612         switch (cmd) {
613         case TEST_INIT:
614                 info->name = "parse_invalid_presence_data";
615                 info->category = "/funcs/func_presence/";
616                 info->summary = "PRESENCESTATE parsing test";
617                 info->description =
618                         "Ensure that parsing function rejects improper values";
619                 return AST_TEST_NOT_RUN;
620         case TEST_EXECUTE:
621                 break;
622         }
623
624         for (i = 0; i < ARRAY_LEN(tests); ++i) {
625                 int parse_result;
626                 char *parse_string = ast_strdup(tests[i]);
627                 if (!parse_string) {
628                         res = AST_TEST_FAIL;
629                         break;
630                 }
631                 parse_result = parse_data(parse_string, &state, &subtype, &message, &options);
632                 if (parse_result == 0) {
633                         ast_log(LOG_WARNING, "Invalid string parsing failed on %s\n", tests[i]);
634                         res = AST_TEST_FAIL;
635                         ast_free(parse_string);
636                         break;
637                 }
638                 ast_free(parse_string);
639         }
640
641         return res;
642 }
643
644 struct test_cb_data {
645         enum ast_presence_state presence;
646         const char *provider;
647         const char *subtype;
648         const char *message;
649         /* That's right. I'm using a semaphore */
650         sem_t sem;
651 };
652
653 static void test_cb(const struct ast_event *event, void *userdata)
654 {
655         struct test_cb_data *cb_data = userdata;
656         cb_data->presence = ast_event_get_ie_uint(event, AST_EVENT_IE_PRESENCE_STATE);
657         cb_data->provider = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_PROVIDER));
658         cb_data->subtype = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_SUBTYPE));
659         cb_data->message = ast_strdup(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_MESSAGE));
660         sem_post(&cb_data->sem);
661 }
662
663 /* XXX This test could probably stand to be moved since
664  * it does not test func_presencestate but rather code in
665  * presencestate.h and presencestate.c. However, the convenience
666  * of presence_write() makes this a nice location for this test.
667  */
668 AST_TEST_DEFINE(test_presence_state_change)
669 {
670         struct ast_event_sub *test_sub;
671         struct test_cb_data *cb_data;
672
673         switch (cmd) {
674         case TEST_INIT:
675                 info->name = "test_presence_state_change";
676                 info->category = "/funcs/func_presence/";
677                 info->summary = "presence state change subscription";
678                 info->description =
679                         "Ensure that presence state changes are communicated to subscribers";
680                 return AST_TEST_NOT_RUN;
681         case TEST_EXECUTE:
682                 break;
683         }
684
685         cb_data = ast_calloc(1, sizeof(*cb_data));
686         if (!cb_data) {
687                 return AST_TEST_FAIL;
688         }
689
690         if (!(test_sub = ast_event_subscribe(AST_EVENT_PRESENCE_STATE,
691                         test_cb, "Test presence state callbacks", cb_data, AST_EVENT_IE_END))) {
692                 return AST_TEST_FAIL;
693         }
694
695         if (sem_init(&cb_data->sem, 0, 0)) {
696                 return AST_TEST_FAIL;
697         }
698
699         presence_write(NULL, "PRESENCESTATE", "CustomPresence:TestPresenceStateChange", "away,down the hall,Quarterly financial meeting");
700         sem_wait(&cb_data->sem);
701         if (cb_data->presence != AST_PRESENCE_AWAY ||
702                         strcmp(cb_data->provider, "CustomPresence:TestPresenceStateChange") ||
703                         strcmp(cb_data->subtype, "down the hall") ||
704                         strcmp(cb_data->message, "Quarterly financial meeting")) {
705                 return AST_TEST_FAIL;
706         }
707
708         ast_free((char *)cb_data->provider);
709         ast_free((char *)cb_data->subtype);
710         ast_free((char *)cb_data->message);
711         ast_free((char *)cb_data);
712
713         ast_db_del("CustomPresence", "TestPresenceStateChange");
714
715         return AST_TEST_PASS;
716 }
717
718 #endif
719
720 static int unload_module(void)
721 {
722         int res = 0;
723
724         res |= ast_custom_function_unregister(&presence_function);
725         res |= ast_presence_state_prov_del("CustomPresence");
726         res |= ast_cli_unregister_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate));
727 #ifdef TEST_FRAMEWORK
728         AST_TEST_UNREGISTER(test_valid_parse_data);
729         AST_TEST_UNREGISTER(test_invalid_parse_data);
730         AST_TEST_UNREGISTER(test_presence_state_change);
731 #endif
732         return res;
733 }
734
735 static int load_module(void)
736 {
737         int res = 0;
738         struct ast_db_entry *db_entry, *db_tree;
739
740         /* Populate the presence state cache on the system with all of the currently
741          * known custom presence states. */
742         db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
743         for (; db_entry; db_entry = db_entry->next) {
744                 const char *dev_name = strrchr(db_entry->key, '/') + 1;
745                 char state_info[1301];
746                 enum ast_presence_state state;
747                 char *message;
748                 char *subtype;
749                 char *options;
750                 if (dev_name <= (const char *) 1) {
751                         continue;
752                 }
753                 ast_copy_string(state_info, db_entry->data, sizeof(state_info));
754                 if (parse_data(state_info, &state, &subtype, &message, &options)) {
755                         ast_log(LOG_WARNING, "Invalid CustomPresence entry %s encountered\n", db_entry->data);
756                         continue;
757                 }
758                 ast_presence_state_changed(state, subtype, message, "CustomPresence:%s", dev_name);
759         }
760         ast_db_freetree(db_tree);
761         db_tree = NULL;
762
763         res |= ast_custom_function_register(&presence_function);
764         res |= ast_presence_state_prov_add("CustomPresence", custom_presence_callback);
765         res |= ast_cli_register_multiple(cli_funcpresencestate, ARRAY_LEN(cli_funcpresencestate));
766 #ifdef TEST_FRAMEWORK
767         AST_TEST_REGISTER(test_valid_parse_data);
768         AST_TEST_REGISTER(test_invalid_parse_data);
769         AST_TEST_REGISTER(test_presence_state_change);
770 #endif
771
772         return res;
773 }
774
775 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gets or sets a presence state in the dialplan",
776         .load = load_module,
777         .unload = unload_module,
778         .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
779 );
780