『スタパライフ』エージェント Ver1.01 autostpl.r


   0:#include <awlib.h>
   1:
   2:char TGTFILE[260];
   3:#define BUFSIZEUNIT 8192
   4:#define BUFFER_SIZE 64*1024
   5:
   6:typedef struct _STRBUF {
   7:    char *text;
   8:    int bsize;
   9:    int esize;
  10:} STRBUF;
  11:
  12:typedef struct _MSGBUF {
  13:    struct _MSGBUF *next;
  14:    char *msgtext;
  15:} MSGBUF;
  16:
  17:MSGBUF *MSGSTACK = NULL;
  18:
  19:int getmessagedate(struct tm *trec,char *datestr) {
  20:    char tmp[40];    
  21:
  22:    strcpy(tmp,datestr);
  23:    /* 01234567890123456789012345678901 */
  24:    /* 2000/12/10 20:20 */
  25:    tmp[4]='\0';
  26:    trec->tm_year=atoi(tmp);
  27:    tmp[7]='\0';
  28:    trec->tm_mon=atoi(tmp+5);
  29:    tmp[10]='\0';
  30:    trec->tm_mday=atoi(tmp+8);
  31:    tmp[13]='\0';
  32:    trec->tm_hour=atoi(tmp+11);
  33:    tmp[16]='\0';
  34:    trec->tm_min=atoi(tmp+14);
  35:    trec->tm_sec=0;
  36:    return 0;
  37:}
  38:
  39:int storemessage(char *idstr,char *subject,char *date,char *text) {
  40:    MSGBUF *tmpmsg;
  41:    struct tm tmp;
  42:    char timestamp[128];
  43:    int result;
  44:    str tmptext;
  45:    
  46:    getmessagedate(&tmp,date);
  47:    strftime(timestamp,sizeof(timestamp),"%c",&tmp);
  48:
  49:    tmpmsg = (MSGBUF *)malloc(sizeof(MSGBUF));
  50:    tmptext = "From foo@bar " + timestamp + "\r\n";
  51:    tmptext = tmptext + "From: スタパ齋藤" + "\r\n";
  52:    tmptext = tmptext + "X-Number: " + idstr + "\r\n";
  53:    tmptext = tmptext + "Date: " + timestamp + "\r\n";
  54:    tmptext = tmptext + "Subject: " + subject + "\r\n";
  55:    tmptext = tmptext + "\r\n";
  56:    tmptext = tmptext + text;
  57:    tmptext = tmptext + "\r\n";
  58:    tmptext = tmptext + "\r\n";
  59:        
  60:    tmpmsg->msgtext = strcpy(malloc(strlen(tmptext)+1),tmptext);
  61:        
  62:    tmpmsg->next = MSGSTACK;
  63:    MSGSTACK = tmpmsg;
  64:    return result;
  65:}
  66:int checknewmessage(QDATA *qd,INET *inet,char *path,char *log_no) {
  67:    char idstr[128];
  68:    HTTP* http;
  69:    static char buffer[BUFFER_SIZE];
  70:    PAGE *page;
  71:    ELEMENTS *ea,*el;
  72:    int len,scan,index;
  73:    int result,count;
  74:    
  75:    strcpy(idstr,log_no);
  76:    if(qfind(qd,QFIND_NUMBER,atoi(idstr))==-1) {
  77:         // HTTP GET メソッドで / を取得する準備
  78:        http = http_open(inet, "GET", path, NULL, 0);
  79:        if (http != NULL) {
  80:            // HTTP のリクエストを実行(上の GET / )
  81:            len = http_request(http, NULL, 0, buffer, BUFFER_SIZE);
  82:            page=page_create(buffer,sizeof(buffer));
  83:            ea=page_elements(page);
  84:            el=page_find_elements(page,NULL,"TABLE");
  85:            for(scan=0;scancount;scan++) {
  86:                index=el->items[scan].number+1;
  87:                if((ea->items[index].tag=="TR") &&
  88:                    (ea->items[index+1].tag=="TD") &&
  89:                    (ea->items[index+2].tag=="") &&
  90:                    (ea->items[index+3].tag=="TD") &&
  91:                    (ea->items[index+4].tag=="") &&
  92:                    (ea->items[index+5].tag=="TR") &&
  93:                    (ea->items[index+6].tag=="TD") &&
  94:                    (ea->items[index+7].tag=="P") &&
  95:                    (ea->items[index+8].tag=="BR") ){
  96:                    count=storemessage(
  97:                        log_no,
  98:                        page_element_text(page,&ea->items[index+4]),
  99:                        page_element_text(page,&ea->items[index+2]),
 100:                        page_element_text(page,&ea->items[index+6]));
 101:                    result=1;
 102:                    break;
 103:                }
 104:            }
 105:            // HTTP をクローズ
 106:            page_free(page);
 107:            http_close(http);
 108:            // ステータス表示
 109:        }
 110:        else result=0;
 111:    }
 112:    else result=0;
 113:    return result;
 114:}
 115:
 116:int checkmessages(INET *inet,char *path,STRBUF *buf) {
 117:    QDATA *qd;
 118:
 119:    PAGE *page;
 120:    ELEMENTS *el;
 121:    ELEMENTS *ea;
 122:    int scan;
 123:    int index;
 124:    int result,count,sep;
 125:    char dirpath[128];
 126:
 127://メッセージのあるURLのパスを求める
 128:    sep=0;
 129:    while(strstr(path+sep,"/")>0){
 130:        sep=strstr(path+sep,"/")-path+1;
 131:    }
 132:    strncpy(dirpath,path,sep);
 133:    
 134:    result=0;
 135:    qd=qopen(TGTFILE);
 136:
 137:    page=page_create(buf->text,buf->esize);
 138:    ea=page_elements(page);
 139:    el=page_find_elements(page,NULL,"TABLE");
 140:
 141:    for(scan=0;scancount;scan++) {
 142:        index=el->items[scan].number+1;
 143:        if((index+5)>=ea->count) break;
 144:        if( (ea->items[index].tag=="TR")&&
 145:            (ea->items[index+1].tag=="TD")&&
 146:            (ea->items[index+2].tag=="FONT")&&
 147:            (ea->items[index+3].tag=="")&&
 148:            (ea->items[index+4].tag=="TD")&&
 149:            (ea->items[index+5].tag=="")){
 150:            count=checknewmessage(qd,inet,dirpath+page_element_att(page,&ea->items[index+7],"href"),
 151:                page_element_text(page,&ea->items[index+3]));
 152:            if(!count) break;
 153:            result += count;
 154:        }
 155:    }
 156:    page_free(page);
 157:    qclose(qd);
 158:    return result;
 159:}
 160:
 161:int getmessages(STRBUF *buf,INET *inet,char *path,int offset) {
 162:    HTTP *http;
 163:    int tmplen;
 164:    char tmp[80];
 165:    int result,count;
 166:
 167:    http=http_open(inet,"GET",path,NULL,0);
 168:    if (http) {
 169:        buf->bsize+=BUFSIZEUNIT;
 170:        buf->text=realloc(buf->text,buf->bsize+1);
 171:        sprintf(tmp,"ダウンロード中... %d メッセージ (%d bytes)",offset,buf->esize);
 172:        setstatustext(tmp);
 173:        tmplen=http_request(http,NULL,0,buf->text+buf->esize,BUFSIZEUNIT+1);
 174:        buf->esize+=tmplen;
 175:        while(tmplen==BUFSIZEUNIT) {
 176:            buf->bsize+=BUFSIZEUNIT;
 177:            buf->text=realloc(buf->text,buf->bsize+1);
 178:            tmplen=http_read(http,buf->text+buf->esize,BUFSIZEUNIT+1);
 179:            buf->esize+=tmplen;
 180:            sprintf(tmp,"ダウンロード中... %d メッセージ (%d bytes)",offset,buf->esize);
 181:            setstatustext(tmp);
 182:        }
 183:        buf->text[buf->esize]='\0';
 184:        sprintf(tmp,"ダウンロード中... %d メッセージ (%d bytes)",offset,buf->esize);
 185:        setstatustext(tmp);
 186:        http_close(http);
 187:    }
 188:    result = 0;
 189:    if(count = checkmessages(inet,path,buf)) {
 190:        if(strstr(buf->text,"<input type=submit value=\"次のページ\">")) result = offset + count;
 191:        else result = 0;
 192:    }
 193:    if(buf->text) {
 194:        free(buf->text);
 195:        buf->esize = 0;
 196:        buf->bsize = 0;
 197:        buf->text = NULL;
 198:    }
 199:    return result;
 200:}
 201:
 202:int main(int argc,char **argv) {
 203:    FILE *out;
 204:    char filename[260];
 205:    QDATA *qd;
 206:    MSGBUF *tmp;
 207:    INET *inet;
 208:    STRBUF buffer;
 209:    LOCATION loc;
 210:    char stat[80];
 211:    int ofs;
 212:    char *scan;
 213:    
 214:    char url[1024],mbox[1024];
 215:    LEGDATA *leg;
 216:    str path;
 217:    int autobrowseropen;
 218:    
 219:    path = getenv("DATA_DIR") + "\\stpl\\user.dat";
 220:    leg = leg_open(path, lmOpenRead);
 221:    if(leg) {
 222:        leg_readstring(leg, "/common/url", url, 1024, "");
 223:        leg_readstring(leg, "/common/mbox", mbox, 1024, "");
 224:        autobrowseropen=leg_readbool(leg, "/common/autobrowseropen", 0);
 225:    }
 226:    leg_close(leg);
 227:
 228:    strcpy(TGTFILE,getenv("DATA_DIR"));
 229:    strcpy(TGTFILE+strlen(TGTFILE),"\\");
 230:    strcpy(TGTFILE+strlen(TGTFILE),mbox);
 231:    memset(&loc,0,sizeof(LOCATION));
 232:    loc.size=sizeof(LOCATION);
 233:    decodeurl(&loc,url);
 234:    
 235:    inet=inet_open(NULL,loc.host,loc.port,INET_SERVICE_HTTP,NULL,NULL,0);
 236:    if(inet) {
 237:        memset(&buffer,0,sizeof(STRBUF));
 238:        ofs=0;
 239:        setstatustext("ダウンロード開始");
 240:        
 241:        do {
 242:            ofs = getmessages(&buffer,inet,loc.path,ofs);
 243:        } while(ofs && !terminated);
 244:    
 245:        ofs = 0;
 246:        if(MSGSTACK) {
 247:            strcpy(filename,TGTFILE);
 248:            scan=strrchr(filename,'.');
 249:            if(!scan) scan=strend(filename);
 250:            strcpy(scan,".g00");
 251:            out=fopen(filename,"a+");
 252:            while(MSGSTACK) {
 253:                ofs++;
 254:                fwrite(MSGSTACK->msgtext,1,strlen(MSGSTACK->msgtext),out);
 255:                tmp = MSGSTACK->next;
 256:                free(MSGSTACK->msgtext);
 257:                free(MSGSTACK);
 258:                MSGSTACK = tmp;
 259:            }
 260:            fclose(out);
 261:            qd=qopen(TGTFILE);
 262:            qupdate(qd);
 263:            qclose(qd);
 264:        }
 265:        sprintf(stat,"完了 (%d 件取得)",ofs);
 266:        setstatustext(stat);
 267:        inet_close(inet);
 268:
 269:        if((ofs>0)&&autobrowseropen)
 270:            spawnl(P_NOWAIT, "stpl\\openstpl.rx", "stpl\\openstpl.rx",NULL);
 271:
 272:    }
 273:    return 0;
 274:}