荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: TurboZV.bbs@bbs.uestc.edu.cn (逝去的年代), 信区: InstallBBS
标  题: 一网深情BBS信息检测的简单实现方法
发信站: 一网深情 (Wed Nov 13 19:28:14 2002)
转信站: SZU!news.tiaozhan.com!news.happynet.org!UESTC

【目的】是过滤某些关键字,以便站务及时处理反动黄色文章。

【结构】很简单,bbsd每发一篇文章,便记录到sniffer.log中,每隔一段时
间便由一个程序来检测这些文章中是否包含某些指定的关键字

【关键字】为了便于关键字的增减,设定Sniffer版,里面的文摘文章的标题
就为关键字

【结果】发表在NetPolice版,标题格式: [userid][board][keyword]
内容为文章内容

废话少说,首先看bbs本身的修改(一网深情用的是自己修改过的FB2000v1219版):
bbs.c 1853行,加入:
#ifdef ENABLE_SNIFF
    /* 增加关键字监测功能 2002-10-18 by ZV*/
    if ((fp_log = fopen("sniff/sniffer.log", "a+")) != NULL) {
        fprintf(fp_log, "%s %s %s\n", currentuser.userid, currboard,
postfile.filename);
        fclose(fp_log);
    }
    /* end here */
#endif


mail.c 383行,修改为:
#ifdef ENABLE_SNIFF
        sprintf(mailfile, BBSHOME"/sniff/%s%s", currentuser.userid, time(NULL));
        rename(tmp_fname, mailfile);
        {
            char tmp[256];
            sprintf(tmp, "%s to %s", tmp_fname, mailfile);
            report(tmp);
        }
#else
        unlink(tmp_fname);
#endif
        sprintf(genbuf, "mailed %s", userid);
        report(genbuf);

#ifdef ENABLE_SNIFF
        /* 增加关键字监测功能 2002-10-18 by ZV*/
        if ((fp_log = fopen("sniff/sniffer.log", "a+")) != NULL) {
            fprintf(fp_log, "%s MAIL sniff/%s\n", currentuser.userid, mailfile);
            fclose(fp_log);
        }
        /* end here */
#endif

mail.c 431行,1471行,都是
在sprintf(genbuf, "mail/%c/%s/%s", toupper(userid[0]), userid, DOT_DIR);后
加入:
#ifdef ENABLE_SNIFF
        /* 增加关键字监测功能 2002-10-18 by ZV*/
        if ((fp_log = fopen("sniff/sniffer.log", "a+")) != NULL) {
            fprintf(fp_log, "%s MAIL mail/%c/%s/%s\n", currentuser.userid,
toupper(userid[0]), userid, newmessage.filename);
            fclose(fp_log);
        }
        /* end here */
#endif


呼~~~ Ok了,下面我们看看检测主程序check.c
就不多说了,很多地方需要自行修改。

为了简单实现,使用了bash的grep,cp
切记在Sniffer版中不要加入类似"|"的东东,Server出问题了就不好了:P

/*
 * 一网深情 BBS 信息检测程序 by ZV 2002-10-18
 *
 */
#include "/root/bbssrc/include/bbs.h"
#define LOGFILE "sniff/sniffer.log"
#define PROCESSFILE "sniff/sniffer.chk"
#define BOARD "NetPolice"
#define DOTDIR BBSHOME"/boards/"BOARD"/.DIR"
#define DIR BBSHOME"/boards/"BOARD
#define MAX_KEY 100
#define MAX_KEY_LEN 200
#define KEYBOARD "Sniffer"
#define KEYFILE BBSHOME"/boards/"KEYBOARD"/.DIR"

int FileSize(char *fname)
{
    struct stat filestat;
    stat(fname, &filestat);
    return filestat.st_size;
}

int isFileExsit(char *fname)
{
    FILE *fp;
    return ((fp = fopen(fname, "rb")) != NULL);
}

int main()
{
    FILE *fplog, *fpkey;
    char userid[STRLEN], board[STRLEN], filename[STRLEN], fname[STRLEN];
    char keys[MAX_KEY][MAX_KEY_LEN], cmd[255];
    int ikey = 0, i, ok;
    struct fileheader info;
    int timer;

    if (isFileExsit(PROCESSFILE)) {
        printf(PROCESSFILE" exists, exit.....\n");
        exit(-1);
    }

    if ((fpkey = fopen(KEYFILE, "r")) == NULL) {
        perror(KEYFILE);
        exit(-1);
    }

    while (!feof(fpkey)) {
        ok = fread(&info, sizeof(info), 1, fpkey);
        if (ok && (strlen(info.title)>1) && (info.accessed[0] & FILE_DIGEST)) {
            strcpy(keys[ikey], info.title);
            ikey++;
        }
    }
    fclose(fpkey);

    rename(LOGFILE, PROCESSFILE);
    if ((fplog = fopen(PROCESSFILE, "r")) == NULL) {
        perror(LOGFILE);
        exit(-1);
    }
    flock(fileno(fplog), LOCK_EX);

    timer = time(NULL);
    while (!feof(fplog)) {
        fscanf(fplog, "%s %s %s\n", userid, board, filename);
        if ((strlen(userid)>2) && (strcmp(BOARD, board)!=0) &&
                (strcmp(KEYBOARD, board)!=0) &&
                (strcmp("bbsadmin", board)!=0)) { /*加入一些屏蔽检测的版*/
            for (i=0; i<ikey; i++) {
                if (strcmp(board, "MAIL") == 0)
                    sprintf(fname, BBSHOME"/%s", filename);
                else
                    sprintf(fname, BBSHOME"/boards/%s/%s", board, filename);

                if (isFileExsit(fname)) {
                    sprintf(cmd, "grep '%s' %s>result", keys[i], fname);
                    printf(">%s\n", cmd);
                    system(cmd);
                    if (FileSize("result") > 1) { /* 有内容 */
                        int fd, t;
                        bzero(&info, sizeof(info));
                        strcpy(info.owner, "deliver");
                        sprintf(info.title, "[%s][%s][%s]", userid, board,
keys[i]);

                        sprintf(info.filename, "M.%d.A", timer++);
                        printf("title: %s\n", info.title);
                        printf("fname: %s\n", info.filename);
                        info.accessed[0] = FILE_NOREPLY;
                        /* write to .DIR */
                        if ((fd = open(DOTDIR, O_RDWR | O_CREAT, 0644)) != -1) {
                            flock(fd, LOCK_EX);
                            lseek(fd, 0, SEEK_END);
                            write(fd, &info, sizeof(info));
                            flock(fd, LOCK_UN);
                            close(fd);
                        }
                        /* copy content */
                        sprintf(cmd, "cp %s "DIR"/%s", fname, info.filename);
                        //printf(")%s\n", cmd);
                        system(cmd);
                        i = ikey; // 退出
                    }
                }
            }
        }
    }

    flock(fileno(fplog), LOCK_UN);
    fclose(fplog);
    unlink(PROCESSFILE);
    return 0;
}

--
 ╭════╮╭═╮╭═╮
 ╰═╮  ╗║║╔║║  ║   说风雨中这点痛算什么
 ╭═╯    ║║║║║  ║
 ║  ╭══╯║  ╰╯  ║       擦干泪不要怕 至少我们还有梦
 ║  ╰══╮╰╮    ╭╯       擦干泪不要问 为什么.......
 ╰════╯  ╰══╯
※ 来源:·一网深情 bbs.uestc.edu.cn·[FROM: 202.115.22.@_@]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店