2 # $Id: robot.tcl,v 1.37 2003/06/10 12:08:17 adam Exp $
4 proc RobotFileNext1 {area lead} {
5 # puts "RobotFileNext1 area=$area lead=$lead"
6 if {[catch {set ns [glob ${area}/*]}]} {
10 if {[file isfile $n]} {
11 set off [string last / $n]
13 return $lead/[string range $n $off end]
17 if {[file isdirectory $n]} {
18 set off [string last / $n]
20 set sb [RobotFileNext1 $n $lead/[string range $n $off end]]
21 if {[string length $sb]} {
29 proc RobotWriteRecord {outf fromurl distance} {
31 puts $outf "<distance>"
33 puts $outf "</distance>"
34 puts $outf "<fromurl>"
36 puts $outf "</fromurl>"
40 proc RobotReadRecord {inf fromurlx distancex} {
41 upvar $fromurlx fromurl
42 upvar $distancex distance
45 set distance [string trim [gets $inf]]
46 # puts "got distance = $distance"
49 set fromurl [string trim [gets $inf]]
52 proc RobotFileNext {task area} {
57 # puts "RobotFileNext robotSeq=$robotSeq($task)"
58 if {$robotSeq($task) < 0} {
61 if {$robotSeq($task) == 0} {
62 if {[catch {set ns($task) [glob $task/$area/*]}]} {
66 # puts "ns=$ns($task)"
67 set off [string length $task/$area]
69 set n [lindex $ns($task) $robotSeq($task)]
71 if {![string length $n]} {
72 set robotSeq($task) -1
74 set statusfile [open $task/status w]
75 puts $statusfile "$status($task,unvisited) $status($task,bad) $status($task,visited)"
80 if {[file isfile $n/frobots.txt]} {
81 puts "ok returning http://[string range $n $off end]/robots.txt"
82 return http://[string range $n $off end]/robots.txt
83 } elseif {[file isdirectory $n]} {
84 set sb [RobotFileNext1 $n http://[string range $n $off end]]
85 if {[string length $sb]} {
89 puts "no more work at end of RobotFileNext n=$n"
95 proc RobotFileExist {task area host path} {
98 if {$debuglevel > 3} {
99 puts "RobotFileExist begin area=$area host=$host path=$path"
101 set lpath [split $path /]
102 set l [llength $lpath]
104 set t [lindex $lpath $l]
106 set npath $task/$area/$host[join [lrange $lpath 0 $l] /d]/f$t
107 if {$debuglevel > 3} {
108 puts "RobotFileExist end npath=$npath"
110 return [file exists $npath]
113 proc RobotFileUnlink {task area host path} {
115 # puts "RobotFileUnlink begin"
116 # puts "area=$area host=$host path=$path"
117 set lpath [split $path /]
118 set l [llength $lpath]
120 set t [lindex $lpath $l]
122 set npath $task/$area/$host[join [lrange $lpath 0 $l] /d]/f$t
123 # puts "npath=$npath"
124 set comp [split $npath /]
125 if {[catch {exec rm [join $comp /]}]} return
127 set l [llength $comp]
130 incr status($task,$area) -1
131 for {set i $l} {$i > 0} {incr i -1} {
132 set path [join [lrange $comp 0 $i] /]
133 if {![catch {glob $path/*}]} return
136 # puts "RobotFileUnlink end"
139 proc RobotFileClose {out} {
140 if [string compare $out stdout] {
145 proc RobotFileOpen {task area host path {mode w}} {
151 puts "RobotFileOpen task=$task path=$path"
153 if {![info exists workdir]} {
156 if {$debuglevel > 3} {
157 puts "RobotFileOpen orgPwd=$orgPwd area=$area host=$host path=$path mode=$mode"
159 if {[string compare $orgPwd $workdir]} {
160 puts "ooops. RobotFileOpen failed"
161 puts "workdir = $workdir"
166 set comp [split $task/$area/$host /]
167 set len [llength $comp]
170 # puts "1 comp=$comp"
172 for {set i 0} {$i <= $len} {incr i} {
173 set d [lindex $comp $i]
174 if {[string length $d] == 0} {
176 } elseif {[catch {cd $d}]} {
177 puts "mkdir (1) d=$d"
180 if {![string compare $area unvisited] && $i == $len && $mode == "w"} {
181 if {[string compare $path /robots.txt]} {
182 set out [open frobots.txt w]
183 puts "creating robots.txt in $d"
185 incr status($task,unvisited)
191 set comp [split $path /]
192 set len [llength $comp]
195 # puts "2 path=$path comp=$comp"
197 for {set i 0} {$i < $len} {incr i} {
198 set d "d[lindex $comp $i]"
199 if {[string length $d] > 1} {
200 if {[catch {cd $d}]} {
201 puts "mkdir (2) d=$d"
207 set d [lindex $comp $len]
208 if {[string length $d]} {
209 set out [open f$d $mode]
211 set out [open f $mode]
214 incr status($task,$area)
221 proc RobotStartJob {fname t} {
225 set f [open $fname r]
227 puts "Reading $fname"
228 regexp {<status>([^<]*)</status>} $xml x status
230 if {$status == "done"} {
234 puts "status = $status"
243 set action $parm(action)
244 if {$type == "domain"} {
245 $action url http://$body/*
247 if {$type == "url"} {
250 if {$type == "mime"} {
254 set control($t,distance) $body
256 set control($t,filestatus) $body
258 if {$status == "pending"} {
259 regsub {<status>[^<]*</status>} $xml {<status>running</status>} xml2
260 set f [open $fname w]
261 puts -nonewline $f $xml2
266 proc RobotDoneJob {t} {
269 if {![info exists daemon_dir]} {
275 set f [open $fname r]
277 puts "Reading $fname"
278 regexp {<status>([^<]*)</status>} $xml x status
280 puts "status = $status"
283 regsub {<status>[^<]*</status>} $xml {<status>done</status>} xml2
284 set f [open $fname w]
285 puts -nonewline $f $xml2
289 proc RobotScanDir {} {
292 if {![info exists daemon_dir]} {
295 foreach d $daemon_dir {
296 if {[catch {set files [glob $d/*.tkl]}]} {
299 foreach fname $files {
300 if {[file isfile $fname] && [file readable $fname]} {
301 set t [file rootname $fname]
302 RobotStartJob $fname $t
308 proc RobotRR {task} {
309 global robotSeq robotsRunning tasks robotsMax status
311 puts "RobotRR -- running=$robotsRunning max=$robotsMax---------------"
312 incr robotsRunning -1
314 # only one task gets through...
315 if {[string compare [lindex $tasks 0] $task]} {
318 puts "RobotRR. task = $task"
319 while {$robotsRunning} {
323 if {[catch {RobotScanDir} msg]} {
324 puts "RobotScanDir failed"
328 set statusfile [open $t/status w]
329 puts $statusfile "$status($t,unvisited) $status($t,bad) $status($t,visited)"
336 proc RobotDaemonSig {} {
342 proc RobotDaemonLoop {} {
343 global daemon_cnt tasks robotsRunning status
351 if {[info exists tasks]} {
352 puts "daemon loop tasks $tasks"
357 while {$robotsRunning} {
361 after 30000 RobotDaemonSig
366 proc RobotRestart {task url sock} {
367 global URL robotsRunning
370 after cancel $URL($sock,cancel)
372 foreach v [array names URL $task,$url,*] {
376 incr robotsRunning -1
380 proc RobotStart {task} {
382 global robotsRunning robotsMax idletime status tasks
384 # puts "RobotStart $task running=$robotsRunning"
386 set url [RobotFileNext $task unvisited]
387 if {[string compare $url done] == 0} {
388 puts "In RobotStart task $task done"
392 if {[string compare $t $task]} {
398 if {![info exists ntasks]} {
407 if {![string length $url]} {
411 if {[string compare $url wait] == 0} {
412 after $idletime [list RobotRR $task]
415 set r [RobotGetUrl $task $url {}]
417 if {$robotsRunning >= $robotsMax} return
419 incr robotsRunning -1
420 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
421 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
424 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
429 proc headSave {task url out} {
432 if {[info exists URL($task,$url,head,last-modified)]} {
433 puts $out "<lastmodified>$URL($task,$url,head,last-modified)</lastmodified>"
436 if {[info exists URL($task,$url,head,date)]} {
437 puts $out " <date>$URL($task,$url,head,date)</date>"
439 if {[info exists URL($task,$url,head,content-length)]} {
440 puts $out " <by>$URL($task,$url,head,content-length)</by>"
442 if {[info exists URL($task,$url,head,server)]} {
443 puts $out " <format>$URL($task,$url,head,server)</format>"
446 puts $out {<publisher>}
447 puts $out " <identifier>$url</identifier>"
448 if {[info exists URL($task,$url,head,content-type)]} {
449 puts $out " <type>$URL($task,$url,head,content-type)</type>"
451 puts $out {</publisher>}
454 proc RobotHref {task url hrefx hostx pathx} {
455 global URL control debuglevel
460 if {$debuglevel > 1} {
461 puts "Ref input url = $url href=$href"
464 if {[string first { } $href] >= 0} {
467 if {[string length $href] > 256} {
471 # Skip pages that have ? in them
472 # if {[string first {?} $url] >= 0 && [string first {?} $href] >= 0} {
475 # get method (if any)
476 if {![regexp {^([^/:]+):(.*)} $href x method hpath]} {
480 if {[string compare $method http]} {
485 if {[regexp {^//([^/]+)([^\#]*)} $hpath x host surl]} {
486 if {![string length $surl]} {
489 if {[info exist control($task,domains)]} {
491 foreach domain $control($task,domains) {
492 if {[string match $domain $host]} {
502 regexp {^([^\#]*)} $hpath x surl
503 set host $URL($task,$url,hostport)
505 if {![string length $surl]} {
508 if {[string first / $surl]} {
510 set curpath $URL($task,$url,path)
511 if {[info exists URL($task,$url,bpath)]} {
512 set curpath $URL($task,$url,bpath)
514 regexp {^([^\#?]*)} $curpath x dpart
515 set l [string last / $dpart]
516 if {[expr $l >= 0]} {
517 set surl [string range $dpart 0 $l]$surl
519 set surl $dpart/$surl
522 set surllist [split $surl /]
525 foreach c $surllist {
530 set path [lrange $path 0 $pathl]
543 if {$debuglevel > 4} {
544 puts "pathl=$pathl output path=$path"
546 set path [join $path /]
547 if {![string length $path]} {
550 regsub -all {~} $path {%7E} path
551 set href "$method://$host$path"
553 if {$debuglevel > 1} {
554 puts "Ref result = $href"
556 return [checkrule $task url $href]
559 proc RobotError {task url code} {
562 puts "Bad URL $url (code $code)"
565 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
566 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
567 RobotReadRecord $inf fromurl distance
570 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
571 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
572 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
573 RobotWriteRecord $outf $fromurl $distance
578 proc RobotRedirect {task url tourl code} {
581 puts "Redirecting from $url to $tourl"
585 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
586 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
587 RobotReadRecord $inf fromurl distance
590 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
591 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
592 RobotWriteRecord $outf $fromurl $distance
595 if {[RobotHref $task $url tourl host path]} {
596 if {![RobotFileExist $task visited $host $path]} {
597 if {![RobotFileExist $task unvisited $host $path]} {
598 set outf [RobotFileOpen $task unvisited $host $path]
599 RobotWriteRecord $outf $fromurl $distance
604 set inf [RobotFileOpen $task visited $host $path r]
605 RobotReadRecord $inf oldurl olddistance
607 if {[string length $olddistance] == 0} {
610 if {[string length $distance] == 0} {
613 puts "distance=$distance olddistance=$olddistance"
614 if {[expr $distance < $olddistance]} {
615 set outf [RobotFileOpen $task unvisited $host $path]
616 RobotWriteRecord $outf $tourl $distance
621 if {[catch {RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)}]} {
627 proc link {task url out href body distance} {
629 if {[expr $distance > $control($task,distance)]} return
631 if {![RobotHref $task $url href host path]} return
634 puts $out "<identifier>$href</identifier>"
635 puts $out "<description>$body</description>"
638 if {![RobotFileExist $task visited $host $path]} {
640 if {![RobotFileExist $task bad $host $path]} {
641 if {[RobotFileExist $task unvisited $host $path]} {
642 set inf [RobotFileOpen $task unvisited $host $path r]
643 RobotReadRecord $inf oldurl olddistance
649 if {[string length $olddistance] == 0} {
652 if {[expr $distance < $olddistance]} {
653 set outf [RobotFileOpen $task unvisited $host $path]
654 RobotWriteRecord $outf $url $distance
657 } elseif {[string compare $href $url]} {
658 set inf [RobotFileOpen $task visited $host $path r]
659 RobotReadRecord $inf xurl olddistance
661 if {[string length $olddistance] == 0} {
664 if {[expr $distance < $olddistance]} {
665 puts "OK remarking url=$url href=$href"
666 puts "olddistance = $olddistance"
667 puts "newdistance = $distance"
668 set outf [RobotFileOpen $task unvisited $host $path]
669 RobotWriteRecord $outf $url $distance
675 proc RobotTextHtml {task url out} {
678 # set title so we can emit it for the body
680 # if true, nothing will be indexed
682 # if true, nothing will be followed
687 if {$control($task,distance) < 1000 && [info exists URL($task,$url,dist)]} {
688 set fdistance $URL($task,$url,dist)
689 set distance [expr $fdistance + 1]
691 htmlSwitch $URL($task,$url,buf) \
695 # collect metadata and save NAME= CONTENT=..
698 puts -nonewline $out "<meta"
699 set al [array names parm]
701 set al [string tolower $a]
702 puts -nonewline $out " $al"
703 puts -nonewline $out {="}
704 puts -nonewline $out $parm($a)
705 puts -nonewline $out {"}
708 set metaname [string tolower $parm($a)]
711 set metacontent $parm($a)
717 # go through robots directives (af any)
718 if {![string compare $metaname robots]} {
719 set direcs [split [string tolower $metacontent] ,]
720 if {[lsearch $direcs noindex] >= 0} {
723 if {[lsearch $direcs nofollow] >= 0} {
728 # don't print title of document content if noindex is used
730 puts $out "<title>$title</title>"
731 regsub -all {<!--[^-]*-->} $body { } abody
732 regsub -all -nocase {<script[^<]*</script>} $abody {} bbody
733 regsub -all {<[^\>]+>} $bbody {} nbody
734 puts $out "<documentcontent>"
736 puts $out "</documentcontent>"
740 if {![info exists parm(href)]} {
743 set href [string trim $parm(href)]
744 if {![RobotHref $task $url href host path]} continue
745 set URL($task,$url,bpath) $path
747 # <a href="...."> .. </a>
748 # we're not using nonest - otherwise body isn't set
749 if {$nofollow} continue
750 if {![info exists parm(href)]} {
753 link $task $url $out [string trim $parm(href)] $body $distance
755 if {$nofollow} continue
756 if {![info exists parm(href)]} {
759 link $task $url $out [string trim $parm(href)] $body $distance
761 if {![info exists parm(src)]} {
764 link $task $url $out [string trim $parm(src)] $body $fdistance
768 proc RobotsTxt {task url} {
771 RobotsTxt0 $task URL(URL($task,$url,hostport),robots) $URL($task,$url,buf)
774 proc RobotsTxt0 {task v buf} {
777 foreach l [split $buf \n] {
778 if {[regexp {([-A-Za-z]+):[ ]*([^\# ]+)} $l match cmd arg]} {
779 set arg [string trim $arg]
780 puts "cmd=$cmd arg=$arg"
781 switch -- [string tolower $cmd] {
784 set pat [string tolower $arg]*
785 set section [string match $pat $agent]
789 puts "rule [list 0 $arg]"
790 lappend $v [list 0 $arg]
795 puts "rule [list 1 $arg]"
796 lappend $v [list 1 $arg]
804 proc RobotTextPlain {task url out} {
807 puts $out "<documentcontent>"
808 regsub -all {<} $URL($task,$url,buf) {\<} content
810 puts $out "</documentcontent>"
812 if {![string compare $URL($task,$url,path) /robots.txt]} {
817 proc RobotWriteMetadata {task url out} {
823 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
824 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
825 RobotReadRecord $inf fromurl distance
828 set URL($task,$url,dist) $distance
829 puts $out "<distance>"
830 puts $out " $distance"
831 puts $out "</distance>"
832 headSave $task $url $out
833 puts "Parsing $url distance=$distance"
834 switch $URL($task,$url,head,content-type) {
836 if {[string length $distance]} {
837 RobotTextHtml $task $url $out
841 RobotTextPlain $task $url $out
847 proc Robot200 {task url} {
850 set out [RobotFileOpen $task raw $URL($task,$url,hostport) $URL($task,$url,path)]
851 puts -nonewline $out $URL($task,$url,buf)
854 set out [RobotFileOpen $task visited $URL($task,$url,hostport) $URL($task,$url,path)]
855 RobotWriteMetadata $task $url $out
858 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
861 proc RobotReadContent {task url sock binary} {
864 set buffer [read $sock 16384]
865 set readCount [string length $buffer]
867 if {$readCount <= 0} {
869 RobotRestart $task $url $sock
870 } elseif {!$binary && [string first \0 $buffer] >= 0} {
872 RobotRestart $task $url $sock
874 # puts "Got $readCount bytes"
875 set URL($task,$url,buf) $URL($task,$url,buf)$buffer
879 proc RobotReadHeader {task url sock} {
880 global URL debuglevel
882 if {$debuglevel > 1} {
883 puts "HTTP head $url"
885 if {[catch {set buffer [read $sock 2148]}]} {
886 RobotError $task $url 404
887 RobotRestart $task $url $sock
890 set readCount [string length $buffer]
892 if {$readCount <= 0} {
893 RobotError $task $url 404
894 RobotRestart $task $url $sock
896 # puts "Got $readCount bytes"
897 set URL($task,$url,buf) $URL($task,$url,buf)$buffer
899 set n [string first \r\n\r\n $URL($task,$url,buf)]
903 set headbuf [string range $URL($task,$url,buf) 0 $n]
905 set URL($task,$url,buf) [string range $URL($task,$url,buf) $n end]
907 regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
908 set lines [split $headbuf \n]
909 foreach line $lines {
910 if {[regexp {^([^:]+):[ ]+([^;]*)} $line x name value]} {
911 set URL($task,$url,head,[string tolower $name]) [string trim $value]
914 puts "HTTP CODE $code"
915 set URL($task,$url,state) skip
918 RobotRedirect $task $url $URL($task,$url,head,location) 301
919 RobotRestart $task $url $sock
922 RobotRedirect $task $url $URL($task,$url,head,location) 302
923 RobotRestart $task $url $sock
926 if {![info exists URL($task,$url,head,content-type)]} {
927 set URL($task,$url,head,content-type) {}
930 switch -glob -- $URL($task,$url,head,content-type) {
935 if {![regexp {/robots.txt$} $url]} {
936 if {![checkrule $task mime $URL($task,$url,head,content-type)]} {
937 RobotError $task $url mimedeny
938 RobotRestart $task $url $sock
942 fileevent $sock readable [list RobotReadContent $task $url $sock $binary]
945 RobotError $task $url $code
946 RobotRestart $task $url $sock
953 proc RobotSockCancel {task url sock} {
955 puts "RobotSockCancel sock=$sock url=$url"
956 RobotError $task $url 401
957 RobotRestart $task $url $sock
960 proc RobotConnect {task url sock} {
961 global URL agent acceptLanguage
963 fconfigure $sock -translation {lf crlf} -blocking 0
964 fileevent $sock readable [list RobotReadHeader $task $url $sock]
965 puts $sock "GET $URL($task,$url,path) HTTP/1.0"
966 puts $sock "Host: $URL($task,$url,host)"
967 puts $sock "User-Agent: $agent"
968 if {[string length $acceptLanguage]} {
969 puts $sock "Accept-Language: $acceptLanguage"
972 set URL($sock,cancel) [after 30000 [list RobotSockCancel $task $url $sock]]
973 if {[catch {flush $sock}]} {
974 RobotError $task $url 404
975 RobotRestart $task $url $sock
983 proc RobotGetUrl {task url phost} {
984 global URL robotsRunning
986 puts "Retrieve running=$robotsRunning url=$url task=$task"
987 if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
990 if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
994 set URL($task,$url,method) $method
995 set URL($task,$url,host) $host
996 set URL($task,$url,hostport) $hostport
997 set URL($task,$url,path) $path
998 set URL($task,$url,state) head
999 set URL($task,$url,buf) {}
1001 if {[string compare $path /robots.txt]} {
1003 if {![info exists URL($hostport,robots)]} {
1004 puts "READING robots.txt for host $hostport"
1005 if {[RobotFileExist $task visited $hostport /robots.txt]} {
1006 set inf [RobotFileOpen $task visited $hostport /robots.txt r]
1007 set buf [read $inf 32768]
1010 set buf "User-agent: *\nAllow: /\n"
1012 RobotsTxt0 $task URL($hostport,robots) $buf
1014 if {[info exists URL($hostport,robots)]} {
1015 foreach l $URL($hostport,robots) {
1016 if {[string first [lindex $l 1] $path] == 0} {
1017 set ok [lindex $l 0]
1023 puts "skipped due to robots.txt"
1027 if [catch {set sock [socket -async $host $port]}] {
1030 RobotConnect $task $url $sock
1035 if {![llength [info commands htmlSwitch]]} {
1036 set e [info sharedlibextension]
1037 if {[catch {load ./tclrobot$e}]} {
1042 set agent "zmbot/0.2"
1043 if {![catch {set os [exec uname -s -r]}]} {
1044 set agent "$agent ($os)"
1047 puts "agent: $agent"
1058 set acceptLanguage {}
1061 # Rules: allow, deny, url
1063 proc checkrule {task type this} {
1067 if {$debuglevel > 3} {
1068 puts "CHECKRULE $type $this"
1070 if {[info exist control($task,alrules)]} {
1071 foreach l $control($task,alrules) {
1072 if {$debuglevel > 3} {
1076 if {[lindex $l 1] != $type} continue
1077 # consider mask (! negates)
1078 set masks [lindex $l 2]
1080 foreach mask $masks {
1081 if {$debuglevel > 4} {
1082 puts "consider single mask $mask"
1084 if {[string index $mask 0] == "!"} {
1085 set mask [string range $mask 1 end]
1086 if {[string match $mask $this]} continue
1088 if {![string match $mask $this]} continue
1092 if {$debuglevel > 4} {
1096 # OK, we have a match
1097 if {[lindex $l 0] == "allow"} {
1098 if {$debuglevel > 3} {
1099 puts "CHECKRULE MATCH OK"
1103 if {$debuglevel > 3} {
1104 puts "CHECKFULE MATCH FAIL"
1110 if {$debuglevel > 3} {
1111 puts "CHECKRULE MATCH OK"
1118 global debuglevel task
1120 if {[RobotHref $task http://www.indexdata.dk/ href host path]} {
1121 if {![RobotFileExist $task visited $host $path]} {
1122 set outf [RobotFileOpen $task unvisited $host $path]
1123 RobotWriteRecord $outf href 0
1124 RobotFileClose $outf
1129 proc deny {type stuff} {
1132 lappend control($task,alrules) [list deny $type $stuff]
1135 proc allow {type stuff} {
1138 lappend control($task,alrules) [list allow $type $stuff]
1141 proc debug {level} {
1144 set debuglevel $level
1148 global tasks task status robotSeq control
1152 if {[info exists tasks]} {
1153 if {[lsearch -exact $tasks $t] >= 0} {
1159 set status($t,unvisited) 0
1160 set status($t,visited) 0
1161 set status($t,bad) 0
1162 set status($t,raw) 0
1163 set status($t,active) 1
1165 set control($t,distance) 10
1169 # Little utility that ensures that at least one task is present (main).
1172 if {![info exist tasks]} {
1181 set l [llength $argv]
1184 puts {tclrobot: usage:}
1185 puts {tclrobot [-j jobs] [-i idle] [-c count] [-d domain] [-D dir] [-r rules] [url ..]}
1186 puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
1193 set arg [lindex $argv $i]
1194 switch -glob -- $arg {
1196 set t [string range $arg 2 end]
1197 if {![string length $t]} {
1198 set t [lindex $argv [incr i]]
1203 set dir [string range $arg 2 end]
1204 if {![string length $dir]} {
1205 set dir [lindex $argv [incr i]]
1207 lappend daemon_dir $dir
1210 set robotsMax [string range $arg 2 end]
1211 if {![string length $robotsMax]} {
1212 set robotsMax [lindex $argv [incr i]]
1217 set control($task,distance) [string range $arg 2 end]
1218 if {![string length $control($task,distance)]} {
1219 set control($task,distance) [lindex $argv [incr i]]
1224 set dom [string range $arg 2 end]
1225 if {![string length $dom]} {
1226 set dom [lindex $argv [incr i]]
1228 lappend control($task,domains) $dom
1231 set idletime [string range $arg 2 end]
1232 if {![string length $idletime]} {
1233 set idletime [lindex $argv [incr i]]
1238 set acceptLanguage [string range $arg 2 end]
1239 if {![string length $acceptLanguage]} {
1240 set acceptLanguage [lindex $argv [incr i]]
1245 set rfile [string range $arg 2 end]
1246 if {![string length $rfile]} {
1247 set rfile [lindex $argv [incr i]]
1249 catch {unset maxdistance}
1251 if {[info exists maxdistance]} {
1252 set control($task,distance) $maxdistance
1258 if {[RobotHref $task http://www.indexdata.dk/ href host path]} {
1259 if {![RobotFileExist $task visited $host $path]} {
1260 set outf [RobotFileOpen $task unvisited $host $path]
1261 RobotWriteRecord $outf href 0
1262 RobotFileClose $outf
1270 if {![info exist robotsMax]} {
1274 if {[info exist daemon_dir]} {
1279 puts "max distance=$control($t,distance)"
1280 if {[info exists control($t,domains)]} {
1281 puts "domains=$control($t,domains)"
1284 puts "max jobs=$robotsMax"
1290 while {$robotsRunning} {
1294 if {[info exists tasks]} {
1296 set statusfile [open $t/status w]
1297 puts $statusfile "$status($t,unvisited) $status($t,bad) $status($t,visited)"