Added README. Ignore case in keywords in robots.txt.
[tclrobot.git] / robot.tcl
1 #!/usr/bin/tclsh 
2 # $Id: robot.tcl,v 1.16 2001/06/06 07:10:31 adam Exp $
3 #
4 proc RobotFileNext1 {area lead} {
5     puts "RobotFileNext1 area=$area lead=$lead"
6     if {[catch {set ns [glob ${area}/*]}]} {
7         return {}
8     }
9     foreach n $ns {
10         if {[file isfile $n]} {
11             set off [string last / $n]
12             incr off 2
13             return $lead/[string range $n $off end]
14         }
15     }
16     foreach n $ns {
17         if {[file isdirectory $n]} {
18             set off [string last / $n]
19             incr off 2
20             set sb [RobotFileNext1 $n $lead/[string range $n $off end]]
21             if {[string length $sb]} {
22                 return $sb
23             }
24         }
25     }
26     return {}
27 }
28
29 proc RobotWriteRecord {outf fromurl distance} {
30     puts $outf "<zmbot>"
31     puts $outf "<distance>"
32     puts $outf $distance
33     puts $outf "</distance>"
34     puts $outf "<fromurl>"
35     puts $outf $fromurl
36     puts $outf "</fromurl>"
37     puts $outf "</zmbot>"
38 }
39
40 proc RobotReadRecord {inf fromurlx distancex} {
41     upvar $fromurlx fromurl
42     upvar $distancex distance
43     gets $inf
44     gets $inf
45     set distance [string trim [gets $inf]]
46     puts "got distance = $distance"
47     gets $inf
48     gets $inf
49     set fromurl [string trim [gets $inf]]
50 }
51
52 proc RobotFileNext {area} {
53     global robotSeq global idleTime ns
54
55     puts "RobotFileNext robotSeq=$robotSeq"
56     if {$robotSeq < 0} {
57         return {}
58     }
59     if {$robotSeq == 0} {
60         if {[catch {set ns [glob ${area}/*]}]} {
61             return {}
62         }
63     }
64     set off [string length $area]
65     incr off
66     set n [lindex $ns $robotSeq]
67     if {![string length $n]} {
68         set robotSeq -1
69         flush stdout
70         puts "------------ N E X T  R O U N D --------"
71         return wait
72     }
73     incr robotSeq
74     if {[file isfile $n/frobots.txt]} {
75         puts "ok returning http://[string range $n $off end]/robots.txt"
76         return http://[string range $n $off end]/robots.txt
77     } elseif {[file isdirectory $n]} {
78         set sb [RobotFileNext1 $n http://[string range $n $off end]]
79         if {[string length $sb]} {
80             return $sb
81         }
82     }
83     puts "no more work at end of RobotFileNext n=$n"
84     puts "ns=$ns"
85     return {}
86 }
87
88
89 proc RobotFileExist {area host path} {
90     puts "RobotFileExist begin area=$area host=$host path=$path"
91     set lpath [split $path /]
92     set l [llength $lpath]
93     incr l -1
94     set t [lindex $lpath $l]
95     incr l -1
96     set npath $area/$host[join [lrange $lpath 0 $l] /d]/f$t
97     puts "RobotFileExist end npath=$npath"
98     return [file exists $npath]
99 }
100
101 proc RobotFileUnlink {area host path} {
102     puts "RobotFileUnlink begin"
103     puts "area=$area host=$host path=$path"
104     set lpath [split $path /]
105     set l [llength $lpath]
106     incr l -1
107     set t [lindex $lpath $l]
108     incr l -1
109     set npath $area/$host[join [lrange $lpath 0 $l] /d]/f$t
110     puts "npath=$npath"
111     set comp [split $npath /]
112     set l [llength $comp]
113     incr l -1
114     if {[catch {exec rm [join $comp /]}]} return
115     incr l -1
116     for {set i $l} {$i > 0} {incr i -1} {
117         set path [join [lrange $comp 0 $i] /]
118         if {![catch {glob $path/*}]} return
119         exec rmdir ./$path
120     }
121     puts "RobotFileUnlink end"
122 }
123
124 proc RobotFileClose {out} {
125     if [string compare $out stdout] {
126         close $out
127     }
128 }
129
130 proc RobotFileOpen {area host path {mode w}} {
131     set orgPwd [pwd]
132     global workdir
133
134     if {![info exists workdir]} {
135         return stdout
136     }
137     puts "RobotFileOpen orgPwd=$orgPwd area=$area host=$host path=$path mode=$mode"
138     if {[string compare $orgPwd $workdir]} {
139         puts "ooops. RobotFileOpen failed"
140         puts "workdir = $workdir"
141         puts "pwd = $orgPwd"
142         exit 1
143     }
144     set comp [split $area/$host$path /]
145     set len [llength $comp]
146     incr len -1
147     for {set i 0} {$i < $len} {incr i} {
148         if {$i > 1} {
149             set d "d[lindex $comp $i]" 
150         } else {
151             set d [lindex $comp $i]
152         }
153         if {[catch {cd ./$d}]} {
154             exec mkdir $d
155             cd ./$d
156             if {![string compare $area unvisited] && $i == 1 && $mode == "w"} {
157                 set out [open frobots.txt w]
158                 puts "creating robots.txt in $d"
159                 close $out
160             }
161         }
162     }
163     set d [lindex $comp $len]
164     if {[string length $d]} {
165         if {[file isdirectory $d]} {
166             set out [open $d/f $mode]
167         } else {
168             set out [open f$d $mode]
169         }
170     } else {
171         set out [open f $mode]
172     }
173     cd $orgPwd
174     return $out
175 }
176
177 proc RobotRR {} {
178     global robotSeq robotsRunning
179
180     incr robotsRunning -1
181     while {$robotsRunning} {
182         vwait robotsRunning
183     }
184     set robotSeq 0
185     RobotStart
186 }
187
188 proc RobotRestart {url sock} {
189     global URL robotsRunning
190
191     close $sock
192     after cancel $URL($sock,cancel) 
193
194     foreach v [array names URL $url,*] {
195         unset URL($v)
196     }
197
198     incr robotsRunning -1
199     RobotStart
200 }
201
202 proc RobotStart {} {
203     global URL
204     global robotsRunning robotsMax idleTime
205   
206     puts "RobotStart"
207     while {1} {
208         set url [RobotFileNext unvisited]
209         if {![string length $url]} {
210             return
211         }
212         incr robotsRunning
213         if {[string compare $url wait] == 0} {
214             after $idleTime RobotRR
215             return
216         }
217         set r [RobotGetUrl $url {}]
218         if {!$r} {
219             if {$robotsRunning >= $robotsMax} return
220         } else {
221             incr robotsRunning -1
222             if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
223                 set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
224                 RobotFileClose $outf
225             }
226             RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
227         }
228     }
229 }
230
231 proc headSave {url out} {
232     global URL
233     
234     if {[info exists URL($url,head,last-modified)]} {
235         puts $out "<lastmodified>$URL($url,head,last-modified)</lastmodified>"
236     }
237     puts $out {<si>}
238     if {[info exists URL($url,head,date)]} {
239         puts $out " <date>$URL($url,head,date)</date>"
240     }
241     if {[info exists URL($url,head,content-length)]} {
242         puts $out " <by>$URL($url,head,content-length)</by>"
243     }
244     if {[info exists URL($url,head,server)]} {
245         puts $out " <format>$URL($url,head,server)</format>"
246     }
247     puts $out {</si>}
248     puts $out {<publisher>}
249     puts $out " <identifier>$url</identifier>"
250     if {[info exists URL($url,head,content-type)]} {
251         puts $out " <type>$URL($url,head,content-type)</type>"
252     }
253     puts $out {</publisher>}
254 }
255
256 proc RobotHref {url hrefx hostx pathx} {
257     global URL domains
258     upvar $hrefx href
259     upvar $hostx host
260     upvar $pathx path
261
262     puts "Ref url = $url href=$href"
263
264     if {[string first { } $href] >= 0} {
265         return 0
266     }
267     if {[string length $href] > 256} {
268         return 0
269     }
270     if {[string first {?} $url] >= 0 && [string first {?} $href] >= 0} {
271         return 0
272     }
273     # get method (if any)
274     if {![regexp {^([^/:]+):(.*)} $href x method hpath]} {
275         set hpath $href
276         set method http
277     } else {
278         if {[string compare $method http]} {
279             return 0
280         }
281     }
282     # get host (if any)
283     if {[regexp {^//([^/]+)([^\#]*)} $hpath x host surl]} {
284         if {![string length $surl]} {
285             set surl /
286         }
287         if {[info exist domains]} {
288             set ok 0
289             foreach domain $domains {
290                 if {[string match $domain $host]} {
291                     set ok 1
292                     break
293                  }
294             }
295             if {!$ok} {
296                 return 0
297             }
298         }
299     } else {
300         regexp {^([^\#]*)} $hpath x surl
301         set host $URL($url,hostport)
302     }
303     if {![string length $surl]} {
304         return 0
305     }
306     if {[string first / $surl]} {
307         # relative path
308         regexp {^([^\#?]*)} $URL($url,path) x dpart
309         set l [string last / $dpart]
310         if {[expr $l >= 0]} {
311             set surl [string range $dpart 0 $l]$surl
312         } else {
313             set surl $dpart/$surl
314         }
315     }
316     set c [split $surl /]
317     set i [llength $c]
318     incr i -1
319     set path [lindex $c $i]
320     incr i -1
321     while {$i >= 0} {
322         switch -- [lindex $c $i] {
323             .. {
324                 incr i -2
325                 if {$i < 0} {
326                     set i 0
327                 }
328             }
329             . {
330                 incr i -1
331             }
332             default {
333                 set path [lindex $c $i]/$path
334                 incr i -1
335             }
336         }
337     }
338     regsub -all {~} $path {%7E} path
339     set href "$method://$host$path"
340     puts "Ref href = $href"
341     return 1
342 }
343
344 proc RobotError {url code} {
345     global URL
346
347     puts "Bad URL $url, $code"
348     set fromurl {}
349     set distance -1
350     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
351         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
352         RobotReadRecord $inf fromurl distance
353         RobotFileClose $inf
354     }
355     RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
356     if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
357         set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
358         RobotWriteRecord $outf $fromurl $distance
359         RobotFileClose $outf
360     }
361 }
362
363 proc RobotRedirect {url tourl code} {
364     global URL
365
366     puts "Redirecting from $url to $tourl"
367
368     set distance {}
369     set fromurl {}
370     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
371         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
372         RobotReadRecord $inf fromurl distance
373         RobotFileClose $inf
374     }
375     if {![RobotFileExist bad $URL($url,hostport) $URL($url,path)]} {
376         set outf [RobotFileOpen bad $URL($url,hostport) $URL($url,path)]
377         RobotWriteRecord $outf $fromurl $distance
378         RobotFileClose $outf
379     }
380     if {[RobotHref $url tourl host path]} {
381         if {![RobotFileExist visited $host $path]} {
382             if {![RobotFileExist unvisited $host $path]} {
383                 set outf [RobotFileOpen unvisited $host $path]
384                 RobotWriteRecord $outf $fromurl $distance
385                 RobotFileClose $outf
386             }
387         } else {
388             set olddistance {}
389             set inf [RobotFileOpen visited $host $path r]
390             RobotReadRecord $inf oldurl olddistance
391             RobotFileClose $inf
392             if {[string length $olddistance] == 0} {
393                 set olddistance 1000
394             }
395             if {[string length $distance] == 0} {
396                 set distance 1000
397             }
398             puts "distance=$distance olddistance=$olddistance"
399             if {[expr $distance < $olddistance]} {
400                 set outf [RobotFileOpen unvisited $host $path]
401                 RobotWriteRecord $outf $tourl $distance
402                 RobotFileClose $outf
403             }
404         }
405     }
406     if {[catch {RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)}]} {
407         puts "unlink failed"
408         exit 1
409     }
410 }
411
412 proc RobotTextHtml {url out} {
413     global URL maxDistance
414
415     set distance 0
416     if {$maxDistance < 1000 && [info exists URL($url,dist)]} {
417         set distance [expr $URL($url,dist) + 1]
418     }
419     htmlSwitch $URL($url,buf) \
420         title {
421             puts $out "<title>$body</title>"
422         } -nonest meta {
423             puts -nonewline $out "<meta"
424             foreach a [array names parm] {
425                 puts -nonewline $out " $a"
426                 puts -nonewline $out {="}
427                 puts -nonewline $out $parm($a)
428                 puts -nonewline $out {"}
429             }
430             puts $out {></meta>}
431         } body {
432             regsub -all -nocase {<script.*</script>} $body {} abody
433             regsub -all {<[^\>]+>} $abody {} nbody
434             puts $out "<documentcontent>"
435             puts $out $nbody
436             puts $out "</documentcontent>"
437         } -nonest a {
438             if {![info exists parm(href)]} {
439                 puts "no href"
440                 continue
441             }
442             if {[expr $distance <= $maxDistance]} {
443                 set href [string trim $parm(href)]
444                 if {![RobotHref $url href host path]} continue
445                 
446                 puts $out "<cr>"
447                 puts $out "<identifier>$href</identifier>"
448                 puts $out "<description>$body</description>"
449                 puts $out "</cr>"
450
451                 if {![RobotFileExist visited $host $path]} {
452                     set olddistance 1000
453                     if {![RobotFileExist bad $host $path]} {
454                         if {[RobotFileExist unvisited $host $path]} {
455                             set inf [RobotFileOpen unvisited $host $path r]
456                             RobotReadRecord $inf oldurl olddistance
457                             RobotFileClose $inf
458                         }
459                     } else {
460                         set olddistance 0
461                     }
462                     if {[string length $olddistance] == 0} {
463                         set olddistance 1000
464                     }
465                     if {[expr $distance < $olddistance]} {
466                         set outf [RobotFileOpen unvisited $host $path]
467                         RobotWriteRecord $outf $url $distance
468                         RobotFileClose $outf
469                     }
470                 } elseif {[string compare $href $url]} {
471                     set inf [RobotFileOpen visited $host $path r]
472                     RobotReadRecord $inf xurl olddistance
473                     close $inf
474                     if {[string length $olddistance] == 0} {
475                         set olddistance 1000
476                     }
477                     if {[expr $distance < $olddistance]} {
478                         puts "OK remarking url=$url href=$href"
479                         puts "olddistance = $olddistance"
480                         puts "newdistance = $distance"
481                         set outf [RobotFileOpen unvisited $host $path]
482                         RobotWriteRecord $outf $url $distance
483                         RobotFileClose $outf
484                     }
485                 }
486             }
487         } -nonest area {
488             if {![info exists parm(href)]} {
489                 puts "no href"
490                 continue
491             }
492             if {[expr $distance <= $maxDistance]} {
493                 set href [string trim $parm(href)]
494                 if {![RobotHref $url href host path]} continue
495                 
496                 puts $out "<cr>"
497                 puts $out "<identifier>$href</identifier>"
498                 puts $out "<description></description>"
499                 puts $out "</cr>"
500
501                 if {![RobotFileExist visited $host $path]} {
502                     set olddistance 1000
503                     if {![RobotFileExist bad $host $path]} {
504                         if {[RobotFileExist unvisited $host $path]} {
505                             set inf [RobotFileOpen unvisited $host $path r]
506                             RobotReadRecord $inf oldurl olddistance
507                             RobotFileClose $inf
508                         }
509                     } else {
510                         set olddistance 0
511                     }
512                     if {[string length $olddistance] == 0} {
513                         set olddistance 1000
514                     }
515                     if {[expr $distance < $olddistance]} {
516                         set outf [RobotFileOpen unvisited $host $path]
517                         RobotWriteRecord $outf $url $distance
518                         RobotFileClose $outf
519                     }
520                 } elseif {[string compare $href $url]} {
521                     set inf [RobotFileOpen visited $host $path r]
522                     RobotReadRecord $inf xurl olddistance
523                     close $inf
524                     if {[string length $olddistance] == 0} {
525                         set olddistance 1000
526                     }
527                     if {[expr $distance < $olddistance]} {
528                         puts "OK remarking url=$url href=$href"
529                         puts "olddistance = $olddistance"
530                         puts "newdistance = $distance"
531                         set outf [RobotFileOpen unvisited $host $path]
532                         RobotWriteRecord $outf $url $distance
533                         RobotFileClose $outf
534                     }
535                 }
536             }
537         }
538 }
539
540 proc RobotsTxt {url} {
541     global agent URL
542
543     RobotsTxt0 URL(URL($url,hostport),robots) $URL($url,buf)
544 }
545
546 proc RobotsTxt0 {v buf} {
547     global URL agent
548     set section 0
549     foreach l [split $buf \n] {
550         if {[regexp {([-A-Za-z]+):[ \t]*([^\#\t ]+)} $l match cmd arg]} {
551             puts "cmd=$cmd arg=$arg"
552             switch -- [string tolower $cmd] {
553                 user-agent {
554                     if {$section} break
555                     set pat [string tolower $arg]*
556                     set section [string match $pat $agent]
557                 }
558                 disallow {
559                     if {$section} {
560                         puts "rule [list 0 $arg]"
561                         lappend $v [list 0 $arg]
562                     }
563                 }
564                 allow {
565                     if {$section} {
566                         puts "rule [list 1 $arg]"
567                         lappend $v [list 1 $arg]
568                     }
569                 }
570             }
571         }
572     }
573 }
574
575 proc RobotTextPlain {url out} {
576     global URL
577
578     puts $out "<documentcontent>"
579     puts $out $URL($url,buf)
580     puts $out "</documentcontent>"
581
582     if {![string compare $URL($url,path) /robots.txt]} {
583         RobotsTxt $url
584     }
585 }
586
587 proc Robot200 {url} {
588     global URL domains
589     
590     set out [RobotFileOpen visited $URL($url,hostport) $URL($url,path)]
591     puts $out "<zmbot>"
592
593     set distance 1000
594     if {[RobotFileExist unvisited $URL($url,hostport) $URL($url,path)]} {
595         set inf [RobotFileOpen unvisited $URL($url,hostport) $URL($url,path) r]
596         RobotReadRecord $inf fromurl distance
597         RobotFileClose $inf
598     }
599     set URL($url,dist) $distance
600     puts $out "<distance>"
601     puts $out "  $distance"
602     puts $out "</distance>"
603     headSave $url $out
604     puts "Parsing $url distance=$distance"
605     switch $URL($url,head,content-type) {
606         text/html {
607             if {[string length $distance]} {
608                 RobotTextHtml $url $out
609             }
610         }
611         text/plain {
612             RobotTextPlain $url $out
613         }
614         application/pdf {
615             set pdff [open test.pdf w]
616             puts -nonewline $pdff $URL($url,buf)
617             close $pdff
618         }
619     }
620     puts $out "</zmbot>"
621     RobotFileClose $out
622     # puts "Parsing done"
623     RobotFileUnlink unvisited $URL($url,hostport) $URL($url,path)
624 }
625
626 proc RobotReadContent {url sock binary} {
627     global URL
628
629     puts "RobotReadContent $url"
630     set buffer [read $sock 16384]
631     set readCount [string length $buffer]
632
633     if {$readCount <= 0} {
634         Robot200 $url
635         RobotRestart $url $sock
636     } elseif {!$binary && [string first \0 $buffer] >= 0} {
637         Robot200 $url
638         RobotRestart $url $sock
639     } else {
640         # puts "Got $readCount bytes"
641         set URL($url,buf) $URL($url,buf)$buffer
642     }
643 }
644
645 proc RobotReadHeader {url sock} {
646     global URL
647
648     puts "RobotReadHeader $url"
649     if {[catch {set buffer [read $sock 2148]}]} {
650         RobotError $url 404
651         RobotRestart $url $sock
652     }
653     set readCount [string length $buffer]
654     
655     if {$readCount <= 0} {
656         RobotError $url 404
657         RobotRestart $url $sock
658     } else {
659         # puts "Got $readCount bytes"
660         set URL($url,buf) $URL($url,buf)$buffer
661         
662         set n [string first \r\n\r\n $URL($url,buf)]
663         if {$n > 1} {
664             set code 0
665             set version {}
666             set headbuf [string range $URL($url,buf) 0 $n]
667             incr n 4
668             set URL($url,buf) [string range $URL($url,buf) $n end]
669             
670             regexp {^HTTP/([0-9.]+)[ ]+([0-9]+)} $headbuf x version code
671             set lines [split $headbuf \n]
672             foreach line $lines {
673                 if {[regexp {^([^:]+):[ ]+([^;]*)} $line x name value]} {
674                     set URL($url,head,[string tolower $name]) [string trim $value]
675                 }
676             }
677             puts "code = $code"
678             set URL($url,state) skip
679             switch $code {
680                 301 {
681                     RobotRedirect $url $URL($url,head,location) 301
682                     RobotRestart $url $sock
683                 }
684                 302 {
685                     RobotRedirect $url $URL($url,head,location) 302
686                     RobotRestart $url $sock
687                 }
688                 200 {
689                     if {![info exists URL($url,head,content-type)]} {
690                         set URL($url,head,content-type) {}
691                     }
692                     set binary 0
693                     switch $URL($url,head,content-type) {
694                         application/pdf {
695                             set binary 1
696                         }
697                     }
698                     fileevent $sock readable [list RobotReadContent $url $sock $binary]
699                 }
700                 default {
701                     RobotError $url $code
702                     RobotRestart $url $sock
703                 }
704             }
705         }
706     }
707 }
708
709 proc RobotSockCancel {url sock} {
710
711     puts "RobotSockCancel sock=$sock url=$url"
712     RobotError $url 401
713     RobotRestart $url $sock
714 }
715
716 proc RobotConnect {url sock} {
717     global URL agent
718
719     fconfigure $sock -translation {lf crlf} -blocking 0
720     fileevent $sock readable [list RobotReadHeader $url $sock]
721     puts $sock "GET $URL($url,path) HTTP/1.0"
722     puts $sock "Host: $URL($url,host)"
723     puts $sock "User-Agent: $agent"
724     puts $sock ""
725     flush $sock
726     set URL($sock,cancel) [after 30000 [list RobotSockCancel $url $sock]]
727 }
728
729 proc RobotNop {} {
730
731 }
732
733 proc RobotGetUrl {url phost} {
734     global URL robotsRunning
735     flush stdout
736     puts "RobotGetUrl --------- robotsRunning=$robotsRunning url=$url"
737     if {![regexp {([^:]+)://([^/]+)(.*)} $url x method hostport path]} {
738         return -1
739     }
740     if {![regexp {([^:]+):([0-9]+)} $hostport x host port]} {
741         set port 80
742         set host $hostport
743     }
744     set URL($url,method) $method
745     set URL($url,host) $host
746     set URL($url,hostport) $hostport
747     set URL($url,path) $path
748     set URL($url,state) head
749     set URL($url,buf) {}
750
751     if {[string compare $path /robots.txt]} {
752         set ok 1
753         if {![info exists URL($hostport,robots)]} {
754             puts "READING robots.txt for host $hostport"
755             if {[RobotFileExist visited $hostport /robots.txt]} {
756                 set inf [RobotFileOpen visited $hostport /robots.txt r]
757                 set buf [read $inf 32768]
758                 close $inf
759             } else {
760                 set buf "User-agent: *\nAllow: /\n"
761             }
762             RobotsTxt0 URL($hostport,robots) $buf
763         }
764         if {[info exists URL($hostport,robots)]} {
765             foreach l $URL($hostport,robots) {
766                 if {[string first [lindex $l 1] $path] == 0} {
767                     set ok [lindex $l 0]
768                     break
769                 }
770             }
771         }
772         if {!$ok} {
773             puts "skipped due to robots.txt"
774             return -1
775         }
776     }
777     if [catch {set sock [socket -async $host $port]}] {
778         return -1
779     }
780     RobotConnect $url $sock
781
782     return 0
783 }
784
785 if {![llength [info commands htmlSwitch]]} {
786     set e [info sharedlibextension]
787     if {[catch {load ./tclrobot$e}]} {
788         load tclrobot$e
789     }
790 }
791
792 set agent "zmbot/0.0"
793 if {![catch {set os [exec uname -s -r]}]} {
794     set agent "$agent ($os)"
795 }
796
797 puts "agent: $agent"
798
799 proc bgerror {m} {
800     global errorInfo
801     puts "BGERROR $m"
802     puts $errorInfo
803 }
804
805 set robotsRunning 0
806 set robotSeq 0
807 set workdir [pwd]
808 set idleTime 60000
809
810 set i 0
811 set l [llength $argv]
812
813 if {$l < 2} {
814     puts {tclrobot: usage [-j jobs] [-i idle] [-c count] [-d domain] [url ..]}
815     puts " Example: -c 3 -d '*.dk' http://www.indexdata.dk/"
816     exit 1
817 }
818
819 while  {$i < $l} {
820     set arg [lindex $argv $i]
821     switch -glob -- $arg {
822         -j* {
823             set robotsMax [string range $arg 2 end]
824             if {![string length $robotsMax]} {
825                 set robotsMax [lindex $argv [incr i]]
826             }
827         }
828         -c* {
829             set maxDistance [string range $arg 2 end]
830             if {![string length $maxDistance]} {
831                 set maxDistance [lindex $argv [incr i]]
832             }
833         }
834         -d* {
835             set dom [string range $arg 2 end]
836             if {![string length $dom]} {
837                 set dom [lindex $argv [incr i]]
838             }
839             lappend domains $dom
840         }
841         -i* {
842             set idleTime [string range $arg 2 end]
843             if {![string length $idleTime]} {
844                 set idleTime [lindex $argv [incr i]]
845             }
846         }
847         default {
848             set href $arg
849             if {[RobotHref http://www.indexdata.dk/ href host path]} {
850                 if {![RobotFileExist visited $host $path]} {
851                     set outf [RobotFileOpen unvisited $host $path]
852                     RobotWriteRecord $outf href 0
853                     RobotFileClose $outf
854                 }
855             }
856         }
857     }
858     incr i
859 }
860
861 if {![info exist domains]} {
862     set domains {*}
863 }
864 if {![info exist maxDistance]} {
865     set maxDistance 50
866 }
867 if {![info exist robotsMax]} {
868     set robotsMax 5
869 }
870
871 puts "domains=$domains"
872 puts "max distance=$maxDistance"
873 puts "max jobs=$robotsMax"
874
875 RobotStart
876
877 while {$robotsRunning} {
878     vwait robotsRunning
879 }