|
| 1 | +#include <fcntl.h> |
| 2 | +#include <errno.h> |
| 3 | +#include "ythtbbs.h" |
| 4 | + |
| 5 | +char * |
| 6 | +fh2fname(struct fileheader *fh) |
| 7 | +{ |
| 8 | + static char s[16]; |
| 9 | + sprintf(s, "M.%d.A", fh->filetime); |
| 10 | + if (fh->accessed & FH_ISDIGEST) |
| 11 | + s[0] = 'G'; |
| 12 | + if (fh->accessed & FILE_ISTOP1) //add by wjbta |
| 13 | + s[0] = 'T'; |
| 14 | + return s; |
| 15 | +} |
| 16 | + |
| 17 | +char * |
| 18 | +bknh2bknname(struct bknheader *bknh) |
| 19 | +{ |
| 20 | + static char s[16]; |
| 21 | + sprintf(s, "B.%d", bknh->filetime); |
| 22 | + return s; |
| 23 | +} |
| 24 | + |
| 25 | +char * |
| 26 | +fh2owner(struct fileheader *fh) |
| 27 | +{ |
| 28 | + if (!fh->owner[0]) |
| 29 | + return "Anonymous"; |
| 30 | + else |
| 31 | + return fh->owner; |
| 32 | +} |
| 33 | + |
| 34 | +char * |
| 35 | +fh2realauthor(struct fileheader *fh) |
| 36 | +{ |
| 37 | + if (!fh->owner[0]) |
| 38 | + return fh->owner + 1; |
| 39 | + else |
| 40 | + return fh->owner; |
| 41 | +} |
| 42 | + |
| 43 | +int |
| 44 | +fh2modifytime(struct fileheader *fh) |
| 45 | +{ |
| 46 | + if (fh->edittime) |
| 47 | + return fh->edittime; |
| 48 | + else |
| 49 | + return fh->filetime; |
| 50 | +} |
| 51 | + |
| 52 | +void |
| 53 | +fh_setowner(struct fileheader *fh, char *owner, int anony) |
| 54 | +{ |
| 55 | + char *ptr; |
| 56 | + bzero(fh->owner, sizeof (fh->owner)); |
| 57 | + if (anony) { |
| 58 | + fh->owner[0] = 0; |
| 59 | + strsncpy(fh->owner + 1, owner, sizeof (fh->owner) - 1); |
| 60 | + return; |
| 61 | + } |
| 62 | + if (!*owner) { |
| 63 | + *fh->owner = 0; |
| 64 | + return; |
| 65 | + } |
| 66 | + strsncpy(fh->owner, owner, sizeof (fh->owner) - 1); |
| 67 | + if (!strchr(owner, '@') && !strchr(owner, '.')) { |
| 68 | + ptr = strchr(fh->owner, ' '); |
| 69 | + if (ptr) |
| 70 | + *ptr = 0; |
| 71 | + return; |
| 72 | + } |
| 73 | + ptr = fh->owner + 1; |
| 74 | + while (*ptr) { |
| 75 | + if (*ptr == '@' || *ptr == '.' || *ptr == ' ') { |
| 76 | + *ptr = '.'; |
| 77 | + *(ptr + 1) = 0; |
| 78 | + return; |
| 79 | + } |
| 80 | + ptr++; |
| 81 | + } |
| 82 | + *--ptr = '.'; |
| 83 | + return; |
| 84 | +} |
| 85 | + |
| 86 | +int |
| 87 | +change_dir(char *direct, struct fileheader *fileinfo, |
| 88 | + void (*func(void *, void *)), int ent, int digestmode, int mode) |
| 89 | +{ |
| 90 | + int i, newent; |
| 91 | + int fd; |
| 92 | + int size = sizeof (struct fileheader); |
| 93 | + struct fileheader xfh, newfileinfo; |
| 94 | + struct flock ldata; |
| 95 | + if (digestmode != 0) |
| 96 | + return -1; |
| 97 | + if ((fd = open(direct, O_RDWR, 0660)) == -1) |
| 98 | + return -2; |
| 99 | + |
| 100 | + if (mode) |
| 101 | + memcpy(&newfileinfo, fileinfo, size); |
| 102 | + newent = ent; |
| 103 | + for (i = newent; i > 0; i--) { |
| 104 | + if (lseek(fd, size * (i - 1), SEEK_SET) == -1) { |
| 105 | + i = 0; |
| 106 | + break; |
| 107 | + |
| 108 | + } |
| 109 | + if (read(fd, &xfh, size) != size) { |
| 110 | + i = 0; |
| 111 | + break; |
| 112 | + } |
| 113 | + if (fileinfo->filetime == xfh.filetime) { |
| 114 | + newent = i; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + if (!i) { |
| 119 | + close(fd); |
| 120 | + return -3; |
| 121 | + } |
| 122 | + memcpy(fileinfo, &xfh, size); |
| 123 | + ldata.l_type = F_WRLCK; |
| 124 | + ldata.l_whence = 0; |
| 125 | + ldata.l_len = size; |
| 126 | + ldata.l_start = size * (newent - 1); |
| 127 | + if (fcntl(fd, F_SETLKW, &ldata) == -1) { |
| 128 | + errlog("reclock error %d", errno); |
| 129 | + close(fd); |
| 130 | + return -4; |
| 131 | + } |
| 132 | + (*func) (fileinfo, &newfileinfo); |
| 133 | + if (lseek(fd, size * (newent - 1), SEEK_SET) == -1) { |
| 134 | + errlog("subrec seek err %d", errno); |
| 135 | + close(fd); |
| 136 | + return -5; |
| 137 | + } |
| 138 | + if (write(fd, fileinfo, size) != size) { |
| 139 | + errlog("subrec write err %d", errno); |
| 140 | + close(fd); |
| 141 | + return -6; |
| 142 | + } |
| 143 | + ldata.l_type = F_UNLCK; |
| 144 | + fcntl(fd, F_SETLK, &ldata); |
| 145 | + close(fd); |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
| 149 | +#define SWITCH_FLAG(x, y) if(x & y) {x &= ~y;} else { x |= y;} |
| 150 | + |
| 151 | +void |
| 152 | +DIR_do_mark(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 153 | +{ |
| 154 | + SWITCH_FLAG(fileinfo->accessed, FH_MARKED); |
| 155 | +} |
| 156 | + |
| 157 | +void |
| 158 | +DIR_do_digest(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 159 | +{ |
| 160 | + SWITCH_FLAG(fileinfo->accessed, FH_DIGEST); |
| 161 | +} |
| 162 | + |
| 163 | +void |
| 164 | +DIR_do_underline(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 165 | +{ |
| 166 | + SWITCH_FLAG(fileinfo->accessed, FH_NOREPLY); |
| 167 | +} |
| 168 | + |
| 169 | +void |
| 170 | +DIR_do_allcanre(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 171 | +{ |
| 172 | + SWITCH_FLAG(fileinfo->accessed, FH_ALLREPLY); |
| 173 | +} |
| 174 | + |
| 175 | +void |
| 176 | +DIR_do_attach(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 177 | +{ |
| 178 | + fileinfo->accessed |= FH_ATTACHED; |
| 179 | +} |
| 180 | + |
| 181 | +void |
| 182 | +DIR_clear_dangerous(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 183 | +{ |
| 184 | + fileinfo->accessed &= ~FH_DANGEROUS; |
| 185 | +} |
| 186 | + |
| 187 | +void |
| 188 | +DIR_do_dangerous(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 189 | +{ |
| 190 | + fileinfo->accessed |= FH_DANGEROUS; |
| 191 | +} |
| 192 | + |
| 193 | +void |
| 194 | +DIR_do_markdel(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 195 | +{ |
| 196 | + SWITCH_FLAG(fileinfo->accessed, FH_DEL); |
| 197 | +} |
| 198 | + |
| 199 | +void |
| 200 | +DIR_do_mark_minus_del(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 201 | +{ //add by mintbaggio@BMY 040321 for minus-postnums delete |
| 202 | + SWITCH_FLAG(fileinfo->accessed, FH_MINUSDEL); |
| 203 | +} |
| 204 | + |
| 205 | +void |
| 206 | +DIR_do_edit(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 207 | +{ |
| 208 | + fileinfo->sizebyte = newfileinfo->sizebyte; |
| 209 | + fileinfo->edittime = newfileinfo->edittime; |
| 210 | +} |
| 211 | + |
| 212 | +void |
| 213 | +DIR_do_changetitle(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 214 | +{ |
| 215 | + strncpy(fileinfo->title, newfileinfo->title, 60); |
| 216 | + fileinfo->title[59] = 0; |
| 217 | +} |
| 218 | + |
| 219 | +void |
| 220 | +DIR_do_evaluate(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 221 | +{ |
| 222 | + fileinfo->staravg50 = newfileinfo->staravg50; |
| 223 | + fileinfo->hasvoted = newfileinfo->hasvoted; |
| 224 | +} |
| 225 | + |
| 226 | +void |
| 227 | +DIR_do_spec(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 228 | +{ |
| 229 | + SWITCH_FLAG(fileinfo->accessed, FH_SPEC); |
| 230 | +} |
| 231 | + |
| 232 | +void |
| 233 | +DIR_do_import(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 234 | +{ |
| 235 | + if (newfileinfo->accessed & FH_ANNOUNCE) |
| 236 | + fileinfo->accessed |= FH_ANNOUNCE; |
| 237 | +} |
| 238 | + |
| 239 | +void |
| 240 | +DIR_do_suremarkdel(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 241 | +{ |
| 242 | + fileinfo->accessed |= FH_DEL; |
| 243 | +} |
| 244 | + |
| 245 | +void DIR_do_top(struct fileheader *fileinfo, struct fileheader *newfileinfo) |
| 246 | +{ SWITCH_FLAG(fileinfo->accessed, FILE_TOP1); } |
| 247 | + |
| 248 | +int |
| 249 | +outgo_post(struct fileheader *fh, char *board, char *id, char *name) |
| 250 | +{ |
| 251 | + FILE *foo; |
| 252 | + if (fh->accessed & FH_INND) |
| 253 | + if ((foo = fopen("inndlog/out.bntp", "a"))) { |
| 254 | + fprintf(foo, "%s\t%s\t%s\t%s\t%s\n", board, |
| 255 | + fh2fname(fh), id, name, fh->title); |
| 256 | + fclose(foo); |
| 257 | + } |
| 258 | + return 0; |
| 259 | +} |
| 260 | + |
| 261 | +/* modifying by ylsdd |
| 262 | + * unlink action is taked within cancelpost if in mail |
| 263 | + * else this item is added to the file '.DELETED' under |
| 264 | + * the board's directory, the filename is not changed. |
| 265 | + * Unlike the fb code which moves the file to the deleted |
| 266 | + * board. |
| 267 | + * */ |
| 268 | +void |
| 269 | +cancelpost(char *board, char *userid, struct fileheader *fh, int owned) |
| 270 | +{ |
| 271 | + struct fileheader postfile; |
| 272 | + FILE *fin; |
| 273 | + int digestmode, len; |
| 274 | + char buf[256], from[STRLEN], *ptr; |
| 275 | + time_t now_t; |
| 276 | + postfile = *fh; |
| 277 | + postfile.accessed &= ~(FH_MARKED | FH_SPEC | FH_DIGEST); |
| 278 | + postfile.deltime = time(&now_t) / (3600 * 24) % 100; |
| 279 | + sprintf(buf, "%-32.32s - %s", fh->title, userid); |
| 280 | + strsncpy(postfile.title, buf, sizeof (postfile.title)); |
| 281 | + digestmode = (owned) ? 5 : 4; |
| 282 | + if (5 == digestmode) |
| 283 | + sprintf(buf, MY_BBS_HOME "/boards/%s/.JUNK", board); |
| 284 | + else |
| 285 | + sprintf(buf, MY_BBS_HOME "/boards/%s/.DELETED", board); |
| 286 | + append_record(buf, &postfile, sizeof (postfile)); |
| 287 | + if (strrchr(fh->owner, '.')) |
| 288 | + return; |
| 289 | + if ((fh->accessed & FH_INND) && fh->filetime > now_t - 14 * 86400) { |
| 290 | + sprintf(buf, MY_BBS_HOME "/boards/%s/%s", board, fh2fname(fh)); |
| 291 | + from[0] = '\0'; |
| 292 | + if ((fin = fopen(buf, "r")) != NULL) { |
| 293 | + while (fgets(buf, sizeof (buf), fin) != NULL) { |
| 294 | + len = strlen(buf) - 1; |
| 295 | + buf[len] = '\0'; |
| 296 | + if (len <= 8) |
| 297 | + break; |
| 298 | + if (strncmp(buf, "发信人: ", 8)) |
| 299 | + continue; |
| 300 | + if ((ptr = strrchr(buf, ')')) != NULL) { |
| 301 | + *ptr = '\0'; |
| 302 | + if ((ptr = strrchr(buf, '(')) |
| 303 | + != NULL) { |
| 304 | + strcpy(from, ptr + 1); |
| 305 | + break; |
| 306 | + } |
| 307 | + } |
| 308 | + } |
| 309 | + fclose(fin); |
| 310 | + } |
| 311 | + sprintf(buf, "%s\t%s\t%s\t%s\t%s\n", |
| 312 | + board, fh2fname(fh), fh->owner, from, fh->title); |
| 313 | + if ((fin = fopen("inndlog/cancel.bntp", "a")) != NULL) { |
| 314 | + fputs(buf, fin); |
| 315 | + fclose(fin); |
| 316 | + } |
| 317 | + } |
| 318 | +} |
| 319 | + |
| 320 | +int |
| 321 | +cmp_title(char *title, struct fileheader *fh1) |
| 322 | +{ |
| 323 | + char *p1; |
| 324 | + if (!strncasecmp(fh1->title, "Re:", 3)) |
| 325 | + p1 = fh1->title + 4; |
| 326 | + else |
| 327 | + p1 = fh1->title; |
| 328 | + return (!strncmp(p1, title, 45)); |
| 329 | +} |
| 330 | + |
| 331 | +int |
| 332 | +fh_find_thread(struct fileheader *fh, char *board) |
| 333 | +{ |
| 334 | + char direct[255]; |
| 335 | + char *p; |
| 336 | + int i; |
| 337 | + int start; |
| 338 | + struct mmapfile mf = { ptr:NULL }; |
| 339 | + struct fileheader *buf1; |
| 340 | + char *title = fh->title; |
| 341 | + int size = sizeof(struct fileheader); |
| 342 | + if (fh->thread != 0) |
| 343 | + return 0; |
| 344 | + fh->thread = fh->filetime; |
| 345 | + sprintf(direct, MY_BBS_HOME "/boards/%s/.DIR", board); |
| 346 | + if (mmapfile(direct, &mf) < 0) |
| 347 | + return -1; |
| 348 | + if (!strncasecmp(title, "Re:", 3)) |
| 349 | + p = title + 4; |
| 350 | + else |
| 351 | + p = title; |
| 352 | + start = mf.size / size; |
| 353 | + for (i = start, buf1 = (struct fileheader *)(mf.ptr + size * (start - 1)); i > start - 100 && i>0; i--, buf1 = (struct fileheader *)(mf.ptr + size * (i - 1))) |
| 354 | + //仅在最近100篇内尝试搜索同主题 |
| 355 | + if (cmp_title(p, buf1)) { |
| 356 | + if (buf1->thread != 0) { |
| 357 | + fh->thread = buf1->thread; |
| 358 | + break; |
| 359 | + } |
| 360 | + } |
| 361 | + mmapfile(NULL, &mf); |
| 362 | + return 0; |
| 363 | +} |
| 364 | + |
| 365 | +int |
| 366 | +Search_Bin(char *ptr, int key, int start, int end) |
| 367 | +{ |
| 368 | + // 在有序表中折半查找其关键字等于key的数据元素。 |
| 369 | + // 若查找到,返回索引 |
| 370 | + // 否则为大于key的最小数据元素索引m,返回(-m-1) |
| 371 | + int low, high, mid; |
| 372 | + struct fileheader *totest; |
| 373 | + low = start; |
| 374 | + high = end; |
| 375 | + while (low <= high) { |
| 376 | + mid = (low + high) / 2; |
| 377 | + totest = (struct fileheader *)(ptr + mid * sizeof(struct fileheader)); |
| 378 | + if (key == totest->filetime) |
| 379 | + return mid; |
| 380 | + else if (key < totest->filetime) |
| 381 | + high = mid - 1; |
| 382 | + else |
| 383 | + low = mid + 1; |
| 384 | + } |
| 385 | + return -(low+1); |
| 386 | +} |
| 387 | + |
| 388 | +int |
| 389 | +add_edit_mark(char *fname, char *userid, time_t now_t, char *fromhost) |
| 390 | +{ |
| 391 | + FILE *fp; |
| 392 | + if ((fp = fopen(fname, "a")) == NULL) |
| 393 | + return 0; |
| 394 | + fprintf(fp, |
| 395 | + "\n[1;36m※ 修改:.%s 于 %15.15s 修改本文.[FROM: %-.20s][m", |
| 396 | + userid, ctime(&now_t) + 4, fromhost); |
| 397 | + fclose(fp); |
| 398 | + return 0; |
| 399 | +} |
0 commit comments