int makefileheader(char *filepath, char *filename, struct fileheader *fh) { struct stat st; char buf1[256], buf2[256], *p; int found = 0; FILE *art; bzero(fh, sizeof (*fh)); if (stat(filepath, &st)) return -1; if ((art = fopen(filepath, "r")) == NULL) return -1; bzero(fh, sizeof (*fh)); if (!fgets(buf1, 256, art)) goto FAIL; p = strchr(buf1 + 8, ' '); if (p) *p = 0; if ((p = strchr(buf1 + 8, '('))) *p = 0; if ((p = strchr(buf1 + 8, '\n'))) *p = 0; printf("%s\n", buf1); fh_setowner(fh, buf1 + 8, 0); printf("%s-\n", fh->owner); if (!fgets(buf2, 256, art)) goto FAIL; if ((p = strchr(buf2 + 8, '\n'))) *p = 0; if ((p = strchr(buf2 + 8, '\r'))) *p = 0; if (filename[0] == 'G') fh->accessed |= FH_ISDIGEST; fh->filetime = atoi(filename + 2); if (!strncmp(buf2 + 8, "Re", 2)) fh->thread = 0; else fh->thread = fh->filetime; strsncpy(fh->title, buf2 + 8, sizeof (fh->title)); found = 1; if (strncmp(buf1, "发信站", 6) && strncmp(buf1, "寄信人: ", 8) && strncmp(buf1, "发信人: ", 8)) found = 0; if ((strncmp(buf2, "标 题: ", 8)) && (strncmp(buf2, "标 题: ", 8))) found = 0; FAIL: fclose(art); if (!found) return -1; return 0; }
int get_mail(char *host, char *oboard, char *iboard) { int fd; FILE *fp0; int i; struct sockaddr_in xs; struct hostent *he; char dir[80], file[80], buf[256], brk[80]; // file: 本地文件名, buf: 临时变量, brk: 分隔符. servernow = host; do_log("gethostbyname %s", host); bzero((char *) &xs, sizeof (xs)); xs.sin_family = AF_INET; if ((he = gethostbyname(host)) != NULL) bcopy(he->h_addr, (char *) &xs.sin_addr, he->h_length); else xs.sin_addr.s_addr = inet_addr(host); xs.sin_port = htons(PORT); do_log("connecting %s", host); fd = socket(AF_INET, SOCK_STREAM, 0); fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); if (connect(fd, (struct sockaddr *) &xs, sizeof (xs)) < 0) { fd_set fds; struct timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 0; FD_ZERO(&fds); FD_SET(fd, &fds); if (select(fd + 1, NULL, &fds, NULL, &timeout) <= 0) { do_log("can't connect to %s", host); close(fd); return; } if (connect(fd, (struct sockaddr *) &xs, sizeof (xs)) < 0) { do_log("can't connect to %s", host); close(fd); return; } } fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK); do_log("connected %s", host); fp0 = fdopen(fd, "r+"); if (fgets(brk, 80, fp0) == 0) goto E; if (strlen(brk) < 10) goto E; fprintf(fp0, "select * from %s where dt < %d\n", oboard, time(0) - last); fflush(fp0); do_log("SEND request %s %d", oboard, time(0) - last); while (1) { FILE *fp; int t; struct fileheader x; char owner[256]; bzero(&x, sizeof (x)); do_log("reading..."); if (fgets(x.title, sizeof (x.title), fp0) == 0) break; if (fgets(owner, sizeof (owner), fp0) == 0) break; fh_setowner(&x, owner, 0); check_str(x.title); check_str(x.owner); removetailspace(x.title); do_log("%s", x.title); sprintf(file, "boards/%s/", iboard); t = trycreatefile(file, "M.%d.A", time(NULL), 100); x.filetime = t; do_log(file); do_log(fh2fname(&x)); fp = fopen(file, "w"); while (1) { if (fgets(buf, 255, fp0) == 0) break; if (!strcmp(buf, brk)) break; fprintf(fp, "%s", buf); } fclose(fp); if (!valid_user(x.owner)) { unlink(file); do_log("bad user: %s", x.owner); continue; } fh_find_thread(&x, iboard); sprintf(dir, "boards/%s/.DIR", iboard); fd = open(dir, O_WRONLY | O_APPEND | O_CREAT, 0660); write(fd, &x, sizeof (x)); close(fd); do_log("updatelastpost %s", iboard); updatelastpost(iboard); }; E:fclose(fp0); close(fd); do_log("done"); }