Optionally display the value of several variables within the Status command.
[asterisk-bristuff.git] / main / privacy.c
blobdfb197f38e45fcf9e396d740e8158327b4fa3352
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Privacy Routines
23 * \author Mark Spencer <markster@digium.com>
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <sys/time.h>
31 #include <signal.h>
32 #include <dirent.h>
34 #include "asterisk/channel.h"
35 #include "asterisk/file.h"
36 #include "asterisk/app.h"
37 #include "asterisk/dsp.h"
38 #include "asterisk/astdb.h"
39 #include "asterisk/callerid.h"
40 #include "asterisk/privacy.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/lock.h"
44 int ast_privacy_check(char *dest, char *cid)
46 char tmp[256] = "";
47 char *trimcid = "";
48 char *n, *l;
49 int res;
50 char key[256], result[256];
51 if (cid)
52 ast_copy_string(tmp, cid, sizeof(tmp));
53 ast_callerid_parse(tmp, &n, &l);
54 if (l) {
55 ast_shrink_phone_number(l);
56 trimcid = l;
58 snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
59 res = ast_db_get("privacy", key, result, sizeof(result));
60 if (!res) {
61 if (!strcasecmp(result, "allow"))
62 return AST_PRIVACY_ALLOW;
63 if (!strcasecmp(result, "deny"))
64 return AST_PRIVACY_DENY;
65 if (!strcasecmp(result, "kill"))
66 return AST_PRIVACY_KILL;
67 if (!strcasecmp(result, "torture"))
68 return AST_PRIVACY_TORTURE;
70 return AST_PRIVACY_UNKNOWN;
73 int ast_privacy_reset(char *dest)
75 if (!dest)
76 return -1;
77 return ast_db_deltree("privacy", dest);
80 int ast_privacy_set(char *dest, char *cid, int status)
82 char tmp[256] = "";
83 char *trimcid = "";
84 char *n, *l;
85 int res;
86 char key[256];
87 if (cid)
88 ast_copy_string(tmp, cid, sizeof(tmp));
89 ast_callerid_parse(tmp, &n, &l);
90 if (l) {
91 ast_shrink_phone_number(l);
92 trimcid = l;
94 if (ast_strlen_zero(trimcid)) {
95 /* Don't store anything for empty Caller*ID */
96 return 0;
98 snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
99 if (status == AST_PRIVACY_UNKNOWN)
100 res = ast_db_del("privacy", key);
101 else if (status == AST_PRIVACY_ALLOW)
102 res = ast_db_put("privacy", key, "allow");
103 else if (status == AST_PRIVACY_DENY)
104 res = ast_db_put("privacy", key, "deny");
105 else if (status == AST_PRIVACY_KILL)
106 res = ast_db_put("privacy", key, "kill");
107 else if (status == AST_PRIVACY_TORTURE)
108 res = ast_db_put("privacy", key, "torture");
109 else
110 res = -1;
111 return res;