Example #1
0
static int
bbsnet_read(int fd, char *buf, int len)
{
    int rc;
    time_t now;

    rc = raw_read(fd, buf, len);
    if (rc > 0) {
        now = time(NULL);
        if (now - last_refresh > 60) {
            uinfo.freshtime = now;
            UPDATE_UTMP(freshtime, uinfo);
            last_refresh = now;
        }
    }
    return rc;
}
Example #2
0
File: chat.c Project: wyat/kbs
static int ent_chat(int chatnum)
{                               /* 进入聊天室 */
    chatcontext *pthis;
    char inbuf[128];
    int ch, cmdpos;
    int currchar;
    int modified;               /* the line is modified? -- wwj */
    int newmail;
    int page_pending = false;
    int chatting = true;
#ifdef NEW_HELP
    int oldhelpmode=helpmode;
#endif

    if (!strcmp(getCurrentUser()->userid, "guest"))
        return -1;
    pthis = (chatcontext *) malloc(sizeof(chatcontext));
    bzero(pthis, sizeof(chatcontext));
    if (!pthis)
        return -1;
    modify_user_mode(CHAT1);
    ch = ent_chat_conn(pthis, chatnum);
    if (ch != 1) {
        free(pthis);
        return ch;
    }
#ifdef NEW_HELP
    helpmode=HELP_CHAT;
#endif
    add_io(pthis->cfd, 0);
    modified = newmail = cmdpos = currchar = 0;
    /* update uinfo */
    uinfo.in_chat = true;
    strcpy(uinfo.chatid, pthis->chatid);
    UPDATE_UTMP(in_chat, uinfo);
    UPDATE_UTMP_STR(chatid, uinfo);
    /* initiate screen */
    clear();
    pthis->chatline = 2;
    move(s_lines, 0);
    outs(msg_seperator);
    move(1, 0);
    outs(msg_seperator);
    print_chatid(pthis);
    memset(inbuf, 0, 80);
    /* chat begin */
    while (chatting) {
        if (chat_checkparse(pthis) == 0)
            break;
        move(b_lines, currchar + 10);
        pthis->outputcount = 0;
        ch = igetkey();
        if (ch==KEY_TALK) {
            int talkpage = servicepage(0, pthis->buf);

            if (talkpage != page_pending) {
                bell();
                oflush();
                printchatline(pthis, pthis->buf);
                page_pending = talkpage;
            }
        }
        if (chat_checkparse(pthis) == 0)
            break;
        if (ch == I_OTHERDATA)
            continue;
        switch (ch) {
            case KEY_UP:
            case KEY_DOWN:
                if (cmdpos == pthis->cmdpos) {
                    strcpy(pthis->lastcmd[cmdpos], inbuf);
                    modified = 0;
                }
                if (ch == KEY_UP) {
                    if (cmdpos != (pthis->cmdpos + 1) % MAXLASTCMD) {
                        int i = (cmdpos + MAXLASTCMD - 1) % MAXLASTCMD;

                        if (pthis->lastcmd[i][0])
                            cmdpos = i;
                    }
                }
                if (ch == KEY_DOWN) {
                    if (cmdpos != pthis->cmdpos)
                        cmdpos = (cmdpos + 1) % MAXLASTCMD;
                }
                strcpy(inbuf, pthis->lastcmd[cmdpos]);
                if (cmdpos == pthis->cmdpos) {
                    modified = 1;
                }
                move(b_lines, 10);
                clrtoeol();
                ch = inbuf[69];
                inbuf[69] = 0;
                outs(inbuf);
                inbuf[69] = ch;
                currchar = strlen(inbuf);
                continue;
#ifdef CHINESE_CHARACTER
            case Ctrl('R'):
                SET_CHANGEDEFINE(getCurrentUser(), DEF_CHCHAR);
                continue;
#endif
            case KEY_LEFT:
                if (currchar)
                    --currchar;
#ifdef CHINESE_CHARACTER
                if (DEFINE(getCurrentUser(), DEF_CHCHAR)) {
                    int i,j=0;
                    for (i=0;i<currchar;i++)
                        if (j) j=0;
                        else if (inbuf[i]<0) j=1;
                    if (j) {
                        currchar--;
                    }
                }
#endif
                continue;
            case KEY_RIGHT:
                if (inbuf[currchar])
                    ++currchar;
#ifdef CHINESE_CHARACTER
                if (DEFINE(getCurrentUser(), DEF_CHCHAR)) {
                    int i,j=0;
                    for (i=0;i<currchar;i++)
                        if (j) j=0;
                        else if (inbuf[i]<0) j=1;
                    if (j) {
                        if (inbuf[currchar])
                            ++currchar;
                    }
                }
#endif
                continue;
            case KEY_ESC:
            case Ctrl('X'):
                inbuf[0] = 0;
                currchar = 0;
                move(b_lines, currchar + 10);
                clrtoeol();
                modified = 1;
                continue;
            case Ctrl('A'):
                currchar = 0;
                continue;
            case Ctrl('E'):
                currchar = strlen(inbuf);
                continue;
        }
        if (!newmail && chkmail(0)) {   /* check mail */
            newmail = 1;
            printchatline(pthis, "\033[32m*** \033[31m当!你有新信来啦...\033[m");
        }
        if (isprint2(ch)) {
            if (currchar < 126) {       /* 未满一行,print it */
                modified = 1;
                if (inbuf[currchar]) {  /* insert */
                    int i;

                    for (i = currchar; inbuf[i] && i < 127; i++);
                    inbuf[i + 1] = '\0';
                    for (; i > currchar; i--)
                        inbuf[i] = inbuf[i - 1];
                } else {        /* append */
                    inbuf[currchar + 1] = '\0';
                }
                inbuf[currchar] = ch;
                ch = inbuf[69]; /* save the end of line */
                inbuf[69] = 0;
                move(b_lines, currchar + 10);
                outs(&inbuf[currchar++]);
                inbuf[69] = ch;
            }
            continue;
        }
        if (ch == '\n' || ch == '\r') {
            if (currchar) {
                if (modified) {
                    /* add to command history */
                    ch = sizeof(pthis->lastcmd[pthis->cmdpos]) - 1;
                    strncpy(pthis->lastcmd[pthis->cmdpos], inbuf, ch);
                    pthis->lastcmd[pthis->cmdpos][ch] = 0;
                    pthis->cmdpos = (pthis->cmdpos + 1) % MAXLASTCMD;
                    cmdpos = pthis->cmdpos;
                } else {        /* use history, so can +1 */
                    cmdpos = (cmdpos + 1) % MAXLASTCMD;
                }
                if (inbuf[0] == '/' && Isspace(inbuf[1])) {     /* discard / b */
                    printchatline(pthis,
                                  "\x1b[37m*** \x1b[32m请输入正确的指令,使用/h寻求帮助\x1b[37m ***\x1b[m");
                } else {
                    chatting = chat_cmd(pthis, inbuf);  /*local命令处理 */
                    if (chatting == 0)
                        chatting = chat_send(pthis, inbuf);
                    if (inbuf[0] == '/') {
                        ch = 1;
                        while (inbuf[ch] != '\0' && inbuf[ch] != ' ')
                            ch++;
                        if (ch > 1) {
                            if (!strncasecmp(inbuf, "/bye", ch))
                                break;
                            if (!strncasecmp(inbuf, "/exit", ch))
                                break;  /*added by alex, 96.9.5 */
                        }
                    }
                }
                modified = 0;
                inbuf[0] = '\0';
                currchar = 0;
                move(b_lines, 10);
                clrtoeol();
            }
            continue;
        }
        if (ch == Ctrl('H') || ch == '\177') {  /*Backspace */
            if (currchar) {
                currchar--;
                inbuf[127] = '\0';
                memcpy(&inbuf[currchar], &inbuf[currchar + 1],
                       127 - currchar);
                move(b_lines, currchar + 10);
                clrtoeol();
                ch = inbuf[69]; /* save the end of line */
                inbuf[69] = 0;
                outs(&inbuf[currchar]);
                inbuf[69] = ch;
            }
#ifdef CHINESE_CHARACTER
            if (DEFINE(getCurrentUser(), DEF_CHCHAR)) {
                int i,j=0;
                for (i=0;i<currchar;i++)
                    if (j) j=0;
                    else if (inbuf[i]<0) j=1;
                if (j) {
                    currchar--;
                    inbuf[127] = '\0';
                    memcpy(&inbuf[currchar], &inbuf[currchar + 1],
                           127 - currchar);
                    move(b_lines, currchar + 10);
                    clrtoeol();
                    ch = inbuf[69]; /* save the end of line */
                    inbuf[69] = 0;
                    outs(&inbuf[currchar]);
                    inbuf[69] = ch;
                }
            }
#endif
            continue;
        }
        if (ch == Ctrl('Z')) {
            r_lastmsg();        /* Leeward 98.07.30 support msgX */
            inbuf[0] = '\0';
            currchar = 0;
            move(b_lines, 10);
            clrtoeol();
            continue;
        }
        if (ch == Ctrl('C') /*|| ch == Ctrl('D') */) {  /* ^C 退出 */
            chat_send(pthis, "/b");
            if (pthis->rec)
                set_rec(pthis,NULL);
            break;
        }
    }
    /* chat end */
    if (pthis->rec)set_rec(pthis,NULL);
    close(pthis->cfd);
    add_io(0, 0);
    uinfo.in_chat = false;
    uinfo.chatid[0] = '\0';
    UPDATE_UTMP(in_chat, uinfo);
    UPDATE_UTMP(chatid[0], uinfo);
    clear();
    free(pthis);
#ifdef NEW_HELP
    helpmode=oldhelpmode;
#endif
    return 0;
}
Example #3
0
File: newio.c Project: zhouqt/kbs
int igetch()
{
    time_t now;
    char c;
    int hasaddio = 1;
    extern int RMSG;


    if ((uinfo.mode == CHAT1 || uinfo.mode == TALK || uinfo.mode == PAGE) && RMSG == true)
        hasaddio = 0;

igetagain:
    if (ibufsize == icurrchar) {
        fd_set readfds, xds;
        struct timeval to;
        int sr, hifd;

        to.tv_sec = 0;
        to.tv_usec = 0;
        hifd = 1;
        FD_ZERO(&readfds);
        FD_SET(0, &readfds);
        if ((hasaddio && (i_newfd))&&(!inremsg)) {
            FD_SET(i_newfd, &readfds);
            if (hifd <= i_newfd)
                hifd = i_newfd + 1;
        }
        //TODO: igetkey重入问题
        if ((uinfo.mode != POSTING && uinfo.mode != SMAIL && uinfo.mode != EDIT) || DEFINE(getCurrentUser(), DEF_LOGININFORM))
            if (scrint&&!inremsg) {
                while (msg_count) {
                    inremsg = true;
                    msg_count--;
                    r_msg();
                    refresh();
                    inremsg = false;
                }
            }
        if (kicked) return KEY_TIMEOUT;
#ifdef SSHBBS
        sr = ssh_select(hifd, &readfds, NULL, NULL, &to);
#else
        sr = select(hifd, &readfds, NULL, NULL, &to);
#endif
        if (sr < 0 && errno == EINTR) {
            if (talkrequest)
                return KEY_TALK;
            if ((uinfo.mode != POSTING && uinfo.mode != SMAIL && uinfo.mode != EDIT) || DEFINE(getCurrentUser(), DEF_LOGININFORM))
                if (scrint&&!inremsg) {
                    while (msg_count) {
                        inremsg = true;
                        msg_count--;
                        r_msg();
                        inremsg = false;
                    }
                    goto igetagain;
                }
        }
        if (sr < 0 && errno != EINTR)
            abort_bbs(0);
        if (sr == 0) {
            refresh();
            if (flushf)
                (*flushf)();

            while (1) {
                int alarm_timeout;

                hifd = 1;
                FD_ZERO(&xds);
                FD_SET(0, &xds);
                FD_ZERO(&readfds);
                FD_SET(0, &readfds);
                if ((hasaddio && (i_newfd))&&(!inremsg)) {
                    FD_SET(i_newfd, &readfds);
                    if (hifd <= i_newfd)
                        hifd = i_newfd + 1;
                }
                alarm_timeout = 0;
                if (i_top)
                    to = *i_top;
                else {
                    while ((i_timeout != 0)||(i_timeoutusec!=0)) {
                        to.tv_sec = i_timeout - (time(0) - i_begintimeout);
                        to.tv_usec = i_timeoutusec;
                        if ((to.tv_sec < 0) ||((to.tv_sec==0)&&(i_timeoutusec==0))) {
                            i_timeout = 0;
                            i_timeoutusec=0;
                            if (i_timeout_func)
                                (*i_timeout_func)(timeout_data);
                            else
                                return KEY_TIMEOUT;
                            if (kicked) return KEY_TIMEOUT;
                            continue;
                        };
                        alarm_timeout = 1;
                        break;
                    };
                    if (!alarm_timeout)
                        to.tv_sec = IDLE_TIMEOUT;
                }
#ifdef SSHBBS
                sr = ssh_select(hifd, &readfds, NULL, &xds, &to);
#else
                sr = select(hifd, &readfds, NULL, &xds, &to);
#endif
                if (sr < 0 && errno == EINTR) {
                    if (talkrequest)
                        return KEY_TALK;
                }
                if (kicked) return KEY_TIMEOUT;
                if ((uinfo.mode != POSTING && uinfo.mode != SMAIL && uinfo.mode != EDIT) || DEFINE(getCurrentUser(), DEF_LOGININFORM))
                    if (!inremsg) {
                        int saveerrno=errno;
                        while (msg_count) {
                            inremsg = true;
                            msg_count--;
                            r_msg();
                            refresh();
                            inremsg = false;
                        }
                        if (sr<0&&saveerrno==EINTR)continue;
                    }
                if (sr == 0 && alarm_timeout) {
                    i_timeout = 0;
                    i_timeoutusec=0;
                    if (i_timeout_func)
                        (*i_timeout_func)(timeout_data);
                    else
                        return KEY_TIMEOUT;
                    continue;
                }
                if (sr >= 0)
                    break;
                if (errno == EINTR)
                    continue;
                else
                    abort_bbs(0);

            }
            if ((sr == 0) && (!i_top))
                abort_bbs(0);
            if (sr == 0)
                return I_TIMEOUT;
            if (FD_ISSET(0, &xds))
                abort_bbs(0);
        }
        if (hasaddio && (i_newfd && FD_ISSET(i_newfd, &readfds)))
            return I_OTHERDATA;
#ifdef SSHBBS
        while ((ibufsize = ssh_read(0, inbuffer + 1, IBUFSIZE)) <= 0)
#else
        while ((ibufsize = read(0, inbuffer + 1, IBUFSIZE)) <= 0)
#endif
        {
            if (ibufsize == 0)
                longjmp(byebye, -1);
            if (ibufsize < 0 && errno != EINTR)
                longjmp(byebye, -1);
        }
        if (!filter_telnet(inbuffer + 1, &ibufsize)) {
            icurrchar = 0;
            ibufsize = 0;
            goto igetagain;
        }

        /*
         * add by KCN for GB/BIG5 encode
         */
        if (convcode) {
            inbuf = big2gb(inbuffer + 1, &ibufsize, 0, getSession());
            if (ibufsize == 0) {
                icurrchar = 0;
                goto igetagain;
            }
        } else
            inbuf = inbuffer + 1;
        /*
         * end
         */
        icurrchar = 0;
        if (ibufsize > IBUFSIZE) {
            ibufsize = 0;
            goto igetagain;
        }
    }

    if (icurrchar >= ibufsize) {
        ibufsize = icurrchar;
        goto igetagain;
    }
    if (((inbuf[icurrchar] == '\n') && (lastch == '\r'))
            || ((inbuf[icurrchar] == '\r') && (lastch == '\n'))) {
        lastch = 0;
        goto igetagain;
    }

    else if (icurrchar != ibufsize) {
        if (((inbuf[icurrchar] == '\n') && (inbuf[icurrchar + 1] == '\r'))
                || ((inbuf[icurrchar] == '\r') && (inbuf[icurrchar + 1] == '\n'))) {
            icurrchar++;
            lastch = 0;
        }
    } else
        lastch = inbuf[icurrchar];

    idle_count = 0;
    c = inbuf[icurrchar];

    switch (c) {
        case Ctrl('@'):
        case Ctrl('L'):
            redoscr();
            icurrchar++;
            now = time(0);
            uinfo.freshtime = now;
            if (now - old > 60) {
                UPDATE_UTMP(freshtime, uinfo);
                old = now;
            }
            goto igetagain;
        case Ctrl('Z'):
            if (scrint&&uinfo.mode!=LOCKSCREEN&&uinfo.mode!=NEW&&uinfo.mode!=LOGIN&&uinfo.mode!=BBSNET &&uinfo.mode!=WINMINE&&!inremsg) {
                icurrchar++;
                inremsg = true;
                r_msg();
                inremsg = false;
                goto igetagain;
            }
            break;
        default:
            break;
    }
    icurrchar++;
    while ((icurrchar != ibufsize) && (inbuf[icurrchar] == 0))
        icurrchar++;
    now = time(0);
    if (Ctrl('T') != c)
        uinfo.freshtime = now;
    /*
     * add by KCN , decrease temp_numposts
     */
    if (lasttime + 60 * 60 * 8 < now) {
        lasttime = now;
        if (temp_numposts > 0)
            temp_numposts--;
    }
    if (now - old > 60) {
        UPDATE_UTMP(freshtime, uinfo);
        old = now;
    }
    return c;
}