Annotation of sbbs/src/sbbs3/gtkchat/chatfuncs.c, revision 1.1

1.1     ! root        1: #include <sys/types.h>
        !             2: #include <sys/uio.h>
        !             3: #include <stdio.h>
        !             4: #include <stdlib.h>
        !             5: #include <fcntl.h>
        !             6: #include <string.h>
        !             7: #include <time.h>
        !             8: #include <utime.h>
        !             9: #include <unistd.h>
        !            10: 
        !            11: #include "sbbs.h"
        !            12: #include "chatfuncs.h"
        !            13: 
        !            14: #define PCHAT_LEN 1000
        !            15: 
        !            16: char                   usrname[128];
        !            17: 
        !            18: static node_t  node;
        !            19: static int             nodenum;
        !            20: static int             in,out;
        !            21: static char            inpath[MAX_PATH+1];
        !            22: static char            outpath[MAX_PATH+1];
        !            23: static scfg_t  cfg;
        !            24: 
        !            25: static int togglechat(int on)
        !            26: {
        !            27:     static int  org_act;
        !            28:        int nodefile;
        !            29: 
        !            30:        if(getnodedat(&cfg,nodenum,&node,&nodefile))
        !            31:                return(-1);
        !            32:     if(on) {
        !            33:         org_act=node.action;
        !            34:         if(org_act==NODE_PCHT)
        !            35:             org_act=NODE_MAIN;
        !            36:         node.misc|=NODE_LCHAT;
        !            37:     }
        !            38:        else {
        !            39:         node.action=org_act;
        !            40:         node.misc&=~NODE_LCHAT;
        !            41:     }
        !            42:        if(putnodedat(&cfg,nodenum,&node,nodefile))
        !            43:                return(-1);
        !            44:     return(0);
        !            45: }
        !            46: 
        !            47: int chat_open(int node_num, char *ctrl_dir)
        !            48: {
        !            49:        char    *p;
        !            50:        char    str[1024];
        !            51: 
        !            52:     /* Read .cfg files here */
        !            53:     memset(&cfg,0,sizeof(cfg));
        !            54:     cfg.size=sizeof(cfg);
        !            55:     SAFECOPY(cfg.ctrl_dir,ctrl_dir);
        !            56:     if(!load_cfg(&cfg, NULL, TRUE, str))
        !            57:         return(-1);
        !            58: 
        !            59:        nodenum=node_num;
        !            60: 
        !            61:        if(getnodedat(&cfg,nodenum,&node,NULL))
        !            62:                return(-1);
        !            63: 
        !            64:        username(&cfg,node.useron,usrname);
        !            65: 
        !            66:        sprintf(outpath,"%slchat.dab",cfg.node_path[nodenum-1]);
        !            67:        if((out=sopen(outpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
        !            68:                ,S_IREAD|S_IWRITE))==-1) {
        !            69:                return(-1);
        !            70:        }
        !            71: 
        !            72:        sprintf(inpath,"%schat.dab",cfg.node_path[nodenum-1]);
        !            73:        if((in=sopen(inpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
        !            74:                ,S_IREAD|S_IWRITE))==-1) {
        !            75:                close(out);
        !            76:                return(-1);
        !            77:     }
        !            78: 
        !            79:        if((p=(char *)malloc(PCHAT_LEN))==NULL) {
        !            80:                close(in);
        !            81:                close(out);
        !            82:                return(-1);
        !            83:     }
        !            84:        memset(p,0,PCHAT_LEN);
        !            85:        write(in,p,PCHAT_LEN);
        !            86:        write(out,p,PCHAT_LEN);
        !            87:        free(p);
        !            88:        lseek(in,0,SEEK_SET);
        !            89:        lseek(out,0,SEEK_SET);
        !            90: 
        !            91:        if(togglechat(TRUE))
        !            92:                return(-1);
        !            93:        return(0);
        !            94: }
        !            95: 
        !            96: int chat_check_remote(void)
        !            97: {
        !            98:        time_t                  now;
        !            99:        static time_t   last_nodechk=0;
        !           100: 
        !           101:        now=time(NULL);
        !           102:        if(now!=last_nodechk) {
        !           103:                if(getnodedat(&cfg,nodenum,&node,NULL)!=0)
        !           104:                        return(-1);                     /* Failed to read nodedat! */
        !           105:                last_nodechk=now;
        !           106:        }
        !           107:        if(node.misc&NODE_LCHAT)
        !           108:                return(1);                              /* Still Waiting */
        !           109: 
        !           110:        if(node.status==NODE_WFC || node.status>NODE_QUIET || node.action!=NODE_PCHT)
        !           111:                return(0);                              /* Remote has gone away */
        !           112: 
        !           113:        if(in==-1)
        !           114:                return(0);                              /* Remote has gone away */
        !           115: 
        !           116:        if(out==-1)
        !           117:                return(-1);                             /* Write error or some such */
        !           118: 
        !           119:        return(2);                                      /* Everything is good! */
        !           120: }
        !           121: 
        !           122: int chat_read_byte(void)
        !           123: {
        !           124:        unsigned char ch=0;
        !           125: 
        !           126:        if(in==-1)
        !           127:                return(-1);
        !           128:        utime(inpath,NULL);
        !           129:        switch(read(in,&ch,1)) {
        !           130:                case -1:
        !           131:                        close(in);
        !           132:                        in=-1;
        !           133:                        return(-1);
        !           134:                case 0:
        !           135:                        if(lseek(in,0,SEEK_SET)==-1);   /* Wrapped */
        !           136:                                return(-1);
        !           137:                        switch(read(in,&ch,1)) {
        !           138:                                case -1:
        !           139:                                        close(in);
        !           140:                                        in=-1;
        !           141:                                        return(-1);
        !           142:                        }
        !           143:                        /* Fall-through */
        !           144:                case 1:
        !           145:                        lseek(in,-1L,SEEK_CUR);
        !           146:                        if(ch) {
        !           147:                                write(in,"",1);
        !           148:                                return(ch);
        !           149:                        }
        !           150:        }
        !           151: 
        !           152:        return(0);
        !           153: }
        !           154: 
        !           155: int chat_write_byte(unsigned char ch)
        !           156: {
        !           157:        if(out==-1)
        !           158:                return(-1);
        !           159:        if(lseek(out,0,SEEK_CUR)>=PCHAT_LEN)
        !           160:                lseek(out,0,SEEK_SET);
        !           161:        switch(write(out,&ch,1)) {
        !           162:                case -1:
        !           163:                        close(out);
        !           164:                        out=-1;
        !           165:                        return(-1);
        !           166:        }
        !           167:        utime(outpath,NULL);
        !           168:        return(0);
        !           169: }
        !           170: 
        !           171: int chat_close(void)
        !           172: {
        !           173:        if(in != -1)
        !           174:                close(in);
        !           175:        if(out != -1)
        !           176:                close(out);
        !           177:        return(togglechat(FALSE));
        !           178: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.