bbslib.c

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

C
2,630
字号
{    int count;    if ((count = apply_users(countusers, 0)) <= 0) {        return 0;    }    return count;}char *get_favboard(int k){    int i, j = 0;    for (i = 0; i < favbrd_list_t; i++)        if (favbrd_list[i].father == favnow) {            if (j == k) {                if (favbrd_list[i].flag == -1)                    return favbrd_list[i].title;                else {                    struct boardheader const *bptr;                    bptr = getboard(favbrd_list[i].flag + 1);                    return (char *) bptr->filename;                }            }            j++;        }    return NULL;}int get_favboard_id(int k){    int i, j = 0;    for (i = 0; i < favbrd_list_t; i++)        if (favbrd_list[i].father == favnow) {            if (j == k)                return i;            j++;        }    return NULL;}int get_favboard_type(int k){    int i, j = 0;    for (i = 0; i < favbrd_list_t; i++)        if (favbrd_list[i].father == favnow) {            if (j == k) {                if (favbrd_list[i].flag == -1)                    return 0;                else {                    return 1;                }            }            j++;        }    return 0;}int add_favboard(char *brdname){    int i;    if (brdname != NULL && *brdname)        i = getbnum(brdname);    else        return -3;              /* err brdname */    i--;    addFavBoard(i);}/* from bbsfadd.c *//*int addtooverride2(char *uident, char *exp){    friends_t tmp;    int  n;    char buf[STRLEN];    memset(&tmp,0,sizeof(tmp));    setuserfile( buf,currentuser->userid, "friends" );    if((!HAS_PERM(currentuser,PERM_ACCOUNTS) && !HAS_PERM(currentuser,PERM_SYSOP)) &&            (get_num_records(buf,sizeof(struct friends))>=MAXFRIENDS) )    {        return -1;    }    if( myfriend( searchuser(uident) , NULL) )        return -2;	if (exp == NULL || exp[0] == '\0')		tmp.exp[0] = '\0';	else	{		strncpy(tmp.exp, exp, sizeof(tmp.exp)-1);		tmp.exp[sizeof(tmp.exp)-1] = '\0';	}    n=append_record(buf,&tmp,sizeof(struct friends));    if(n!=-1)        getfriendstr();    else        return -3;    return n;}*//*int post_file(char *filename, postinfo_t *pi){	FILE *fp;	FILE *fp2;    struct fileheader pf;	char filepath[STRLEN];	char buf[STRLEN];	memset(&pf, 0, sizeof(pf));	if (get_unifile(buf, pi->board, 0) == -1)		return -1;	setbfile(filepath, pi->board, buf);	strcpy(pf.filename, buf);	strncpy(pf.owner, pi->userid, STRLEN);	strncpy(pf.title, pi->title, STRLEN);	pf.accessed[0] = pi->access & 0xFF;	pf.accessed[1] = (pi->access >> 8) & 0xFF;	fp = fopen(filepath, "w");	fp2 = fopen(filename, "r");		write_header2(fp, pi->board, pi->title, pi->userid, pi->username,		   	pi->anony);	write_file2(fp, fp2);	fclose(fp2);	fclose(fp);    if ( pi->local == 1 )	{		pf.filename[ STRLEN - 1 ] = 'L';		pf.filename[ STRLEN - 2 ] = 'L';	}    else	{		pf.filename[ STRLEN - 1 ] = 'S';		pf.filename[ STRLEN - 2 ] = 'S';		outgo_post2(&pf, pi->board, pi->userid, pi->username, pi->title);	}	setbfile(filepath, pi->board, DOT_DIR);	if (append_record(filepath, &pf, sizeof(pf)) == -1)   	{		sprintf(buf, "post file '%s' on '%s': append_record failed!",				pf.title, pi->board);		report(buf);		return -1;	}	sprintf(buf,"posted file '%s' on '%s'", pf.title, pi->board);	report(buf);	return 0;}*//* * get_unifile() 用于获得一个独一无二的文件. *     filename  文件名缓冲区 *     key       关键字, 可以是版名或者是用户名 *     mode      == 0, 表示 filename 是发表文章用的文件名 *                     此时 key 是版名 *               == 1, 表示 filename 是投递邮件用的文件名 *                     此时 key 是用户名 */int get_unifile(char *filename, char *key, int mode){    int fd;    char *ip;    char filepath[STRLEN];    int now;                    /* added for mail to SYSOP: Bigman 2000.8.11 */    now = time(NULL);    sprintf(filename, "M.%d.A", now);    if (mode == 0) {        if (getbcache(key) == NULL)            return -1;        setbfile(filepath, key, filename);    } else {        if (searchuser(key) == 0)            return -1;        setmailfile(filepath, key, filename);    }    ip = strrchr(filename, 'A');    while ((fd = open(filepath, O_CREAT | O_EXCL | O_WRONLY, 0644)) == -1) {        if (*ip == 'Z')            ip++, *ip = 'A', *(ip + 1) = '\0';        else            (*ip)++;        if (mode == 0)            setbfile(filepath, key, filename);        else            setmailfile(filepath, key, filename);    }    close(fd);    return 0;}/* 获得一个用户的在线用户数目 */int count_user_online(char *uid){    return apply_utmp(NULL, 0, uid, 0);}/* 获得当前用户的 utmp 号 */int get_curr_utmpent(){    return get_utmpent_num(u_info);}/* 以下的代码是cgi和php都使用的*/static struct user_info www_guest_uinfo;static int www_new_guest_entry(){    struct public_data *pub;    int fd, i, j;    time_t now;    struct userec *user;    fd = www_guest_lock();    if (fd == -1)        return -1;    setpublicshmreadonly(0);    pub = get_publicshm();    if (pub->www_guest_count >= MAX_WWW_GUEST) {        setpublicshmreadonly(1);        return -1;    }    user = currentuser;    getuser("guest", &currentuser);    if (currentuser == NULL)        return NULL;    now = time(NULL);    if ((now > wwwguest_shm->uptime + 240) || (now < wwwguest_shm->uptime - 240)) {        newbbslog(BBSLOG_USIES, "WWW guest:Clean guest table:%d", wwwguest_shm->uptime);        wwwguest_shm->uptime = now;        for (i = 0; i < MAX_WWW_GUEST; i++) {            struct user_info guestinfo;            if (!(wwwguest_shm->use_map[i / 32] & (1 << (i % 32))) || (now - wwwguest_shm->guest_entry[i].freshtime < MAX_WWW_GUEST_IDLE_TIME))                continue;            newbbslog(BBSLOG_USIES, "EXIT: Stay:%3ld (guest)[%d %d](www)", now - wwwguest_shm->guest_entry[i].freshtime, wwwguest_shm->guest_entry[i].key);            /*             * 清除use_map              */            guestinfo.currentboard = wwwguest_shm->guest_entry[i].currentboard;            do_after_logout(currentuser, &guestinfo, i, 1);            wwwguest_shm->use_map[i / 32] &= ~(1 << (i % 32));            if (pub->www_guest_count > 0) {                pub->www_guest_count--;                /*                 * 清除数据                  */                bzero(&wwwguest_shm->guest_entry[i], sizeof(struct WWW_GUEST_S));            }        }    }    for (i = 0; i < MAX_WWW_MAP_ITEM; i++)        if (wwwguest_shm->use_map[i] != 0xFFFFFFFF) {            int map = wwwguest_shm->use_map[i];            for (j = 0; j < 32; j++)                if ((map & 1) == 0) {                    wwwguest_shm->use_map[i] |= 1 << j;                    wwwguest_shm->guest_entry[i * 32 + j].freshtime = time(0);                    /*                     * 避免被kick下去                      */                    break;                } else                    map = map >> 1;            break;        }    if (i != MAX_WWW_MAP_ITEM) {        pub->www_guest_count++;        if (get_utmp_number() + getwwwguestcount() > get_publicshm()->max_user) {            save_maxuser();        }    }    currentuser = user;    setpublicshmreadonly(1);    www_guest_unlock(fd);    if (i == MAX_WWW_MAP_ITEM)        return -1;    return i * 32 + j;}struct WWW_GUEST_S *www_get_guest_entry(int idx){    return &wwwguest_shm->guest_entry[idx];}static int www_free_guest_entry(int idx){    int fd;    struct public_data *pub;    struct user_info guestinfo;    if ((idx < 0) || (idx > MAX_WWW_GUEST))        return -1;    guestinfo.currentboard = wwwguest_shm->guest_entry[idx].currentboard;    do_after_logout(currentuser, &guestinfo, idx, 1);    setpublicshmreadonly(0);    pub = get_publicshm();    fd = www_guest_lock();    if (wwwguest_shm->use_map[idx / 32] & (1 << (idx % 32))) {        wwwguest_shm->use_map[idx / 32] &= ~(1 << (idx % 32));        if (pub->www_guest_count > 0)            pub->www_guest_count--;    }    www_guest_unlock(fd);    setpublicshmreadonly(1);    return 0;}void www_data_detach(){    shmdt(wwwguest_shm);    wwwguest_shm = NULL;}int www_data_init(){    struct userec *guest;    /*     * www_guest_info目前先使用一个全局变量来做,这样     * 会导致线程不安全:P   但是对于进程模型的cgi 和php     * 足够了     */    bzero(&www_guest_uinfo, sizeof(www_guest_uinfo));    www_guest_uinfo.active = true;    www_guest_uinfo.uid = getuser("guest", &guest);    if (www_guest_uinfo.uid == 0)        return -1;    www_guest_uinfo.invisible = true;    www_guest_uinfo.pid = 1;    www_guest_uinfo.mode = WEBEXPLORE;    strcpy(www_guest_uinfo.username, guest->username);    strcpy(www_guest_uinfo.userid, guest->userid);    www_guest_uinfo.pager = 0;    {        struct userdata ud;        read_userdata(guest->userid, &ud);        strcpy(www_guest_uinfo.realname, ud.realname);    }    www_guest_uinfo.utmpkey = 0;    /*     * destuid 将被用来存放www guest表的入口      */    www_guest_uinfo.destuid = 0;    if (resolve_guest_table() != 0)        return -1;    return 0;}int www_user_init(int useridx, char *userid, int key, struct userec **x, struct user_info **y, long compat_telnet){    /*     * printf("utmpuserid = %s\n", id);     */    /*     * printf("utmpnum = %s\n", num);      */    if (userid && !strcasecmp(userid, "new"))        return -1;    if ((userid == NULL) || strcasecmp(userid, "guest")) {        /*         * 非guest在线用户处理          */        if (useridx < 1 || useridx >= MAXACTIVE) {            return -1;        }        (*y) = get_utmpent(useridx);        if (                    /*(strncmp((*y)->from, fromhost, IPLEN))|| */               ((*y)->utmpkey != key))            return -2;        if ((((*y)->active == 0)) || ((*y)->userid[0] == 0)            || ((compat_telnet == 0) && ((*y)->mode != WEBEXPLORE)))            return -3;        if (userid && strcmp((*y)->userid, userid))            return -4;        getuser((*y)->userid, x);        if (*x == 0)            return -5;#ifdef HAVE_BRC_CONTROL#if USE_TMPFS==1        init_brc_cache((*x)->userid, true);#endif#endif    } else {        /*         * guest用户处理          */        struct WWW_GUEST_S *guest_info;        if ((useridx < 0) || (useridx >= MAX_WWW_GUEST))            return -1;        guest_info = &wwwguest_shm->guest_entry[useridx];        if (guest_info->key != key) {            return -2;        }        strncpy(www_guest_uinfo.from, fromhost, IPLEN);        www_guest_uinfo.freshtime = guest_info->freshtime;        www_guest_uinfo.utmpkey = key;        www_guest_uinfo.destuid = useridx;        www_guest_uinfo.logintime = guest_info->logintime;        *y = &www_guest_uinfo;        getuser("guest", x);        if (*x == NULL)            return -1;    }    set_idle_time(*y, time(0));    return 0;}int www_user_login(struct userec *user, int useridx, int kick_multi, char *fromhost, char *fullfrom, struct user_info **ppuinfo, int *putmpent){    int ret;    char buf[255];    struct userdata ud;    char genbuf[STRLEN];    struct userec *uc;    if (user != NULL && strcasecmp(user->userid, "guest")) {        struct user_info ui;        int utmpent;        time_t t;        int multi_ret = 1;        int tmp;        while (multi_ret != 0) {            int lres;            int num;            struct user_info uin;            multi_ret = multilogin_user(user, useridx, 1);            if ((multi_ret != 0) && (!kick_multi))                return -1;            if (multi_ret == 0)                break;            if (!(num = search_ulist(&uin, cmpuids2, useridx)))                continue;       /* user isn't logged in */            if (uin.pid == 1) {                clear_utmp(num, useridx, uin.pid);                continue;            }            if (!uin.active || (kill(uin.pid, 0) == -1)) {                clear_utmp(num, useridx, uin.pid);                continue;       /* stale entry in utmp file */            }        /*---	modified by period	first try SIGHUP	2000-11-08	---*/            lres = kill(uin.pid, SIGHUP);            sleep(1);            if (lres)        /*---	---*/                kill(uin.pid, 9);            clear_utmp(num, useridx, uin.pid);        }        if (!HAS_PERM(user, PERM_BASIC))            return 3;        if (check_ban_IP(fromhost, buf) > 0)            return 4;        t = user->lastlogin;        if (abs(t - time(0)) < 5)            return 5;        user->lastlogin = time(0);        user->numlogins++;        strncpy(user->lasthost, fromhost, IPLEN);        user->lasthost[IPLEN - 1] = '\0';       /* add by binxun ,fix the bug */        read_userdata(user->userid, &ud);        user->userlevel &= (~PERM_SUICIDE);        if (!HAS_PERM(user, PERM_LOGINOK) && !HAS_PERM(user, PERM_SYSOP)) {            if (strchr(ud.realemail, '@')                && valid_ident(ud.realemail)) {                user->userlevel |= PERM_DEFAULT;                /*                 * if (HAS_PERM(user, PERM_DENYPOST)  )                 * user->userlevel &= ~PERM_POST;                 */            }#ifdef HAVE_TSINGHUA_INFO_REGISTER            else if (strlen(ud.realemail) >= 3) {                strncpy(genbuf, ud.realemail + strlen(ud.realemail) - 3, 3);                if (!strncasecmp(genbuf, "#TH", 3)) {                    getuser(user->userid, &uc);                    if (time(NULL) - uc->firstlogin >= REGISTER_TSINGHUA_WAIT_TIME)                        if (auto_register(user->userid, ud.realemail, STRLEN) == 0)                            user->userlevel |= PERM_DEFAULT;                }                read_userdata(user->userid, &ud);            }#endif        }        memset(&ui, 0, sizeof(struct user_info));        ui.active = true;        /*         * Bigman 2000.8.29 智囊团能够隐身         */        if ((HAS_PERM(user, PERM_CHATCLOAK)             || HAS_PERM(user, PERM_CLOAK))            && (user->flags & CLOAK_FLAG))            ui.invisible = true;        ui.pager = 0;        if (DEFINE(user, DEF_FRIENDCALL)) {            ui.pager |= FRIEND_PAGER;        }        if (user->flags & PAGER_FLAG) {            ui.pager |= ALL_PAGER;            ui.pager |= FRIEND_PAGER;

⌨️ 快捷键说明

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