bbslib.c

来自「bbs server linux平台下软件源码」· C语言 代码 · 共 2,630 行 · 第 1/5 页

C
2,630
字号
        return c - 'a' + 10;    if (c >= 'A' && c <= 'F')        return c - 'A' + 10;    if (c >= '0' && c <= '9')        return c - '0';    return 0;}int __unhcode(char *s){    int m, n;    for (m = 0, n = 0; s[m] != 0; m++, n++) {        if (s[m] == '+') {            s[n] = ' ';            continue;        }        if (s[m] == '%') {            s[n] = __to16(s[m + 1]) * 16 + __to16(s[m + 2]);            m += 2;            continue;        }        s[n] = s[m];    }    s[n] = 0;}char *getparm(char *var){    int n;    for (n = 0; n < parm_num; n++)        if (!strcasecmp(parm_name[n], var))            return parm_val[n];    return "";}/* smh_init() 需要修改 */int shm_init(){    resolve_ucache();    resolve_utmp();    resolve_boards();    www_data_init();}int user_init(struct userec **x, struct user_info **y){    char id[20], num[20];    int uid, key;    strsncpy(id, getparm("UTMPUSERID"), 13);    strsncpy(num, getparm("UTMPNUM"), 12);    /*     * printf("utmpuserid = %s\n", id);      */    /*     * printf("utmpnum = %s\n", num);      */    key = atoi(getparm("UTMPKEY"));    utmpent = atoi(num);    if (id[0] == '\0')        return -1;    if (www_user_init(utmpent, id, key, x, y, 0) == 0 && strcasecmp("guest", currentuser->userid))        return 1;    return 0;}int del_mail(int ent, struct fileheader *fh, char *direct){    char buf[PATHLEN];    char *t;    char genbuf[PATHLEN];    struct stat st;    if (strstr(direct, ".DELETED")) {        strcpy(buf, direct);        t = strrchr(buf, '/') + 1;        strcpy(t, fh->filename);        if (lstat(buf, &st) == 0 && S_ISREG(st.st_mode) && st.st_nlink == 1)            currentuser->usedspace -= st.st_size;    }    strcpy(buf, direct);    if ((t = strrchr(buf, '/')) != NULL)        *t = '\0';    if (!delete_record(direct, sizeof(*fh), ent, (RECORD_FUNC_ARG) cmpname, fh->filename)) {        sprintf(genbuf, "%s/%s", buf, fh->filename);        if (strstr(direct, ".DELETED"))            unlink(genbuf);        else {            strcpy(buf, direct);            t = strrchr(buf, '/') + 1;            strcpy(t, ".DELETED");            append_record(buf, fh, sizeof(*fh));        }        return 0;    }    return 1;}int post_mail(char *userid, char *title, char *file, char *id, char *nickname, char *ip, int sig){    FILE *fp, *fp2;    char buf3[256], dir[256];    struct fileheader header;    struct stat st;    struct userec *touser;      /*peregrine for updating used space */    int unum;    int t, i;    bzero(&header, sizeof(header));    strcpy(header.owner, id);    for (i = 0; i < 100; i++) {        t = time(0) + i;        sprintf(buf3, "mail/%c/%s/M.%d.A", toupper(userid[0]), userid, i + time(0));        if (!file_exist(buf3))            break;    }    if (i >= 99)        return -1;    if (false == canIsend2(currentuser, userid)) {        return -2;    }    unum = getuser(userid, &touser);    if (!HAS_PERM(currentuser, PERM_SYSOP) && chkusermail(touser)) {    /*Haohamru.99.4.05 */        return -3;    }    sprintf(header.filename, "M.%d.A", t);    strsncpy(header.title, title, 60);    fp = fopen(buf3, "w");    if (fp == NULL)        return -4;    fp2 = fopen(file, "r");    fprintf(fp, "寄信人: %s (%s)\n", id, nickname);    fprintf(fp, "标  题: %s\n", title);    fprintf(fp, "发信站: %s (%s)\n", BBSNAME, wwwCTime(time(0)));    fprintf(fp, "来  源: %s\n\n", ip);    if (fp2) {        while (fgets(buf3, 256, fp2) != NULL) {            fprintf2(fp, buf3);        }        fclose(fp2);    }    fprintf(fp, "\n--\n");    sig_append(fp, id, sig);    fprintf(fp, "\n[1;%dm※ 来源:.%s %s.[FROM: %.20s]\n", 31 + rand() % 7, BBSNAME, NAME_BBS_ENGLISH, ip);    fclose(fp);    sprintf(buf3, "mail/%c/%s/%s", toupper(userid[0]), userid, header.filename);        /*ft.buf3 have changed.added by binxun. */    if (stat(buf3, &st) != -1)        touser->usedspace += st.st_size;    sprintf(dir, "mail/%c/%s/.DIR", toupper(userid[0]), userid);    fp = fopen(dir, "a");    if (fp == NULL)        return -5;    fwrite(&header, sizeof(header), 1, fp);    fclose(fp);    setmailcheck(userid);    /*     * 添加Log Bigman: 2003.4.7      */    newbbslog(BBSLOG_USER, "mailed(www) %s %s", userid, title);    return 0;}int outgo_post2(struct fileheader *fh, char *board, char *userid, char *username, char *title){    FILE *foo;    if (foo = fopen("innd/out.bntp", "a")) {        fprintf(foo, "%s\t%s\t%s\t%s\t%s\n", board, fh->filename, userid, username, title);        fclose(foo);    }}void add_loginfo2(FILE * fp, char *board, struct userec *user, int anony){    FILE *fp2;    int color;    char fname[STRLEN];    color = (user->numlogins % 7) + 31; /* 颜色随机变化 */    sethomefile(fname, currentuser->userid, "signatures");    if ((fp2 = fopen(fname, "r")) == NULL ||    /* 判断是否已经 存在 签名档 */        user->signature == 0 || anony == 1) {        fputs("\n--\n", fp);    } else {                    /*Bigman 2000.8.10修改,减少代码 */        fprintf(fp, "\n");    }    /*     * 由Bigman增加:2000.8.10 Announce版匿名发文问题      */    if (!strcmp(board, "Announce"))        fprintf(fp, "[%2dm※ 来源:·%s http://%s·[FROM: %s]\n", color, BBS_FULL_NAME, BBS_FULL_NAME);    else        fprintf(fp, "\n[%2dm※ 来源:·%s http://%s·[FROM: %s]\n", color, BBS_FULL_NAME, NAME_BBS_ENGLISH, (anony) ? NAME_ANONYMOUS_FROM : user->lasthost);    if (fp2)        fclose(fp2);    return;}void write_header2(FILE * fp, char *board, char *title, char *userid, char *username, int anony){    if (!strcmp(board, "Announce"))        fprintf(fp, "发信人: %s (%s), 信区: %s\n", "SYSOP", NAME_SYSOP, board);    else        fprintf(fp, "发信人: %s (%s), 信区: %s\n", anony ? board : userid, anony ? NAME_ANONYMOUS : username, board);    fprintf(fp, "标  题: %s\n发信站: %s (%24.24s)\n\n", title, "BBS " NAME_BBS_CHINESE "站", Ctime(time(0)));}/* fp 		for destfile*//* fp2		for srcfile*/int write_file2(FILE * fp, FILE * fp2){    char buf3[1024];    char *ptr;    while (1) {        if (fgets(buf3, sizeof(buf3), fp2) == NULL)            break;        ptr = strrchr(buf3, '\r');        if (ptr != NULL) {            if (*(ptr + 1) == '\n') {                *ptr = '\n';                *(ptr + 1) = '\0';            }        }        fprintf2(fp, buf3);    }}/* return value:   >0		success   -1		write .DIR failed*/int post_article(char *board, char *title, char *file, struct userec *user, char *ip, int sig, int local_save, int anony, struct fileheader *oldx, char *attach_dir){    struct fileheader post_file;    char filepath[MAXPATH], fname[STRLEN];    char buf[256];    int fd, anonyboard;    int retvalue;    FILE *fp, *fp2;    memset(&post_file, 0, sizeof(post_file));    anonyboard = anonymousboard(board); /* 是否为匿名版 */    /*     * 自动生成 POST 文件名      */    setbfile(filepath, board, "");    if (GET_POSTFILENAME(post_file.filename, filepath) != 0) {        return -1;    }    setbfile(filepath, board, post_file.filename);    anony = anonyboard && anony;    strncpy(post_file.owner, anony ? board : getcurruserid(), OWNER_LEN);    post_file.owner[OWNER_LEN - 1] = 0;    if ((!strcmp(board, "Announce")) && (!strcmp(post_file.owner, board)))        strcpy(post_file.owner, "SYSOP");    fp = fopen(filepath, "w");    fp2 = fopen(file, "r");#ifndef RAW_ARTICLE    write_header2(fp, board, title, user->userid, user->username, anony);#endif    write_file2(fp, fp2);    fclose(fp2);    if (!anony)        addsignature(fp, user, sig);#ifndef RAW_ARTICLE    add_loginfo2(fp, board, user, anony);       /*添加最后一行 */#endif    strncpy(post_file.title, title, STRLEN);    if (local_save == 1) {      /* local save */        post_file.innflag[1] = 'L';        post_file.innflag[0] = 'L';    } else {        post_file.innflag[1] = 'S';        post_file.innflag[0] = 'S';        outgo_post2(&post_file, board, user->userid, user->username, title);    }    setbfile(buf, board, DOT_DIR);    /*     * 在boards版版主发文自动添加文章标记 Bigman:2000.8.12      */    if (!strcmp(board, "Board") && !HAS_PERM(currentuser, PERM_OBOARDS)        && HAS_PERM(currentuser, PERM_BOARDS)) {        post_file.accessed[0] |= FILE_SIGN;    }    if (attach_dir != NULL) {        char tmp[STRLEN];        struct stat st;        snprintf(tmp, MAXPATH, "%s/.index", attach_dir);        if (stat(tmp, &st) >= 0 && st.st_size > 0)            post_file.attachment = 1;    }    fclose(fp);    post_file.eff_size = get_effsize(filepath);    retvalue = after_post(currentuser, &post_file, board, oldx);    if (attach_dir != NULL) {        if ((fp = fopen(filepath, "a")) != NULL) {            snprintf(filepath, MAXPATH, "%s/.index", attach_dir);            if ((fp2 = fopen(filepath, "r")) != NULL) {                fputs("\n", fp);                while (!feof(fp2)) {                    char *name;                    long size, begin, save_size;                    char *ptr;                    fgets(buf, 256, fp2);                    name = strchr(buf, ' ');                    if (name == NULL)                        continue;                    *name = 0;                    name++;                    ptr = strchr(name, '\n');                    if (ptr)                        *ptr = 0;                    if (-1 == (fd = open(buf, O_RDONLY)))                        continue;                    if (post_file.attachment == 0) {                        /*                         * log the attachment begin                          */                        post_file.attachment = ftell(fp) + 1;                    }                    fwrite(ATTACHMENT_PAD, ATTACHMENT_SIZE, 1, fp);                    fwrite(name, strlen(name) + 1, 1, fp);                    BBS_TRY {                        if (safe_mmapfile_handle(fd, O_RDONLY, PROT_READ, MAP_SHARED, (void **) &ptr, (size_t *) & size) == 0) {                            size = 0;                            save_size = htonl(size);                            fwrite(&save_size, sizeof(save_size), 1, fp);                        } else {                            save_size = htonl(size);                            fwrite(&save_size, sizeof(save_size), 1, fp);                            begin = ftell(fp);                            fwrite(ptr, size, 1, fp);                        }                    }                    BBS_CATCH {                        ftruncate(fileno(fp), begin + size);                        fseek(fp, begin + size, SEEK_SET);                    }                    BBS_END end_mmapfile((void *) ptr, size, -1);                    close(fd);                }            }            fclose(fp);        }    }    if (retvalue == 0) {#ifdef WWW_GENERATE_STATIC        generate_static(DIR_MODE_NORMAL, &post_file, board, oldx);#endif    }    return post_file.id;}int sig_append(FILE * fp, char *id, int sig){    FILE *fp2;    char path[256];    char buf[256];    int i = 0, skip_lines = 0;    struct userec *x = NULL;    if (sig <= 0 || sig > MAX_SIGNATURES)        return -1;    getuser(id, &x);    if (x == 0)        return -1;    sethomefile(path, x->userid, "signatures");    fp2 = fopen(path, "r");    if (fp2 == NULL)        return -1;    /*     * 跳过前面的 (sig - 1)*6 行       */    do {        if (skip_lines == (sig - 1) * 6)            break;        skip_lines++;    } while ((fgets(buf, sizeof(buf), fp2)) != NULL);    /*     * 检查是否可以读入第 sig 个签名档      */    if (skip_lines == (sig - 1) * 6) {        /*         * 读入签名档并写入 fp 对应的文件中          */        for (i = skip_lines; i < skip_lines + 6; i++) {            if (fgets(buf, sizeof(buf), fp2) == NULL)                break;            unix_string(buf);            if (buf[0] != '\n')                fprintf(fp, "%s", buf);        }        /*         * 读入签名档成功,设置默认签名档为当前使用的签名档          */        if (i > skip_lines)            x->signature = sig;    }    fclose(fp2);    if (i > skip_lines)        return sig;    return 0;}int has_BM_perm(struct userec *user, char *board){    bcache_t *x;    char buf[256], *bm;    x = getbcache(board);    if (x == 0)        return 0;    if (user_perm(user, PERM_BLEVELS) || user_perm(user, PERM_SYSOP))        return 1;    if (!user_perm(user, PERM_BOARDS))        return 0;    strcpy(buf, x->BM);    bm = strtok(buf, ",: ;&()\n");    while (bm) {        if (!strcasecmp(bm, user->userid))            return 1;        bm = strtok(0, ",: ;&()\n");    }    return 0;}int count_mails(char *id, int *total, int *unread){    struct fileheader x1;    char buf[256];    int n;    FILE *fp;    *total = 0;    *unread = 0;    {        struct userec *x;        if (getuser(id, &x) == 0)            return 0;    }    sprintf(buf, "%s/mail/%c/%s/.DIR", BBSHOME, toupper(id[0]), id);    fp = fopen(buf, "r");    if (fp == 0)        return -1;    while (fread(&x1, sizeof(x1), 1, fp) > 0) {        (*total)++;        if (!(x1.accessed[0] & FILE_READ))            (*unread)++;    }    fclose(fp);    return 0;}int setmsgfile(char *buf, char *id){    if (buf == NULL || id == NULL)        return -1;    sethomefile(buf, id, SYS_MSGFILE);    return 0;}int setmsgfilelog(char *buf, char *id){    if (buf == NULL || id == NULL)        return -1;    sethomefile(buf, id, SYS_MSGFILELOG);    return 0;}extern char MsgDesUid[14];int send_msg(char *srcid, int srcutmp, char *destid, int destutmp, char *msg){    int i;    uinfo_t *uin;    /*     * 滤掉特殊字符,应该写成一个函数      */    for (i = 0; i < (int) strlen(msg); i++)        if ((0 < msg[i] && msg[i] <= 27) || msg[i] == -1)            msg[i] = 32;    if (destutmp == 0)        uin = t_search(destid, destutmp);    else        uin = get_utmpent(destutmp);    if (uin == NULL)        return -1;    if (strcasecmp(uin->userid, destid))        return -1;    strcpy(MsgDesUid, uin->userid);    return sendmsgfunc(uin, msg, 2);}int count_life_value(struct userec *urec){    int i;    i = (time(0) - urec->lastlogin) / 60;    if (urec->userlevel & PERM_SPECIAL8)        return (360 * 24 * 60 - i) / 1440;    if ((urec->userlevel & PERM_XEMPT) || !strcasecmp(urec->userid, "guest"))        return 999;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?