int kill_all_spam(va_list pvar) { char *choose[3] = {"11)作者", "22)標題", msg_choose_cancel}; char buf[128], mode[3]; int i; boardheader *bp; fileheader *fhdr = NULL; extern boardheader *bcache; extern int numboards; fhdr = va_arg(pvar, fileheader *); mode[0] = getans2(b_lines, 0, "刪除此 ", choose, 3, 'q'); if(mode[0] == '1') sprintf(buf, "終結所有看板中的 [%.40s] 嗎? ", fhdr->owner); else if(mode[0] == '2') sprintf(buf, "終結所有看板中的 [%.40s] 嗎? ", fhdr->title); else return RC_FOOT; if (getans2(b_lines, 0, buf, 0, 2, 'n') != 'y') return RC_FOOT; resolve_boards(); for (bp = bcache, i = 0; i < numboards; i++, bp++) { TagNum = 0; setbdir(buf, bp->brdname); outmsg(buf); refresh(); if(mode[0] != '1') TagThread(buf,fhdr->title, 0); else TagThread(buf, fhdr->owner, 1); if (TagNum) { delete_range2(buf, 0, 0); setbtotal(i); } } if(mode[0] != '1') log_usies("SPAM title ", currtitle); else log_usies("SPAM user", currtitle); TagNum = 0; return RC_CHDIR; }
int TagPruner(int bid) { boardheader_t *bp=NULL; char direct[PATHLEN]; assert(bid >= 0); /* bid == 0 means in mailbox */ if (bid && currstat != RMAIL) { bp = getbcache(bid); if (is_readonly_board(bp->brdname)) return DONOTHING; setbdir(direct, bp->brdname); } else if(currstat == RMAIL) { sethomedir(direct, cuser.userid); } else { vmsg("抱歉,程式異常 - 請至 " BN_BUGREPORT " 報告您剛的詳細步驟。"); return FULLUPDATE; } if (IsEmptyTagList() || (currstat == READING && !(currmode & MODE_BOARD))) return DONOTHING; if (vans("刪除所有標記[N]?") != 'y') return READ_REDRAW; // ready to start. outmsg("處理中,請稍後..."); refresh(); // first, delete and backup all files apply_record(direct, _iter_delete_tagged, sizeof(fileheader_t), direct); // now, delete the header #ifdef SAFE_ARTICLE_DELETE if(bp && !(currmode & MODE_DIGEST) && bp->nuser >= SAFE_ARTICLE_DELETE_NUSER) safe_delete_range(currdirect, 0, 0); else #endif delete_range(currdirect, 0, 0); TagNum = 0; if (bid) setbtotal(bid); else if(currstat == RMAIL) setupmailusage(); return NEWDIRECT; }
static int choose_board_read(choose_t *cp) { choose_board_t *cbrd = cp->data; board_data_t *ptr = cbrd->brds + cp->cur; if (ptr->flag & BOARD_DIR_FLAG) { int parent = cbrd->parent; const char *prefix = cbrd->prefix; bool recursive = cbrd->recursive; bool goodbrd = cbrd->goodbrd; int cur = cp->cur; cbrd->parent = ptr->pos; cbrd->prefix = NULL; cbrd->recursive = true; cbrd->goodbrd = (ptr->flag & BOARD_CUSTOM_FLAG) ? true : false; choose_board(cbrd); cbrd->parent = parent; cbrd->prefix = prefix; cbrd->recursive = recursive; cbrd->goodbrd = goodbrd; cp->cur = cur; } else { brc_initial(currentuser.userid, ptr->name); changeboard(&currbp, currboard, ptr->name); memcpy(currBM, ptr->BM, BM_LEN - 1); char buf[STRLEN]; if (DEFINE(DEF_FIRSTNEW)) { setbdir(buf, currboard); int tmp = unread_position(buf, ptr); int page = tmp - t_lines / 2; getkeep(buf, page > 1 ? page : 1, tmp + 1); } board_read(); brc_zapbuf(cbrd->zapbuf + ptr->pos); ptr->total = -1; currBM[0] = '\0'; } return FULLUPDATE; }
static int undo_board(struct boardheader* bh, void* arg) { char fname[256]; setbdir(DIR_MODE_NORMAL, fname, bh->filename); undo_index(fname); setbdir(DIR_MODE_DIGEST, fname, bh->filename); undo_index(fname); setbdir(DIR_MODE_THREAD, fname, bh->filename); undo_index(fname); setbdir(DIR_MODE_MARK, fname, bh->filename); undo_index(fname); setbdir(DIR_MODE_DELETED, fname, bh->filename); undo_index(fname); setbdir(DIR_MODE_JUNK, fname, bh->filename); undo_index(fname); setbdir(DIR_MODE_ORIGIN, fname, bh->filename); undo_index(fname); setbdir(DIR_MODE_ZHIDING, fname, bh->filename); undo_index(fname); board_update_toptitle(getbid(bh->filename, NULL), false); printf("board %s is undone.\n", bh->filename); return 0; }
static char * get_file_name(char *boardname, int threadid, char *fname) { char dirfile[256]; int fd; struct fileheader fh; fname[0]='\0'; setbdir(DIR_MODE_NORMAL, dirfile, boardname); if ((fd = open(dirfile, O_RDWR, 0644)) < 0) return fname; if (get_records_from_id(fd, threadid, &fh, 1, NULL) == 0) { close(fd); return fname; } close(fd); strcpy(fname, fh.filename); return fname; }
static int cancel_article(char *board, char *file) { struct fileheader header; char dirname[MAXPATHLEN]; char *basename; time_t time; int fd, ent; char old_path[255]; if (file == NULL || strlen(file) < 3) return 0; basename = (file[1]=='/') ? (file + 2) : file; if (basename[0] != 'M' || basename[1] != '.' || (time = atoi(basename + 2)) <= 0) return 0; getcwd(old_path, 255); chdir(BBSHOME); setbdir(DIR_MODE_NORMAL,dirname,board); if ((fd = open(dirname, O_RDWR)) == -1) { chdir(old_path); return 0; } ent = search_record_back(fd, sizeof(struct fileheader),0,cmp_filename, file, &header,1); if (ent) { struct write_dir_arg delarg; struct userec user; init_write_dir_arg(&delarg); delarg.fd=fd; delarg.ent=ent; getCurrentUser() = &user; strcpy(user.userid, "<innd>"); do_del_post(getCurrentUser(),&delarg,&header,board,DIR_MODE_NORMAL,0,getSession()); free_write_dir_arg(&delarg); } close(fd); chdir(old_path); return 0; }
static int a_pastetagpost(menu_t * pm, int mode) { boardheader_t *bh = NULL; char dirname[PATHLEN]; _iter_paste_tag_param param = {0}; param.pm = pm; param.mode = mode; if (TagBoard == 0){ sethomedir(dirname, cuser.userid); } else{ bh = getbcache(TagBoard); setbdir(dirname, bh->brdname); param.bh = bh; } // prevent if anything wrong if (TagNum > MAXTAGS || TagNum < 0) { vmsg("內部錯誤。請把你剛剛進行的完整步驟貼到 " BN_BUGREPORT " 板。"); return 0; } if (IsEmptyTagList()) return 0; /* since we use different tag features, * copyqueue is not required/used. */ copyqueue_reset(); apply_record(dirname, _iter_paste_tag, sizeof(fileheader_t), ¶m); TagNum = 0; return param.copied; }
int generate_board_title(struct boardheader *bh,void* arg) { int fd2, total, i; char olddirect[PATHLEN]; char *ptr; struct fileheader * ptr1; struct BoardStatus* bs; struct stat buf; unsigned int iid; setbdir(DIR_MODE_NORMAL, olddirect, bh->filename); if ((fd2 = open(olddirect, O_RDWR, 0664)) == -1) { perror(olddirect); return 0; } if (safe_mmapfile_handle(fd2, PROT_READ | PROT_WRITE, MAP_SHARED, &ptr, &buf.st_size) == 0) { close(fd2); return 0; } total = buf.st_size / (sizeof(struct fileheader)); ptr1 = (struct fileheader *) ptr; iid = ptr1[total-1].id; if(iid <=0) iid = 1; end_mmapfile((void *) ptr, buf.st_size, -1); close(fd2); bs=getbstatus(getbid(bh->filename,NULL)); if(bs) bs->nowid= iid + 1; return 0; }
int more_web(char *fpath, int promptend) { char *ch; char genbuf[41]; if (ch = strstr(fpath, "mailto:")) { if (!HAS_PERM(PERM_LOGINOK)) { outmsg("[41m ±zªºÅv¤£¨¬µLªk¨Ï¥Îinternet mail... [m"); return 0; } if (!not_addr(&ch[7]) && getdata(b_lines - 1, 0, "[±H«H]¥DÃD¡G", genbuf, 40, DOECHO, 0)) { do_send(&ch[7], genbuf); } else { outmsg("[41m ¦¬«H¤Hemail ©Î ¼ÐÃD ¦³»~... [m"); } return 0; } #if 0 if (ch = strstr(fpath, "gopher://")) { ITEM item; strcpy(item.X.G.server, &ch[9]); strcpy(item.X.G.path, "1/"); item.X.G.port = 70; gem(fpath, &item, 0); return 0; } #endif /* wildcat : ¤ä´©ª½±µ¶i¤J¬ÝªO */ if (ch = strstr(fpath, "board://")) { char bname[20], bpath[60], oldch[STRLEN]; struct stat st; int mode0 = currutmp->mode; int stat0 = currstat; int pos; boardheader *bhdr, *getbcache(); strcpy(oldch, ch); strcpy(bname, strtok(oldch + 8, "#")); setbpath(bpath, bname); if ((*bname == '\0') || (stat(bpath, &st) == -1)) { pressanykey(err_bid); return RC_FULL; } if (bhdr = getbcache(bname)) { if (Ben_Perm(bhdr) != 1) { pressanykey("§A¨S¦³¶i¤J¸ÓªOªºÅv"); return 0; } } else { pressanykey("§A¨S¦³¶i¤J¸ÓªOªºÅv"); return 0; } /* shakalaca.000123: ¤ä´©¬Ý¬Y¤@½g */ if (ch = strstr(fpath, "#")) { fileheader fhdr; pos = atoi(ch + 1); setbdir(bpath, bname); rec_get(bpath, &fhdr, sizeof(fileheader), pos); setbfile(bpath, bname, fhdr.filename); more(bpath, 0); } else { /* shakalaca.000124: ¸Ñ¨M "¥¼Åª" °ÝÃD.. */ brc_initial(bname); Read(); } currutmp->mode = mode0; currstat = stat0; return 0; } }
int calcboard(struct boardheader *bh, void *arg) { char fn[80]; int fd, total, i; struct stat buf; struct flock ldata; struct fileheader *ptr1; char *ptr; int size = sizeof(struct fileheader); setbdir(0, fn, bh->filename); printf("\r%s:\n", bh->filename); if ((fd = open(fn, O_RDWR, 0664)) == -1) { bbslog("user", "%s", "recopen err"); return 0; /* 创建文件发生错误 */ } fstat(fd, &buf); ldata.l_type = F_RDLCK; ldata.l_whence = 0; ldata.l_len = 0; ldata.l_start = 0; fcntl(fd, F_SETLKW, &ldata); total = buf.st_size / size; if ((i = safe_mmapfile_handle(fd, PROT_READ | PROT_WRITE, MAP_SHARED, (void **) &ptr, &buf.st_size)) != 1) { if (i == 2) end_mmapfile((void *) ptr, buf.st_size, -1); ldata.l_type = F_UNLCK; fcntl(fd, F_SETLKW, &ldata); close(fd); return 0; } ptr1 = (struct fileheader *) ptr; for (i = 0; i < total; i++) { struct stat st; char *p, *op; char ffn[80]; int j; off_t fsize; printf("\r%d", i); setbfile(ffn, bh->filename, ptr1->filename); { int k, abssize = 0, entercount = 0, ignoreline = 0; char *attach; long attach_len; j = safe_mmapfile(ffn, O_RDONLY, PROT_READ, MAP_SHARED, (void **) &p, &fsize, NULL); op = p; if (j) { k = fsize; while (k) { /* if (NULL != (checkattach(p, k, &attach_len, &attach))) { k -= (attach - p) + attach_len; p = attach + attach_len; continue; } */ if (k >= 3 && *p == '\n' && *(p + 1) == '-' && *(p + 2) == '-' && *(p + 3) == '\n') break; if (*p == '\n') { entercount++; ignoreline = 0; } if (k >= 5 && *p == '\n' && *(p + 1) == '\xa1' && *(p + 2) == '\xbe' && *(p + 3) == ' ' && *(p + 4) == '\xd4' && *(p + 5) == '\xda') ignoreline = 1; if (k >= 2 && *p == '\n' && *(p + 1) == ':' && *(p + 2) == ' ') ignoreline = 2; if (k >= 2 && *p == KEY_ESC && *(p + 1) == '[' && *(p + 2) == 'm') ignoreline = 3; k--; p++; if (entercount >= 4 && !ignoreline) abssize++; } ptr1->eff_size = abssize; end_mmapfile((void *) op, fsize, -1); } } //ptr1->posttime = get_posttime(ptr1); set_posttime(ptr1); ptr1++; } end_mmapfile((void *) ptr, buf.st_size, -1); ldata.l_type = F_UNLCK; fcntl(fd, F_SETLKW, &ldata); close(fd); return 0; }
static inline int process_article(const struct fileheader *f,int n,const struct boardheader *b){ static const struct flock lck_set={.l_type=F_RDLCK,.l_whence=SEEK_SET,.l_start=0,.l_len=0,.l_pid=0}; static const struct flock lck_clr={.l_type=F_UNLCK,.l_whence=SEEK_SET,.l_start=0,.l_len=0,.l_pid=0}; static struct stat st; static struct tm *p; static char name[BOUND]; static int fd,i,j,k,l; static time_t timestamp; static const char *S,*M,*N; static void *vp; do{ if((timestamp=get_posttime(f))<from||timestamp>to) break; if(ISSET(PARAM_P)&&strcmp(f->owner,post)) break; setbfile(name,b->filename,f->filename); if(stat(name,&st)==-1||!S_ISREG(st.st_mode)||st.st_size<size) break; if((fd=open(name,O_RDONLY #ifdef O_NOATIME |O_NOATIME #endif /* O_NOATIME */ ,0644))==-1) break; if(fcntl(fd,F_SETLKW,&lck_set)==-1){ close(fd); break; } vp=mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,fd,0); fcntl(fd,F_SETLKW,&lck_clr); close(fd); if((S=(const char*)vp)==MAP_FAILED) break; for(p=NULL,j=0,i=0;S[i]&&i<st.st_size;i++){ #define EQUAL(cp,cs) (((cp)==(cs))||(ISSET(PARAM_I)&&((cp)==toupper(cs)))) while(j>0&&!EQUAL(P[j],S[i])) j=L[j-1]; if(EQUAL(P[j],S[i])) j++; if(!P[j]){ M=&S[l=((i-j)+1)]; if(!ISSET(PARAM_N)){ for(k=0,N=M;!(N<S);N--) if((*N)&0x80) k++; if(!(k&0x01)) continue; } if(!p&&!(p=localtime(×tamp))) continue; count++; fprintf(out,"%6d %-20.20s %4d %4s %04d%02d%02d%02d%02d%02d %-17.17s %6d %-13.13s %s\n", n,b->filename,current,mode,(p->tm_year+1900),(p->tm_mon+1),(p->tm_mday), (p->tm_hour),(p->tm_min),(p->tm_sec),f->filename,l,f->owner,f->title); if(ISSET(PARAM_S)) break; j=L[j-1]; } #undef EQUAL } munmap(vp,st.st_size); number++; } while(0); return 0; } static inline int process_board(const struct boardheader *b,int n,void *v){ static char name[BOUND]; do{ if(ISSET(PARAM_A)) break; if(ISSET(PARAM_U)){ if(!check_read_perm(user,b)) return -1; break; } if(ISSET(PARAM_B)) break; if(!public_board(b)) return -2; } while(0); current=n; if(!ISSET(PARAM_Q)) fprintf(stdout,"正在处理版面 %-29.29s ... ",b->filename); if(!ISSET(PARAM_E)){ mode="版面"; setbdir(DIR_MODE_NORMAL,name,b->filename); APPLY_RECORD(name,process_article,sizeof(struct fileheader),b,0,true); } if(ISSET(PARAM_D)){ mode="回收"; setbdir(DIR_MODE_DELETED,name,b->filename); APPLY_RECORD(name,process_article,sizeof(struct fileheader),b,0,true); } if(ISSET(PARAM_J)){ mode="自删"; setbdir(DIR_MODE_JUNK,name,b->filename); APPLY_RECORD(name,process_article,sizeof(struct fileheader),b,0,true); } if(!ISSET(PARAM_Q)) fprintf(stdout,"%s\n","处理完成!"); return 0; } int main(int argc,char **argv){ #define EXIT(msg) do{fprintf(stderr,"%s\n",(msg));if(out)fclose(out);exit(__LINE__);}while(0) const struct boardheader *board; char name[BOUND],path[BOUND]; const char *desc; int ret; double cost; if(!getcwd(path,BOUND)) EXIT("获取当前工作目录时发生错误"); if(chdir(BBSHOME)==-1) EXIT("切换工作目录时发生错误..."); if((mark=time(NULL))==(time_t)(-1)) EXIT("获取时间时发生错误..."); resolve_ucache(); resolve_boards(); to=mark; opterr=0; while((ret=getopt(argc,argv,"r:f:t:ab:u:p:djesnio:qh"))!=-1){ switch(ret){ #define CHECK_CONFLICT(param) do{if(ISSET(param))EXIT("给定的选项间存在冲突...");}while(0) #define CHECK_DEPENDENCE(param) do{if(!ISSET(param))EXIT("给定的选项间缺少依赖...");}while(0) #define CHECK_DUP(param) do{if(ISSET(param))EXIT("给定的选项中存在重复...");}while(0) #define SET(param) do{CHECK_DUP(param);flag|=(param);}while(0) case 'r': CHECK_CONFLICT(PARAM_F|PARAM_T); SET(PARAM_R); do{ struct tm t,*p; int n; if(!isdigit(optarg[0])) EXIT("选项 -r 的参数无法解析..."); n=atoi(optarg); if(!(p=localtime(&mark))) EXIT("解析时间时发生错误..."); memcpy(&t,p,sizeof(struct tm)); t.tm_hour=0; t.tm_min=0; t.tm_sec=0; if((from=mktime(&t))==(time_t)(-1)) EXIT("设定时间时发生错误..."); } while(0); break; #define PARSE2(p) ((((p)[0]*10)+((p)[1]*1))-('0'*11)) #define PARSE4(p) ((PARSE2(p)*100)+(PARSE2(&(p)[2])*1)) case 'f': CHECK_CONFLICT(PARAM_R); SET(PARAM_F); do{ struct tm t; int i; for(i=0;optarg[i];i++) if(!isdigit(optarg[i])) break; if(i!=14) EXIT("选项 -f 的参数无法解析..."); memset(&t,0,sizeof(struct tm)); t.tm_year=(PARSE4(optarg)-1900); t.tm_mon=(PARSE2(&optarg[4])-1); t.tm_mday=PARSE2(&optarg[6]); t.tm_hour=PARSE2(&optarg[8]); t.tm_min=PARSE2(&optarg[10]); t.tm_sec=PARSE2(&optarg[12]); if((from=mktime(&t))==(time_t)(-1)) EXIT("设定时间时发生错误..."); } while(0); break; case 't': CHECK_CONFLICT(PARAM_R); SET(PARAM_T); do{ struct tm t; int i; for(i=0;optarg[i];i++) if(!isdigit(optarg[i])) break; if(i!=14) EXIT("选项 -t 的参数无法解析..."); memset(&t,0,sizeof(struct tm)); t.tm_year=(PARSE4(optarg)-1900); t.tm_mon=(PARSE2(&optarg[4])-1); t.tm_mday=PARSE2(&optarg[6]); t.tm_hour=PARSE2(&optarg[8]); t.tm_min=PARSE2(&optarg[10]); t.tm_sec=PARSE2(&optarg[12]); if((from=mktime(&t))==(time_t)(-1)) EXIT("设定时间时发生错误..."); } while(0); break; #undef PARSE2 #undef PARSE4 case 'a': CHECK_CONFLICT(PARAM_B|PARAM_U); SET(PARAM_A); break; case 'b': CHECK_CONFLICT(PARAM_A|PARAM_U); SET(PARAM_B); if(!(current=getbid(optarg,&board))) EXIT("选项 -b 所指定的版面无法获取..."); break; case 'u': CHECK_CONFLICT(PARAM_A|PARAM_B); SET(PARAM_U); do{ struct userec *u; if(!getuser(optarg,&u)) EXIT("选项 -u 所指定的用户无法获取..."); user=u; } while(0); break; case 'p': SET(PARAM_P); snprintf(post,OWNER_LEN,"%s",optarg); break; case 'd': SET(PARAM_D); break; case 'j': SET(PARAM_J); break; case 'e': CHECK_DEPENDENCE(PARAM_D|PARAM_J); SET(PARAM_E); break; case 's': SET(PARAM_S); break; case 'n': SET(PARAM_N); break; case 'i': SET(PARAM_I); break; case 'o': SET(PARAM_O); if(optarg[0]!='/') snprintf(name,BOUND,"%s/%s",path,optarg); else snprintf(name,BOUND,"%s",optarg); break; case 'q': SET(PARAM_Q); break; case 'h': usage(); return 0; default: usage(); EXIT("不可识别的选项..."); break; #undef CHECK_CONFLICT #undef CHECK_DEPENDENCE #undef CHECK_DUP #undef SET } } if(from>to){ usage(); EXIT("当前时间设定不合法..."); } if(!ISSET(PARAM_Q)&&setvbuf(stdout,NULL,_IONBF,BUFSIZ)) EXIT("调整文件缓冲时发生错误..."); if((argc-optind)!=1){ usage(); EXIT("不可识别的参数..."); } set_pattern(argv[optind]); set_link(argv[optind]); if(!size) EXIT("模式串不能为空串..."); if(!ISSET(PARAM_O)) snprintf(name,BOUND,"%s/res_%lu.us",path,mark); if(!(out=fopen(name,"w"))) EXIT("打开文件时发生错误..."); fprintf(out,"%6s %-20.20s %4s %4s %-14.14s %-17.17s %6s %-13.13s %s\n", "文章号","版面名称"," BID","位置","发表时间","文件名","偏移量","作者","标题"); if(!(P[0]&0x80)) flag|=PARAM_N; if(ISSET(PARAM_B)) process_board(board,current,NULL); else APPLY_BIDS(process_board,NULL); fclose(out); cost=difftime(time(NULL),mark); if(cost>86400){ cost/=86400; desc="天"; } else if(cost>3600){ cost/=3600; desc="小时"; } else if(cost>60){ cost/=60; desc="分钟"; } else desc="秒"; fprintf(stdout,"\n操作已完成! 共处理 %d 篇文章, 获得 %d 处匹配, 耗时 %.2lf %s!\n", number,count,cost,desc); return 0; #undef EXIT }
int generate_board_title(struct boardheader *bh,void* arg) { int fd2, size = sizeof(fileheader), total, i; struct boardheader btmp; char olddirect[PATHLEN]; char *ptr, *t, *t2; #ifdef GEN_ORIGIN FILE *fp=NULL; char opath[512]; #endif struct fileheader * ptr1; struct search_temp { bool has_pre; int digest; int thread_id; int id; int next; } *index=NULL; struct stat buf; int gen_threadid; struct BoardStatus* bs; #ifdef GEN_ORIGIN setbdir(DIR_MODE_ORIGIN, opath, bh->filename); #endif setbdir(DIR_MODE_NORMAL, olddirect, bh->filename); gen_threadid=1; if ((fd2 = open(olddirect, O_RDWR, 0664)) == -1) { perror(olddirect); return 0; } BBS_TRY { if (!safe_mmapfile_handle(fd2,PROT_READ|PROT_WRITE,MAP_SHARED,&ptr,&buf.st_size)) { close(fd2); return 0; } #ifdef GEN_ORIGIN fp = fopen(opath, "wb"); #endif total = buf.st_size / size; index = (struct search_temp *) malloc(sizeof(*index) * total); ptr1 = (struct fileheader *) ptr; for (i = 0; i < total; i++, ptr1++) { int j; t2 = ptr1->title; if (!strncmp(t2, "Re:", 3)) { index[i].has_pre = true; t2 += 4; } else index[i].has_pre = false; index[i].thread_id = 0; index[i].digest = simple_digest(t2, ARTICLE_TITLE_LEN); for (j = i - 1; j >= 0; j--) { struct fileheader *tmppost; if (index[j].digest != index[i].digest) continue; tmppost = ((struct fileheader *)(ptr + j * size)); t = tmppost->title; if (index[j].has_pre) t += 4; if (!strcmp(t, t2)) { index[j].next = i; index[i].thread_id = index[j].thread_id; index[i].next = 0; break; } } if (index[i].thread_id == 0) { index[i].thread_id = gen_threadid; index[i].id = gen_threadid; index[i].next = 0; gen_threadid++; } else { index[i].id = gen_threadid; gen_threadid++; } } ptr1 = (struct fileheader *) ptr; for (i = 0; i < total; i++, ptr1++) { ptr1->id = index[i].id; ptr1->groupid = index[i].thread_id; ptr1->reid = index[i].thread_id; #ifdef GEN_ORIGIN if (index[i].has_pre == false) fwrite(ptr1, 1, sizeof(struct fileheader), fp); #endif } } BBS_CATCH { } BBS_END; free(index); end_mmapfile((void *) ptr, buf.st_size, -1); close(fd2); #ifdef GEN_ORIGIN fclose(fp); #endif memcpy(&btmp,getbcache(bh->filename),sizeof(btmp)); bs=getbstatus(getbid(bh->filename,NULL)); bs->nowid=gen_threadid + 1; set_board(getbid(bh->filename,NULL), &btmp,NULL); setboardmark(bh->filename, 1); setboardtitle(bh->filename, 1); setboardorigin(bh->filename, 1); return 0; }
int gen_super_filter_index2(char *index, struct fileheader* fileinfo, char * boardname, int isbm) { struct fileheader *ptr1; struct flock ldata, ldata2; int fd, fd2, size = sizeof(fileheader), total, i, count = 0; char direct[PATHLEN]; char newdirect[PATHLEN]; char *ptr; struct stat buf; int load_content=0, found=0, load_stat=0; int gid = fileinfo->groupid; //TODO: 这么大的index! load_content = (strstr(index, "content")!=NULL||strstr(index, "文章内容")!=NULL); load_stat = (strstr(index, "asize")!=NULL||strstr(index, "总长度")!=NULL); setbdir(DIR_MODE_NORMAL, direct, boardname); setbdir(DIR_MODE_SUPERFITER, newdirect, boardname); if ((fd = open(newdirect, O_WRONLY | O_CREAT, 0664)) == -1) { bbslog("user", "%s", "recopen err"); return -9999; } ldata.l_type = F_WRLCK; ldata.l_whence = 0; ldata.l_len = 0; ldata.l_start = 0; if (fcntl(fd, F_SETLKW, &ldata) == -1) { bbslog("user", "%s", "reclock err"); close(fd); return -9999; } if ((fd2 = open(direct, O_RDONLY, 0664)) == -1) { bbslog("user", "%s", "recopen err"); ldata.l_type = F_UNLCK; fcntl(fd, F_SETLKW, &ldata); close(fd); return -9999; } fstat(fd2, &buf); ldata2.l_type = F_RDLCK; ldata2.l_whence = 0; ldata2.l_len = 0; ldata2.l_start = 0; fcntl(fd2, F_SETLKW, &ldata2); total = buf.st_size / size; if ((i = safe_mmapfile_handle(fd2, PROT_READ, MAP_SHARED, (void **) &ptr, &buf.st_size)) != 1) { if (i == 2) end_mmapfile((void *) ptr, buf.st_size, -1); ldata2.l_type = F_UNLCK; fcntl(fd2, F_SETLKW, &ldata2); close(fd2); ldata.l_type = F_UNLCK; fcntl(fd, F_SETLKW, &ldata); close(fd); return -9999; } ptr1 = (struct fileheader *) ptr; libs = (char*)malloc(LIBLEN); for (i = 0; i < total; i++) { struct stat st; char* p; char ffn[80]; int j=0; off_t fsize; libptr = libs; ferr = 0; set_vard(fvars+fget_var("cid"), fileinfo->id); set_vard(fvars+fget_var("creid"), fileinfo->reid); set_vard(fvars+fget_var("cgroupid"), fileinfo->groupid); set_vars(fvars+fget_var("cauthor"), fileinfo->owner); set_vars(fvars+fget_var("cfname"), fileinfo->filename); set_vard(fvars+fget_var("cftime"), get_posttime(fileinfo)); set_vard(fvars+fget_var("ceffsize"), fileinfo->eff_size); set_vard(fvars+fget_var("no"), i+1); set_vard(fvars+fget_var("文章号"), i+1); set_vard(fvars+fget_var("id"), ptr1->id); set_vard(fvars+fget_var("reid"), ptr1->reid); set_vard(fvars+fget_var("groupid"), ptr1->groupid); set_vard(fvars+fget_var("thread"), ptr1->groupid==gid); set_vard(fvars+fget_var("本主题"), ptr1->groupid==gid); set_vard(fvars+fget_var("origin"), ptr1->id==ptr1->groupid); set_vard(fvars+fget_var("原作"), ptr1->id==ptr1->groupid); set_vard(fvars+fget_var("m"), ptr1->accessed[0]&FILE_MARKED); set_vard(fvars+fget_var("保留"), ptr1->accessed[0]&FILE_MARKED); set_vard(fvars+fget_var("g"), ptr1->accessed[0]&FILE_DIGEST); set_vard(fvars+fget_var("文摘"), ptr1->accessed[0]&FILE_DIGEST); set_vard(fvars+fget_var("b"), (ptr1->accessed[0]&FILE_MARKED)&&(ptr1->accessed[0]&FILE_DIGEST)); if (isbm) { set_vard(fvars+fget_var("noreply"), ptr1->accessed[1]&FILE_READ); set_vard(fvars+fget_var("不可回复"), ptr1->accessed[1]&FILE_READ); set_vard(fvars+fget_var("sign"), ptr1->accessed[0]&FILE_SIGN); set_vard(fvars+fget_var("标记"), ptr1->accessed[0]&FILE_SIGN); #ifdef PERCENT_SIGN_SUPPORT set_vard(fvars+fget_var("percent"), ptr1->accessed[0]&FILE_PERCENT); set_vard(fvars+fget_var("百分号"), ptr1->accessed[0]&FILE_PERCENT); #endif #ifdef FILTER set_vard(fvars+fget_var("censor"), ptr1->accessed[1]&FILE_CENSOR); set_vard(fvars+fget_var("审核"), ptr1->accessed[1]&FILE_CENSOR); #endif set_vard(fvars+fget_var("del"), ptr1->accessed[1]&FILE_DEL); set_vard(fvars+fget_var("删除"), ptr1->accessed[1]&FILE_DEL); set_vard(fvars+fget_var("import"), ptr1->accessed[0]&FILE_IMPORTED); set_vard(fvars+fget_var("精华"), ptr1->accessed[0]&FILE_IMPORTED); } set_vard(fvars+fget_var("attach"), ptr1->attachment); set_vard(fvars+fget_var("附件"), ptr1->attachment); set_vars(fvars+fget_var("title"), ptr1->title); set_vars(fvars+fget_var("标题"), ptr1->title); set_vars(fvars+fget_var("author"), ptr1->owner); set_vars(fvars+fget_var("作者"), ptr1->owner); set_vars(fvars+fget_var("fname"), ptr1->filename); set_vars(fvars+fget_var("文件名"), ptr1->filename); set_vard(fvars+fget_var("my"), !strcmp(ptr1->owner,getCurrentUser()->userid)); set_vard(fvars+fget_var("我的"), !strcmp(ptr1->owner,getCurrentUser()->userid)); #ifdef HAVE_BRC_CONTROL set_vard(fvars+fget_var("unread"), brc_unread(ptr1->id, getSession())); set_vard(fvars+fget_var("未读"), brc_unread(ptr1->id, getSession())); #endif setbfile(ffn, boardname, ptr1->filename); set_vard(fvars+fget_var("ftime"), get_posttime(ptr1)); set_vard(fvars+fget_var("时间"), get_posttime(ptr1)); set_vard(fvars+fget_var("effsize"), ptr1->eff_size); set_vard(fvars+fget_var("有效长度"), ptr1->eff_size); if(load_stat) { if(stat(ffn, &st)!=-1) { set_vard(fvars+fget_var("asize"), st.st_size); set_vard(fvars+fget_var("总长度"), st.st_size); } else { set_vard(fvars+fget_var("asize"), 0); set_vard(fvars+fget_var("总长度"), 0); } } if(load_content) { set_vars(fvars+fget_var("content"), ptr1->filename); set_vars(fvars+fget_var("文章内容"), ptr1->filename); j = safe_mmapfile(ffn, O_RDONLY, PROT_READ, MAP_SHARED, (void **) &p, &fsize, NULL); if(j) { set_vars(fvars+fget_var("content"), p); set_vars(fvars+fget_var("文章内容"), p); } } ferr=0; feval(fvars+fget_var("res"), index, 0, strlen(index)-1); if(ferr) break; if(fvars[fget_var("res")].s) { write(fd, ptr1, size); count++; found++; } if(load_content) { if(j) end_mmapfile((void*)p, fsize, -1); } ptr1++; } free(libs); end_mmapfile((void *) ptr, buf.st_size, -1); ldata2.l_type = F_UNLCK; fcntl(fd2, F_SETLKW, &ldata2); close(fd2); ftruncate(fd, count * size); ldata.l_type = F_UNLCK; fcntl(fd, F_SETLKW, &ldata); /* 退出互斥区域*/ close(fd); if(ferr) { return -ferr; } else { return count; } }
int refresh_board(char* bname) { int fd, fd2, origincount, acount, i, j, replycount; char dirpath[256], originpath[256]; char *head, *ahead; struct fileheader *ptr, *aptr, *topic, *lastpost; struct stat buf, abuf; if (setboardorigin(bname, -1)) board_regenspecial(bname, DIR_MODE_ORIGIN, NULL); setbdir(DIR_MODE_NORMAL, dirpath, bname); setbdir(DIR_MODE_ORIGIN, originpath, bname); fd = open(originpath, O_RDWR, 0644); if (fd == -1) { printf("cannot open %s.\n", originpath); return 0; } fd2 = open(dirpath, O_RDWR, 0644); if (fd2 == -1) { close(fd); printf("cannot open %s.\n", dirpath); return 0; } BBS_TRY { if (!safe_mmapfile_handle(fd, PROT_READ | PROT_WRITE, MAP_SHARED, &head, &buf.st_size)) { printf("cannot mmap %s.\n", originpath); close(fd); return 0; } if (!safe_mmapfile_handle(fd2, PROT_READ | PROT_WRITE, MAP_SHARED, &ahead, &abuf.st_size)) { printf("cannot mmap %s.\n", dirpath); close(fd); close(fd2); return 0; } origincount = buf.st_size / sizeof(struct fileheader); acount = abuf.st_size / sizeof(struct fileheader); ptr = (struct fileheader *)head; for (i=0; i<origincount; i++) { replycount = 0; topic = NULL; lastpost = NULL; aptr = (struct fileheader *)ahead; for (j=0; j<acount; j++) { if (aptr->groupid == ptr->id) { replycount++; if (aptr->groupid == aptr->id) topic = aptr; lastpost = aptr; } aptr++; } ptr->replycount = replycount; if (topic) topic->replycount = replycount; if (lastpost) { strcpy(ptr->last_owner, lastpost->owner); ptr->last_posttime = get_posttime(lastpost); if (topic) { strcpy(topic->last_owner, ptr->owner); topic->last_posttime = ptr->last_posttime; } } ptr++; } } BBS_CATCH { } BBS_END; end_mmapfile((void *)head, buf.st_size, -1); end_mmapfile((void *)ahead, abuf.st_size, -1); close(fd); close(fd2); printf("board %s is done.\n", bname); return 0; }
static char * get_file_info(char *boardname, int threadid, char *title, char *userid, char *filename, int pic) { char dirfile[256]; int fd; struct fileheader fh; setbdir(DIR_MODE_NORMAL, dirfile, boardname); if ((fd = open(dirfile, O_RDWR, 0644)) < 0) return NULL; if (get_records_from_id(fd, threadid, &fh, 1, NULL) == 0) { close(fd); return NULL; } close(fd); #ifdef NEWSMTH // pig2532 Feb 2008: ignore topic with FEN flag in top10 if (fh.accessed[1] & FILE_FEN) { printf("skip:%s/%d/%s/%s\n", boardname, fh.id, fh.owner, fh.title); return NULL; } #endif /* NEWSMTH */ if (pic) { if (!fh.attachment) { return NULL; } else { // 读取楼主的第一个附件,判断是否为图片,是则输出该图以备缩略之用,否则返回NULL char fn[PATHLEN]; int fd_pic; char *src, *dst, *dot; off_t size_src, size_dst; char *aname, *start; long asize; setbfile(fn, boardname, fh.filename); if ((fd = open(fn, O_RDONLY)) == -1) return NULL; if (readw_lock(fd, 0, SEEK_SET, 0) == -1) { close(fd); return NULL; } if (safe_mmapfile_handle(fd, PROT_READ, MAP_PRIVATE, &src, &size_src) == 0) { un_lock(fd, 0, SEEK_SET, 0); close(fd); return NULL; } if (!(aname = checkattach(src + fh.attachment, size_src - fh.attachment, &asize, &start))) return NULL; dot = strrchr(aname, '.'); // 是否图片? if (get_attachment_type_from_ext(dot) != ATTACH_IMG) { end_mmapfile(src, size_src, -1); un_lock(fd, 0, SEEK_SET, 0); close(fd); return NULL; } sprintf(fn, "hotpic/%s_%d", boardname, threadid); if ((fd_pic = open(fn, O_RDWR | O_CREAT, 0600)) == -1) { end_mmapfile(src, size_src, -1); un_lock(fd, 0, SEEK_SET, 0); close(fd); return NULL; } ftruncate(fd_pic, asize); if (safe_mmapfile_handle(fd_pic, PROT_WRITE, MAP_SHARED, &dst, &size_dst) == 0) { close(fd_pic); un_lock(fd, 0, SEEK_SET, 0); close(fd); return NULL; } printf("HOTPIC: %s_%d\n", boardname, threadid); memcpy(dst, start, asize); end_mmapfile(dst, size_dst, -1); end_mmapfile(src, size_src, -1); close(fd_pic); un_lock(fd, 0, SEEK_SET, 0); close(fd); } } strncpy(title, fh.title, 80); title[80]=0; strncpy(userid, fh.owner, IDLEN); userid[IDLEN]='\0'; strncpy(filename, fh.filename, FILENAME_LEN); filename[FILENAME_LEN]='\0'; return title; }