2 # $Id: robot.tcl,v 1.38 2003/06/10 12:12:35 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"
229 if {![regexp {<status>([^<]*)</status>} $xml x status]} {
232 if {$status == "done"} {
236 puts "status = $status"
245 set action $parm(action)
246 if {$type == "domain"} {
247 $action url http://$body/*
249 if {$type == "url"} {
252 if {$type == "mime"} {
256 set control($t,distance) $body
258 set control($t,filestatus) $body
260 if {$status == "pending"} {
261 regsub {<status>[^<]*</status>} $xml {<status>running</status>} xml2
262 set f [open $fname w]
263 puts -nonewline $f $xml2
268 proc RobotDoneJob {t} {
271 if {![info exists daemon_dir]} {
277 set f [open $fname r]
279 puts "Reading $fname"
280 regexp {<status>([^<]*)</status>} $xml x status
282 puts "status = $status"
285 regsub {<status>[^<]*</status>} $xml {<status>done</status>} xml2
286 set f [open $fname w]
287 puts -nonewline $f $xml2
291 proc RobotScanDir {} {
294 if {![info exists daemon_dir]} {
297 foreach d $daemon_dir {
298 if {[catch {set files [glob $d/*.tkl]}]} {
301 foreach fname $files {
302 if {[file isfile $fname] && [file readable $fname]} {
303 set t [file rootname $fname]
304 RobotStartJob $fname $t
310 proc RobotRR {task} {
311 global robotSeq robotsRunning tasks robotsMax status
313 puts "RobotRR -- running=$robotsRunning max=$robotsMax---------------"
314 incr robotsRunning -1
316 # only one task gets through...
317 if {[string compare [lindex $tasks 0] $task]} {
320 puts "RobotRR. task = $task"
321 while {$robotsRunning} {
325 if {[catch {RobotScanDir} msg]} {
326 puts "RobotScanDir failed"
330 set statusfile [open $t/status w]
331 puts $statusfile "$status($t,unvisited) $status($t,bad) $status($t,visited)"
338 proc RobotDaemonSig {} {
344 proc RobotDaemonLoop {} {
345 global daemon_cnt tasks robotsRunning status
353 if {[info exists tasks]} {
354 puts "daemon loop tasks $tasks"
359 while {$robotsRunning} {
363 after 30000 RobotDaemonSig
368 proc RobotRestart {task url sock} {
369 global URL robotsRunning
372 after cancel $URL($sock,cancel)
374 foreach v [array names URL $task,$url,*] {
378 incr robotsRunning -1
382 proc RobotStart {task} {
384 global robotsRunning robotsMax idletime status tasks
386 # puts "RobotStart $task running=$robotsRunning"
388 set url [RobotFileNext $task unvisited]
389 if {[string compare $url done] == 0} {
390 puts "In RobotStart task $task done"
394 if {[string compare $t $task]} {
400 if {![info exists ntasks]} {
409 if {![string length $url]} {
413 if {[string compare $url wait] == 0} {
414 after $idletime [list RobotRR $task]
417 set r [RobotGetUrl $task $url {}]
419 if {$robotsRunning >= $robotsMax} return
421 incr robotsRunning -1
422 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
423 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
426 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
431 proc headSave {task url out} {
434 if {[info exists URL($task,$url,head,last-modified)]} {
435 puts $out "<lastmodified>$URL($task,$url,head,last-modified)</lastmodified>"
438 if {[info exists URL($task,$url,head,date)]} {
439 puts $out " <date>$URL($task,$url,head,date)</date>"
441 if {[info exists URL($task,$url,head,content-length)]} {
442 puts $out " <by>$URL($task,$url,head,content-length)</by>"
444 if {[info exists URL($task,$url,head,server)]} {
445 puts $out " <format>$URL($task,$url,head,server)</format>"
448 puts $out {<publisher>}
449 puts $out " <identifier>$url</identifier>"
450 if {[info exists URL($task,$url,head,content-type)]} {
451 puts $out " <type>$URL($task,$url,head,content-type)</type>"
453 puts $out {</publisher>}
456 proc RobotHref {task url hrefx hostx pathx} {
457 global URL control debuglevel
462 if {$debuglevel > 1} {
463 puts "Ref input url = $url href=$href"
466 if {[string first { } $href] >= 0} {
469 if {[string length $href] > 256} {
473 # Skip pages that have ? in them
474 # if {[string first {?} $url] >= 0 && [string first {?} $href] >= 0} {
477 # get method (if any)
478 if {![regexp {^([^/:]+):(.*)} $href x method hpath]} {
482 if {[string compare $method http]} {
487 if {[regexp {^//([^/]+)([^\#]*)} $hpath x host surl]} {
488 if {![string length $surl]} {
491 if {[info exist control($task,domains)]} {
493 foreach domain $control($task,domains) {
494 if {[string match $domain $host]} {
504 regexp {^([^\#]*)} $hpath x surl
505 set host $URL($task,$url,hostport)
507 if {![string length $surl]} {
510 if {[string first / $surl]} {
512 set curpath $URL($task,$url,path)
513 if {[info exists URL($task,$url,bpath)]} {
514 set curpath $URL($task,$url,bpath)
516 regexp {^([^\#?]*)} $curpath x dpart
517 set l [string last / $dpart]
518 if {[expr $l >= 0]} {
519 set surl [string range $dpart 0 $l]$surl
521 set surl $dpart/$surl
524 set surllist [split $surl /]
527 foreach c $surllist {
532 set path [lrange $path 0 $pathl]
545 if {$debuglevel > 4} {
546 puts "pathl=$pathl output path=$path"
548 set path [join $path /]
549 if {![string length $path]} {
552 regsub -all {~} $path {%7E} path
553 set href "$method://$host$path"
555 if {$debuglevel > 1} {
556 puts "Ref result = $href"
558 return [checkrule $task url $href]
561 proc RobotError {task url code} {
564 puts "Bad URL $url (code $code)"
567 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
568 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
569 RobotReadRecord $inf fromurl distance
572 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
573 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
574 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
575 RobotWriteRecord $outf $fromurl $distance
580 proc RobotRedirect {task url tourl code} {
583 puts "Redirecting from $url to $tourl"
587 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
588 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
589 RobotReadRecord $inf fromurl distance
592 if {![RobotFileExist $task bad $URL($task,$url,hostport) $URL($task,$url,path)]} {
593 set outf [RobotFileOpen $task bad $URL($task,$url,hostport) $URL($task,$url,path)]
594 RobotWriteRecord $outf $fromurl $distance
597 if {[RobotHref $task $url tourl host path]} {
598 if {![RobotFileExist $task visited $host $path]} {
599 if {![RobotFileExist $task unvisited $host $path]} {
600 set outf [RobotFileOpen $task unvisited $host $path]
601 RobotWriteRecord $outf $fromurl $distance
606 set inf [RobotFileOpen $task visited $host $path r]
607 RobotReadRecord $inf oldurl olddistance
609 if {[string length $olddistance] == 0} {
612 if {[string length $distance] == 0} {
615 puts "distance=$distance olddistance=$olddistance"
616 if {[expr $distance < $olddistance]} {
617 set outf [RobotFileOpen $task unvisited $host $path]
618 RobotWriteRecord $outf $tourl $distance
623 if {[catch {RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)}]} {
629 proc link {task url out href body distance} {
631 if {[expr $distance > $control($task,distance)]} return
633 if {![RobotHref $task $url href host path]} return
636 puts $out "<identifier>$href</identifier>"
637 puts $out "<description>$body</description>"
640 if {![RobotFileExist $task visited $host $path]} {
642 if {![RobotFileExist $task bad $host $path]} {
643 if {[RobotFileExist $task unvisited $host $path]} {
644 set inf [RobotFileOpen $task unvisited $host $path r]
645 RobotReadRecord $inf oldurl olddistance
651 if {[string length $olddistance] == 0} {
654 if {[expr $distance < $olddistance]} {
655 set outf [RobotFileOpen $task unvisited $host $path]
656 RobotWriteRecord $outf $url $distance
659 } elseif {[string compare $href $url]} {
660 set inf [RobotFileOpen $task visited $host $path r]
661 RobotReadRecord $inf xurl olddistance
663 if {[string length $olddistance] == 0} {
666 if {[expr $distance < $olddistance]} {
667 puts "OK remarking url=$url href=$href"
668 puts "olddistance = $olddistance"
669 puts "newdistance = $distance"
670 set outf [RobotFileOpen $task unvisited $host $path]
671 RobotWriteRecord $outf $url $distance
677 proc RobotTextHtml {task url out} {
680 # set title so we can emit it for the body
682 # if true, nothing will be indexed
684 # if true, nothing will be followed
689 if {$control($task,distance) < 1000 && [info exists URL($task,$url,dist)]} {
690 set fdistance $URL($task,$url,dist)
691 set distance [expr $fdistance + 1]
693 htmlSwitch $URL($task,$url,buf) \
697 # collect metadata and save NAME= CONTENT=..
700 puts -nonewline $out "<meta"
701 set al [array names parm]
703 set al [string tolower $a]
704 puts -nonewline $out " $al"
705 puts -nonewline $out {="}
706 puts -nonewline $out $parm($a)
707 puts -nonewline $out {"}
710 set metaname [string tolower $parm($a)]
713 set metacontent $parm($a)
719 # go through robots directives (af any)
720 if {![string compare $metaname robots]} {
721 set direcs [split [string tolower $metacontent] ,]
722 if {[lsearch $direcs noindex] >= 0} {
725 if {[lsearch $direcs nofollow] >= 0} {
730 # don't print title of document content if noindex is used
732 puts $out "<title>$title</title>"
733 regsub -all {<!--[^-]*-->} $body { } abody
734 regsub -all -nocase {<script[^<]*</script>} $abody {} bbody
735 regsub -all {<[^\>]+>} $bbody {} nbody
736 puts $out "<documentcontent>"
738 puts $out "</documentcontent>"
742 if {![info exists parm(href)]} {
745 set href [string trim $parm(href)]
746 if {![RobotHref $task $url href host path]} continue
747 set URL($task,$url,bpath) $path
749 # <a href="...."> .. </a>
750 # we're not using nonest - otherwise body isn't set
751 if {$nofollow} continue
752 if {![info exists parm(href)]} {
755 link $task $url $out [string trim $parm(href)] $body $distance
757 if {$nofollow} continue
758 if {![info exists parm(href)]} {
761 link $task $url $out [string trim $parm(href)] $body $distance
763 if {![info exists parm(src)]} {
766 link $task $url $out [string trim $parm(src)] $body $fdistance
770 proc RobotsTxt {task url} {
773 RobotsTxt0 $task URL(URL($task,$url,hostport),robots) $URL($task,$url,buf)
776 proc RobotsTxt0 {task v buf} {
779 foreach l [split $buf \n] {
780 if {[regexp {([-A-Za-z]+):[ ]*([^\# ]+)} $l match cmd arg]} {
781 set arg [string trim $arg]
782 puts "cmd=$cmd arg=$arg"
783 switch -- [string tolower $cmd] {
786 set pat [string tolower $arg]*
787 set section [string match $pat $agent]
791 puts "rule [list 0 $arg]"
792 lappend $v [list 0 $arg]
797 puts "rule [list 1 $arg]"
798 lappend $v [list 1 $arg]
806 proc RobotTextPlain {task url out} {
809 puts $out "<documentcontent>"
810 regsub -all {<} $URL($task,$url,buf) {\<} content
812 puts $out "</documentcontent>"
814 if {![string compare $URL($task,$url,path) /robots.txt]} {
819 proc RobotWriteMetadata {task url out} {
825 if {[RobotFileExist $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)]} {
826 set inf [RobotFileOpen $task unvisited $URL($task,$url,hostport) $URL($task,$url,path) r]
827 RobotReadRecord $inf fromurl distance
830 set URL($task,$url,dist) $distance
831 puts $out "<distance>"
832 puts $out " $distance"
833 puts $out "</distance>"
834 headSave $task $url $out
835 puts "Parsing $url distance=$distance"
836 switch $URL($task,$url,head,content-type) {
838 if {[string length $distance]} {
839 RobotTextHtml $task $url $out
843 RobotTextPlain $task $url $out
849 proc Robot200 {task url} {
852 set out [RobotFileOpen $task raw $URL($task,$url,hostport) $URL($task,$url,path)]
853 puts -nonewline $out $URL($task,$url,buf)
856 set out [RobotFileOpen $task visited $URL($task,$url,hostport) $URL($task,$url,path)]
857 RobotWriteMetadata $task $url $out
860 RobotFileUnlink $task unvisited $URL($task,$url,hostport) $URL($task,$url,path)
863 proc RobotReadContent {task url sock binary} {
866 set buffer [read $sock 16384]
867 set readCount [string length $buffer]
869 if {$readCount <= 0} {
871 RobotRestart $task $url $sock
872 } elseif {!$binary && [string first \0 $buffer] >= 0} {
874 RobotRestart $task $url $sock
876 # puts "Got $readCount bytes"
877 set URL($task,$url,buf) $URL($task,$url,buf)$buffer
881 proc RobotReadHeader {task url sock} {
882 global URL debuglevel
884 if {$debuglevel > 1} {
885 puts "HTTP head $url"
887 if {[catch {set buffer [read $sock 2148]}]} {
888 RobotError $task $url 404
889 RobotRestart $task $url $sock
892 set readCount [string length $buffer]
894 if {$readCount <= 0} {
895 RobotError $task $url 404
896 RobotRestart $task $url $sock
898 # puts "Got $readCount bytes"
899 set URL($task,$url,buf) $URL($task,$url,buf)$buffer
901 set n [string first \r\n\r\n $URL($task,$url,buf)]
905 set headbuf [string range $URL($task,$url,buf) 0 $n]
907 set URL($task,$url,buf) [string range $URL($task,$url,buf) $n end]
909 regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
910 set lines [split $headbuf \n]
911 foreach line $lines {
912 if {[regexp {^([^:]+):[ ]+([^;]*)} $line x name value]} {
913 set URL($task,$url,head,[string tolower $name]) [string trim $value]
916 puts "HTTP CODE $code"
917 set URL($task,$url,state) skip
920 RobotRedirect $task $url $URL($task,$url,head,location) 301
921 RobotRestart $task $url $sock
924 RobotRedirect $task $url $URL($task,$url,head,location) 302
925 RobotRestart $task $url $sock
928 if {![info exists URL($task,$url,head,content-type)]} {
929 set URL($task,$url,head,content-type) {}
932 switch -glob -- $URL($task,$url,head,content-type) {
937 if {![regexp {/robots.txt$} $url]} {
938 if {![checkrule $task mime $URL($task,$url,head,content-type)]} {
939 RobotError $task $url mimedeny
940 RobotRestart $task $url $sock
944 fileevent $sock readable [list RobotReadContent $task $url $sock $binary]
947 RobotError $task $url $code
948 RobotRestart $task $url $sock
955 proc RobotSockCancel {task url sock} {
957 puts "RobotSockCancel sock=$sock url=$url"
958 RobotError $task $url 401
959 RobotRestart $task $url $sock
962 proc RobotConnect {task url sock} {
963 global URL agent acceptLanguage
965 fconfigure $sock -translation {lf crlf} -blocking 0
966 fileevent $sock readable [list RobotReadHeader $task $url $sock]
967 puts $sock "GET $URL($task,$url,path) HTTP/1.0"
968 puts $sock "Host: $URL($task,$url,host)"
969 puts $sock "User-Agent: $agent"
970 if {[string length $acceptLanguage]} {
971 puts $sock "Accept-Language: $acceptLanguage"
974 set URL($sock,cancel) [after 30000 [list RobotSockCancel $task $url $sock]]
975 if {[catch {flush $sock}]} {
976 RobotError $task $url 404
977 RobotRestart $task $url $sock
985 proc RobotGetUrl {task url phost} {
986 global URL robotsRunning
988 puts "Retrieve running=$robotsRunning url=$url task=$task"
989 if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
992 if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
996 set URL($task,$url,method) $method
997 set URL($task,$url,host) $host
998 set URL($task,$url,hostport) $hostport
999 set URL($task,$url,path) $path
1000 set URL($task,$url,state) head
1001 set URL($task,$url,buf) {}
1003 if {[string compare $path /robots.txt]} {
1005 if {![info exists URL($hostport,robots)]} {
1006 puts "READING robots.txt for host $hostport"
1007 if {[RobotFileExist $task visited $hostport /robots.txt]} {
1008 set inf [RobotFileOpen $task visited $hostport /robots.txt r]
1009 set buf [read $inf 32768]
1012 set buf "User-agent: *\nAllow: /\n"
1014 RobotsTxt0 $task URL($hostport,robots) $buf
1016 if {[info exists URL($hostport,robots)]} {
1017 foreach l $URL($hostport,robots) {
1018 if {[string first [lindex $l 1] $path] == 0} {
1019 set ok [lindex $l 0]
1025 puts "skipped due to robots.txt"
1029 if [catch {set sock [socket -async $host $port]}] {
1032 RobotConnect $task $url $sock
1037 if {![llength [info commands htmlSwitch]]} {
1038 set e [info sharedlibextension]
1039 if {[catch {load ./tclrobot$e}]} {
1044 set agent "zmbot/0.2"
1045 if {![catch {set os [exec uname -s -r]}]} {
1046 set agent "$agent ($os)"
1049 puts "agent: $agent"
1060 set acceptLanguage {}
1063 # Rules: allow, deny, url
1065 proc checkrule {task type this} {
1069 if {$debuglevel > 3} {
1070 puts "CHECKRULE $type $this"
1072 if {[info exist control($task,alrules)]} {
1073 foreach l $control($task,alrules) {
1074 if {$debuglevel > 3} {
1078 if {[lindex $l 1] != $type} continue
1079 # consider mask (! negates)
1080 set masks [lindex $l 2]
1082 foreach mask $masks {
1083 if {$debuglevel > 4} {
1084 puts "consider single mask $mask"
1086 if {[string index $mask 0] == "!"} {
1087 set mask [string range $mask 1 end]
1088 if {[string match $mask $this]} continue
1090 if {![string match $mask $this]} continue
1094 if {$debuglevel > 4} {
1098 # OK, we have a match
1099 if {[lindex $l 0] == "allow"} {
1100 if {$debuglevel > 3} {
1101 puts "CHECKRULE MATCH OK"
1105 if {$debuglevel > 3} {
1106 puts "CHECKFULE MATCH FAIL"
1112 if {$debuglevel > 3} {
1113 puts "CHECKRULE MATCH OK"
1120 global debuglevel task
1122 if {[RobotHref $task http://www.indexdata.dk/ href host path]} {
1123 if {![RobotFileExist $task visited $host $path]} {
1124 set outf [RobotFileOpen $task unvisited $host $path]
1125 RobotWriteRecord $outf href 0
1126 RobotFileClose $outf
1131 proc deny {type stuff} {
1134 lappend control($task,alrules) [list deny $type $stuff]
1137 proc allow {type stuff} {
1140 lappend control($task,alrules) [list allow $type $stuff]
1143 proc debug {level} {
1146 set debuglevel $level
1150 global tasks task status robotSeq control
1154 if {[info exists tasks]} {
1155 if {[lsearch -exact $tasks $t] >= 0} {
1161 set status($t,unvisited) 0
1162 set status($t,visited) 0
1163 set status($t,bad) 0
1164 set status($t,raw) 0
1165 set status($t,active) 1
1167 set control($t,distance) 10
1171 # Little utility that ensures that at least one task is present (main).
1174 if {![info exist tasks]} {
1183 set l [llength $argv]
1186 puts {tclrobot: usage:}
1187 puts {tclrobot [-j jobs] [-i idle] [-c count] [-d domain] [-D dir] [-r rules] [url ..]}
1188 puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
1195 set arg [lindex $argv $i]
1196 switch -glob -- $arg {
1198 set t [string range $arg 2 end]
1199 if {![string length $t]} {
1200 set t [lindex $argv [incr i]]
1205 set dir [string range $arg 2 end]
1206 if {![string length $dir]} {
1207 set dir [lindex $argv [incr i]]
1209 lappend daemon_dir $dir
1212 set robotsMax [string range $arg 2 end]
1213 if {![string length $robotsMax]} {
1214 set robotsMax [lindex $argv [incr i]]
1219 set control($task,distance) [string range $arg 2 end]
1220 if {![string length $control($task,distance)]} {
1221 set control($task,distance) [lindex $argv [incr i]]
1226 set dom [string range $arg 2 end]
1227 if {![string length $dom]} {
1228 set dom [lindex $argv [incr i]]
1230 lappend control($task,domains) $dom
1233 set idletime [string range $arg 2 end]
1234 if {![string length $idletime]} {
1235 set idletime [lindex $argv [incr i]]
1240 set acceptLanguage [string range $arg 2 end]
1241 if {![string length $acceptLanguage]} {
1242 set acceptLanguage [lindex $argv [incr i]]
1247 set rfile [string range $arg 2 end]
1248 if {![string length $rfile]} {
1249 set rfile [lindex $argv [incr i]]
1251 catch {unset maxdistance}
1253 if {[info exists maxdistance]} {
1254 set control($task,distance) $maxdistance
1260 if {[RobotHref $task http://www.indexdata.dk/ href host path]} {
1261 if {![RobotFileExist $task visited $host $path]} {
1262 set outf [RobotFileOpen $task unvisited $host $path]
1263 RobotWriteRecord $outf href 0
1264 RobotFileClose $outf
1272 if {![info exist robotsMax]} {
1276 if {[info exist daemon_dir]} {
1281 puts "max distance=$control($t,distance)"
1282 if {[info exists control($t,domains)]} {
1283 puts "domains=$control($t,domains)"
1286 puts "max jobs=$robotsMax"
1292 while {$robotsRunning} {
1296 if {[info exists tasks]} {
1298 set statusfile [open $t/status w]
1299 puts $statusfile "$status($t,unvisited) $status($t,bad) $status($t,visited)"