home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / r / rusn-09.zip / RUSN-FUN.PAS < prev    next >
Pascal/Delphi Source File  |  1992-11-03  |  18KB  |  855 lines

  1. {
  2.  
  3. rusn-fun.pas - rusnews functions
  4.  
  5. }
  6.  
  7. function integertozstring(i, width: integer): string;
  8.  
  9. var
  10.   result: string;
  11.  
  12. begin
  13.   str(i,result);
  14.   while length(result)<width do
  15.     result := '0'+result;
  16.   integertozstring := result;
  17. end;
  18.  
  19. function time: string;
  20.  
  21. var
  22.   h,m,s,s00: word;
  23.  
  24. begin
  25.   gettime(h,m,s,s00);
  26.   time := integertozstring(h,2)+':'+integertozstring(m,2)+':'+
  27.    integertozstring(s,2);
  28. end;
  29.  
  30. function timedigits: string;
  31.  
  32. var
  33.   h,m,s,s00: word;
  34.  
  35. begin
  36.   gettime(h,m,s,s00);
  37.   timedigits :=
  38.    integertozstring(h,2)+integertozstring(m,2)+integertozstring(s,2);
  39. end;
  40.  
  41. function cdow: string;
  42.  
  43. var
  44.   y,m,d,dow: word;
  45.   result: string;
  46.  
  47. begin
  48.   getdate(y,m,d,dow);
  49.   result := 'Sunday';
  50.   if dow=1 then result := 'Monday';
  51.   if dow=2 then result := 'Tuesday';
  52.   if dow=3 then result := 'Wednesday';
  53.   if dow=4 then result := 'Thursday';
  54.   if dow=5 then result := 'Friday';
  55.   if dow=6 then result := 'Saturday';
  56.   cdow := result;
  57. end;
  58.  
  59. function dayofmonth: integer;
  60.  
  61. var
  62.   y,m,d,dow: word;
  63.  
  64. begin
  65.   getdate(y,m,d,dow);
  66.   dayofmonth := d;
  67. end;
  68.  
  69. function month: integer;
  70.  
  71. var
  72.   y,m,d,dow: word;
  73.  
  74. begin
  75.   getdate(y,m,d,dow);
  76.   month := m;
  77. end;
  78.  
  79. function monthname: string;
  80.  
  81. var
  82.   themonth: integer;
  83.   result: string;
  84.  
  85. begin
  86.   themonth := month;
  87.   result := 'January';
  88.   if themonth=2  then result := 'February';
  89.   if themonth=3  then result := 'March';
  90.   if themonth=4  then result := 'April';
  91.   if themonth=5  then result := 'May';
  92.   if themonth=6  then result := 'June';
  93.   if themonth=7  then result := 'July';
  94.   if themonth=8  then result := 'August';
  95.   if themonth=9  then result := 'September';
  96.   if themonth=10 then result := 'October';
  97.   if themonth=11 then result := 'November';
  98.   if themonth=12 then result := 'December';
  99.   monthname := result;
  100. end;
  101.  
  102. function year: integer;
  103.  
  104. var
  105.   y,m,d,dow: word;
  106.  
  107. begin
  108.   getdate(y,m,d,dow);
  109.   year := y;
  110. end;
  111.  
  112. function getenv(s: string): string;
  113.  
  114. var
  115.   i: integer;
  116.   envseg: word;
  117.   envread: integer;
  118.   firstb: byte;
  119.   thisb: byte;
  120.   varname: string;
  121.   vardata: string;
  122.   done: boolean;
  123.   result: string;
  124.  
  125. begin
  126.   result := '';
  127.   envseg := memw[prefixseg:$2c];
  128.   envread := 0;
  129.   repeat
  130.     firstb := mem[envseg:envread];
  131.     if firstb>0 then
  132.       begin
  133.         varname := '';
  134.         repeat
  135.           thisb := mem[envseg:envread];
  136.           inc(envread);
  137.           if thisb<>ord('=') then
  138.             varname := varname+chr(thisb);
  139.         until thisb=ord('=');
  140.         vardata := '';
  141.         repeat
  142.           thisb := mem[envseg:envread];
  143.           inc(envread);
  144.           if thisb>0 then
  145.             vardata := vardata+chr(thisb);
  146.         until thisb=0;
  147.         done := (varname=s);
  148.         if done then
  149.           result := vardata;
  150.     end;
  151.   until (firstb=0) or done;
  152.   getenv := result;
  153. end;
  154.  
  155. function basesitename(s: string): string;
  156.  
  157. var
  158.   atbang: integer;
  159.   atpercent: integer;
  160.   atat: integer;
  161.   result: string;
  162.   work: string;
  163.   atdot: integer;
  164.  
  165. begin
  166.   result := uucpname;
  167.   atbang := pos('!',s);
  168.   atpercent := pos('%',s);
  169.   atat := pos('@',s);
  170.   if atbang>0 then
  171.     begin
  172.       work := s;
  173.       while atbang>0 do
  174.         begin
  175.           result := copy(work,1,atbang-1);
  176.           work := copy(work,atbang+1,255);
  177.           atbang := pos('!',work);
  178.         end;
  179.     end
  180.   else if atpercent>0 then
  181.     begin
  182.       result := copy(s,atpercent+1,255);
  183.       atat := pos('@',result);
  184.       if atat>0 then
  185.         result := copy(result,1,atat-1);
  186.     end
  187.   else if atat>0 then
  188.     begin
  189.       result := copy(s,atat+1,255);
  190.     end;
  191.   atdot := pos('.',result);
  192.   if atdot>0 then
  193.     result := copy(result,1,atdot-1);
  194.   basesitename := result;
  195. end;
  196.  
  197. function unquote(s: string): string;
  198.  
  199. begin
  200.   if (copy(s,1,1)='"') and (copy(s,length(s),1)='"') then
  201.     unquote := copy(s,2,length(s)-2)
  202.   else
  203.     unquote := s;
  204. end;
  205.  
  206. function unslash(s: string): string;
  207.  
  208. var
  209.   i: integer;
  210.   result: string;
  211.  
  212. begin
  213.   result := s;
  214.   for i := 1 to length(result) do
  215.     if result[i]='/' then
  216.       result[i] := '\';
  217.   unslash := result;
  218. end;
  219.  
  220. function ununderscore(s: string): string;
  221.  
  222. var
  223.   i: integer;
  224.   result: string;
  225.  
  226. begin
  227.   result := s;
  228.   for i := 1 to length(result) do
  229.     if result[i]='_' then
  230.       result[i] := ' ';
  231.   ununderscore := result;
  232. end;
  233.  
  234. function atoi(s: string): integer;
  235.  
  236. var
  237.   result: integer;
  238.   code: word;
  239.  
  240. begin
  241.   val(s,result,code);
  242.   atoi := result;
  243. end;
  244.  
  245. function itoa(i: integer): string;
  246.  
  247. begin
  248.   itoa := integertozstring(i,0);
  249. end;
  250.  
  251. function upper(s: string): string;
  252.  
  253. var
  254.   result: string;
  255.   i: integer;
  256.  
  257. begin
  258.   result := s;
  259.   for i := 1 to length(s) do
  260.     result[i] := upcase(result[i]);
  261.   upper := result;
  262. end;
  263.  
  264. function lower(s: string): string;
  265.  
  266. var
  267.   result: string;
  268.   i: integer;
  269.  
  270. begin
  271.   result := s;
  272.   for i := 1 to length(s) do
  273.     if (result[i]>='A') and (result[i]<='Z') then
  274.       result[i] := chr(ord(result[i])-ord('A')+ord('a'));
  275.   lower := result;
  276. end;
  277.  
  278. function ltrim(s: string): string;
  279.  
  280. var
  281.   result: string;
  282.  
  283. begin
  284.   result := s;
  285.   while ((result[1]=' ') or (result[1]=^I)) and (length(result)>0) do
  286.     result := copy(result,2,255);
  287.   ltrim := result;
  288. end;
  289.  
  290. function trim(s: string): string;
  291.  
  292. var
  293.   result: string;
  294.  
  295. begin
  296.   result := s;
  297.   while ((result[length(result)]=' ') or (result[length(result)]=^I)) and
  298.    (length(result)>0) do
  299.     result := copy(result,1,length(result)-1);
  300.   trim := result;
  301. end;
  302.  
  303. function newseqnumber: integer;
  304.  
  305. var
  306.   seqf: text;
  307.   seqfn: string;
  308.   newseq: integer;
  309.  
  310. begin
  311.   if wafversion='1.64' then
  312.     seqfn := waffledir+'\system\'+'seqf'
  313.   else
  314.     seqfn := waffledir+'\uucp\'+'sequence';
  315.   assign(seqf,seqfn);
  316.   reset(seqf);
  317.   readln(seqf,newseq);
  318.   close(seqf);
  319.   rewrite(seqf);
  320.   writeln(seqf,integertozstring(newseq+1,4));
  321.   close(seqf);
  322.   newseqnumber := newseq;
  323. end;
  324.  
  325. function randomletter: char;
  326.  
  327. begin
  328.   if random(2)=0 then
  329.     randomletter := chr(ord('a')+random(26))
  330.   else
  331.     randomletter := chr(ord('A')+random(26));
  332. end;
  333.  
  334. function randomdigit: char;
  335.  
  336. begin
  337.   randomdigit := chr(ord('0')+random(10));
  338. end;
  339.  
  340. function newmessageid: string;
  341.  
  342. begin
  343.   newmessageid :=
  344.    '<'+itoa(year mod 100)+integertozstring(month,2)+
  345.    integertozstring(dayofmonth,2)+'.'+timedigits+'.'+
  346.    randomdigit+randomletter+randomdigit+'.'+newsreadername+'.'+
  347.    'w'+copy(wafversion,1,1)+copy(wafversion,3,2)+'w'+'@'+node+'>';
  348. end;
  349.  
  350. function getfromaddr(from: string): string;
  351.  
  352. var
  353.   result: string;
  354.   at: integer;
  355.  
  356. begin
  357.   at := pos('<',from);
  358.   if at>0 then
  359.     result := copy(from,at+1,length(from)-at-1)
  360.   else
  361.     begin
  362.       at := pos(' ',from);
  363.       if at>0 then
  364.         result := copy(from,1,at-1)
  365.       else
  366.         result := from;
  367.     end;
  368.   getfromaddr := result;
  369. end;
  370.  
  371. function getfromname(from: string): string;
  372.  
  373. var
  374.   result: string;
  375.   at: integer;
  376.  
  377. begin
  378.   at := pos('(',from);
  379.   if at>0 then
  380.     result := copy(from,at+1,length(from)-at-1)
  381.   else
  382.     begin
  383.       at := pos('<',from);
  384.       if at>1 then
  385.         result := copy(from,1,at-2)
  386.       else
  387.         result := '';
  388.     end;
  389.   getfromname := result;
  390. end;
  391.  
  392. function getgroup(s: string): string;
  393.  
  394. begin
  395.   getgroup := ltrim(trim(copy(s,1,pos(' ',s)-1)));
  396. end;
  397.  
  398. function getalreadyread(s: string): integer;
  399.  
  400. begin
  401.   getalreadyread := atoi(ltrim(trim(copy(s,pos(' ',s)+1,255))));
  402. end;
  403.  
  404. function joinedtogroup(group: string): boolean;
  405.  
  406. var
  407.   result: boolean;
  408.   s: string;
  409.  
  410. begin
  411.   result := false;
  412.   reset(joinf);
  413.   while not eof(joinf) and not result do
  414.     begin
  415.       readln(joinf,s);
  416.       if getgroup(s)=group then
  417.         result := true;
  418.     end;
  419.   joinedtogroup := result;
  420. end;
  421.  
  422. function parseheadername(s: string): string;
  423.  
  424. begin
  425.   parseheadername := copy(s,1,pos(':',s)-1);
  426. end;
  427.  
  428. function parseheadervalue(s: string): string;
  429.  
  430. begin
  431.   parseheadervalue := copy(s,pos(':',s)+2,255);
  432. end;
  433.  
  434. function chop(s: string; i: integer): string;
  435.  
  436. var
  437.   result: string;
  438.  
  439. begin
  440.   chop := copy(s,i+1,255);
  441. end;
  442.  
  443. function nore(s: string): string;
  444.  
  445. begin
  446.   if copy(s,1,4)='Re: ' then
  447.     nore := chop(s,4)
  448.   else
  449.     nore := s;
  450. end;
  451.  
  452. function subjkilled(subject: string): boolean;
  453.  
  454. var
  455.   i: integer;
  456.   result: boolean;
  457.  
  458. begin
  459.  
  460. {Subject: only match if exact - it'll be put in the kill file that way}
  461. {anyway modulo Re: of course}
  462.  
  463.   result := false;
  464.   for i := 1 to numkillss do
  465.     if killsubjsp^[i]=nore(subject) then
  466.       result := true;
  467.   subjkilled := result;
  468. end;
  469.  
  470. function fromkilled(from: string): boolean;
  471.  
  472. var
  473.   i: integer;
  474.   result: boolean;
  475.  
  476. begin
  477. {From: match if that address found anywhere - so that if they change their}
  478. {posting software or whatever you'll still find it.}
  479.  
  480.   result := false;
  481.   for i := 1 to numkillfs do
  482.     if pos(killfromsp^[i],from)>0 then
  483.       result := true;
  484.  
  485.   fromkilled := result;
  486. end;
  487.  
  488. function getstaticvalue(name: string): string;
  489.  
  490. var
  491.   result: string;
  492.   infile: text;
  493.   s: string;
  494.   foundname: string;
  495.  
  496. begin
  497.   result := '';
  498.   assign(infile,wafenv);
  499.   reset(infile);
  500.   while (result='') and not eof(infile) do
  501.     begin
  502.       readln(infile,s);
  503.       if s<>'' then
  504.         if copy(s,1,1)<>'#' then
  505.           begin
  506.             foundname := trim(ltrim(copy(s,1,pos(':',s)-1)));
  507.             if foundname=name then
  508.               begin
  509.                 result := trim(ltrim(copy(s,pos(':',s)+1,255)));
  510.               end;
  511.           end;
  512.     end;
  513.   close(infile);
  514.   getstaticvalue := result;
  515. end;
  516.  
  517. function getheaderline(infilename, fieldname: string): string;
  518.  
  519. var
  520.   infile: file;
  521.   foundblank: boolean;
  522.   foundline: boolean;
  523.   result: string;
  524.   s: string;
  525.   ufieldname: string;
  526.   headerbytesseen: integer;
  527.   morelinesinheader: boolean;
  528.  
  529. function nextlinefrombuf: string;
  530.  
  531. var
  532.   result: string;
  533.   gotcrlf: boolean;
  534.   c: char;
  535.  
  536. begin
  537.   result := '';
  538.   gotcrlf := false;
  539.   while (headerbytesseen<headerbytesinmem) and not gotcrlf do
  540.     begin
  541.       inc(headerbytesseen);
  542.       c := headerbuf[headerbytesseen];
  543.       if (c=#13) then
  544.         gotcrlf := true
  545.       else if c<>#10 then
  546.         result := result+c;
  547.     end;
  548.   nextlinefrombuf := result;
  549. end;
  550.  
  551. begin
  552.   result := '';
  553.   ufieldname := upper(fieldname);
  554.   if headerinmem<>infilename then
  555.     begin
  556.       assign(infile,infilename);
  557.       reset(infile,1);
  558.       blockread(infile,headerbuf,headerbufsize,headerbytesinmem);
  559.       headerinmem := infilename;
  560.       close(infile);
  561.     end;
  562.   foundblank := false;
  563.   foundline := false;
  564. {$ifdef oldheader}
  565.   while (not eof(f)) and (not foundblank) and (not foundline) do
  566.     begin
  567.       readln(f,s);
  568.       if copy(upper(ltrim(s)),1,length(fieldname))=ufieldname then
  569.         begin
  570.           foundline := true;
  571.           result := ltrim(trim(copy(trim(s),length(fieldname)+1,255)));
  572.           if not eof(f) then
  573.             begin
  574.               readln(f,s);
  575.               if copy(s,1,1)=' ' then
  576.                 result := result+s;
  577.             end;
  578.         end
  579.       else if length(trim(s))=0 then
  580.         foundblank := true;
  581.     end;
  582.   close(f);
  583. {$endif}
  584.   headerbytesseen := 0;
  585.   while (headerbytesseen<headerbytesinmem) and
  586.    (not foundblank) and (not foundline) do
  587.     begin
  588.       s := nextlinefrombuf;
  589.       if copy(upper(ltrim(s)),1,length(fieldname))=ufieldname then
  590.         begin
  591.           foundline := true;
  592.           result := ltrim(trim(copy(trim(s),length(fieldname)+1,255)));
  593.           if headerbytesseen<headerbytesinmem then
  594.             begin
  595.               morelinesinheader := true;
  596.               while morelinesinheader do
  597.                 begin
  598.                   s := nextlinefrombuf;
  599.                   if copy(s,1,1)=' ' then
  600.                     result := result+' '+ltrim(s)
  601.                   else
  602.                     morelinesinheader := false;
  603.                 end;
  604.             end;
  605.         end
  606.       else if length(trim(s))=0 then
  607.         foundblank := true;
  608.     end;
  609.   getheaderline := result
  610. end;
  611.  
  612. function monthstringtointeger(monthstr: string): integer;
  613.  
  614. var
  615.   result: integer;
  616.   lowermonthstr: string;
  617.  
  618. begin
  619.   result := 12;
  620.   lowermonthstr := lower(monthstr);
  621.   if lowermonthstr='jan' then result := 1
  622.   else if lowermonthstr='feb' then result := 2
  623.   else if lowermonthstr='mar' then result := 3
  624.   else if lowermonthstr='apr' then result := 4
  625.   else if lowermonthstr='may' then result := 5
  626.   else if lowermonthstr='jun' then result := 6
  627.   else if lowermonthstr='jul' then result := 7
  628.   else if lowermonthstr='aug' then result := 8
  629.   else if lowermonthstr='sep' then result := 9
  630.   else if lowermonthstr='oct' then result := 10
  631.   else if lowermonthstr='nov' then result := 11;
  632.   monthstringtointeger := result;
  633. end;
  634.  
  635. function isdigit(c: char): boolean;
  636.  
  637. begin
  638.   isdigit := (c>='0') and (c<='9');
  639. end;
  640.  
  641. function islower(c: char): boolean;
  642.  
  643. begin
  644.   islower := (c>='a') and (c<='z');
  645. end;
  646.  
  647. function snatchint(var s: string): integer;
  648.  
  649. var
  650.   intsofar: integer;
  651.  
  652. begin
  653.   intsofar := 0;
  654.   while (length(s)>0) and not isdigit(s[1]) do
  655.     s := chop(s,1);
  656.   while (length(s)>0) and isdigit(s[1]) do
  657.     begin
  658.       intsofar := 10*intsofar+ord(s[1])-ord('0');
  659.       s := chop(s,1);
  660.     end;
  661.   snatchint := intsofar;
  662. end;
  663.  
  664. function stringtodatestring(datestr: string): datestringt;
  665.  
  666. var
  667.   result: datestringt;
  668.   workstr: string;
  669.   dayofmonth: integer;
  670.   monthstr: string;
  671.   year: integer;
  672.  
  673. begin
  674.   if datestr='' then
  675.     result := '99991231'
  676.   else
  677.     begin
  678.       workstr := datestr;
  679.       dayofmonth := snatchint(workstr);
  680.       workstr := ltrim(workstr);
  681.       monthstr := copy(workstr,1,3);
  682.       workstr := ltrim(chop(workstr,4));
  683.       year := snatchint(workstr);
  684.       if year<100 then
  685.         inc(year,1900);
  686.       result := integertozstring(year,4)+
  687.        integertozstring(monthstringtointeger(monthstr),2)+
  688.        integertozstring(dayofmonth,2);
  689.     end;
  690.   stringtodatestring := result;
  691. end;
  692.  
  693. function firstartfirst(a,b: integer): boolean; {assuming subject the same}
  694.  
  695. var
  696.   result: boolean;
  697.  
  698. begin
  699.   result := true;
  700.   if indents[a]>indents[b] then
  701.     result := false;
  702.   if (indents[a]=indents[b]) and (datesp^[a]>datesp^[b]) then
  703.     result := false;
  704.   firstartfirst := result;
  705. end;
  706.  
  707. function max(a,b: integer): integer;
  708.  
  709. begin
  710.   if a>b then max := a else max := b;
  711. end;
  712.  
  713. function min(a,b: integer): integer;
  714.  
  715. begin
  716.   min := -max(-a,-b);
  717. end;
  718.  
  719. function getuniqfile(basename: string): string;
  720.  
  721. var
  722.   result: integer;
  723.   fileinfo: searchrec;
  724.   filefound: string;
  725.  
  726. begin
  727.   result := 0;
  728.   findfirst(basename+'.*',archive,fileinfo);
  729.   while doserror=0 do
  730.     begin
  731.       filefound := fileinfo.name;
  732.       while pos('.',filefound)>0 do
  733.         filefound := copy(filefound,pos('.',filefound)+1,255);
  734.       result := max(result,atoi(filefound));
  735.       findnext(fileinfo);
  736.     end;
  737.   getuniqfile := basename+'.'+itoa(result+1);
  738. end;
  739.  
  740. function getbasedir(group: string): string;
  741.  
  742. var
  743.   result: string;
  744.   infilen: string;
  745.   infile: text;
  746.   s: string;
  747.   mangledgroup: string;
  748.  
  749. begin
  750.   result := '';
  751.   infilen := waffledir+'\system\'+'usenet';
  752.   assign(infile,infilen);
  753.   {$I-}
  754.   reset(infile);
  755.   {$I+}
  756.   if ioresult=0 then
  757.     begin
  758.       while (result='') and not eof(infile) do
  759.         begin
  760.           readln(infile,s);
  761.           if (copy(ltrim(s),1,length(group))=group) and
  762.            (pos('/dir=',s)>0) then
  763.             begin
  764.               result := trim(ltrim(copy(s,pos('/dir=',s)+5,255)));
  765.             end;
  766.         end;
  767.       close(infile);
  768.     end;
  769.   if result='' then
  770.     begin
  771.       mangledgroup := currgroup;
  772.       while pos('.',mangledgroup)>0 do
  773.         begin
  774.           result := result+
  775.            copy(mangledgroup,1,min(8,pos('.',mangledgroup)-1))+'\';
  776.           mangledgroup := copy(mangledgroup,pos('.',mangledgroup)+1,255);
  777.         end;
  778.       result := mainnewsdir+mainnewsdirsuf+result+
  779.        copy(mangledgroup,1,min(8,length(mangledgroup)));
  780.     end;
  781.   getbasedir := unquote(unslash(result));
  782. end;
  783.  
  784. function getnextgroup: string;
  785.  
  786. var
  787.   foundgroup: string;
  788.   result: string;
  789.  
  790. begin
  791.   result := '';
  792.   reset(joinf);
  793.   foundgroup := '';
  794.  
  795.   if not eof(joinf) then
  796.     begin
  797.       if currgroup='' then
  798.         begin
  799.           readln(joinf,foundgroup);
  800.           result := getgroup(foundgroup);
  801.         end
  802.       else
  803.         begin
  804.           while not eof(joinf) and (foundgroup<>currgroup) do
  805.             begin
  806.               readln(joinf,foundgroup);
  807.               foundgroup := getgroup(foundgroup);
  808.             end;
  809.           if not eof(joinf) then
  810.             begin
  811.               readln(joinf,foundgroup);
  812.               result := getgroup(foundgroup);
  813.             end;
  814.         end;
  815.     end;
  816.   getnextgroup := result;
  817. end;
  818.  
  819. function alreadyseen(newsgroups: string): boolean;
  820.  
  821. var
  822.   i: integer;
  823.   newsglist: string;
  824.   result: boolean;
  825.   found: boolean;
  826.  
  827. begin
  828.   result := false;
  829.   if currgroup<>'control' then
  830.     begin
  831.       found := false;
  832.       newsglist := ','+newsgroups+',';
  833.       i := 1;
  834.       while (i<numjoined) and not found do
  835.         begin
  836.           if (joinedgroups[i]<>'news.answers') and
  837.            (pos(','+joinedgroups[i]+',',newsglist)<>0) then
  838.             begin
  839.               found := true;
  840.               result := (joinedgroups[i]<>currgroup);
  841.             end;
  842.           inc(i);
  843.         end;
  844.     end;
  845.   alreadyseen := result;
  846. end;
  847.  
  848. function screenline(s: string): string;
  849.  
  850. begin
  851.   if length(trim(s))<=79 then
  852.     screenline := trim(s)
  853.   else
  854.     screenline := copy(s,1,78)+'<';
  855. end;