Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

censor.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/******************************************************************************
CENSOR.C     Profanity censor functions.

    Copyright 1993 - 2000 Paul J. Sidorsky

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

This module contains the profanity censor.
******************************************************************************/

#include "top.h"

/* load_censor_words() - Loads words to censor from CENSOR.CFG.
   Parameters:  None.
   Returns:  TRUE on success, FALSE if an error occurred.
*/
char load_censor_words(void)
    {
    FILE *fil; /* CENSOR.CFG file stream. */
    XINT d, count = 0; /* Loop counter, number of words loaded. */
    char loadstr[256]; /* Loading buffer. */

    numcensorwords = 0;

    /* Open the file. */
    sprintf(outbuf, "%scensor.cfg", cfg.toppath);
    fil = fopen(outbuf, "rt");
    if (fil == NULL)
        {
        return 0;
        }

    /* Count the number of non-comment lines in the file to get an
       approximate estimation of the number of words. */
    rewind(fil);
    while(!feof(fil))
        {
        fgets(loadstr, 256, fil);
        if (loadstr[0] != ';')
            {
            count++;
            }
        }

    /* Round up to the nearest multiple of three, then divide the counted
       number of lines by three, because there are three lines per word
       definition. */
    count += (count % 3);
    count /= 3;

    /* Allocate memory for the counted number of definition. */
    cwords = calloc(count, sizeof(censor_word_typ));
    if (cwords == NULL)
        {
        fclose(fil);
        return 0;
        }

    /* Load and process each line. */
    rewind(fil);
    for (d = 0; d < count; d++)
        {
        /* Keep reading until a non-comment line is found. */
        do
            {
            fgets(loadstr, 256, fil);
            }
        while(loadstr[0] == ';');

        /* Set the whole-word flag and level.  See CENSOR.CFG comments for
           more information. */
        if (toupper(loadstr[0]) == 'W')
            {
            cwords[d].wholeword = 1;
            cwords[d].level = atoi(&loadstr[1]);
            }
        else
            {
            cwords[d].wholeword = 0;
            cwords[d].level = atoi(loadstr);
            }

        /* Load the word to be censored. */
        fgets(loadstr, 256, fil);
        stripcr(loadstr);
        /* All censor text is dynamically allocated to save space. */
        cwords[d].word = malloc(strlen(loadstr) + 1);
        if (cwords[d].word == NULL)
            {
            fclose(fil);
            return 0;
            }
        strcpy(cwords[d].word, loadstr);

        /* Load the replacement text, if any. */
        fgets(loadstr, 256, fil);
        stripcr(loadstr);
        cwords[d].changeto = malloc(strlen(loadstr) + 1);
        if (cwords[d].changeto == NULL)
            {
            fclose(fil);
            return 0;
            }
        strcpy(cwords[d].changeto, loadstr);
        }

    fclose(fil);

    /* Set the global censor word counter. */
    numcensorwords = count;

    return 1;
    }

/* censorinput() - Censors a string of text.
   Parameters:  str - String containing text to be censored, and buffer to
                      hold the new, possibly modified string.
   Returns:  TRUE if the string should be blocked, FALSE if the string is
             permitted.
   Notes:  Changed text is not considered in the return code.  So long as
           the final string is displayable, even if the censor has changed
           the contents, it will return TRUE.  str must be at least
           MAXSTRLEN+1 characters long.
*/
char censorinput(char *str)
    {
    /* Counter, current character, current newstring character, current
       unfiltered string character, word replacement flag. */
    XINT d, p = 0, np = 0, fp = 0, flg;
    /* New string, filtered string. */
    char newstr[512], filtstr[512];

    /* The censor uses three strings.  filtstr contains a colour-code
       filtered copy of the string and is used for word detection.  newstr
       contains the new string including colour codes and modified text.
       str is used as the actual source.  The reason the filtered string
       cannot be used is because the colour codes still need to be
       displayed later. */

    /* Initialize the strings. */
    filter_string(filtstr, str);
    newstr[0] = '\0';
    
    /* Loop for each character in the filtered string.  The censor tests
       for offensive text beginning at every position in the string. */
    for (p = 0; p < strlen(filtstr); p++)
        {
        /* Loop through known offensive text. */
        for (d = 0, flg = 0; d < numcensorwords; d++)
            {
            /* Only test if this text is not longer than the remainder of
               the string to be tested. */
            if (strlen(&filtstr[p]) >= strlen(cwords[d].word))
                {
                /* Look for the text starting at the current position. */
                if (!strnicmp(&filtstr[p], cwords[d].word,
                              strlen(cwords[d].word)))
                    {
                    if (cwords[d].wholeword)
                        {
                        /* The criteria for a "whole word" is if there is
                           a whitespace character before and after the word.
                           The start and end of the string is considered
                           whitespace. */
                        if (p > 0)
                            {
                            if (isalpha(str[p - 1]))
                                {
                                /* An alpha character was found preceding
                                   the text, don't censor. */
                                continue;
                                }
                            }
                        if (isalpha(filtstr[p + strlen(cwords[d].word)]))
                            {
                            /* An alpha character was found after the
                               text, don't censor. */
                            continue;
                            }
                        }

                    /* If we've gotten to this point, the censor found a
                       match and needs to respond. */
                    switch(cwords[d].level)
                        {
                        /* Level 1:  Immediate disconnection. */
                        case 1:
                            top_output(OUT_SCREEN, getlang("CensorToss1"),
                                       cwords[d].word);
/* od_sleep() uses seconds in OS/2, milliseconds in DOS/Win32. */
#ifdef __OS2__
                            od_sleep(1);
#else
                            od_sleep(1000);
#endif
                            od_exit(10, TRUE);
                            /* This line is never reached but included for
                               completeness. */
                            return 1;
                        /* Level 2 - Warn, toss if applicable, don't allow. */
                        case 2:
                            /* Warn the user. */
                            top_output(OUT_SCREEN, getlang("CensorWarn2"),
                                       cwords[d].word);
                            /* Reset the high warning counter and timer. */
                            censorwarningshigh++;
                            lastcensorwarnhigh = time(NULL);
                            /* Do a warning check, toss if too many. */
                            if (censorwarningshigh >= cfg.maxcensorwarnhigh)
                                {
                                top_output(OUT_SCREEN, getlang("CensorToss2"),
                                           cwords[d].word);
#ifdef __OS2__
                                od_sleep(1);
#else
                                od_sleep(1000);
#endif
                                od_exit(10, TRUE);
                                }
                            /* Block the text. */
                            return 1;
                        /* Level 3 - Warn without tossing, don't allow. */
                        case 3:
                            /* Warn the user. */
                            top_output(OUT_SCREEN, getlang("CensorWarn3"),
                                       cwords[d].word);
                            /* Reset the high warning counter and timer. */
                            censorwarningshigh++;
                            lastcensorwarnhigh = time(NULL);
                            /* Block the text. */
                            return 1;
                        /* Level 4 - Warn, toss if applicable, allow. */
                        case 4:
                            /* Warn the user. */
                            top_output(OUT_SCREEN, getlang("CensorWarn4"),
                                       cwords[d].word);
                            /* Reset the low warning counter and timer. */
                            censorwarningslow++;
                            lastcensorwarnlow = time(NULL);
                            /* Do a warning check, toss if too many. */
                            if (censorwarningslow >= cfg.maxcensorwarnlow)
                                {
                                top_output(OUT_SCREEN, getlang("CensorToss4"),
                                           cwords[d].word);
#ifdef __OS2__
                                od_sleep(1);
#else
                                od_sleep(1000);
#endif
                                od_exit(10, TRUE);
                                }
                            /* The text will still be allowed. */
                            break;
                        /* Warn, don't toss, allow. */
                        case 5:
                            /* Warn the user. */
                            top_output(OUT_SCREEN, getlang("CensorWarn5"),
                                       cwords[d].word);
                            /* Reset the low warning counter and timer. */
                            censorwarningslow++;
                            lastcensorwarnlow = time(NULL);
                            /* The text will still be allowed. */
                            break;
                        }

                    /* Change the text if there is replacement text
                       available.  Note that this can occur even if one
                       of the actions above was not taken.  This is
                       known as level 6 of the censor (replace only). */
                    if (cwords[d].changeto[0])
                        {
                        XINT e; /* Word position counter. */

                        /* Flag that the text is being replaced. */
                        flg = 1;
                        /* Skip any colour codes that preceed the text to
                           be replaced.  The colour codes are still copied
                           into the new string. */
                        while(str[fp] == '^')
                            {
                            newstr[np++] = str[fp++];
                            newstr[np++] = str[fp++];
                            newstr[np] = '\0';
                            }
                        /* Append the replacement text and reset the
                           newstring pointer. */
                        strcat(newstr, cwords[d].changeto);
                        np = strlen(newstr);
                        /* Advance the copy pointer past the old text.  It
                           is assumed that the new text won't need to be
                           censored. */
                        p += (strlen(cwords[d].word) - 1);
                        /* Advance the unfiltered pointer past the old
                           text. */
                        for (e = 0; e < strlen(cwords[d].word); e++)
                            {
                            fp++;
                            }
                        /* Notify the user of the replacement. */
                        top_output(OUT_SCREEN, getlang("CensorReplace"));
                        /* Break out of the word-check loop to begin
                           testing the next position. */
                        break;
                        }
                    }
                }
            }

        /* Only copy the text if the text replacer didn't do it above. */
        if (!flg)
            {
            /* Skip any colour codes that preceed the text to
               be replaced.  The colour codes are still copied
               into the new string. */
            while(str[fp] == '^')
                {
                newstr[np++] = str[fp++];
                newstr[np++] = str[fp++];
                newstr[np] = '\0';
                }
            /* Copy this character into the new string. */
            newstr[np++] = str[fp++];
            newstr[np] = '\0';
            }
        }

    /* Copy the new string to the original buffer.  The string is truncated
       if it is longer than MAXSTRLEN because TOP's message routines can't
       handle larger strings. */
    memset(str, 0, MAXSTRLEN + 1);
    strncpy(str, newstr, MAXSTRLEN);

    /* The string was permitted. */
    return 0;
    }