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:char NEXTPAGE[128];
8:
9:typedef struct _STRBUF {
10: char *text;
11: int bsize;
12: int esize;
13:} STRBUF;
14:
15:typedef struct _MSGBUF {
16: struct _MSGBUF *next;
17: char *msgtext;
18:} MSGBUF;
19:
20:MSGBUF *MSGSTACK = NULL;
21:
22:int getmessagedate(struct tm *trec,char *datestr) {
23: char tmp[40];
24:
25: strcpy(tmp,datestr);
26: /* 01234567890123456789012345678901 */
27: /* 2000/12/10 20:20 */
28: tmp[4]='\0';
29: trec->tm_year=atoi(tmp);
30: tmp[7]='\0';
31: trec->tm_mon=atoi(tmp+5);
32: tmp[10]='\0';
33: trec->tm_mday=atoi(tmp+8);
34: tmp[13]='\0';
35: trec->tm_hour=atoi(tmp+11);
36: tmp[16]='\0';
37: trec->tm_min=atoi(tmp+14);
38: trec->tm_sec=0;
39: return 0;
40:}
41:
42:int storemessage(char *idstr,char *subject,char *date,char *imgfile,char *text) {
43: MSGBUF *tmpmsg;
44: struct tm tmp;
45: char timestamp[128];
46: int result;
47: str tmptext;
48:
49: getmessagedate(&tmp,date);
50: strftime(timestamp,sizeof(timestamp),"%c",&tmp);
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(index+9>=ea->count) break;
94://log 12以降の形式
95: if((ea->items[index].tag=="TR") &&
96: (ea->items[index+1].tag=="TD") &&
97: (ea->items[index+2].tag=="") &&
98: (ea->items[index+3].tag=="TD") &&
99: (ea->items[index+4].tag=="") &&
100: (ea->items[index+5].tag=="TR") &&
101: (ea->items[index+6].tag=="TD") &&
102: (ea->items[index+7].tag=="P") &&
103: (ea->items[index+8].tag=="BR") ){
104://イメージファイルの保存
105: imagefile="";
106: if(COLLECTIONIMAGE){
107: temppath = getenv("DATA_DIR")+"\\stpl";
108: imagefile=page_element_att(page,&ea->items[index+9],"src");
109: if(fp = fopen(temppath+"\\"+imagefile, "r")) {
110: fclose(fp);
111: }
112: else{
113: img=download(basepath+imagefile,temppath);
114: }
115: }
116://ログの追記
117: count=storemessage(
118: log_no,
119: page_element_text(page,&ea->items[index+4]),
120: page_element_text(page,&ea->items[index+2]),
121: imagefile,
122: page_element_text(page,&ea->items[index+6]));
123: result=1;
124: break;
125: }
126://log 11以前の形式
127: else if((ea->items[index].tag=="TR") &&
128: (ea->items[index+1].tag=="TD") &&
129: (ea->items[index+2].tag=="") &&
130: (ea->items[index+3].tag=="TD") &&
131: (ea->items[index+4].tag=="") &&
132: (ea->items[index+5].tag=="TR") &&
133: (ea->items[index+6].tag=="TD") &&
134: (ea->items[index+7].tag=="P") &&
135: (ea->items[index+8].tag=="") &&
136: (ea->items[index+9].tag=="BR") ){
137://ログの追記
138: count=storemessage(
139: log_no,
140: page_element_text(page,&ea->items[index+4]),
141: page_element_text(page,&ea->items[index+2]),
142: "",
143: page_element_text(page,&ea->items[index+6]));
144: result=1;
145: break;
146: }
147:
148: }
149: // HTTP をクローズ
150: page_free(page);
151: http_close(http);
152: // ステータス表示
153: }
154: else result=0;
155: }
156: else result=0;
157: return result;
158:}
159:
160:int checkmessages(INET *inet,char *host,STRBUF *buf) {
161: QDATA *qd;
162:
163: PAGE *page;
164: ELEMENTS *el;
165: ELEMENTS *ea;
166: int scan;
167: int index;
168: int result,count,sep;
169: char dirpath[128];
170:
171://メッセージのあるURLのパスを求める
172: sep=0;
173: while(strstr(NEXTPAGE+sep,"/")>0){
174: sep=strstr(NEXTPAGE+sep,"/")-NEXTPAGE+1;
175: }
176: strncpy(dirpath,NEXTPAGE,sep);
177:
178://初期化処理
179: result=0;
180: qd=qopen(TGTFILE);
181:
182://ページ解析
183: page=page_create(buf->text,buf->esize);
184: ea=page_elements(page);
185:
186://前ページのアドレス取得
187: strcpy(NEXTPAGE,"");
188: if ((ea->items[11].tag=="A") &&
189: (page_element_text(page,&ea->items[11])=="前の20件"))
190: strcpy(NEXTPAGE,dirpath);
191: strcpy(NEXTPAGE+strlen(NEXTPAGE),page_element_att(page,&ea->items[11],"href"));
192:
193: el=page_find_elements(page,NULL,"TABLE");
194:
195: for(scan=0;scancount;scan++) {
196: index=el->items[scan].number+1;
197: if((index+5)>=ea->count) break;
198: if( (ea->items[index].tag=="TR")&&
199: (ea->items[index+1].tag=="TD")&&
200: (ea->items[index+2].tag=="FONT")&&
201: (ea->items[index+3].tag=="")&&
202: (ea->items[index+4].tag=="TD")&&
203: (ea->items[index+5].tag=="")){
204: count=checknewmessage(qd,inet,dirpath+page_element_att(page,&ea->items[index+7],"href"),
205: "http://"+host+dirpath,page_element_text(page,&ea->items[index+3]));
206: if(!count) break;
207: result += count;
208: }
209: }
210: page_free(page);
211: qclose(qd);
212: return result;
213:}
214:
215:int getmessages(STRBUF *buf,INET *inet,char *host,int offset) {
216: HTTP *http;
217: int tmplen;
218: char tmp[80];
219: int result,count;
220:
221: http=http_open(inet,"GET",NEXTPAGE,NULL,0);
222: if (http) {
223: buf->bsize+=BUFSIZEUNIT;
224: buf->text=realloc(buf->text,buf->bsize+1);
225: sprintf(tmp,"ダウンロード中... %d メッセージ (%d bytes)",offset,buf->esize);
226: setstatustext(tmp);
227: tmplen=http_request(http,NULL,0,buf->text+buf->esize,BUFSIZEUNIT+1);
228: buf->esize+=tmplen;
229: while(tmplen==BUFSIZEUNIT) {
230: buf->bsize+=BUFSIZEUNIT;
231: buf->text=realloc(buf->text,buf->bsize+1);
232: tmplen=http_read(http,buf->text+buf->esize,BUFSIZEUNIT+1);
233: buf->esize+=tmplen;
234: sprintf(tmp,"ダウンロード中... %d メッセージ (%d bytes)",offset,buf->esize);
235: setstatustext(tmp);
236: }
237: buf->text[buf->esize]='\0';
238: sprintf(tmp,"ダウンロード中... %d メッセージ (%d bytes)",offset,buf->esize);
239: setstatustext(tmp);
240: http_close(http);
241: }
242: result = 0;
243: if(count = checkmessages(inet,host,buf)) {
244: if(strlen(NEXTPAGE)>0) result = offset + count;
245: else result = 0;
246: }
247: if(buf->text) {
248: free(buf->text);
249: buf->esize = 0;
250: buf->bsize = 0;
251: buf->text = NULL;
252: }
253: return result;
254:}
255:
256:int main(int,char **) {
257: FILE *out;
258: char filename[260];
259: QDATA *qd;
260: MSGBUF *tmp;
261: INET *inet;
262: STRBUF buffer;
263: LOCATION loc;
264: char stat[80];
265: int ofs,i;
266: char *scan;
267:
268: char url[1024],mbox[1024],basepath[1024];
269: LEGDATA *leg;
270: str path;
271: int autobrowseropen,UseCustomBrowser;
272:
273: path = getenv("DATA_DIR") + "\\stpl\\user.dat";
274: leg = leg_open(path, lmOpenRead);
275: if(leg) {
276: leg_readstring(leg, "/common/url", url, 1024, "");
277: leg_readstring(leg, "/common/mbox", mbox, 1024, "");
278: autobrowseropen=leg_readbool(leg, "/common/autobrowseropen", 0);
279: UseCustomBrowser=leg_readbool(leg, "/common/UseCustomBrowser", 0);
280: COLLECTIONIMAGE=leg_readbool(leg, "/common/CollectionImage", 0);
281: }
282: leg_close(leg);
283: strcpy(TGTFILE,getenv("DATA_DIR"));
284: strcpy(TGTFILE+strlen(TGTFILE),"\\");
285: strcpy(TGTFILE+strlen(TGTFILE),mbox);
286: memset(&loc,0,sizeof(LOCATION));
287: loc.size=sizeof(LOCATION);
288: decodeurl(&loc,url);
289:
290: for (i=0;i<strlen(loc.path);i++){
291: if(strstr(url+i,"/")==0){
292: strncpy(basepath,loc.path,i);
293: break;
294: }
295: }
296:
297: inet=inet_open(NULL,loc.host,loc.port,INET_SERVICE_HTTP,NULL,NULL,0);
298: if(inet) {
299: memset(&buffer,0,sizeof(STRBUF));
300: ofs=0;
301: setstatustext("ダウンロード開始");
302: strcpy(NEXTPAGE,loc.path);
303:
304: do {
305: ofs = getmessages(&buffer,inet,loc.host,ofs);
306: } while(ofs && !terminated);
307:
308: ofs = 0;
309: if(MSGSTACK) {
310: strcpy(filename,TGTFILE);
311: scan=strrchr(filename,'.');
312: if(!scan) scan=strend(filename);
313: strcpy(scan,".g00");
314: out=fopen(filename,"a+");
315: while(MSGSTACK) {
316: ofs++;
317: fwrite(MSGSTACK->msgtext,1,strlen(MSGSTACK->msgtext),out);
318: tmp = MSGSTACK->next;
319: free(MSGSTACK->msgtext);
320: free(MSGSTACK);
321: MSGSTACK = tmp;
322: }
323: fclose(out);
324: qd=qopen(TGTFILE);
325: qupdate(qd);
326: qclose(qd);
327: }
328: sprintf(stat,"完了 (%d 件取得)",ofs);
329: setstatustext(stat);
330: inet_close(inet);
331:
332: if((ofs>0)&&autobrowseropen){
333: if(UseCustomBrowser) spawnl(P_NOWAIT, "stpl\\stplbrowser.pas", "stpl\\stplbrowser.pas","-H",url,"-T","スタパライフ",mbox,NULL);
334: else spawnl(P_NOWAIT, "browser\\unibrowser.pas", "browser\\unibrowser.pas","-H",url,"-T","スタパライフ",mbox,NULL);
335: }
336: }
337: return 0;
338:}
|