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