bbslib.c

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

C
2,630
字号
        }        if (DEFINE(user, DEF_FRIENDMSG)) {            ui.pager |= FRIENDMSG_PAGER;        }        if (DEFINE(user, DEF_ALLMSG)) {            ui.pager |= ALLMSG_PAGER;            ui.pager |= FRIENDMSG_PAGER;        }        ui.uid = useridx;        strncpy(ui.from, fromhost, IPLEN);        ui.logintime = time(0); /* for counting user's stay time */        /*         * refer to bbsfoot.c for details          */        ui.freshtime = time(0);        ui.mode = WEBEXPLORE;        strncpy(ui.userid, user->userid, 20);        strncpy(ui.realname, ud.realname, 20);        strncpy(ui.username, user->username, 40);#ifdef HAVE_WFORUM        ui.yank = 1;#endif        utmpent = getnewutmpent2(&ui);        if (utmpent == -1)            ret = 1;        else {            struct user_info *u;            u = get_utmpent(utmpent);            u->pid = 1;            if (addto_msglist(utmpent, user->userid) < 0) {                bbslog("3system", "can't add msg:%d %s!!!\n", utmpent, user->userid);                *ppuinfo = u;                *putmpent = utmpent;                ret = 2;            } else {                *ppuinfo = u;                *putmpent = utmpent;                ret = 0;            }            getfriendstr(currentuser, u);            do_after_login(currentuser, utmpent, 0);        }    } else {        /*         * TODO:alloc guest table          */        int idx = www_new_guest_entry();        if (idx < 0)            ret = 5;        else {            int tmp = rand() % 100000000;            wwwguest_shm->guest_entry[idx].key = tmp;            wwwguest_shm->guest_entry[idx].logintime = time(0);            www_guest_uinfo.logintime = wwwguest_shm->guest_entry[idx].logintime;            wwwguest_shm->guest_entry[idx].freshtime = time(0);            www_guest_uinfo.freshtime = wwwguest_shm->guest_entry[idx].freshtime;            www_guest_uinfo.destuid = idx;            www_guest_uinfo.utmpkey = tmp;            *ppuinfo = &www_guest_uinfo;            *putmpent = idx;            getuser("guest", &currentuser);            ret = 0;            do_after_login(currentuser, idx, 1);        }    }    if ((ret == 0) || (ret == 2)) {        snprintf(buf, sizeof(buf), "ENTER ?@%s (ALLOC %d) [www]", fullfrom, *putmpent);        newbbslog(BBSLOG_USIES, "%s", buf);    }    return ret;}static void setflags(struct userec *u, int mask, int value){    if (((u->flags & mask) && 1) != value) {        if (value)            u->flags |= mask;        else            u->flags &= ~mask;    }}int www_user_logoff(struct userec *user, int useridx, struct user_info *puinfo, int userinfoidx){    int stay = 0;    struct userec *x = NULL;    stay = abs(time(0) - puinfo->logintime);    /*     * 上站时间超过 2 小时按 2 小时计      */    if (stay > 7200)        stay = 7200;    user->stay += stay;    record_exit_time(user->userid);    if (strcasecmp(user->userid, "guest")) {        newbbslog(BBSLOG_USIES, "EXIT: Stay:%3ld (%s)[%d %d](www)", stay / 60, user->username, get_curr_utmpent(), useridx);        if (!puinfo->active)            return 0;        setflags(user, PAGER_FLAG, (puinfo->pager & ALL_PAGER));        if ((HAS_PERM(user, PERM_CHATCLOAK) || HAS_PERM(user, PERM_CLOAK)))            setflags(user, CLOAK_FLAG, puinfo->invisible);        clear_utmp(userinfoidx, useridx, 1);    } else {        newbbslog(BBSLOG_USIES, "EXIT: Stay:%3ld (guest)[%d %d](www)", stay / 60, puinfo->destuid, useridx);        www_free_guest_entry(puinfo->destuid);    }    return 0;}time_t set_idle_time(struct user_info * uentp, time_t t){    if (strcasecmp(uentp->userid, "guest"))        uentp->freshtime = t;    else {        int idx;        idx = uentp->destuid;        if (idx >= 1 && idx < MAX_WWW_GUEST)            wwwguest_shm->guest_entry[uentp->destuid].freshtime = t;    }    return t;}int can_enter_chatroom(){    if (HAS_PERM(currentuser, PERM_CHAT))        return 1;    else        return 0;}int can_send_mail(){    if (HAS_PERM(currentuser, PERM_DENYMAIL))        return 0;    else if (HAS_PERM(currentuser, PERM_LOGINOK)) {        if (chkusermail(currentuser))            return 0;        return 1;    } else        return 0;}int can_reply_post(char *board, char *filename){    char dirpath[STRLEN];    struct fileheader fh;    int pos;    if ((!strcmp(board, "News")) || (!strcmp(board, "Original")))        return 0;    setbdir(0, dirpath, board);    pos = search_record(dirpath, &fh, sizeof(fh), (RECORD_FUNC_ARG) cmpname, filename);    if (pos <= 0)        return 0;    if (fh.accessed[1] & FILE_READ)        return 0;    else        return 1;}char bin2hex(int val){    int i;    i = val & 0x0F;    if (i >= 0 && i < 10)        return '0' + i;    else        return 'A' + (i - 10);}char *encode_url(char *buf, const char *str, size_t buflen){    int i, j;    int len;    unsigned char c;    int buflenm1;    len = strlen(str);    buf[buflen - 1] = '\0';    buflenm1 = buflen - 1;    for (i = 0, j = 0; i < len && j < buflenm1; i++) {        c = (unsigned char) str[i];        if (!isalnum(c)) {            buf[j++] = '%';            if (j < buflenm1)                buf[j++] = bin2hex((c >> 4) & 0x0F);            if (j < buflenm1)                buf[j++] = bin2hex(c & 0x0F);        } else {            buf[j] = str[i];            j++;        }    }    buf[j] = '\0';    return buf;}/* * buflen is a value-result variable. When it is passed to the function, * its value is the buffer length (including the trailing '\0' character). * When the function returned, its value is the number of characters  * actually copied to buf (excluding the trailing '\0' character).*/char *string_copy(char *buf, const char *str, size_t * buflen){    size_t i;    size_t len;    if (*buflen == 0)        return buf;    len = *buflen - 1;    for (i = 0; i < len; i++) {        if (str[i] == '\0') {            buf[i] = str[i];            break;        }        buf[i] = str[i];    }    *buflen = i;    buf[i] = '\0';    return buf;}char *encode_html(char *buf, const char *str, size_t buflen){    size_t i, j, k;    size_t len;    bzero(buf, buflen);    len = strlen(str);    for (i = 0, j = 0; i < len && j < buflen; i++) {        switch (str[i]) {        case '\"':            k = buflen - j;            string_copy(&buf[j], "&quot;", &k);            j += k;            break;        case '&':            k = buflen - j;            string_copy(&buf[j], "&amp;", &k);            j += k;            break;            /*             * case ' ':             * snprintf(&buf[j], buflen-j, "&nbsp;");             * j = strlen(buf);             * break;              */        case '>':            k = buflen - j;            string_copy(&buf[j], "&gt;", &k);            j += k;            break;        case '<':            k = buflen - j;            string_copy(&buf[j], "&lt;", &k);            j += k;            break;        default:            buf[j] = str[i];            j++;        }    }    buf[buflen - 1] = '\0';    return buf;}int is_BM(struct boardheader *board, struct userec *user){    char BM[STRLEN];    strncpy(BM, board->BM, sizeof(BM) - 1);    BM[sizeof(BM) - 1] = '\0';    return chk_currBM(BM, user);}int is_owner(struct fileheader *fh, struct userec *user){    if (!strcmp(fh->owner, user->userid))        return 1;    else        return 0;}int can_delete_post(struct boardheader *board, struct fileheader *fh, struct userec *user){    if (is_BM(board, user) || is_owner(fh, user))        return 1;    else        return 0;}int can_edit_post(struct boardheader *board, struct fileheader *fh, struct userec *user){    if (is_BM(board, user) || is_owner(fh, user))        return 1;    else        return 0;}int get_seccode_index(char prefix){    int i;    for (i = 0; i < SECNUM; i++) {        if (strchr(seccode[i], prefix) != NULL)            return i;    }    return -1;}char *http_encode_string(char *str, size_t len){    char *buf;    if (len == 0)        return NULL;    buf = (char *) malloc(len);    if (buf == NULL)        return NULL;    encode_url(buf, str, len);    strncpy(str, buf, len - 1);    str[len - 1] = '\0';    free(buf);    return str;}/* Convert string to Unix format */char *unix_string(char *str){    char *ptr1, *ptr2;    ptr1 = ptr2 = str;    while (*ptr1 != '\0') {        if (*ptr1 == '\r' && *(ptr1 + 1) == '\n') {            ptr1++;            continue;        }        if (ptr1 != ptr2)            *ptr2 = *ptr1;        ptr1++;        ptr2++;    }    *ptr2 = '\0';    return str;}static void print_font_style(unsigned int style, buffered_output_t * output){    char font_class[8];    char font_style[STRLEN];    char font_str[256];    unsigned int bg;    if (STYLE_ISSET(style, FONT_BG_SET)) {        bg = 8;    } else        bg = STYLE_GET_BG(style);    sprintf(font_class, "f%01d%02d", bg, STYLE_GET_FG(style));    font_style[0] = '\0';    if (STYLE_ISSET(style, FONT_STYLE_UL))        strcat(font_style, "text-decoration: underline; ");    if (STYLE_ISSET(style, FONT_STYLE_ITALIC))        strcat(font_style, "font-style: italic; ");    if (font_style[0] != '\0')        sprintf(font_str, "<font class=\"%s\" style=\"%s\">", font_class, font_style);    else        sprintf(font_str, "<font class=\"%s\">", font_class);    output->output(font_str, strlen(font_str), output);}static void html_output(char *buf, size_t buflen, buffered_output_t * output){    size_t i;    for (i = 0; i < buflen; i++) {        switch (buf[i]) {        case '&':            output->output("&amp;", 5, output);            break;        case '<':            output->output("&lt;", 4, output);            break;        case '>':            output->output("&gt;", 4, output);            break;        case ' ':            output->output("&nbsp;", 6, output);            break;        default:            output->output(&buf[i], 1, output);        }    }}static void print_raw_ansi(char *buf, size_t buflen, buffered_output_t * output){    size_t i;    for (i = 0; i < buflen; i++) {        if (buf[i] == 0x1b)            html_output("*", 1, output);        else if (buf[i] == '\n') {            output->output(" <br /> ", 8, output);        } else {            html_output(&buf[i], 1, output);        }    }}static void generate_font_style(unsigned int *style, unsigned int *ansi_val, size_t len){    size_t i;    unsigned int color;    for (i = 0; i < len; i++) {        if (ansi_val[i] == 0)            STYLE_ZERO(*style);        else if (ansi_val[i] == 1)            STYLE_SET(*style, FONT_FG_BOLD);        else if (ansi_val[i] == 4)            STYLE_SET(*style, FONT_STYLE_UL);        else if (ansi_val[i] == 5)            STYLE_SET(*style, FONT_STYLE_BLINK);        else if (ansi_val[i] >= 30 && ansi_val[i] <= 37) {            color = ansi_val[i] - 30;            STYLE_SET_FG(*style, color);        } else if (ansi_val[i] >= 40 && ansi_val[i] <= 47) {            /*             * user explicitly specify background color              */            /*             * STYLE_SET(*style, FONT_BG_SET);              */            color = ansi_val[i] - 40;            STYLE_SET_BG(*style, color);        }    }}void output_ansi_html(char *buf, size_t buflen, buffered_output_t * output, char *attachlink){    unsigned int font_style = 0;    unsigned int ansi_state;    unsigned int ansi_val[STRLEN];    int ival = 0;    size_t i;    char *ptr = buf;    char *ansi_begin;    char *ansi_end;    char *ubbstart_begin, *ubbmiddle_begin, *ubbfinish_begin;    int attachmatched;    char link[256];    long attachPos[MAXATTACHMENTCOUNT];    long attachLen[MAXATTACHMENTCOUNT];    char *attachFileName[MAXATTACHMENTCOUNT];    enum ATTACHMENTTYPE attachType[MAXATTACHMENTCOUNT];    int attachShowed[MAXATTACHMENTCOUNT];    char UBBCode[256];    int UBBCodeLen;    enum UBBTYPE UBBCodeType;    int isUBBMiddleOutput;    int UBBArg1, UBBArg2, UBBArg3;    char UBBStrArg[256];    if (ptr == NULL)        return;    STATE_ZERO(ansi_state);    bzero(ansi_val, sizeof(ansi_val));    bzero(attachShowed, sizeof(attachShowed));    attachmatched = 0;    for (i = 0; i < buflen; i++) {        long attach_len;        char *attachptr, *attachfilename;        if (attachmatched >= MAXATTACHMENTCOUNT) {            break;        }        if (attachlink && ((attachfilename = checkattach(buf + i, buflen - i, &attach_len, &attachptr)) != NULL)) {            char *extension;            int type;            extension = attachfilename + strlen(attachfilename);            i += (attachptr - buf - i) + attach_len - 1;            if (i > buflen)                continue;            attachPos[attachmatched] = attachfilename - buf;            attachLen[attachmatched] = attach_len;            attachFileName[attachmatched] = (char *) malloc(256);            strncpy(attachFileName[attachmatched], attachfilename, 255);            attachFileName[attachmatched][255] = 0;            attachType[attachmatched] = ATTACH_OTHERS;            extension--;            while ((*extension != '.') && (*extension != NULL))                extension--;            if (*extension == '.') {                extension++;                if (!strcasecmp(extension, "bmp") || !strcasecmp(extension, "jpg")                    || !strcasecmp(extension, "png") || !strcasecmp(extension, "jpeg")                    || !strcasecmp(extension, "pcx") || !strcasecmp(extension, "gif"))                    attachType[attachmatched] = ATTACH_IMG;                else if (!strcasecmp(extension, "swf"))                    attachType[attachmatched] = ATTACH_FLASH;            }            attachmatched++;        }    }    for (i = 0; i < buflen; i++) {        lo

⌨️ 快捷键说明

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