/[LeafOK_CVS]/lbbs/src/bwf.c
ViewVC logotype

Contents of /lbbs/src/bwf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Fri Nov 7 04:58:09 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.1: +2 -0 lines
Content type: text/x-csrc
Load / Reload BWF config file on startup / reload

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * bwf
4 * - bad words filter
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #define PCRE2_CODE_UNIT_WIDTH 8
10
11 #include "bwf.h"
12 #include "log.h"
13 #include <errno.h>
14 #include <pcre2.h>
15 #include <stdio.h>
16 #include <string.h>
17
18 enum _bwf_constant_t
19 {
20 BWF_max_pattern_len = 64,
21 BWF_max_pattern_cnt = 1000,
22 };
23
24 static char bwf_pattern_str[(BWF_max_pattern_len + 1) * BWF_max_pattern_cnt];
25 static pcre2_code *bwf_code = NULL;
26
27 int bwf_load(const char *filename)
28 {
29 FILE *fp;
30 char line[BWF_max_pattern_len + 1];
31 size_t len_line;
32 char *p = bwf_pattern_str;
33 int line_id = 0;
34 int errorcode;
35 PCRE2_SIZE erroroffset;
36
37 if (filename == NULL)
38 {
39 log_error("NULL pointer error\n");
40 return -1;
41 }
42
43 if ((fp = fopen(filename, "r")) == NULL)
44 {
45 log_error("fopen(%s) error: %d\n", filename, errno);
46 return -2;
47 }
48
49 while (fgets(line, sizeof(line), fp) != NULL)
50 {
51 len_line = strnlen(line, sizeof(line) - 1);
52 if (!feof(fp) && line[len_line - 1] != '\n')
53 {
54 log_error("Data line %d (len=%d) is truncated\n", line_id, len_line);
55 bwf_pattern_str[0] = '\0';
56 return -3;
57 }
58
59 if (line[len_line - 1] == '\n')
60 {
61 line[len_line - 1] = '\0';
62 }
63
64 if (p > bwf_pattern_str)
65 {
66 *p++ = '|';
67 }
68
69 if (len_line + 2 > sizeof(bwf_pattern_str) - 1 - (size_t)(p - bwf_pattern_str))
70 {
71 log_error("Data in %s exceed length limit %d\n", filename, sizeof(bwf_pattern_str) - 1);
72 bwf_pattern_str[0] = '\0';
73 return -3;
74 }
75
76 *p++ = '(';
77 p = mempcpy(p, line, strnlen(line, sizeof(bwf_pattern_str) - 1 - (size_t)(p - bwf_pattern_str)));
78 *p++ = ')';
79 line_id++;
80 }
81
82 *p = '\0';
83
84 fclose(fp);
85
86 #ifdef _DEBUG
87 log_error("Debug: bwf_pattern_str: %s\n", bwf_pattern_str);
88 #endif
89
90 bwf_unload();
91
92 bwf_code = pcre2_compile((PCRE2_SPTR)bwf_pattern_str, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errorcode, &erroroffset, NULL);
93 if (bwf_code == NULL)
94 {
95 log_error("pcre2_compile() error: %d", errorcode);
96 return -4;
97 }
98
99 return 0;
100 }
101
102 void bwf_unload(void)
103 {
104 if (bwf_code != NULL)
105 {
106 pcre2_code_free(bwf_code);
107 bwf_code = NULL;
108 }
109 }
110
111 int check_badwords(char *str, char c_mask)
112 {
113 pcre2_match_data *match_data;
114 PCRE2_SIZE *ovector;
115 int ret;
116 int i;
117
118 if (bwf_code == NULL)
119 {
120 log_error("BWF not loaded\n");
121 return -1;
122 }
123
124 match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);
125
126 ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL);
127 if (ret == PCRE2_ERROR_NOMATCH)
128 {
129 ret = 0;
130 }
131 else if (ret < 0)
132 {
133 log_error("pcre2_match() error: %d\n", ret);
134 }
135 else if (ret == 0)
136 {
137 log_error("Vector of offsets is too small\n");
138 }
139 else // ret >= 1
140 {
141 ovector = pcre2_get_ovector_pointer(match_data);
142 i = ret - 1;
143
144 if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1)
145 {
146 log_error("Bug: match pattern #%d with invalid offsets [%d, %d)",
147 i, ovector[i * 2], ovector[i * 2 + 1]);
148 ret = -2;
149 }
150 else
151 {
152 #ifdef _DEBUG
153 log_error("Debug: match pattern #%d at offsets [%d, %d]\n",
154 i, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);
155 #endif
156 memset(str + ovector[i * 2], c_mask, ovector[i * 2 + 1] - ovector[i * 2]);
157 }
158 }
159
160 pcre2_match_data_free(match_data);
161
162 return ret;
163 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1