1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
|
/* This file Copyright 1992, 1993 by Clifford A. Adams */
/* scorefile.c
*
* A simple "proof of concept" scoring file for headers.
* (yeah, right. :)
*/
#include "EXTERN.h"
#include "common.h"
#ifdef SCORE
/* if SCORE is undefined, no code should be compiled */
#include "list.h"
#include "hash.h"
#include "cache.h"
#include "bits.h" /* absfirst */
#include "head.h"
#include "search.h" /* regex matches */
#include "ngdata.h"
#include "ng.h"
#include "term.h" /* finish_command() */
#include "util.h"
#include "util2.h"
#include "env.h" /* getval */
#include "rt-util.h"
#include "mempool.h"
#include "score.h" /* shared stuff... */
#ifdef SCAN_ART
#include "scanart.h"
#include "samain.h" /* for sa_authscored macro */
#endif
#include "url.h"
#include "INTERN.h"
#include "scorefile.h"
#include "scorefile.ih"
/* list of score array markers (in htype field of score entry) */
/* entry is a file marker. Score is the file level */
#define SF_FILE_MARK_START (-1)
#define SF_FILE_MARK_END (-2)
/* other misc. rules */
#define SF_KILLTHRESHOLD (-3)
#define SF_NEWAUTHOR (-4)
#define SF_REPLY (-5)
static int sf_file_level INIT(0); /* how deep are we? */
static char sf_buf[LBUFLEN];
static char** sf_extra_headers = NULL;
static int sf_num_extra_headers = 0;
static bool sf_has_extra_headers;
/* Must be called before any other sf_ routine (once for each group) */
void
sf_init()
{
int i;
char* s;
int level; /* depth of newsgroup score file */
sf_num_entries = 0;
level = 0;
sf_extra_headers = NULL;
sf_num_extra_headers = 0;
/* initialize abbreviation list */
sf_abbr = (char**)safemalloc(256 * sizeof (char*));
bzero((char*)sf_abbr, 256 * sizeof (char*));
if (sf_verbose)
printf("\nReading score files...\n") FLUSH;
sf_file_level = 0;
/* find # of levels */
strcpy(sf_buf,filexp("%C"));
level = 0;
for (s = sf_buf; *s; s++)
if (*s == '.')
level++; /* count dots in group name */
level++;
/* the main read-in loop */
for (i = 0; i <= level; i++)
if ((s = sf_get_filename(i)) != NULL)
sf_do_file(s);
/* do post-processing (set thresholds and detect extra header usage) */
sf_has_extra_headers = FALSE;
/* set thresholds from the sf_entries */
reply_active = newauthor_active = kill_thresh_active = FALSE;
for (i = 0; i < sf_num_entries; i++) {
if (sf_entries[i].head_type >= HEAD_LAST)
sf_has_extra_headers = TRUE;
switch (sf_entries[i].head_type) {
case SF_KILLTHRESHOLD:
kill_thresh_active = TRUE;
kill_thresh = sf_entries[i].score;
if (sf_verbose) {
int j;
/* rethink? */
for (j = i+1; j < sf_num_entries; j++)
if (sf_entries[j].head_type == SF_KILLTHRESHOLD)
break;
if (j == sf_num_entries) /* no later thresholds */
printf("killthreshold %d\n",kill_thresh) FLUSH;
}
break;
case SF_NEWAUTHOR:
newauthor_active = TRUE;
newauthor = sf_entries[i].score;
if (sf_verbose) {
int j;
/* rethink? */
for (j = i+1; j < sf_num_entries; j++)
if (sf_entries[j].head_type == SF_NEWAUTHOR)
break;
if (j == sf_num_entries) /* no later newauthors */
printf("New Author score: %d\n",newauthor) FLUSH;
}
break;
case SF_REPLY:
reply_active = TRUE;
reply_score = sf_entries[i].score;
if (sf_verbose) {
int j;
/* rethink? */
for (j = i+1; j < sf_num_entries; j++)
if (sf_entries[j].head_type == SF_REPLY)
break;
if (j == sf_num_entries) /* no later reply rules */
printf("Reply score: %d\n",reply_score) FLUSH;
}
break;
}
}
}
void
sf_clean()
{
int i;
for (i = 0; i < sf_num_entries; i++) {
if (sf_entries[i].compex != NULL) {
free_compex(sf_entries[i].compex);
free(sf_entries[i].compex);
}
}
mp_free(MP_SCORE1); /* free memory pool */
if (sf_abbr) {
for (i = 0; i < 256; i++)
if (sf_abbr[i]) {
free(sf_abbr[i]);
sf_abbr[i] = NULL;
}
free(sf_abbr);
}
if (sf_entries)
free(sf_entries);
sf_entries = NULL;
for (i = 0; i < sf_num_extra_headers; i++)
free(sf_extra_headers[i]);
sf_num_extra_headers = 0;
sf_extra_headers = NULL;
}
/* rename sf_num_entries (to ?) */
/* use macro instead of all the "sf_entries[sf_num_entries-1]"?
* call it "sf_recent_entry" or "sf_last_entry"?
*/
void
sf_grow()
{
int i;
sf_num_entries++;
if (sf_num_entries == 1) {
sf_entries = (SF_ENTRY*)safemalloc(sizeof (SF_ENTRY));
} else {
sf_entries = (SF_ENTRY*)saferealloc((char*)sf_entries,
sf_num_entries * sizeof (SF_ENTRY));
}
i = sf_num_entries-1;
sf_entries[i].compex = NULL; /* init */
sf_entries[i].flags = 0;
sf_entries[i].str1 = NULL;
sf_entries[i].str2 = NULL;
}
/* Returns -1 if no matching extra header found, otherwise returns offset
* into the sf_extra_headers array.
*/
int
sf_check_extra_headers(head)
char* head; /* header name, (without ':' character) */
{
int i;
char* s;
static char lbuf[LBUFLEN];
/* convert to lower case */
safecpy(lbuf,head,sizeof lbuf - 1);
for (s = lbuf; *s; s++) {
if (isalpha(*s) && isupper(*s))
*s = tolower(*s); /* convert to lower case */
}
for (i = 0; i < sf_num_extra_headers; i++) {
if (strEQ(sf_extra_headers[i],lbuf))
return i;
}
return -1;
}
/* adds the header to the list of known extra headers if it is not already
* known.
*/
void
sf_add_extra_header(head)
char* head; /* new header name, (without ':' character) */
{
static char lbuf[LBUFLEN]; /* ick. */
int len;
char* colonptr; /* points to ':' character */
char* s;
char* s2;
/* check to see if it's already known */
/* first see if it is a known system header */
safecpy(lbuf,head,sizeof lbuf - 2);
len = strlen(lbuf);
lbuf[len] = ':';
lbuf[len+1] = '\0';
colonptr = lbuf+len;
if (set_line_type(lbuf,colonptr) != SOME_LINE)
return; /* known types should be interpreted in normal way */
/* then check to see if it's a known extra header */
if (sf_check_extra_headers(head) >= 0)
return;
sf_num_extra_headers++;
sf_extra_headers = (char**)saferealloc((char*)sf_extra_headers,
sf_num_extra_headers * sizeof (char*));
s = savestr(head);
for (s2 = s; *s2; s2++) {
if (isalpha(*s2) && isupper(*s2))
*s2 = tolower(*s2); /* convert to lower case */
}
sf_extra_headers[sf_num_extra_headers-1] = s;
}
char*
sf_get_extra_header(art,hnum)
ART_NUM art; /* article number to check */
int hnum; /* header number: offset into sf_extra_headers */
{
char* s;
char* head; /* header text */
int len; /* length of header */
static char lbuf[LBUFLEN];
parseheader(art); /* fast if already parsed */
head = sf_extra_headers[hnum];
len = strlen(head);
for (s = headbuf; s && *s && *s != '\n'; s++) {
if (strncaseEQ(head,s,len)) {
s = index(s,':');
if (!s)
return nullstr;
s++; /* skip the colon */
while (*s == ' ' || *s == '\t') s++;
if (!*s)
return nullstr;
head = s; /* now point to start of new text */
s = index(s,'\n');
if (!s)
return nullstr;
*s = '\0';
safecpy(lbuf,head,sizeof lbuf - 1);
*s = '\n';
return lbuf;
}
s = index(s,'\n'); /* '\n' will be skipped on loop increment */
}
return nullstr;
}
/* move to util.c ? */
/* Returns TRUE if text pointed to by s is a text representation of
* the number 0. Used for error checking.
* Note: does not check for trailing garbage ("+00kjsdfk" returns TRUE).
*/
bool
is_text_zero(s)
char* s;
{
return *s == '0' || ((*s == '+' || *s == '-') && s[1]=='0');
}
/* keep this one outside the functions because it is shared */
static char sf_file[LBUFLEN];
/* filenames of type a/b/c/foo.bar.misc for group foo.bar.misc */
char*
sf_get_filename(level)
int level;
{
char* s;
strcpy(sf_file,filexp(getval("SCOREDIR",DEFAULT_SCOREDIR)));
strcat(sf_file,"/");
#ifdef SHORTSCORENAMES
strcat(sf_file,filexp("%C"));
s1 = rindex(sf_file,'/'); /* find last slash in filename */
if (!level)
*s1 = '\0'; /* cut off slash for global */
i = level;
while (i--) {
*s1++ = '/';
while (*s1 != '.' && *s1 != '\0') s1++;
if (*s1 == '\0' && i > 0) /* not enough levels exist */
return NULL; /* get_file2 wouldn't work either... */
*s1 = '\0';
}
strcat(sf_file,"/SCORE");
#else /* !SHORTSCORENAMES */
if (!level) {
/* allow environment variable later... */
strcat(sf_file,"global");
} else {
strcat(sf_file,filexp("%C"));
s = rindex(sf_file,'/');
/* maybe redo this logic later... */
while (level--) {
if (*s == '\0') /* no more name to match */
return NULL;
while (*s && *s != '.') s++;
if (*s && level)
s++;
}
*s = '\0'; /* cut end of score file */
}
#endif /* SHORTSCORENAMES */
return sf_file;
}
/* given a string, if no slashes prepends SCOREDIR env. variable */
char*
sf_cmd_fname(s)
char* s;
{
static char lbuf[LBUFLEN];
char* s1;
s1 = index(s,'/');
if (s1)
return s;
/* no slashes in this filename */
strcpy(lbuf,getval("SCOREDIR",DEFAULT_SCOREDIR));
strcat(lbuf,"/");
strcat(lbuf,s);
return lbuf;
}
/* returns TRUE if good command, FALSE otherwise */
bool
sf_do_command(cmd,check)
char* cmd; /* text of command */
bool_int check; /* if TRUE, just check, don't execute */
{
char* s;
int i;
char ch;
if (strnEQ(cmd,"killthreshold",13)) {
/* skip whitespace and = sign */
for (s = cmd+13; *s && (*s == ' ' || *s == '\t' || *s == '='); s++) ;
/* make **sure** that there is a number here */
i = atoi(s);
if (i == 0) /* it might not be a number */
if (!is_text_zero(s)) {
printf("\nBad killthreshold: %s",cmd);
return FALSE; /* continue looping */
}
if (check)
return TRUE;
sf_grow();
sf_entries[sf_num_entries-1].head_type = SF_KILLTHRESHOLD;
sf_entries[sf_num_entries-1].score = i;
return TRUE;
}
if (strnEQ(cmd,"savescores",10)) {
/* skip whitespace and = sign */
for (s = cmd+10; *s && (*s == ' ' || *s == '\t' || *s == '='); s++) ;
if (strnEQ(s,"off",3)) {
if (!check)
sc_savescores = FALSE;
return TRUE;
}
if (*s) { /* there is some argument */
if (check)
return TRUE;
sc_savescores = TRUE;
return TRUE;
}
printf("Bad savescores command: |%s|\n",cmd) FLUSH;
return FALSE;
}
if (strnEQ(cmd,"newauthor",9)) {
/* skip whitespace and = sign */
for (s = cmd+9; *s && (*s == ' ' || *s == '\t' || *s == '='); s++) ;
/* make **sure** that there is a number here */
i = atoi(s);
if (i == 0) /* it might not be a number */
if (!is_text_zero(s)) {
printf("\nBad newauthor: %s",cmd);
return FALSE; /* continue looping */
}
if (check)
return TRUE;
sf_grow();
sf_entries[sf_num_entries-1].head_type = SF_NEWAUTHOR;
sf_entries[sf_num_entries-1].score = i;
return TRUE;
}
if (strnEQ(cmd,"include",7)) {
if (check)
return TRUE;
s = cmd+7;
while (*s == ' ' || *s == '\t') s++; /* skip whitespace */
if (!*s) {
printf("Bad include command (missing filename)\n");
return FALSE;
}
sf_do_file(filexp(sf_cmd_fname(s)));
return TRUE;
}
if (strnEQ(cmd,"exclude",7)) {
if (check)
return TRUE;
s = cmd+7;
while (*s == ' ' || *s == '\t') s++; /* skip whitespace */
if (!*s) {
printf("Bad exclude command (missing filename)\n");
return FALSE;
}
sf_exclude_file(filexp(sf_cmd_fname(s)));
return TRUE;
}
if (strnEQ(cmd,"header",6)) {
char* s2;
s = cmd+7;
while (*s == ' ' || *s == '\t') s++; /* skip whitespace */
for (s2 = s; *s2 && *s2 != ':'; s2++) ;
if (!s2) {
printf("\nBad header command (missing :)\n%s\n",cmd) FLUSH;
return FALSE;
}
if (check)
return TRUE;
*s2 = '\0';
sf_add_extra_header(s);
*s2 = ':';
return TRUE;
}
if (strnEQ(cmd,"begin",5)) {
s = cmd+6;
while (*s == ' ' || *s == '\t') s++; /* skip whitespace */
if (strnEQ(s,"score",5)) {
/* do something useful later */
return TRUE;
}
return TRUE;
}
if (strnEQ(cmd,"reply",5)) {
/* skip whitespace and = sign */
for (s = cmd+5; *s && (*s == ' ' || *s == '\t' || *s == '='); s++) ;
/* make **sure** that there is a number here */
i = atoi(s);
if (i == 0) /* it might not be a number */
if (!is_text_zero(s)) {
printf("\nBad reply command: %s\n",cmd);
return FALSE; /* continue looping */
}
if (check)
return TRUE;
sf_grow();
sf_entries[sf_num_entries-1].head_type = SF_REPLY;
sf_entries[sf_num_entries-1].score = i;
return TRUE;
}
if (strnEQ(cmd,"file",4)) {
if (check)
return TRUE;
s = cmd+4;
while (*s == ' ' || *s == '\t') s++; /* skip whitespace */
if (!*s) {
printf("Bad file command (missing parameters)\n");
return FALSE;
}
ch = *s++;
while ((*s == ' ') || (*s == '\t'))
s++; /* skip whitespace */
if (!*s) {
printf("Bad file command (missing parameters)\n");
return FALSE;
}
if (sf_abbr[(int)ch])
free(sf_abbr[(int)ch]);
sf_abbr[(int)ch] = savestr(sf_cmd_fname(s));
return TRUE;
}
if (strnEQ(cmd,"end",3)) {
s = cmd+4;
while (*s == ' ' || *s == '\t') s++; /* skip whitespace */
if (strnEQ(s,"score",5)) {
/* do something useful later */
return TRUE;
}
return TRUE;
}
if (strnEQ(cmd,"newsclip",8)) {
printf("Newsclip is no longer supported.\n") FLUSH;
return FALSE;
}
/* no command matched */
printf("Unknown command: |%s|\n",cmd) FLUSH;
return FALSE;
}
COMPEX* sf_compex INIT(NULL);
char*
sf_freeform(start1,end1)
char* start1; /* points to first character of keyword */
char* end1; /* points to last character of keyword */
{
char* s;
bool error;
char ch;
error = FALSE; /* be optimistic :-) */
/* cases are # of letters in keyword */
switch (end1-start1+1) {
case 7:
if (strnEQ(start1,"pattern",7)) {
sf_pattern_status = TRUE;
break;
}
error = TRUE;
break;
case 4:
#ifdef UNDEF
/* here is an example of a hypothetical freeform key with an argument */
if (strnEQ(start1,"date",4)) {
char* s1;
int datenum;
/* skip whitespace and = sign */
for (s = end1+1; *s && (*s == ' ' || *s == '\t'); s++) ;
if (!*s) { /* ran out of line */
printf("freeform: date keyword: ran out of input\n");
return s;
}
datenum = atoi(s);
printf("Date: %d\n",datenum) FLUSH;
while (isdigit(*s)) s++; /* skip datenum */
end1 = s; /* end of key data */
break;
}
#endif
error = TRUE;
break;
default:
error = TRUE;
break;
}
if (error) {
s = end1+1;
ch = *s;
*s = '\0';
printf("Scorefile freeform: unknown key: |%s|\n",start1) FLUSH;
*s = ch;
return NULL; /* error indicated */
}
/* no error, so skip whitespace at end of key */
for (s = end1+1; *s && (*s == ' ' || *s == '\t'); s++) ;
return s;
}
bool
sf_do_line(line,check)
char* line;
bool_int check; /* if TRUE, just check the line, don't act. */
{
char ch;
char* s;
char* s2;
int i,j;
if (!line || !*line)
return TRUE; /* very empty line */
s = line + strlen(line) - 1;
if (*s == '\n')
*s = '\0'; /* kill the newline */
ch = line[0];
if (ch == '#') /* comment */
return TRUE;
/* reset any per-line bitflags */
sf_pattern_status = FALSE;
if (isalpha(ch)) /* command line */
return sf_do_command(line,check);
/* skip whitespace */
for (s = line; *s && (*s == ' ' || *s == '\t'); s++) ;
if (!*s || *s == '#')
return TRUE; /* line was whitespace or comment after whitespace */
/* convert line to lowercase (make optional later?) */
for (s2 = s; *s2 != '\0'; s2++) {
if (isupper(*s2))
*s2 = tolower(*s2); /* convert to lower case */
}
i = atoi(s);
if (i == 0) { /* it might not be a number */
if (!is_text_zero(s)) {
printf("\nBad scorefile line:\n|%s|\n",s);
return FALSE;
}
}
/* add the line as a scoring entry */
while (isdigit(*s) || *s == '+' || *s == '-' || *s == ' ' || *s == '\t')
s++; /* skip score */
while (TRUE) {
for (s2 = s; *s2 && !(*s2 == ' ' || *s2 == '\t'); s2++) ;
s2--;
if (*s2 == ':') /* did header */
break; /* go to set header routine */
s = sf_freeform(s,s2);
if (!s || !*s) { /* used up all the line's text, or error */
printf("Scorefile entry error error (freeform parse). ");
printf("Line was:\n|%s|\n",line) FLUSH;
return FALSE; /* error */
}
s2 = s;
} /* while */
/* s is start of header name, s2 points to the ':' character */
j = set_line_type(s,s2);
if (j == SOME_LINE) {
*s2 = '\0';
j = sf_check_extra_headers(s);
*s2 = ':';
if (j >= 0)
j += HEAD_LAST;
else {
printf("Unknown score header type. Line follows:\n|%s|\n",line);
return FALSE;
}
}
/* skip whitespace */
for (s = ++s2; *s && (*s == ' ' || *s == '\t'); s++) ;
if (!*s) { /* no pattern */
printf("Empty score pattern. Line follows:\n|%s|\n",line) FLUSH;
return FALSE;
}
if (check)
return TRUE; /* limits of check */
sf_grow(); /* acutally make an entry */
sf_entries[sf_num_entries-1].head_type = j;
sf_entries[sf_num_entries-1].score = i;
if (sf_pattern_status) { /* in pattern matching mode */
sf_entries[sf_num_entries-1].flags |= 1;
sf_entries[sf_num_entries-1].str1 = mp_savestr(s,MP_SCORE1);
sf_compex = (COMPEX*)safemalloc(sizeof (COMPEX));
init_compex(sf_compex);
/* compile arguments: */
/* 1st is COMPEX to store compiled regex in */
/* 2nd is search string */
/* 3rd should be TRUE if the search string is a regex */
/* 4th is TRUE for case-insensitivity */
s2 = compile(sf_compex,s,TRUE,TRUE);
if (s2 != NULL) {
printf("Bad pattern : |%s|\n",s) FLUSH;
printf("Compex returns: |%s|\n",s2) FLUSH;
free_compex(sf_compex);
free(sf_compex);
sf_entries[sf_num_entries-1].compex = NULL;
return FALSE;
} else
sf_entries[sf_num_entries-1].compex = sf_compex;
}
else {
sf_entries[sf_num_entries-1].flags &= 0xfe;
sf_entries[sf_num_entries-1].str2 = NULL;
/* Note: consider allowing * wildcard on other header filenames */
if (j == FROM_LINE) { /* may have * wildcard */
if ((s2 = index(s,'*')) != NULL) {
sf_entries[sf_num_entries-1].str2 = mp_savestr(s2+1,MP_SCORE1);
*s2 = '\0';
}
}
sf_entries[sf_num_entries-1].str1 = mp_savestr(s,MP_SCORE1);
}
return TRUE;
}
void
sf_do_file(fname)
char* fname;
{
char* s;
int sf_fp;
int i;
char* safefilename;
#ifdef SCOREFILE_CACHE
sf_fp = sf_open_file(fname);
if (sf_fp < 0)
return;
#else
fp = fopen(fname,"r");
if (!fp)
return;
#endif
sf_file_level++;
if (sf_verbose) {
for (i = 1; i < sf_file_level; i++)
printf("."); /* maybe later putchar... */
printf("Score file: %s\n",fname) FLUSH;
}
safefilename = savestr(fname);
/* add end marker to scoring array */
sf_grow();
sf_entries[sf_num_entries-1].head_type = SF_FILE_MARK_START;
/* file_level is 1 to n */
sf_entries[sf_num_entries-1].score = sf_file_level;
sf_entries[sf_num_entries-1].str2 = NULL;
sf_entries[sf_num_entries-1].str1 = savestr(safefilename);
#ifdef SCOREFILE_CACHE
while ((s = sf_file_getline(sf_fp)) != NULL) {
strcpy(sf_buf,s);
s = sf_buf;
#else
while ((s = fgets(sf_buf,1020,fp)) != NULL) { /* consider buffer size */
#endif
(void)sf_do_line(s,FALSE);
}
#ifndef SCOREFILE_CACHE
fclose(fp);
#endif
/* add end marker to scoring array */
sf_grow();
sf_entries[sf_num_entries-1].head_type = SF_FILE_MARK_END;
/* file_level is 1 to n */
sf_entries[sf_num_entries-1].score = sf_file_level;
sf_entries[sf_num_entries-1].str2 = NULL;
sf_entries[sf_num_entries-1].str1 = savestr(safefilename);
free(safefilename);
sf_file_level--;
}
int
score_match(str,ind)
char* str; /* string to match on */
int ind; /* index into sf_entries */
{
char* s1;
char* s2;
char* s3;
s1 = sf_entries[ind].str1;
s2 = sf_entries[ind].str2;
if (sf_entries[ind].flags & 1) { /* pattern style match */
if (sf_entries[ind].compex != NULL) {
/* we have a good pattern */
s2 = execute(sf_entries[ind].compex,str);
if (s2 != NULL)
return TRUE;
}
return FALSE;
}
/* default case */
if ((s3 = STRSTR(str,s1)) != NULL && (!s2 || STRSTR(s3+strlen(s1),s2)))
return TRUE;
return FALSE;
}
int
sf_score(a)
ART_NUM a;
{
int sum,i,j;
int h; /* header type */
char* s; /* misc */
bool old_untrim; /* old value of untrim_cache */
if (is_unavailable(a))
return LOWSCORE; /* unavailable arts get low negative score. */
/* if there are no score entries, then the answer is real easy and quick */
if (sf_num_entries == 0)
return 0;
old_untrim = untrim_cache;
untrim_cache = TRUE;
sc_scoring = TRUE; /* loop prevention */
sum = 0;
/* parse the header now if there are extra headers */
/* (This could save disk accesses.) */
if (sf_has_extra_headers)
parseheader(a);
for (i = 0; i < sf_num_entries; i++) {
h = sf_entries[i].head_type;
if (h <= 0) /* don't use command headers for scoring */
continue; /* the outer for loop */
/* if this head_type has been done before, this entry
has already been done */
if (sf_entries[i].flags & 2) { /* rule has been applied */
sf_entries[i].flags &= 0xfd; /* turn off flag */
continue; /* ...with the next rule */
}
/* sf_get_line will return ptr to buffer (already lowercased string) */
s = sf_get_line(a,h);
if (!s || !*s) /* no such line for the article */
continue; /* with the sf_entries. */
/* do the matches for this header */
for (j = i; j < sf_num_entries; j++) {
/* see if there is a match */
if (h == sf_entries[j].head_type) {
if (j != i) { /* set flag only for future rules */
sf_entries[j].flags |= 2; /* rule has been applied. */
}
if (score_match(s,j)) {
sum = sum + sf_entries[j].score;
if (h == FROM_LINE)
article_ptr(a)->scoreflags |= SFLAG_AUTHOR;
if (sf_score_verbose)
sf_print_match(j);
}
}
}
}
if (newauthor_active && !(article_ptr(a)->scoreflags & SFLAG_AUTHOR)) {
sum = sum+newauthor; /* add new author bonus */
if (sf_score_verbose) {
printf("New Author: %d\n",newauthor) FLUSH;
/* consider: print which file the bonus came from */
}
}
if (reply_active) {
/* should be in cache if a rule above used the subject */
s = fetchcache(a, SUBJ_LINE, TRUE);
/* later: consider other possible reply forms (threading?) */
if (s && subject_has_Re(s,(char**)NULL)) {
sum = sum+reply_score;
if (sf_score_verbose) {
printf("Reply: %d\n",reply_score);
/* consider: print which file the bonus came from */
}
}
}
untrim_cache = old_untrim;
sc_scoring = FALSE;
return sum;
}
/* returns changed score line or NULL if no changes */
char*
sf_missing_score(line)
char* line;
{
static char lbuf[LBUFLEN];
int i;
char* s;
/* save line since it is probably pointing at (the TRN-global) buf */
s = savestr(line);
printf("Possibly missing score.\n\
Type a score now or delete the colon to abort this entry:\n") FLUSH;
buf[0] = ':';
buf[1] = FINISHCMD;
i = finish_command(TRUE); /* print the CR */
if (!i) { /* there was no score */
free(s);
return NULL;
}
strcpy(lbuf,buf+1);
i = strlen(lbuf);
lbuf[i] = ' ';
lbuf[i+1] = '\0';
strcat(lbuf,s);
free(s);
return lbuf;
}
/* Interprets the '\"' command for creating new score entries online */
/* consider using some external buffer rather than the 2 internal ones */
void
sf_append(line)
char* line;
{
char* scoreline; /* full line to add to scorefile */
char* scoretext; /* text after the score# */
char filechar; /* filename character from line */
char* filename; /* expanded filename */
static char filebuf[LBUFLEN];
FILE* fp;
char ch; /* misc */
char* s;
if (!line)
return; /* do nothing with empty string */
filechar = *line; /* ch is file abbreviation */
if (filechar == '?') { /* list known file abbreviations */
int i;
printf("List of abbreviation/file pairs\n") ;
for (i = 0; i < 256; i++)
if (sf_abbr[i])
printf("%c %s\n",(char)i,sf_abbr[i]) FLUSH;
printf("\" [The current newsgroup's score file]\n") FLUSH;
printf("* [The global score file]\n") FLUSH;
return;
}
/* skip whitespace after filechar */
scoreline = line+1;
while (*scoreline == ' ' || *scoreline == '\t') scoreline++;
ch = *scoreline; /* first non-whitespace after filechar */
/* If the scorefile line does not begin with a number,
and is not a valid command, request a score */
if (!isdigit(ch) && ch != '+' && ch != '-' && ch != ':' && ch != '!'
&& ch != '#') {
if (!sf_do_line(scoreline,TRUE)) { /* just checking */
scoreline = sf_missing_score(scoreline);
if (!scoreline) { /* no score typed */
printf("Score entry aborted.\n") FLUSH;
return;
}
}
}
/* scoretext = first non-whitespace after score# */
for (scoretext = scoreline;
isdigit(*scoretext) || *scoretext == '+' || *scoretext == '-'
|| *scoretext == ' ' || *scoretext == '\t';
scoretext++) {
;
}
/* special one-character shortcuts */
if (*scoretext && scoretext[1] == '\0') {
static char lbuf[LBUFLEN];
switch(*scoretext) {
case 'F': /* domain-shortened FROM line */
strcpy(lbuf,scoreline);
lbuf[strlen(lbuf)-1] = '\0';
strcat(lbuf,filexp("from: %y"));
scoreline = lbuf;
break;
case 'S': /* current subject */
strcpy(lbuf,scoreline);
s = fetchcache(art,SUBJ_LINE,TRUE);
if (!s || !*s) {
printf("No subject: score entry aborted.\n");
return;
}
if (s[0] == 'R' && s[1] == 'e' && s[2] == ':' && s[3] == ' ')
s += 4;
/* change this next line if LBUFLEN changes */
sprintf(lbuf+(strlen(lbuf)-1),"subject: %.900s",s);
scoreline = lbuf;
break;
default:
printf("\nBad scorefile line: |%s| (not added)\n", line) FLUSH;
return;
}
printf("%s\n",scoreline) FLUSH;
}
/* test the scoring line unless filechar is '!' (meaning do it now) */
if (!sf_do_line(scoreline,filechar!='!')) {
printf("Bad score line (ignored)\n") FLUSH;
return;
}
if (filechar == '!')
return; /* don't actually append to file */
if (filechar == '"') { /* do local group */
/* Note: should probably be changed to use sf_ file functions */
strcpy(filebuf,getval("SCOREDIR",DEFAULT_SCOREDIR));
#ifdef SHORTSCORENAMES
strcat(filebuf,"/%c/SCORE");
#else
strcat(filebuf,"/%C");
#endif
filename = filebuf;
}
else if (filechar == '*') { /* do global scorefile */
/* Note: should probably be changed to use sf_ file functions */
strcpy(filebuf,getval("SCOREDIR",DEFAULT_SCOREDIR));
strcat(filebuf,"/global");
filename = filebuf;
}
else if (!(filename = sf_abbr[(int)filechar])) {
printf("\nBad file abbreviation: %c\n",filechar) FLUSH;
return;
}
filename = filexp(sf_cmd_fname(filename)); /* allow shortcuts */
/* make sure directory exists... */
makedir(filename,MD_FILE);
#ifdef SCOREFILE_CACHE
sf_file_clear();
#endif
if ((fp = fopen(filename,"a")) != NULL) { /* open (or create) for append */
fprintf(fp,"%s\n",scoreline);
fclose(fp);
}
else /* unsuccessful in opening file */
printf("\nCould not open (for append) file %s\n",filename);
return;
}
/* returns a lowercased copy of the header line type h in private buffer */
char*
sf_get_line(a,h)
ART_NUM a;
int h;
{
static char sf_getline[LBUFLEN];
char* s;
if (h <= SOME_LINE) {
printf("sf_get_line(%d,%d): bad header type\n",(int)a,h) FLUSH;
printf("(Internal error: header number too low)\n") FLUSH;
*sf_getline = '\0';
return sf_getline;
}
if (h >= HEAD_LAST) {
if (h-HEAD_LAST < sf_num_extra_headers)
s = sf_get_extra_header(a,h-HEAD_LAST);
else {
printf("sf_get_line(%d,%d): bad header type\n",(int)a,h) FLUSH;
printf("(Internal error: header number too high)\n") FLUSH;
*sf_getline = '\0';
return sf_getline;
}
} else if (h == SUBJ_LINE)
s = fetchcache(a,h,TRUE); /* get compressed copy */
else
s = prefetchlines(a,h,FALSE); /* don't make a copy */
if (!s)
*sf_getline = '\0';
else
safecpy(sf_getline,s,sizeof sf_getline - 1);
for (s = sf_getline; *s; s++)
if (isupper(*s))
*s = tolower(*s);
*s = tolower(*s);
return sf_getline;
}
/* given an index into sf_entries, print information about that index */
void
sf_print_match(indx)
int indx;
{
int i,j,k;
int level,tmplevel; /* level is initialized iff used */
char* head_name;
char* pattern;
for (i = indx; i >= 0; i--) {
j = sf_entries[i].head_type;
if (j == SF_FILE_MARK_START) /* found immediate inclusion. */
break;
if (j == SF_FILE_MARK_END) { /* found included file, skip */
tmplevel = sf_entries[i].score;
for (k = i; k >= 0; k--) {
if (sf_entries[k].head_type == SF_FILE_MARK_START
&& sf_entries[k].score == tmplevel)
break; /* inner for loop */
}
i = k; /* will be decremented again */
}
}
if (i >= 0)
level = sf_entries[i].score;
/* print the file markers. */
for ( ; i >= 0; i--) {
if (sf_entries[i].head_type == SF_FILE_MARK_START
&& sf_entries[i].score <= level) {
level--; /* go out... */
for (k = 0; k < level; k++)
printf("."); /* make putchar later? */
printf("From file: %s\n",sf_entries[i].str1);
if (level == 0) /* top level */
break; /* out of the big for loop */
}
}
if (sf_entries[indx].flags & 1) /* regex type */
pattern = "pattern ";
else
pattern = "";
if (sf_entries[indx].head_type >= HEAD_LAST)
head_name = sf_extra_headers[sf_entries[indx].head_type-HEAD_LAST];
else
head_name = htype[sf_entries[indx].head_type].name;
printf("%d %s%s: %s", sf_entries[indx].score,pattern,head_name,
sf_entries[indx].str1);
if (sf_entries[indx].str2)
printf("*%s",sf_entries[indx].str2);
printf("\n");
}
void
sf_exclude_file(fname)
char* fname;
{
int start,end;
int newnum;
SF_ENTRY* tmp_entries;
for (start = 0; start < sf_num_entries; start++)
if (sf_entries[start].head_type == SF_FILE_MARK_START
&& strEQ(sf_entries[start].str1,fname))
break;
if (start == sf_num_entries) {
printf("Exclude: file |%s| was not included\n",fname) FLUSH;
return;
}
for (end = start+1; end < sf_num_entries; end++)
if (sf_entries[end].head_type==SF_FILE_MARK_END
&& strEQ(sf_entries[end].str1,fname))
break;
if (end == sf_num_entries) {
printf("Exclude: file |%s| is incomplete at exclusion command\n",
fname) FLUSH;
/* insert more explanation later? */
return;
}
newnum = sf_num_entries-(end-start)-1;
#ifdef UNDEF
/* Deal with exclusion of all scorefile entries.
* This cannot happen since the exclusion command has to be within a
* file. Code kept in case online exclusions allowed later.
*/
if (newnum==0) {
sf_num_entries = 0;
free(sf_entries);
sf_entries = NULL;
return;
}
#endif
tmp_entries = (SF_ENTRY*)safemalloc(newnum*sizeof(SF_ENTRY));
/* copy the parts into tmp_entries */
if (start > 0)
bcopy((char*)sf_entries,(char*)tmp_entries,start * sizeof (SF_ENTRY));
if (end < sf_num_entries-1)
bcopy((char*)(sf_entries+end+1), (char*)(tmp_entries+start),
(sf_num_entries-end-1) * sizeof (SF_ENTRY));
free(sf_entries);
sf_entries = tmp_entries;
sf_num_entries = newnum;
if (sf_verbose)
printf("Excluded file: %s\n",fname) FLUSH;
}
void
sf_edit_file(filespec)
char* filespec; /* file abbrev. or name */
{
char filebuf[LBUFLEN]; /* clean up buffers */
char filechar; /* which file to do? */
char* fname_noexpand; /* non-expanded filename */
if (!filespec || !*filespec)
return; /* empty, do nothing (error later?) */
filechar = *filespec;
/* if more than one character use as filename */
if (filespec[1])
strcpy(filebuf,filespec);
else if (filechar == '"') { /* edit local group */
/* Note: should probably be changed to use sf_ file functions */
strcpy(filebuf,getval("SCOREDIR",DEFAULT_SCOREDIR));
#ifdef SHORTSCORENAMES
strcat(filebuf,"/%c/SCORE");
#else
strcat(filebuf,"/%C");
#endif
}
else if (filechar == '*') { /* edit global scorefile */
/* Note: should probably be changed to use sf_ file functions */
strcpy(filebuf,getval("SCOREDIR",DEFAULT_SCOREDIR));
strcat(filebuf,"/global");
}
else { /* abbreviation */
if (!sf_abbr[(int)filechar]) {
printf("\nBad file abbreviation: %c\n",filechar) FLUSH;
return;
}
strcpy(filebuf,sf_abbr[(int)filechar]);
}
fname_noexpand = sf_cmd_fname(filebuf);
strcpy(filebuf,filexp(fname_noexpand));
/* make sure directory exists... */
if (makedir(filebuf,MD_FILE) == 0) {
(void)edit_file(fname_noexpand);
#ifdef SCOREFILE_CACHE
sf_file_clear();
#endif
}
else
printf("Can't make %s\n",filebuf) FLUSH;
}
/* returns file number */
/* if file number is negative, the file does not exist or cannot be opened */
#ifdef SCOREFILE_CACHE
static int
sf_open_file(name)
char* name;
{
FILE* fp;
char* temp_name;
char* s;
int i;
if (!name || !*name)
return 0; /* unable to open */
for (i = 0; i < sf_num_files; i++)
if (strEQ(sf_files[i].fname,name)) {
if (sf_files[i].num_lines < 0) /* nonexistent */
return -1; /* no such file */
sf_files[i].line_on = 0;
return i;
}
sf_num_files++;
sf_files = (SF_FILE*)saferealloc((char*)sf_files,
sf_num_files * sizeof (SF_FILE));
sf_files[i].fname = savestr(name);
sf_files[i].num_lines = 0;
sf_files[i].num_alloc = 0;
sf_files[i].line_on = 0;
sf_files[i].lines = NULL;
temp_name = NULL;
if (strncaseEQ(name,"URL:",4)) {
#ifdef USEURL
char lbuf[1024];
safecpy(lbuf,name,sizeof lbuf - 4);
name = lbuf;
temp_name = temp_filename();
if (!url_get(name+4,temp_name))
name = NULL;
else
name = temp_name;
#else
printf("\nThis copy of strn does not have URL support.\n") FLUSH;
name = NULL;
#endif
}
if (!name) {
sf_files[i].num_lines = -1;
return -1;
}
fp = fopen(name,"r");
if (!fp) {
sf_files[i].num_lines = -1;
return -1;
}
while ((s = fgets(sf_buf,LBUFLEN-4,fp)) != NULL) {
if (sf_files[i].num_lines >= sf_files[i].num_alloc) {
sf_files[i].num_alloc += 100;
sf_files[i].lines = (char**)saferealloc((char*)sf_files[i].lines,
sf_files[i].num_alloc*sizeof(char**));
}
/* CAA: I kind of like the next line in a twisted sort of way. */
sf_files[i].lines[sf_files[i].num_lines++] = mp_savestr(s,MP_SCORE2);
}
fclose(fp);
if (temp_name)
UNLINK(temp_name);
return i;
}
#endif /* SCOREFILE_CACHE */
#ifdef SCOREFILE_CACHE
static void
sf_file_clear()
{
int i;
for (i = 0; i < sf_num_files; i++) {
if (sf_files[i].fname)
free(sf_files[i].fname);
if (sf_files[i].num_lines > 0) {
/* memory pool takes care of freeing line contents */
free(sf_files[i].lines);
}
}
mp_free(MP_SCORE2);
if (sf_files)
free(sf_files);
sf_files = (SF_FILE*)NULL;
sf_num_files = 0;
}
#endif /* SCOREFILE_CACHE */
#ifdef SCOREFILE_CACHE
static char*
sf_file_getline(fnum)
int fnum;
{
if (fnum < 0 || fnum >= sf_num_files)
return NULL;
if (sf_files[fnum].line_on >= sf_files[fnum].num_lines)
return NULL; /* past end of file, or empty file */
/* below: one of the more twisted lines of my career (:-) */
return sf_files[fnum].lines[sf_files[fnum].line_on++];
}
#endif /* SCOREFILE_CACHE */
#endif /* SCORE */
|