when a PRI call must be moved to a different B channel at the request of the other...
[asterisk-bristuff.git] / main / privacy.c
blobb27bb5046bf840f02f8b317ec0ab54ec0ed439dc
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 <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/time.h>
34 #include <signal.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <dirent.h>
39 #include "asterisk/channel.h"
40 #include "asterisk/file.h"
41 #include "asterisk/app.h"
42 #include "asterisk/dsp.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/options.h"
45 #include "asterisk/astdb.h"
46 #include "asterisk/callerid.h"
47 #include "asterisk/privacy.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/lock.h"
51 int ast_privacy_check(char *dest, char *cid)
53 char tmp[256] = "";
54 char *trimcid = "";
55 char *n, *l;
56 int res;
57 char key[256], result[256];
58 if (cid)
59 ast_copy_string(tmp, cid, sizeof(tmp));
60 ast_callerid_parse(tmp, &n, &l);
61 if (l) {
62 ast_shrink_phone_number(l);
63 trimcid = l;
65 snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
66 res = ast_db_get("privacy", key, result, sizeof(result));
67 if (!res) {
68 if (!strcasecmp(result, "allow"))
69 return AST_PRIVACY_ALLOW;
70 if (!strcasecmp(result, "deny"))
71 return AST_PRIVACY_DENY;
72 if (!strcasecmp(result, "kill"))
73 return AST_PRIVACY_KILL;
74 if (!strcasecmp(result, "torture"))
75 return AST_PRIVACY_TORTURE;
77 return AST_PRIVACY_UNKNOWN;
80 int ast_privacy_reset(char *dest)
82 if (!dest)
83 return -1;
84 return ast_db_deltree("privacy", dest);
87 int ast_privacy_set(char *dest, char *cid, int status)
89 char tmp[256] = "";
90 char *trimcid = "";
91 char *n, *l;
92 int res;
93 char key[256];
94 if (cid)
95 ast_copy_string(tmp, cid, sizeof(tmp));
96 ast_callerid_parse(tmp, &n, &l);
97 if (l) {
98 ast_shrink_phone_number(l);
99 trimcid = l;
101 if (ast_strlen_zero(trimcid)) {
102 /* Don't store anything for empty Caller*ID */
103 return 0;
105 snprintf(key, sizeof(key), "%s/%s", dest, trimcid);
106 if (status == AST_PRIVACY_UNKNOWN)
107 res = ast_db_del("privacy", key);
108 else if (status == AST_PRIVACY_ALLOW)
109 res = ast_db_put("privacy", key, "allow");
110 else if (status == AST_PRIVACY_DENY)
111 res = ast_db_put("privacy", key, "deny");
112 else if (status == AST_PRIVACY_KILL)
113 res = ast_db_put("privacy", key, "kill");
114 else if (status == AST_PRIVACY_TORTURE)
115 res = ast_db_put("privacy", key, "torture");
116 else
117 res = -1;
118 return res;