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

Annotation of /lbbs/src/bwf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Fri Nov 7 04:51:06 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Content type: text/x-csrc
Add BWF - bad word filter

1 sysadm 1.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_code = pcre2_compile((PCRE2_SPTR)bwf_pattern_str, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errorcode, &erroroffset, NULL);
91     if (bwf_code == NULL)
92     {
93     log_error("pcre2_compile() error: %d", errorcode);
94     return -4;
95     }
96    
97     return 0;
98     }
99    
100     void bwf_unload(void)
101     {
102     if (bwf_code != NULL)
103     {
104     pcre2_code_free(bwf_code);
105     bwf_code = NULL;
106     }
107     }
108    
109     int check_badwords(char *str, char c_mask)
110     {
111     pcre2_match_data *match_data;
112     PCRE2_SIZE *ovector;
113     int ret;
114     int i;
115    
116     if (bwf_code == NULL)
117     {
118     log_error("BWF not loaded\n");
119     return -1;
120     }
121    
122     match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL);
123    
124     ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL);
125     if (ret == PCRE2_ERROR_NOMATCH)
126     {
127     ret = 0;
128     }
129     else if (ret < 0)
130     {
131     log_error("pcre2_match() error: %d\n", ret);
132     }
133     else if (ret == 0)
134     {
135     log_error("Vector of offsets is too small\n");
136     }
137     else // ret >= 1
138     {
139     ovector = pcre2_get_ovector_pointer(match_data);
140     i = ret - 1;
141    
142     if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1)
143     {
144     log_error("Bug: match pattern #%d with invalid offsets [%d, %d)",
145     i, ovector[i * 2], ovector[i * 2 + 1]);
146     ret = -2;
147     }
148     else
149     {
150     #ifdef _DEBUG
151     log_error("Debug: match pattern #%d at offsets [%d, %d]\n",
152     i, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]);
153     #endif
154     memset(str + ovector[i * 2], c_mask, ovector[i * 2 + 1] - ovector[i * 2]);
155     }
156     }
157    
158     pcre2_match_data_free(match_data);
159    
160     return ret;
161     }

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