/[cvs]/wolfpack/wolfpack.tcl
ViewVC logotype

Contents of /wolfpack/wolfpack.tcl

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.22 - (show annotations) (download) (as text)
Tue Jan 14 10:07:23 2003 UTC (16 years, 9 months ago) by tothwolf
Branch: MAIN
Changes since 1.21: +2 -2 lines
File MIME type: application/x-tcl
*** empty log message ***

1 #! /bin/sh
2 # \
3 # Nice little hack to find latest version of tclsh in PATH \
4 # \
5 # NOTE: backslash and semicolon placements are important! \
6 # \
7 # Search for tclsh[0-9].[0-9] in each valid dir in PATH \
8 for dir in $(echo $PATH | sed 's/:/ /g'); \
9 do \
10 if test -d $dir; \
11 then \
12 files=$(/bin/ls $dir | egrep '^tclsh[0-9]\.[0-9]$'); \
13 if test "$files" != ""; \
14 then \
15 versions="${versions:+$versions }$(echo $files | sed 's/tclsh//g')"; \
16 fi; \
17 fi; \
18 done; \
19 # Loop over each version to find the latest version of tclsh \
20 for ver in $versions; \
21 do \
22 tmpver=$(echo $ver | sed 's/\.//g'); \
23 if test "$lasttmpver" != ""; \
24 then \
25 if test "$tmpver" -gt "$lasttmpver"; \
26 then \
27 lastver=$ver; \
28 lasttmpver=$tmpver; \
29 fi; \
30 else \
31 lastver=$ver; \
32 lasttmpver=$tmpver; \
33 fi; \
34 done; \
35 # Use the latest tclsh version found, otherwise fall back to 'tclsh' \
36 exec tclsh$lastver "$0" "$@"
37 ###############################################################################
38 ##
39 ## Wolfpack - A modular Tcl system for Eggdrop 1.3.0+ with Tcl 8.0+
40 ## Copyright (C) 1998-2003 Tothwolf <tothwolf@concentric.net>
41 ##
42 ## This program is free software; you can redistribute it and/or modify
43 ## it under the terms of the GNU General Public License as published by
44 ## the Free Software Foundation; either version 2 of the License, or
45 ## (at your option) any later version.
46 ##
47 ## This program is distributed in the hope that it will be useful,
48 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
49 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 ## GNU General Public License for more details.
51 ##
52 ## You should have received a copy of the GNU General Public License
53 ## along with this program; if not, write to the Free Software
54 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
55 ##
56 ###############################################################################
57 ##
58 ## You should not need to edit anything in any of these files.
59 ##
60 ## './wolfpack.tcl -c' may be used to configure initial settings.
61 ## './wolfpack.tcl --help' will list all avaliable options.
62 ##
63 ## Use '.wpconf' in dcc chat with the bot to configure everything else.
64 ##
65 ###############################################################################
66 ##
67 ## $Id: wolfpack.tcl,v 1.21 2003/01/14 09:56:30 tothwolf Exp $
68 ##
69
70 # make sure Tcl version is compatible with this code (we use namespaces)
71 if {[catch {package require Tcl 8.0}]} then {
72 if {[info exists argv0]} then {
73 puts "Wolfpack: Error: wolfpack requires Tcl 8.0 or later to load."
74 } else {
75 putloglev o * "Wolfpack: Error: wolfpack requires Tcl 8.0 or later to load."
76 }
77 return 0
78 }
79
80 namespace eval :: {
81
82 # Eggdrop doesn't currently set argv0, so we use that to detect load type.
83 global argv0
84
85 if {![info exists argv0]} then {
86 # HACK: quick fix for buggy tcl variables in older bots
87 catch {set version}
88 catch {set numversion}
89 # require eggdrop 1.3.0 or later
90 if {(![info exists version]) || (![info exists numversion]) || \
91 ($numversion < 1030000)} then {
92 putloglev o * "Wolfpack: Error: wolfpack requires Eggdrop 1.3.0 or later to load."
93 return 0
94 }
95 } else {
96 # emulate eggdrop's putloglev when loading with tclsh
97 if {![string compare "" [info commands putloglev]]} then {
98 proc putloglev {level channel text} {
99 puts $text
100 }
101 }
102 }
103
104 ##
105 ## Log tcl messages where they can be seen
106 ##
107 ## Args: text
108 ## Returns: nothing
109 ##
110 proc tclLog {text} {
111 # Tcl's tclLog embeds newlines in it's output
112 if {[string first \n $text] == -1} then {
113 putloglev o * "Wolfpack: \[Tcl\]: $text"
114 } else {
115 foreach line [split $text \n] {
116 putloglev o * "Wolfpack: \[Tcl\]: $line"
117 }
118 }
119 return
120 }
121
122 } ;# namespace ::
123
124 namespace eval ::wp {
125
126 # manage package information
127 package forget ::wp
128 package provide ::wp 1.9.9.0
129
130 # set namespace variable
131 set NamespaceCurrent [namespace current]
132
133 ##
134 ## WARNING: If you change these, you may render your module database useless!
135 ##
136
137 # Max number of lines to scan in a module file
138 set moduleDatabaseConfig(scanlines) 30
139
140 # Max depth to list directories in module path
141 set moduleDatabaseConfig(maxdepth) 4
142
143 # Module database version
144 set moduleDatabaseConfig(version) 2.0
145
146 # Module database header
147 set moduleDatabaseConfig(header) "Wolfpack module database "
148
149 # Module database defaults
150 set moduleDatabaseConfig(defaults) "{version 0.1} {description {(no description)}} {load 0}"
151
152 # Versioned module database formats
153 array set moduleDatabaseFormat {
154 2.0 "{version config help description provides requires} {load file md5sum}"
155 }
156
157 # md5 style, command name and result string index
158 array set md5Format {
159 bsd "md5 3"
160 gnu "md5sum 0"
161 }
162
163 # Configuration file
164 set IconfigDefaults(cfgfile) wolfpack.conf
165
166 # Interactive configuration prompt
167 set IconfigDefaults(prompt) "wolfpack> "
168
169 # Configuration defaults, values and descriptions
170 array set configDefaults {
171 modulepath {word modules/ {} {Module path}}
172 configpath {word wpconf/ {} {Config path}}
173 datapath {word wpdata/ {} {Data path}}
174 helppath {word wphelp/ {} {Help path}}
175 moddbfile {word wolfpack.db {} {Module database file}}
176 rebuild {{range {0 1}} 1 {} {Automatically rebuild database}}
177 verbose {{range {0 1}} 0 {} {Verbose operation}}
178 time {{range {0 1}} 0 {} {Time database compare and rebuild}}
179 commandchar {word - {} {Character to prefix public commands with}}
180 }
181
182 # Command line option defaults
183 array set optionData {
184 config 0
185 rebuild 0
186 norebuild 0
187 time 0
188 verbose 0
189 quiet 0
190 debug 0
191 }
192
193 # Exported commands
194 set ExportList {
195 md5Sum
196 md5Init
197 replaceExpr
198 listFiles
199 listSubdirs
200 findFiles
201 createFile
202 createDir
203 backupFile
204 listSave
205 listLoad
206 arraySave
207 arrayLoad
208 arraySetAll
209 arrayFindElementName
210 arrayMaxElementDataLength
211 listMaxElementLength
212 splitList
213 arraySearch
214 commandChar
215 dataFormatDefault
216 dataFormatValue
217 dataFormatReplace
218 dataFormatBuild
219 dataFormatConvert
220 scanModule
221 getModuleDatabaseData
222 setModuleDatabaseData
223 saveModuleDatabase
224 loadModuleDatabase
225 compareModuleDatabase
226 rebuildModuleDatabase
227 listModules
228 moduleExists
229 moduleLoaded
230 moduleBindUnbind
231 moduleLoad
232 moduleUnload
233 moduleConfigSave
234 moduleConfigLoad
235 moduleConfigCheckdefs
236 moduleConfig
237 moduleDataSave
238 moduleDataLoad
239 moduleDataBackup
240 moduleData
241 buildCommandTable
242 whichCommand
243 whichModule
244 whichModuleCommand
245 compareVersion
246 wpLog
247 }
248
249 ##
250 ## Create md5 checksum for a file
251 ##
252 ## Args: filename
253 ## Returns: md5 checksum if successful
254 ## Errors: permission denied,
255 ## no such file,
256 ## not a file,
257 ## can't exec md5 command
258 ##
259 proc md5Sum {file} {
260 variable md5Config
261
262 if {![file exists $file]} then {
263 error "$file: no such file"
264 } else {
265 if {![file isfile $file]} then {
266 error "$file: not a file"
267 } else {
268 if {![file readable $file]} then {
269 error "$file: permission denied"
270 } else {
271 if {[catch {set sum [lindex [exec $md5Config(command) $file] $md5Config(index)]} result]} then {
272 error "$file: $result"
273 } else {
274 return $sum
275 }
276 }
277 }
278 }
279 }
280
281 ##
282 ## Init md5 command
283 ##
284 ## Args: none
285 ## Returns: 1 if a useable md5 command found
286 ## 0 otherwise
287 ##
288 proc md5Init {} {
289 variable md5Config
290 variable md5Format
291
292 foreach type [array names md5Format] {
293 foreach {command index} $md5Format($type) {break}
294 if {([catch {exec $command ""} result]) && \
295 (![regexp -- "^couldn't execute" $result])} then {
296 set md5Config(command) $command
297 set md5Config(index) $index
298 return 1
299 }
300 }
301 return 0
302 }
303
304 ##
305 ## Replace all occurances of an expression in a string with the given text
306 ##
307 ## Args: string, expr, replacement text
308 ## Returns: string
309 ##
310 proc replaceExpr {string expr {replace ""}} {
311 while {[regexp -nocase -- $expr $string]} {
312 regsub -all -- $expr $string $replace string
313 }
314 return $string
315 }
316
317 ##
318 ## List files in a path
319 ##
320 ## Args: path
321 ## Returns: list of files in the given path,
322 ## nothing if no files in the given path
323 ## Errors: permission denied,
324 ## no such directory,
325 ## not a directory
326 ##
327 proc listFiles {path} {
328 if {![file exists $path]} then {
329 error "$path: no such directory"
330 } else {
331 if {![file isdirectory $path]} then {
332 error "$path: not a directory"
333 } else {
334 if {![file readable $path]} then {
335 error "$path: permission denied"
336 } else {
337 set ret ""
338 foreach name [lsort [glob -nocomplain -- [file join $path *]]] {
339 if {[file isfile $name]} then {
340 lappend ret $name
341 }
342 }
343 return $ret
344 }
345 }
346 }
347 }
348
349 ##
350 ## List subdirs in a path
351 ##
352 ## Args: path
353 ## Returns: list of subdirs in the given path,
354 ## nothing if no subdirs in the given path
355 ## Errors: permission denied,
356 ## no such directory,
357 ## not a directory
358 ##
359 proc listSubdirs {path} {
360 if {![file exists $path]} then {
361 error "$path: no such directory"
362 } else {
363 if {![file isdirectory $path]} then {
364 error "$path: not a directory"
365 } else {
366 if {![file readable $path]} then {
367 error "$path: permission denied"
368 } else {
369 set ret ""
370 foreach name [lsort [glob -nocomplain -- [file join $path *]]] {
371 if {[file isdirectory $name]} then {
372 lappend ret $name
373 }
374 }
375 return $ret
376 }
377 }
378 }
379 }
380
381 ##
382 ## List files with a set ext in a path and its subdirs up to a set depth
383 ##
384 ## Args: path, max search depth, file extension
385 ## Returns: list of files with a set ext in the given path and its subdirs,
386 ## nothing if no matching files are found
387 ##
388 proc findFiles {path depth {ext ""}} {
389 set ret ""
390 set foundDirs "$path "
391 set searchDirs $path
392 for {
393 set currentDepth 0
394 } {($currentDepth <= $depth) || (!$depth)} {
395 incr currentDepth
396 } {
397 set subDirs ""
398 foreach dir $searchDirs {
399 if {[catch {set dirList [listSubdirs $dir]} result]} then {
400 wpLog o * "Error: unable to get file listing: $result"
401 } elseif {[string compare "" $dirList]} then {
402 append subDirs $dirList " "
403 }
404 }
405 if {![string compare "" $subDirs]} then {
406 break
407 }
408 append foundDirs $subDirs " "
409 set searchDirs $subDirs
410 }
411 foreach dir $foundDirs {
412 if {[catch {set files [listFiles $dir]} result]} then {
413 wpLog o * "Error: unable to get file listing: $result"
414 } else {
415 if {[string compare "" $ext]} then {
416 foreach file $files {
417 if {![string compare $ext \
418 [string tolower [file extension $file]]]} then {
419 lappend ret $file
420 }
421 }
422 } else {
423 set ret $files
424 }
425 }
426 }
427 return $ret
428 }
429
430 ##
431 ## Check if a file exists, and create it if not
432 ##
433 ## Args: filename, verbose {-1,0,1}, description, force new file
434 ## Returns: 1 if the file was created successfully
435 ## 0 if the operation failed
436 ## -1 if the file already exists
437 ##
438 proc createFile {file {verbose 0} {desc "file "} {force 0}} {
439 if {($force) || (![file exists $file])} then {
440 if {[catch {set fd [open $file w]} result]} then {
441 if {$verbose >= 0} then {
442 wpLog o * "Error: unable to create ${desc}`[file tail $file]': $result"
443 }
444 } else {
445 if {(!$force) && ($verbose >= 1)} then {
446 wpLog o * "Warning: ${desc}`[file tail $file]' does not exist -- creating"
447 }
448 close $fd
449 return 1
450 }
451 } elseif {[file isfile $file]} then {
452 return -1
453 } elseif {$verbose >= 0} then {
454 wpLog o * "Error: not a file: $file"
455 }
456 return 0
457 }
458
459 ##
460 ## Check if a directory exists, and create it if not
461 ##
462 ## Args: directory, verbose {-1,0,1}, description
463 ## Returns: 1 if the directory was created successfully
464 ## 0 if the operation failed
465 ## -1 if the directory already exists
466 ##
467 proc createDir {dir {verbose 0} {desc "directory "}} {
468 if {![file exists $dir]} then {
469 if {[catch {file mkdir $dir} result]} then {
470 if {$verbose >= 0} then {
471 wpLog o * "Error: unable to create ${desc}`[file tail $dir]': $result"
472 }
473 } else {
474 if {$verbose >= 1} then {
475 wpLog o * "Warning: ${desc}`[file tail $dir]' does not exist -- creating"
476 }
477 return 1
478 }
479 } elseif {[file isdirectory $dir]} then {
480 return -1
481 } elseif {$verbose >= 0} then {
482 wpLog o * "Error: not a directory: $dir"
483 }
484 return 0
485 }
486
487 ##
488 ## Create a backup of the given file with an optional suffix
489 ##
490 ## Args: filename, suffix, verbose {-1,0,1}
491 ## Returns: 1 if successful
492 ## -1 if file is 0 in size
493 ## 0 otherwise
494 ##
495 proc backupFile {file {verbose 0} {suffix ~bak}} {
496 variable NamespaceCurrent
497
498 if {[string compare "" $suffix]} then {
499 if {[file size $file]} then {
500 if {[catch {
501 file copy -force $file $file${suffix}
502 } result]} then {
503 if {$verbose >= 0} then {
504 wpLog o * $NamespaceCurrent "Error: unable to create backup file for `[file tail $file]': $result"
505 }
506 } else {
507 return 1
508 }
509 } else {
510 return -1
511 }
512 }
513 return 0
514 }
515
516 ##
517 ## Save data from a list into a file
518 ##
519 ## Args: list, filename, verbose {-1,0,1}, description, access flag
520 ## Returns: 1 is successful,
521 ## 0 otherwise
522 ##
523 proc listSave {listName file {verbose 0} {desc "file "} {access w}} {
524 upvar 1 $listName list
525
526 if {[createFile $file $verbose $desc]} then {
527 if {[catch {set fd [open $file $access]} result]} then {
528 if {$verbose >= 0} then {
529 wpLog o * "Error: unable to open ${desc}`$file' for writing: $result"
530 }
531 } else {
532 if {[info exists list]} then {
533 foreach data $list {
534 puts $fd [list $data]
535 }
536 }
537 close $fd
538 return 1
539 }
540 }
541 return 0
542 }
543
544 ##
545 ## Load data into a list from a file
546 ##
547 ## Args: list, filename, verbose {-1,0,1}, description, ignore regsub
548 ## Returns: 1 if successful,
549 ## 0 otherwise
550 ##
551 proc listLoad {listName file {verbose 0} {desc "file "} {ignore "^#"}} {
552 upvar 1 $listName list
553
554 if {[createFile $file $verbose $desc]} then {
555 if {[catch {set fd [open $file r]} result]} then {
556 if {$verbose >= 0} then {
557 wpLog o * "Error: unable to open ${desc}`$file' for reading: $result"
558 }
559 } else {
560 if {[info exists list]} then {
561 unset list
562 }
563 while {![eof $fd]} {
564 set line [replaceExpr [gets $fd] "^ "]
565 if {([string compare "" $line]) && \
566 (![regexp -- $ignore $line])} then {
567 append list $line " "
568 }
569 }
570 close $fd
571 return 1
572 }
573 }
574 return 0
575 }
576
577 ##
578 ## Save data from an array info a file
579 ##
580 ## Args: array, filename, verbose {-1,0,1}, description, access flag
581 ## Returns: 1 is successful,
582 ## 0 otherwise
583 ##
584 proc arraySave {arrayName file {verbose 0} {desc "file "} {access w}} {
585 upvar 1 $arrayName array
586
587 if {[createFile $file $verbose $desc]} then {
588 if {[catch {set fd [open $file $access]} result]} then {
589 if {$verbose >= 0} then {
590 wpLog o * "Error: unable to open ${desc}`$file' for writing: $result"
591 }
592 } else {
593 if {[array exists array]} then {
594 foreach name [lsort [array names array]] {
595 puts $fd "[list $name] [list $array($name)]"
596 }
597 close $fd
598 return 1
599 } else {
600 close $fd
601 }
602 }
603 }
604 return 0
605 }
606
607 ##
608 ## Load data into an array from a file
609 ##
610 ## Args: array, filename, verbose {-1,0,1}, description, ignore regsub
611 ## Returns: 1 if successful,
612 ## 0 otherwise
613 ##
614 proc arrayLoad {arrayName file {verbose 0} {desc "file "} {ignore "^#"}} {
615 upvar 1 $arrayName array
616
617 if {[createFile $file $verbose $desc]} then {
618 if {[catch {set fd [open $file r]} result]} then {
619 if {$verbose >= 0} then {
620 wpLog o * "Error: unable to open ${desc}`$file' for reading: $result"
621 }
622 } else {
623 if {[info exists array]} then {
624 unset array
625 }
626 while {![eof $fd]} {
627 set line [replaceExpr [gets $fd] "^ "]
628 if {([string compare "" $line]) && \
629 (![regexp -- $ignore $line])} then {
630 set array([lindex $line 0]) [lindex $line 1]
631 }
632 }
633 close $fd
634 return 1
635 }
636 }
637 return 0
638 }
639
640 ##
641 ## Set all elements in the given array the the given value
642 ##
643 ## Args: array name, value
644 ## Returns: 1 if the array exists
645 ## 0 otherwise
646 ##
647 proc arraySetAll {arrayName {value ""}} {
648 upvar 1 $arrayName array
649
650 if {[array exists array]} then {
651 foreach name [array names array] {
652 set array($name) $value
653 }
654 return 1
655 }
656 return 0
657 }
658
659 ##
660 ## Find the given element in an array
661 ##
662 ## Args: array name, element name
663 ## Returns: case sensitive element name if found,
664 ## nothing otherwise
665 ##
666 proc arrayFindElementName {array element} {
667 set list [lsort [array names $array]]
668 set index [lsearch -exact [string tolower $list] [string tolower $name]]
669 if {$index != -1} then {
670 return [lindex $list $index]
671 }
672 return
673 }
674
675 ##
676 ## Return length of longest data in an array at index
677 ##
678 ## Args: array name
679 ## Returns: length of longest name in an array
680 ##
681 proc arrayMaxElementDataLength {arrayName index} {
682 upvar 1 $arrayName array
683
684 set maxlength 0
685 foreach {name data} [array get array] {
686 set length [string length [lindex $data $index]]
687 if {$length > $maxlength} then {
688 set maxlength $length
689 }
690 }
691 return $maxlength
692 }
693
694 ##
695 ## Return length of the longest element in a list
696 ##
697 ## Args: list
698 ## Returns: length of longest element in the given list
699 ##
700 proc listMaxElementLength {list} {
701 set maxlength 0
702 foreach data $list {
703 set length [string length $data]
704 if {$length > $maxlength} then {
705 set maxlength $length
706 }
707 }
708 return $maxlength
709 }
710
711 ##
712 ## Split up a list into multiple elements
713 ##
714 ## Args: text, max list length, split char, trim chars
715 ## Returns: split list
716 ##
717 # FIXME: improve this
718 proc splitList {text {splitLength 75} {splitChar " "} {trimChars " "}} {
719 # Sanity check splitLength and splitChar
720 if {($splitLength >= 1) && ([string compare "" $splitChar])} then {
721 set elementSplitLength [expr $splitLength - 1]
722 set stringLength [string length $text] ;# Total length of string
723 set subStringLength 0 ;# Text left over
724 set elementLength 0 ;# Element length counter
725 set elementStartIndex 0 ;# Start of split element
726 set elementEndIndex 0 ;# End of split element
727 for {
728 set stringIndex 0
729 } {$stringIndex < $stringLength} {
730 incr stringIndex
731 } {
732 # If element length greater than/equal to split length,
733 # Or element length equal to split length - 1,
734 # And character at current string index is splitChar
735 if {(($elementLength >= $splitLength) ||
736 ($elementLength == $elementSplitLength)) &&
737 (![string compare $splitChar \
738 [string index $text $stringIndex]])} then {
739 # Split substring element from text
740 set string [string range $text $elementStartIndex $elementEndIndex]
741 # Append substring element list to list
742 lappend list [string trim $string $trimChars]
743 # Reset element length counter
744 set elementLength 0
745 # Start split of next element at the end + 1 of the current one
746 set elementStartIndex [expr $elementEndIndex + 1]
747 # Reset end of next element to the start of the next element
748 set elementEndIndex $elementStartIndex
749 # Track remaining text length
750 set subStringLength [expr $subStringLength + [string length $string]]
751 } else {
752 # Increment element length
753 incr elementLength
754 # Increment end of next element
755 incr elementEndIndex
756 }
757 }
758 # Append any left over text as a new element
759 if {$stringLength > $subStringLength} then {
760 lappend list [string trim [string range $text $subStringLength end] $trimChars]
761 }
762 # Whew...that was alot of work!
763 if {[info exists list]} then {
764 return $list
765 }
766 }
767 return
768 }
769
770 ##
771 ## Search an array for a given word or regexp
772 ##
773 ## Args: array name, word/regexp
774 ## Returns: list of indexes that match the given word/regexp
775 ##
776 proc arraySearch {array word} {
777 set word [string tolower $word]
778 set ret ""
779 foreach {name data} [array get $array] {
780 set string [string tolower $data]
781 if {[lsearch -regexp $string $word] != -1} then {
782 for {
783 set index 0
784 set indexes ""
785 } {
786 if {[regexp -- .*$word $string]} then {
787 lappend indexes $index
788 }
789 } {
790 incr index
791 }
792 lappend ret [list [concat $name $indexes]]
793 }
794 }
795 return $ret
796 }
797
798 ##
799 ## Return current command character
800 ##
801 ## Args: type {pub,msg,dcc,fil}
802 ## Returns: command character for the type given
803 ##
804 proc commandChar {type} {
805 global botnick
806 variable configData
807
808 switch -exact -- $type {
809 pub {
810 return $configData(commandchar)
811 }
812 msg {
813 return "/msg $botnick "
814 }
815 dcc {
816 return
817 }
818 fil {
819 return
820 }
821 default {
822 error "Unknown command character type: $type"
823 }
824 }
825 return
826 }
827
828 ##
829 ## Find option default for the given option name in a data list
830 ##
831 ## Args: data list, option name
832 ## Returns: option default if found,
833 ## nothing otherwise
834 ##
835 proc dataFormatDefault {list option} {
836 foreach i $list {
837 if {![string compare $option [lindex $i 0]]} then {
838 return [lindex $i 1]
839 }
840 }
841 return
842 }
843
844 ##
845 ## Find option value for the given option name in a data list
846 ##
847 ## Args: data format, data list, option name
848 ## Returns: option value if found,
849 ## nothing otherwise
850 ##
851 proc dataFormatValue {format data option} {
852 if {[set index [lsearch -exact $format $option]] != -1} then {
853 return [lindex $data $index]
854 }
855 return
856 }
857
858 ##
859 ## Replace option data in the given data list with a new value
860 ##
861 ## Args: data format, data list, option name, new value
862 ## Returns: data list
863 ##
864 proc dataFormatReplace {format data option value} {
865 if {[set index [lsearch -exact $format $option]] != -1} then {
866 return [lreplace $data $index $index $value]
867 }
868 return $data
869 }
870
871 ##
872 ## Create a data format list for a given data format and options
873 ##
874 ## Args: data format list, defaults, options {{option1 value} ...}
875 ## Returns: data format list with options and values in proper order
876 ##
877 proc dataFormatBuild {format defaults args} {
878 set ret ""
879 foreach arg $args {
880 set [lindex $arg 0] [lindex $arg 1]
881 }
882 foreach opt $format {
883 if {[info exists $opt]} then {
884 lappend ret [set $opt]
885 } else {
886 lappend ret [dataFormatDefault $defaults $opt]
887 }
888 }
889 return $ret
890 }
891
892 ##
893 ## Convert a data list from one format to another
894 ##
895 ## Args: from format, to format, data list
896 ## Returns: data list
897 ##
898 proc dataFormatConvert {fromFormat toFormat data} {
899 set ret ""
900 set index 0
901 foreach opt $fromFormat {
902 set $opt [lindex $data $index]
903 incr index
904 }
905 foreach opt $toFormat {
906 if {[info exists $opt]} then {
907 lappend ret [set $opt]
908 } else {
909 lappend ret [dataFormatDefault $defaults $opt]
910 }
911 }
912 return $ret
913 }
914
915 ##
916 ## Scan the given file for module options
917 ##
918 ## Args: file, args {only scan for these options}
919 ## Returns: list of module options if the given file is a module,
920 ## nothing otherwise
921 ## Errors: unable to open file for reading
922 ##
923 proc scanModule {file args} {
924 variable moduleDatabaseConfig
925 variable moduleDatabaseFormat
926
927 if {[catch {set fd [open $file r]} result]} then {
928 error $result
929 } else {
930 set ret ""
931 if {![string compare "" $args]} then {
932 set baseOptions [lindex $moduleDatabaseFormat([set moduleDatabaseConfig(version)]) 0]
933 set extraOptions [lindex $moduleDatabaseFormat([set moduleDatabaseConfig(version)]) 1]
934 set scanOptions "name $baseOptions"
935 set formatOptions "name $baseOptions $extraOptions"
936 } else {
937 set scanOptions $args
938 set formatOptions $args
939 }
940 for {
941 set lineCount 0
942 set optionCount 0
943 set continuedLine 0
944 } {(![eof $fd]) && ($lineCount <= $moduleDatabaseConfig(scanlines))} {
945 incr lineCount
946 } {
947 gets $fd line
948 if {[regexp -- "^# .*:.*" $line]} then {
949 set opt [string trimright [lindex $line 1] :]
950 if {[lsearch -glob $scanOptions $opt] != -1} then {
951 set data [string trimright [string trimleft [string range $line [string first : $line] end] " \t:"] " \t\\"]
952 if {![info exists $opt]} then {
953 set $opt $data
954 } else {
955 append $opt " $data"
956 }
957 }
958 if {[regexp -- \\\\$ $line]} then {
959 set continuedLine 1
960 } else {
961 set continuedLine 0
962 }
963 } elseif {($continuedLine) && ([info exists opt])} then {
964 append $opt " [string trimright [string trimleft $line " \t#"] " \t\\"]"
965 if {![regexp -- \\\\$ $line]} then {
966 set continuedLine 0
967 }
968 }
969 }
970 close $fd
971 if {(![string compare "" $args]) && \
972 ((![info exists name]) || \
973 ([catch {set md5sum [md5Sum $file]}]))} then {
974 return
975 }
976 foreach option $formatOptions {
977 if {(![info exists $option]) || \
978 (![string compare "" [set $option]])} then {
979 set $option [dataFormatDefault $moduleDatabaseConfig(defaults) $option]
980 }
981 lappend ret [set $option]
982 }
983 return $ret
984 }
985 }
986
987 ##
988 ## Get data from module db data array
989 ##
990 ## Args: module name, data type
991 ## Returns: data for the given module's data type if it exists,
992 ## nothing otherwise
993 ##
994 proc getModuleDatabaseData {module type} {
995 variable moduleDatabaseConfig
996 variable moduleDatabaseFormat
997 variable moduleDatabaseData
998
999 if {[moduleExists $module]} then {
1000 set index [lsearch -exact [join $moduleDatabaseFormat([set moduleDatabaseConfig(version)])] $type]
1001 if {$index != -1} then {
1002 return [lindex $moduleDatabaseData($module) $index]
1003 }
1004 }
1005 return
1006 }
1007
1008 ##
1009 ## Set data in module db data array
1010 ##
1011 ## Args: module name, data type, data
1012 ## Returns: 1 if valid module and data type,
1013 ## 0 otherwise
1014 ##
1015 proc setModuleDatabaseData {module type data} {
1016 variable moduleDatabaseConfig
1017 variable moduleDatabaseFormat
1018 variable moduleDatabaseData
1019 variable moduleDatabaseDataChanged
1020
1021 if {[moduleExists $module]} then {
1022 set index [lsearch -exact [join $moduleDatabaseFormat([set moduleDatabaseConfig(version)])] $type]
1023 if {$index != -1} then {
1024 set moduleDatabaseData($module) [lreplace $moduleDatabaseData($module) $index $index [list $data]]
1025 set moduleDatabaseDataChanged 1
1026 return 1
1027 }
1028 }
1029 return 0
1030 }
1031
1032 ##
1033 ## Save module database
1034 ##
1035 ## Args: verbose {-1,0,1}
1036 ## Returns: 1 if successful,
1037 ## 0 otherwise
1038 ##
1039 proc saveModuleDatabase {{verbose 0}} {
1040 variable configData
1041 variable moduleDatabaseConfig
1042 variable moduleDatabaseData
1043 variable moduleDatabaseDataChanged
1044
1045 if {[createFile $configData(moddbfile) $verbose "module database file "]} then {
1046 if {[catch {set fd [open $configData(moddbfile) w]} result]} then {
1047 if {$verbose >= 0} then {
1048 wpLog o * "Error: unable to open module database file `$configData(moddbfile)' for writing: $result"
1049 }
1050 } else {
1051 puts $fd "# $moduleDatabaseConfig(header)$moduleDatabaseConfig(version)"
1052 close $fd
1053 set ret [arraySave moduleDatabaseData $configData(moddbfile) $verbose "module database file " a]
1054 if {$ret} then {
1055 set moduleDatabaseDataChanged 0
1056 }
1057 return $ret
1058 }
1059 }
1060 return 0
1061 }
1062
1063 ##
1064 ## Load module database
1065 ##
1066 ## Args: verbose {-1,0,1}
1067 ## Returns: 1 if successful,
1068 ## 0 otherwise
1069 ##
1070 proc loadModuleDatabase {{verbose 0}} {
1071 variable configData
1072 variable moduleDatabaseConfig
1073 variable moduleDatabaseFormat
1074 variable moduleDatabaseData
1075 variable moduleDatabaseDataChanged
1076
1077 if {![file exists $configData(moddbfile)]} then {
1078 return -1
1079 } else {
1080 if {[catch {set fd [open $configData(moddbfile) r]} result]} then {
1081 if {$verbose >= 0} then {
1082 wpLog o * "Error: unable to open module database file `$configData(moddbfile)' for reading: $result"
1083 }
1084 } else {
1085 set firstline [replaceExpr [gets $fd] "^ "]
1086 if {[regexp -- "^# $moduleDatabaseConfig(header)" $firstline]} then {
1087 regsub -all -- "^# $moduleDatabaseConfig(header)" $firstline "" version
1088 if {![string compare [set version [string trim $version]] $moduleDatabaseConfig(version)]} then {
1089 close $fd
1090 return [arrayLoad moduleDatabaseData $configData(moddbfile) $verbose "module database file "]
1091 } elseif {[info exists moduleDatabaseFormat($version)]} then {
1092 if {[info exists moduleDatabaseData]} then {
1093 unset moduleDatabaseData
1094 }
1095 while {![eof $fd]} {
1096 set line [replaceExpr [gets $fd] "^ "]
1097 if {([string compare "" $line]) && \
1098 (![regexp -- "^#" $line])} then {
1099 set moduleDatabaseData([lindex $line 0]) [dataFormatConvert [join $moduleDatabaseFormat($version)] [join $moduleDatabaseFormat([set moduleDatabaseConfig(version)])] [lindex $line 1]]
1100 }
1101 }
1102 close $fd
1103 set moduleDatabaseDataChanged 0
1104 return 1
1105 } else {
1106 wpLog o * "Error: unknown module database version: $version"
1107 }
1108 } else {
1109 wpLog o * "Error: unknown module database format: [string trimleft $firstline " \t#"]"
1110 }
1111 }
1112 close $fd
1113 }
1114 return 0
1115 }
1116
1117 ##
1118 ## Compare loaded module database data to module files
1119 ##
1120 ## Args: verbose {-1,0,1}
1121 ## Returns: 1 if data matches,
1122 ## 0 otherwise
1123 ##
1124 proc compareModuleDatabase {{verbose 0}} {
1125 variable configData
1126 variable moduleDatabaseConfig
1127
1128 foreach path $configData(modulepath) {
1129 set files [findFiles $path $moduleDatabaseConfig(maxdepth) .tcl]
1130 if {[string compare "" $files]} then {
1131 set comparedModules ""
1132 set moduleList [listModules]
1133 # Return early if we don't have any modules listed in the db
1134 if {![string compare "" $moduleList]} then {
1135 return 0
1136 }
1137 foreach file $files {
1138 set shortfile [string trimleft [string range $file [string length $path] end] /]
1139 if {$verbose >= 1} then {
1140 wpLog o * "Comparing file `$shortfile'"
1141 }
1142 if {[catch {set name [lindex [scanModule $file name] 0]} result]} then {
1143 if {$verbose >= 0} then {
1144 wpLog o * "Error: unable to open file `$shortfile' for reading: $result"
1145 }
1146 }
1147 # Process this file if it's a module
1148 if {[string compare "" $name]} then {
1149 # Get module filename from db
1150 set filename [getModuleDatabaseData $name file]
1151 # Compare module filename and make sure it wasn't renamed or moved
1152 if {[string compare $file $filename]} then {
1153 return 0
1154 }
1155 # Compare md5 from module db and sure the module hasn't changed
1156 if {([catch {set md5sum [md5Sum $filename]}]) || \
1157 ([string compare [getModuleDatabaseData $name md5sum] $md5sum])} then {
1158 return 0
1159 }
1160 # Append module name to list of compared modules
1161 lappend comparedModules $name
1162 }
1163 }
1164 # Compare list of compared modules with list of modules from the db
1165 if {[string compare [lsort $comparedModules] $moduleList]} then {
1166 return 0
1167 }
1168 }
1169 }
1170 return 1
1171 }
1172
1173 ##
1174 ## Rebuild module database
1175 ##
1176 ## Args: verbose {-1,0,1}
1177 ## Returns: nothing
1178 ##
1179 proc rebuildModuleDatabase {{verbose 0}} {
1180 variable configData
1181 variable moduleDatabaseConfig
1182 variable moduleDatabaseFormat
1183 variable moduleDatabaseData
1184
1185 foreach path $configData(modulepath) {
1186 set files [findFiles $path $moduleDatabaseConfig(maxdepth) .tcl]
1187 set loadIndex [lsearch -exact [join $moduleDatabaseFormat([set moduleDatabaseConfig(version)])] load]
1188 foreach file $files {
1189 set shortfile [string trimleft [string range $file [string length $path] end] /]
1190 if {$verbose >= 1} then {
1191 wpLog o * "Scanning file `$shortfile'"
1192 }
1193 if {[catch {set data [scanModule $file]} result]} then {
1194 if {$verbose >= 0} then {
1195 wpLog o * "Warning: unable to open file `$shortfile' for reading: $result"
1196 }
1197 } else {
1198 set name [lindex $data 0]
1199 if {[string compare "" $name]} then {
1200 if {[moduleExists $name]} then {
1201 set tmp($name) [lreplace [lrange $data 1 end] $loadIndex $loadIndex [lindex $moduleDatabaseData($name) $loadIndex]]
1202 } else {
1203 set tmp($name) [lrange $data 1 end]
1204 }
1205 }
1206 }
1207 }
1208 }
1209 if {[info exists moduleDatabaseData]} then {
1210 unset moduleDatabaseData
1211 }
1212 if {[info exists tmp]} then {
1213 array set moduleDatabaseData [array get tmp]
1214 }
1215 return
1216 }
1217
1218 ##
1219 ## List all modules in the database
1220 ##
1221 ## Args: none
1222 ## Returns: list of modules in module database
1223 ##
1224 proc listModules {{loaded 0}} {
1225 variable moduleDatabaseData
1226 variable moduleLoadedList
1227
1228 if {$loaded} then {
1229 return [lsort $moduleLoadedList]
1230 }
1231 return [lsort [array names moduleDatabaseData]]
1232 }
1233
1234 ##
1235 ## Check if the given module exists
1236 ##
1237 ## Args: module name
1238 ## Returns: 1 if the given module exists
1239 ## 0 otherwise
1240 ##
1241 proc moduleExists {module} {
1242 variable moduleDatabaseData
1243
1244 if {[info exists moduleDatabaseData($module)]} then {
1245 return 1
1246 }
1247 return 0
1248 }
1249
1250 ##
1251 ## Check if a module is loaded
1252 ##
1253 ## Args: module name
1254 ## Returns: 1 if the given module is loaded
1255 ## 0 otherwise
1256 ##
1257 proc moduleLoaded {module} {
1258 variable moduleLoadedList
1259
1260 if {[lsearch -exact $moduleLoadedList $module] != -1} then {
1261 return 1
1262 }
1263 return 0
1264 }
1265
1266 ##
1267 ## Add/remove bindings for a given module
1268 ##
1269 ## Args: mode {bind|unbind}, module,
1270 ## args {{type ...} {option ...} {regsub ... ...}}
1271 ## Returns: nothing
1272 ##
1273 proc moduleBindUnbind {mode module args} {
1274 variable NamespaceCurrent
1275
1276 if {[info exists ${NamespaceCurrent}::${module}::bindDefaults]} then {
1277 set bindtypes ""
1278 set bindoptions ""
1279 foreach option $args {
1280 switch -exact -- [lindex $option 0] {
1281 type {
1282 # dcc msg pub ...
1283 foreach opt [lrange $option 1 end] {
1284 lappend bindtypes $opt
1285 }
1286 }
1287 option {
1288 # commandchar noauto ...
1289 foreach opt [lrange $option 1 end] {
1290 lappend bindoptions $opt
1291 }
1292 }
1293 regsub {
1294 # replace regexp 'replacewhat' with 'replacewith'
1295 foreach opt [lrange $option 1 end] {
1296 foreach {rwhat rwith} $opt {break}
1297 set replace($rwhat) $rwith
1298 }
1299 }
1300 }
1301 }
1302 foreach {proc data} [array get ${NamespaceCurrent}::${module}::bindDefaults] {
1303 foreach bind $data {
1304 foreach {type flags mask options help} $bind {break}
1305 if {([string compare "" $bindtypes]) && \
1306 ([lsearch -exact $bindtypes $type] == -1)} then {
1307 continue
1308 }
1309 if {[string compare "" $bindoptions]} then {
1310 set found 0
1311 foreach opt $options {
1312 if {[lsearch -exact $bindoptions $opt] != -1} then {
1313 set found 1
1314 break
1315 }
1316 }
1317 if {!$found} then {
1318 continue
1319 }
1320 }
1321 if {[lsearch -exact $options commandchar] != -1} then {
1322 set mask "[commandChar pub]$mask"
1323 }
1324 if {[array exists replace]} then {
1325 foreach {replacewhat replacewith} [array get replace] {
1326 regsub -all -- $replacewhat $mask $replacewith mask
1327 }
1328 }
1329 # bind/unbind
1330 $mode $type $flags $mask ${NamespaceCurrent}::${module}::$proc
1331 }
1332 }
1333 }
1334 return
1335 }
1336
1337 ##
1338 ## Load a module
1339 ##
1340 ## Args: module name, verbose {-1,0,1}, args {loop detection}
1341 ## Returns: nothing
1342 ## Errors: if unable to load module
1343 ##
1344 proc moduleLoad {module {verbose 0} args} {
1345 variable NamespaceCurrent
1346 variable moduleLoadedList
1347
1348 if {[lsearch -exact [set loop [lindex $args 0]] $module] == -1} then {
1349 if {[moduleExists $module]} then {
1350 set preload ""
1351 set requires [getModuleDatabaseData $module requires]
1352 foreach required $requires {
1353 set preloadModule [whichModuleCommand $required]
1354 if {[string compare "" $preloadModule]} then {
1355 if {([lsearch -exact $preload $preloadModule] == -1) && \
1356 ([string compare wp $preloadModule]) && \
1357 (![moduleLoaded $preloadModule])} then {
1358 lappend preload $preloadModule
1359 }
1360 } else {
1361 error "required command `$required' not found."
1362 }
1363 }
1364 if {[string compare "" $preload]} then {
1365 foreach premod $preload {
1366 if {[catch {moduleLoad $premod $verbose [concat $loop $module]} result]} then {
1367 error $result
1368 }
1369 }
1370 }
1371 if {[catch {source [getModuleDatabaseData $module file]} result]} then {
1372 error $result
1373 } else {
1374 package forget $module
1375 package provide ${NamespaceCurrent}::${module} [getModuleDatabaseData $module version]
1376 moduleConfigLoad $module 1
1377 moduleConfigCheckdefs $module 1
1378 moduleDataLoad $module 1
1379 # Imported commands '# requires: ...'
1380 if {[string compare "" $requires]} then {
1381 set Eval "namespace eval ${NamespaceCurrent}::${module} \{\n namespace forget *\n namespace import"
1382 foreach required $requires {
1383 if {[string compare "" [set command [whichCommand $required]]]} then {
1384 append Eval " $command"
1385 }
1386 }
1387 append Eval "\n\}"
1388 eval $Eval
1389 }
1390 # Exported commands '# provides: ...'
1391 if {[string compare "" [set provides [getModuleDatabaseData $module provides]]]} then {
1392 set Eval "namespace eval ${NamespaceCurrent}::${module} \{\n namespace export"
1393 foreach provided $provides {
1394 append Eval " $provided"
1395 }
1396 append Eval "\n\}"
1397 eval $Eval
1398 }
1399 if {[string compare "" [info commands ${NamespaceCurrent}::${module}::ModuleInit]]} then {
1400 ${NamespaceCurrent}::${module}::ModuleInit
1401 }
1402 # FIXME: check for bindings? duplicates?
1403 moduleBindUnbind bind $module
1404 if {![getModuleDatabaseData $module load]} then {
1405 setModuleDatabaseData $module load 1
1406 }
1407 if {[lsearch -exact $moduleLoadedList $module] == -1} then {
1408 lappend moduleLoadedList $module
1409 if {$verbose >= 1} then {
1410 wpLog o * "Module loaded: $module"
1411 }
1412 }
1413 }
1414 } else {
1415 error "No such module: $module"
1416 }
1417 } else {
1418 regsub -all -- " " $loop " -> " loop
1419 error "Preload endless loop: $loop -> $module"
1420 }
1421 return
1422 }
1423
1424 ##
1425 ## Unload a module
1426 ##
1427 ## Args: module name, verbose {-1,0,1}
1428 ## Returns: nothing
1429 ## Errors: if unable to completely unload module
1430 ##
1431 proc moduleUnload {module {verbose 0}} {
1432 variable NamespaceCurrent
1433 variable moduleLoadedList
1434
1435 # FIXME: handle dependant modules and modules that can't be unloaded
1436 if {[moduleExists $module]} then {
1437 if {[string compare "" [info commands ${NamespaceCurrent}::${module}::ModuleDestroy]]} then {
1438 ${NamespaceCurrent}::${module}::ModuleDestroy
1439 }
1440 # FIXME: check for bindings?
1441 moduleBindUnbind unbind $module
1442 moduleConfigSave $module 1
1443 moduleDataSave $module 1
1444 if {[catch {namespace delete ${NamespaceCurrent}::${module}} result]} then {
1445 error $result
1446 } else {
1447 package forget ${NamespaceCurrent}::${module}
1448 if {[getModuleDatabaseData $module load] == 1} then {
1449 setModuleDatabaseData $module load 0
1450 }
1451 set index [lsearch -exact $moduleLoadedList $module]
1452 if {$index != -1} then {
1453 set moduleLoadedList [lreplace $moduleLoadedList $index $index]
1454 if {$verbose >= 1} then {
1455 wpLog o * "Module unloaded: $module"
1456 }
1457 }
1458 }
1459 } else {
1460 error "No such module: $module"
1461 }
1462 return
1463 }
1464
1465 ##
1466 ## Save configuration settings for the given module
1467 ##
1468 ## Args: module, force {0,1}, verbose {-1,0,1}
1469 ## Returns: 1 if settings saved
1470 ## 0 otherwise
1471 ##
1472 proc moduleConfigSave {module {force 0} {verbose 0}} {
1473 variable NamespaceCurrent
1474 variable configData
1475
1476 if {([string compare "" \
1477 [set file [getModuleDatabaseData $module config]]]) && \
1478 ([createDir $configData(configpath)])} then {
1479 set cfgfile [file join $configData(configpath) $file]
1480 if {([getModuleDatabaseData $module load]) && \
1481 (($force) || \
1482 (![info exists ${NamespaceCurrent}::${module}::configDataChanged]) || \
1483 ([set ${NamespaceCurrent}::${module}::configDataChanged]))} then {
1484 if {[arraySave ${NamespaceCurrent}::${module}::configData $cfgfile 0 "$module configuration file "]} then {
1485 if {$verbose >= 1} then {
1486 wpLog o * "Writing $module config file..."
1487 }
1488 set ${NamespaceCurrent}::${module}::configDataChanged 0
1489 return 1
1490 } elseif {$verbose >= 0} then {
1491 wpLog o * "Error writing $module config file."
1492 }
1493 }
1494 }
1495 return 0
1496 }
1497
1498 ##
1499 ## Load configuration settings for the given module
1500 ##
1501 ## Args: module, force {0,1}, verbose {-1,0,1}
1502 ## Returns: 1 if settings loaded
1503 ## 0 otherwise
1504 ##
1505 proc moduleConfigLoad {module {force 0} {verbose 0}} {
1506 variable NamespaceCurrent
1507 variable configData
1508
1509 if {([string compare "" \
1510 [set file [getModuleDatabaseData $module config]]]) && \
1511 ([createDir $configData(configpath)])} then {
1512 set cfgfile [file join $configData(configpath) $file]
1513 if {($force) || \
1514 (![info exists ${NamespaceCurrent}::${module}::configDataChanged]) || \
1515 (![set ${NamespaceCurrent}::${module}::configDataChanged])} then {
1516 if {[arrayLoad ${NamespaceCurrent}::${module}::configData $cfgfile 0 "$module configuration file "]} then {
1517 if {$verbose >= 1} then {
1518 wpLog o * "Loading $module config file..."
1519 }
1520 set ${NamespaceCurrent}::${module}::configDataChanged 0
1521 return 1
1522 } elseif {$verbose >= 0} then {
1523 wpLog o * "Error loading $module config file."
1524 }
1525 }
1526 }
1527 return 0
1528 }
1529
1530 ##
1531 ## Check and set default configuration settings for the given module
1532 ##
1533 ## Args: module, force {0,1}, verbose {-1,0,1}
1534 ## Returns: nothing
1535 ##
1536 proc moduleConfigCheckdefs {module {force 0} {verbose 0}} {
1537 variable NamespaceCurrent
1538
1539 if {([array exists ${NamespaceCurrent}::${module}::configDefaults]) && \
1540 ([string compare "" [getModuleDatabaseData $module config]])} then {
1541 set Changed 0
1542 # Unset unknown variables
1543 foreach name [array names ${NamespaceCurrent}::${module}::configData] {
1544 if {![info exists ${NamespaceCurrent}::${module}::configDefaults($name)]} then {
1545 unset ${NamespaceCurrent}::${module}::configData($name)
1546 set Changed 1
1547 }
1548 }
1549 # Set missing variables to defaults
1550 foreach {name data} [array get ${NamespaceCurrent}::${module}::configDefaults] {
1551 if {![info exists ${NamespaceCurrent}::${module}::configData($name)]} then {
1552 set ${NamespaceCurrent}::${module}::configData($name) [lindex $data 1]
1553 set Changed 1
1554 }
1555 }
1556 # FIXME: do this with a trace?
1557 if {$Changed} then {
1558 set ${NamespaceCurrent}::${module}::configDataChanged 1
1559 }
1560 }
1561 return
1562 }
1563
1564 ##
1565 ## Handle config data for a list of modules
1566 ##
1567 ## Args: action {load|save|checkdefs}, module list, force {0,1},
1568 ## verbose {-1,0,1}
1569 ## Returns: nothing
1570 ##
1571 proc moduleConfig {action modules {force 0} {verbose 0}} {
1572 if {![string compare * $modules]} then {
1573 set modules [listModules 1]
1574 }
1575 switch -exact -- $action {
1576 save {
1577 foreach module $modules {
1578 if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1579 moduleConfigSave $module $force $verbose
1580 }
1581 }
1582 }
1583 load {
1584 foreach module $modules {
1585 if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1586 moduleConfigLoad $module $force $verbose
1587 }
1588 }
1589 }
1590 checkdefs {
1591 foreach module $modules {
1592 if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1593 moduleConfigCheckdefs $module $force $verbose
1594 }
1595 }
1596 }
1597 }
1598 return
1599 }
1600
1601 ##
1602 ## Save data for the given module
1603 ##
1604 ## Args: module, force {0,1}, verbose {-1,0,1}
1605 ## Returns: nothing
1606 ##
1607 proc moduleDataSave {module {force 0} {verbose 0}} {
1608 variable NamespaceCurrent
1609 variable configData
1610
1611 if {([info exists ${NamespaceCurrent}::${module}::dataOptions]) && \
1612 ([createDir $configData(datapath)])} then {
1613 foreach {name data} [array get ${NamespaceCurrent}::${module}::dataOptions] {
1614 foreach {type file desc} $data {break}
1615 if {([info exists type]) && ([info exists file]) && \
1616 ([info exists desc])} then {
1617 set Changed ${NamespaceCurrent}::${module}::${name}Changed
1618 if {($force) || ((![info exists $Changed]) || ([set $Changed]))} then {
1619 if {[${type}Save ${NamespaceCurrent}::${module}::$name [file join $configData(datapath) $file]]} then {
1620 if {$verbose >= 1} then {
1621 wpLog o * $NamespaceCurrent "Writing $desc data file..."
1622 }
1623 set $Changed 0
1624 } elseif {$verbose >= 0} then {
1625 wpLog o * $NamespaceCurrent "Error writing $desc data file!"
1626 }
1627 }
1628 }
1629 }
1630 }
1631 return
1632 }
1633
1634 ##
1635 ## Load data for the given module
1636 ##
1637 ## Args: module, force {0,1}, verbose {-1,0,1}
1638 ## Returns: nothing
1639 ##
1640 proc moduleDataLoad {module {force 0} {verbose 0}} {
1641 variable NamespaceCurrent
1642 variable configData
1643
1644 if {([info exists ${NamespaceCurrent}::${module}::dataOptions]) && \
1645 ([createDir $configData(datapath)])} then {
1646 foreach {name data} [array get ${NamespaceCurrent}::${module}::dataOptions] {
1647 foreach {type file desc} $data {break}
1648 if {([info exists type]) && ([info exists file]) && \
1649 ([info exists desc])} then {
1650 set Changed ${NamespaceCurrent}::${module}::${name}Changed
1651 if {($force) || ((![info exists $Changed]) || ([set $Changed]))} then {
1652 if {[${type}Load ${NamespaceCurrent}::${module}::$name [file join $configData(datapath) $file]]} then {
1653 if {$verbose >= 1} then {
1654 wpLog o * $NamespaceCurrent "Reloading $desc data file..."
1655 }
1656 set $Changed 0
1657 } elseif {$verbose >= 0} then {
1658 wpLog o * $NamespaceCurrent "Error reloading $desc data file!"
1659 }
1660 }
1661 }
1662 }
1663 }
1664 return
1665 }
1666
1667 ##
1668 ## Backup data for the given module
1669 ##
1670 ## Args: module, force {0,1}, verbose {-1,0,1}
1671 ## Returns: nothing
1672 ##
1673 proc moduleDataBackup {module {force 0} {verbose 0}} {
1674 variable NamespaceCurrent
1675 variable configData
1676
1677 if {([info exists ${NamespaceCurrent}::${module}::dataOptions]) && \
1678 ([createDir $configData(datapath)])} then {
1679 foreach {name data} [array get ${NamespaceCurrent}::${module}::dataOptions] {
1680 foreach {type file desc} $data {break}
1681 if {([info exists type]) && ([info exists file]) && \
1682 ([info exists desc])} then {
1683 if {[set result [backupFile [file join $configData(datapath) $file] $verbose]]} then {
1684 if {($result >= 1) && ($verbose >= 1)} then {
1685 wpLog o * $NamespaceCurrent "Backing up $desc data file..."
1686 }
1687 } elseif {$verbose >= 0} then {
1688 wpLog o * $NamespaceCurrent "Error backing up $desc data file!"
1689 }
1690 }
1691 }
1692 }
1693 return
1694 }
1695
1696 ##
1697 ## Handle data for a list of modules
1698 ##
1699 ## Args: action {load|save|backup}, module list, force {0,1},
1700 ## verbose {-1,0,1}
1701 ## Returns: nothing
1702 ##
1703 proc moduleData {action modules {force 0} {verbose 0}} {
1704 if {![string compare * $modules]} then {
1705 set modules [listModules 1]
1706 }
1707 switch -exact -- $action {
1708 save {
1709 foreach module $modules {
1710 if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1711 moduleDataSave $module $force $verbose
1712 }
1713 }
1714 }
1715 load {
1716 foreach module $modules {
1717 if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1718 moduleDataLoad $module $force $verbose
1719 }
1720 }
1721 }
1722 backup {
1723 foreach module $modules {
1724 if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1725 moduleDataBackup $module $force $verbose
1726 }
1727 }
1728 }
1729 }
1730 return
1731 }
1732
1733 ##
1734 ## Builds command matching table from module database
1735 ##
1736 ## Args: none
1737 ## Returns: nothing
1738 ##
1739 proc buildCommandTable {{verbose 0}} {
1740 variable NamespaceCurrent
1741 variable ExportList
1742 variable commandTable
1743
1744 foreach command $ExportList {
1745 if {![info exists tmp($command)]} then {
1746 if {$verbose >= 2} then {
1747 wpLog o * "Adding command: `$command' as `${NamespaceCurrent}::$command'"
1748 }
1749 set tmp($command) ${NamespaceCurrent}::$command
1750 } elseif {$verbose >= 0} then {
1751 wpLog o * "Warning: ignoring duplicate command `$command', conflicts with `$tmp($command)'"
1752 }
1753 }
1754 foreach module [listModules] {
1755 if {[string compare "" [set provides [getModuleDatabaseData $module provides]]]} then {
1756 foreach command $provides {
1757 if {![info exists tmp($command)]} then {
1758 if {$verbose >= 2} then {
1759 wpLog o * "Adding command: `$command' as `${NamespaceCurrent}::${module}::$command'"
1760 }
1761 set tmp($command) ${NamespaceCurrent}::${module}::$command
1762 } elseif {$verbose >= 0} then {
1763 wpLog o * "Warning: ignoring duplicate command `$command', conflicts with `$tmp($command)'"
1764 }
1765 }
1766 }
1767 }
1768 if {[info exists commandTable]} then {
1769 unset commandTable
1770 }
1771 array set commandTable [array get tmp]
1772 return
1773 }
1774
1775 ##
1776 ## Return full namespace path for the given command
1777 ##
1778 ## Args: command
1779 ## Returns: full namespace path for the given command if it exists
1780 ## nothing otherwise
1781 ##
1782 proc whichCommand {command} {
1783 variable commandTable
1784
1785 if {[info exists commandTable($command)]} then {
1786 return $commandTable($command)
1787 }
1788 return
1789 }
1790
1791 ##
1792 ## Return full namespace path for the given module
1793 ##
1794 ## Args: module
1795 ## Returns: full namespace path for the given module if it's loaded
1796 ## nothing otherwise
1797 ##
1798 proc whichModule {module} {
1799 variable NamespaceCurrent
1800
1801 if {![string compare $module [namespace tail $NamespaceCurrent] $module]} then {
1802 return $NamespaceCurrent
1803 } elseif {[moduleLoaded $module]} then {
1804 return ${NamespaceCurrent}::$module
1805 }
1806 return
1807 }
1808
1809 ##
1810 ## Return module name that provides the given command
1811 ##
1812 ## Args: command
1813 ## Returns: name of module that provides the given command
1814 ## nothing otherwise
1815 ##
1816 proc whichModuleCommand {command} {
1817 variable NamespaceCurrent
1818 variable commandTable
1819
1820 if {[info exists commandTable($command)]} then {
1821 if {![string compare ${NamespaceCurrent}::$command \
1822 $commandTable($command)]} then {
1823 return [namespace tail $NamespaceCurrent]
1824 }
1825 return [namespace tail [namespace qualifiers $commandTable($command)]]
1826 }
1827 return
1828 }
1829
1830 ##
1831 ## Compare the given version to eggdrop's version
1832 ##
1833 ## Args: version
1834 ## Returns: 0 if eggdrop's version is older then the given version
1835 ## 1 if eggdrop's version matches the given version
1836 ## 2 if eggdrop's version is newer then the given version
1837 ##
1838 proc compareVersion {version} {
1839 global numversion
1840
1841 if {[string compare "" $version]} then {
1842 if {([info exists numversion]) || \
1843 (![catch {set numversion}]) || \
1844 ([info exists numversion])} then {
1845 if {[regexp -- \\. $version]} then {
1846 regsub -all -- \\. $version 0 version
1847 set version ${version}00
1848 }
1849 if {![regexp -- \[^0-9\] $version]} then {
1850 if {$numversion > $version} then {
1851 return 2
1852 } elseif {$numversion == $version} then {
1853 return 1
1854 }
1855 }
1856 }
1857 }
1858 return 0
1859 }
1860
1861 ##
1862 ## Log module information
1863 ##
1864 ## Args: level, channel, args ({<namespace>} {<text>} | {<text>})
1865 ## Returns: nothing
1866 ##
1867 proc wpLog {level channel args} {
1868 if {[llength $args] == 2} then {
1869 if {[string compare wp [set namespace [namespace tail [lindex $args 0]]]]} then {
1870 putloglev $level $channel "Wolfpack: \[$namespace\] [lindex $args 1]"
1871 } else {
1872 putloglev $level $channel "Wolfpack: [lindex $args 1]"
1873 }
1874 } else {
1875 putloglev $level $channel "Wolfpack: [join $args]"
1876 }
1877 return
1878 }
1879
1880 ##
1881 ## Trace configuration changes
1882 ##
1883 ## Args: name1, name2, operation
1884 ## Returns: nothing
1885 ##
1886 proc TraceConfigData {name1 name2 op} {
1887 variable configData
1888 variable oldConfigData
1889
1890 if {[string compare $oldConfigData($name2) $configData($name2)]} then {
1891 switch -exact -- $name2 {
1892 commandchar {
1893 foreach module [listModules 1] {
1894 moduleBindUnbind unbind $module "type pub" "option commandchar" "regsub [list ^[commandChar pub] $oldConfigData(commandchar)]"
1895 moduleBindUnbind bind $module "type pub" "option commandchar"
1896 }
1897 }
1898 }
1899 set oldConfigData($name2) $configData($name2)
1900 }
1901 return
1902 }
1903
1904 ##
1905 ## Evaluate command line arguments
1906 ##
1907 ## Args: none
1908 ## Returns: nothing
1909 ##
1910 proc EvalArgs {argc argv argv0} {
1911 variable NamespaceCurrent
1912 variable optionData
1913
1914 # Make sure defaults are sane
1915 arraySetAll optionData 0
1916 for {set index 0} {$index < $argc} {incr index} {
1917 set option [lindex $argv $index]
1918 switch -regexp -- $option {
1919 (^--config$) {
1920 set optionData(config) 1
1921 }
1922 (^--rebuild$) {
1923 set optionData(rebuild) 1
1924 }
1925 (^--norebuild$) {
1926 set optionData(norebuild) 1
1927 }
1928 (^--time$) {
1929 set optionData(time) 1
1930 }
1931 (^--verbose$) {
1932 incr optionData(verbose)
1933 }
1934 (^--quiet$) {
1935 incr optionData(quiet) -1
1936 }
1937 (^--debug$) {
1938 set optionData(debug) 1
1939 }
1940 (^--help$) {
1941 ShowUsage $argv0
1942 exit
1943 }
1944 (^--version$) {
1945 puts "[file tail $argv0] version [package require ${NamespaceCurrent}]"
1946 exit
1947 }
1948 (^-\[^-\]*$) {
1949 set suboptions [split $option ""]
1950 set sublength [llength [split $suboptions]]
1951 for {set subindex 0} {$subindex < $sublength} {incr subindex} {
1952 set suboption [lindex $suboptions $subindex]
1953 switch -exact -- $suboption {
1954 - {
1955 continue
1956 }
1957 c {
1958 set optionData(config) 1
1959 }
1960 r {
1961 set optionData(rebuild) 1
1962 }
1963 n {
1964 set optionData(norebuild) 1
1965 }
1966 t {
1967 set optionData(time) 1
1968 }
1969 v {
1970 incr optionData(verbose)
1971 }
1972 q {
1973 incr optionData(quiet) -1
1974 }
1975 d {
1976 set optionData(debug) 1
1977 }
1978 default {
1979 if {(![info exists invalidopt]) || \
1980 ($subindex == 1) || \
1981 ([lsearch -exact $invalidopt $option] == -1)} then {
1982 lappend invalidopt $option
1983 }
1984 }
1985 }
1986 }
1987 }
1988 default {
1989 lappend invalidopt $option
1990 }
1991 }
1992 }
1993
1994 # complain about invalid command line arguments
1995 if {[info exists invalidopt]} then {
1996 foreach option $invalidopt {
1997 puts stderr "[file tail $argv0]: unrecognized option `$option'"
1998 }
1999 exit 1
2000 }
2001 }
2002
2003 ##
2004 ## Show usage information
2005 ##
2006 ## Args: none
2007 ## Returns: nothing
2008 ##
2009 proc ShowUsage {argv0} {
2010 # FIXME: code missing options
2011 puts "Usage: [file tail $argv0] <options>"
2012 puts " Valid options:"
2013 puts " -c, --config start interactive configuration"
2014 #puts " -u, --update update module database"
2015 puts " -r, --rebuild force rebuild of module database"
2016 puts " -n, --norebuild don't rebuild module database even if it's outdated"
2017 puts " -t, --time time compare and rebuild of module database"
2018 #puts " -i, --include <mod> include `module' when building database"
2019 #puts " -e, --exclude <mod> exclude `module' when building database"
2020 #puts " -m, --module <mod> only update database for `module'"
2021 puts " -v, --verbose use more than once for more verbose operation"
2022 puts " -q, --quiet use more than once for quieter operation"
2023 puts " -d, --debug start in debug mode with tclsh"
2024 puts " --help show this help"
2025 puts " --version show version information"
2026 }
2027
2028 ##
2029 ## Enter interactive configuration
2030 ##
2031 ## Args: none
2032 ## Returns: nothing
2033 ##
2034 proc Iconfig {} {
2035 variable NamespaceCurrent
2036 variable IconfigDefaults
2037
2038 fileevent stdin readable ${NamespaceCurrent}::IconfigReadStdin
2039 puts "Entering wolfpack configuration system..."
2040 puts "Type 'help' for help."
2041 puts -nonewline $IconfigDefaults(prompt)
2042 flush stdout
2043 vwait forever
2044 }
2045
2046 ##
2047 ## Read stdin and process commands
2048 ##
2049 ## Args: none
2050 ## Returns: nothing
2051 ##
2052 # FIXME: readline-like support?
2053 proc IconfigReadStdin {} {
2054 variable IconfigDefaults
2055 variable configData
2056
2057 set exit 0
2058 if {[eof stdin]} {
2059 set exit 1
2060 }
2061 set stdin [string trimright [gets stdin]]
2062 set data [join [lrange [split $stdin] 1 end]]
2063 if {[string compare "" $stdin]} then {
2064 switch -exact -- [lindex [split $stdin] 0] {
2065 set {
2066 IconfigSet $data
2067 }
2068 enable {
2069 IconfigEnable $data
2070 }
2071 disable {
2072 IconfigDisable $data
2073 }
2074 modules {
2075 IconfigModules $data
2076 }
2077 info {
2078 IconfigInfo $data
2079 }
2080 help {
2081 IconfigHelp $data
2082 }
2083 quit {
2084 set exit 1
2085 }
2086 default {
2087 puts "What? You need 'help'"
2088 }
2089 }
2090 }
2091 if {(!$exit) && (![eof stdin])} then {
2092 puts -nonewline $IconfigDefaults(prompt)
2093 flush stdout
2094 } else {
2095 # Save configuration data
2096 arraySave configData $IconfigDefaults(cfgfile) 0 "configuration file "
2097 # Save module database
2098 saveModuleDatabase
2099 puts ""
2100 flush stdout
2101 exit
2102 }
2103 return
2104 }
2105
2106 ##
2107 ## Set configuration settings
2108 ##
2109 ## Args: data
2110 ## Returns: nothing
2111 ##
2112 proc IconfigSet {data} {
2113 variable configData
2114 variable configDefaults
2115
2116 if {![string compare "" $data]} then {
2117 set fmtlen1 [arrayMaxElementDataLength configDefaults 3]
2118 if {$fmtlen1 < 13} then {
2119 set fmtlen1 13 ;# 'Description: '
2120 }
2121 set names [array names configData]
2122 set fmtlen2 [listMaxElementLength $names]
2123 if {$fmtlen2 < 8} then {
2124 set fmtlen2 8 ;# 'Option: '
2125 }
2126 puts ""
2127 puts "Current settings:"
2128 puts ""
2129 # FIXME: this needs improvement
2130 puts [format " %-${fmtlen1}s %-${fmtlen2}s %s" Description: Option: Value:]
2131 foreach option [lsort $names] {
2132 if {[info exists configDefaults($option)]} then {
2133 puts [format " %-${fmtlen1}s %-${fmtlen2}s %s" [lindex $configDefaults($option) 3] $option $configData($option)]
2134 } else {
2135 puts [format " %-${fmtlen1}s %-${fmtlen2}s %s" "" $option $configData($option)]
2136 }
2137 }
2138 puts ""
2139 } else {
2140 set option [lindex [split $data] 0]
2141 if {![info exists configData($option)]} then {
2142 puts "Invalid option: $option"
2143 } else {
2144 set value [join [lrange [split $data] 1 end]]
2145 if {![string compare "" $value]} then {
2146 puts "Currently: $configData($option)"
2147 } else {
2148 set configData($option) $value
2149 puts "Set $option to: $value"
2150 }
2151 }
2152 }
2153 return
2154 }
2155
2156 ##
2157 ## Enable a module
2158 ##
2159 ## Args: data
2160 ## Returns: nothing
2161 ##
2162 proc IconfigEnable {data} {
2163 set module [lindex [split $data] 0]
2164 if {![string compare "" $module]} then {
2165 puts "Usage: enable <module>"
2166 } elseif {[moduleExists $module]} then {
2167 if {![getModuleDatabaseData $module load]} then {
2168 setModuleDatabaseData $module load 1
2169 puts "Enabled module: $module"
2170 } else {
2171 puts "Module `$module' is already enabled."
2172 }
2173 } else {
2174 puts "Invalid module: $module"
2175 }
2176 return
2177 }
2178
2179 ##
2180 ## Disable a module
2181 ##
2182 ## Args: data
2183 ## Returns: nothing
2184 ##
2185 proc IconfigDisable {data} {
2186 set module [lindex [split $data] 0]
2187 if {![string compare "" $module]} then {
2188 puts "Usage: disable <module>"
2189 } elseif {[moduleExists $module]} then {
2190 if {[getModuleDatabaseData $module load] == 1} then {
2191 setModuleDatabaseData $module load 0
2192 puts "Disabled module: $module"
2193 } else {
2194 puts "Module `$module' is already disabled."
2195 }
2196 } else {
2197 puts "Invalid module: $module"
2198 }
2199 return
2200 }
2201
2202 ##
2203 ## List modules
2204 ##
2205 ## Args: data
2206 ## returns: nothing
2207 ##
2208 # FIXME: format the list of modules better (proc from texttools?)
2209 proc IconfigModules {data} {
2210 if {[llength [set modules [listModules]]]} then {
2211 set what [lindex [split $data] 0]
2212 if {![string compare "" $what]} then {
2213 set what "all"
2214 }
2215 switch -exact -- $what {
2216 * -
2217 all {
2218 puts "Available modules:"
2219 foreach line [splitList $modules 65 " " " "] {
2220 puts " $line"
2221 }
2222 }
2223 enabled {
2224 set list ""
2225 foreach module $modules {
2226 if {[getModuleDatabaseData $module load]} {
2227 lappend list $module
2228 }
2229 }
2230 if {[llength $list]} then {
2231 puts "Enabled modules:"
2232 foreach line [splitList $list 65 " " " "] {
2233 puts " $line"
2234 }
2235 } else {
2236 puts "No modules enabled"
2237 }
2238 }
2239 disabled {
2240 set list ""
2241 foreach module $modules {
2242 if {![getModuleDatabaseData $module load]} {
2243 lappend list $module
2244 }
2245 }
2246 if {[llength $list]} then {
2247 puts "Disabled modules:"
2248 foreach line [splitList $list 65 " " " "] {
2249 puts " $line"
2250 }
2251 } else {
2252 puts "No modules disabled"
2253 }
2254 }
2255 default {
2256 puts "Error: 'option' must be one of: all, enabled, disabled"
2257 }
2258 }
2259 } else {
2260 puts "Error: No modules available."
2261 }
2262 return
2263 }
2264
2265 ##
2266 ## Show info for the given module
2267 ##
2268 ## Args: data {*|all|enabled|disabled}
2269 ## returns: nothing
2270 ##
2271 # FIXME: add multiple module support as in config.tcl and or integrate?
2272 proc IconfigInfo {data} {
2273 set module [lindex [split $data] 0]
2274 if {![string compare "" $module]} then {
2275 puts "Usage: info <module>"
2276 } elseif {[moduleExists $module]} then {
2277 puts "Info for module: $module"
2278 puts ""
2279 puts "Filename: [getModuleDatabaseData $module file]"
2280 puts "MD5: [getModuleDatabaseData $module md5sum]"
2281 puts "Version: [getModuleDatabaseData $module version]"
2282 if {[getModuleDatabaseData $module load]} then {
2283 puts "Enabled: yes"
2284 } else {
2285 puts "Enabled: no"
2286 }
2287 if {[string compare "" [set config [getModuleDatabaseData $module config]]]} then {
2288 puts "Config file: $config"
2289 }
2290 if {[string compare "" [set help [getModuleDatabaseData $module help]]]} then {
2291 puts "Help file: $help"
2292 }
2293 if {[string compare "" [set provides [getModuleDatabaseData $module provides]]]} then {
2294 puts "Provided commands:"
2295 foreach line [splitList $provides 65 " " " "] {
2296 puts " $line"
2297 }
2298 }
2299 if {[string compare "" [set requires [getModuleDatabaseData $module requires]]]} then {
2300 puts "Required commands:"
2301 foreach line [splitList $requires 65 " " " "] {
2302 puts " $line"
2303 }
2304 }
2305 puts "Description:"
2306 foreach line [splitList [getModuleDatabaseData $module description] 65 " " " "] {
2307 puts " $line"
2308 }
2309 } else {
2310 puts "Invalid module: $module"
2311 }
2312 return
2313 }
2314
2315 ##
2316 ## Show configuration help
2317 ##
2318 ## Args: data
2319 ## Returns: nothing
2320 ##
2321 proc IconfigHelp {data} {
2322 if {![string compare "" $data]} then {
2323 set data help
2324 }
2325 switch -exact -- $data {
2326 set {
2327 puts "### set \[option\] \[value\]"
2328 puts " Sets an option to what you specify."
2329 puts " Shows current setting for an option if no new value is given."
2330 puts " Shows current settings for all options if no option is given."
2331 }
2332 enable {
2333 puts "### enable <module>"
2334 puts " Enables the given module."
2335 }
2336 disable {
2337 puts "### disable <module>"
2338 puts " Disables the given module."
2339 }
2340 info {
2341 puts "### info <module>"
2342 puts " Show info for the given module."
2343 }
2344 modules {
2345 puts "### modules \[what\]"
2346 puts " Available options: all, enabled, disabled"
2347 puts " all: Shows all available modules"
2348 puts " enabled: Shows enabled modules"
2349 puts " disabled: Shows disabled modules"
2350 }
2351 help {
2352 puts "### Avaliable commands:"
2353 puts " set \[option\] \[value\]"
2354 puts " enable <module>"
2355 puts " disable <module>"
2356 puts " info <module>"
2357 puts " modules \[what\]"
2358 puts " help \[command\]"
2359 puts " quit"
2360 puts "You can get help on individual commands: 'help <command>'"
2361 }
2362 quit {
2363 puts "### quit"
2364 puts " Quits interactive configuration."
2365 }
2366 default {
2367 puts "No help available on that."
2368 }
2369 }
2370 return
2371 }
2372
2373 ##
2374 ## Inline startup and init code
2375 ##
2376
2377 wpLog o * "wolfpack.tcl v[package require $NamespaceCurrent] loading..."
2378
2379 # Init md5Sum command
2380 if {![md5Init]} then {
2381 wpLog o * "Error: can't find a usable md5 command!"
2382 return 0
2383 }
2384
2385 # Export commands
2386 eval namespace export [join $ExportList]
2387
2388 # Set missing variables to default values
2389 if {![info exists configDataChanged]} then {
2390 set configDataChanged 0
2391 }
2392 if {![info exists moduleDatabaseDataChanged]} then {
2393 set moduleDatabaseDataChanged 0
2394 }
2395 if {![info exists moduleLoadedList]} then {
2396 set moduleLoadedList ""
2397 }
2398
2399 # Load configuration data
2400 arrayLoad configData $IconfigDefaults(cfgfile) 0 "configuration file "
2401
2402 # Unset unknown configuration variables
2403 foreach name [array names configData] {
2404 if {![info exists configDefaults($name)]} then {
2405 unset configData($name)
2406 set configDataChanged 1
2407 }
2408 }
2409
2410 # Set missing configuration variables to defaults
2411 foreach {name data} [array get configDefaults] {
2412 if {![info exists configData($name)]} then {
2413 set configData($name) [lindex $data 1]
2414 set configDataChanged 1
2415 }
2416 }
2417
2418 # Set oldConfigData configuration variables to match
2419 foreach {name data} [array get configData] {
2420 if {![info exists oldConfigData($name)]} then {
2421 set oldConfigData($name) $data
2422 }
2423 }
2424
2425 # Trace configData changes
2426 trace variable configData w ${NamespaceCurrent}::TraceConfigData
2427
2428 # Save configuration data if changed
2429 if {$configDataChanged} then {
2430 arraySave configData $IconfigDefaults(cfgfile) 0 "configuration file "
2431 }
2432
2433 # Eval command line arguments if loading with tclsh
2434 if {[info exists argv0]} then {
2435 EvalArgs $argc $argv $argv0
2436 }
2437
2438 # Check verbose/quiet options
2439 if {$optionData(quiet)} then {
2440 set Verbose $optionData(quiet)
2441 } elseif {$configData(verbose)} then {
2442 set Verbose $configData(verbose)
2443 } else {
2444 set Verbose $optionData(verbose)
2445 }
2446
2447 # Check rebuild/norebuild options
2448 if {(!$optionData(norebuild)) && ($optionData(rebuild))} then {
2449 set RebuildDatabase 1
2450 } else {
2451 set RebuildDatabase 0
2452 }
2453
2454 # Sanity check: old eggdrop versions used [time] for a timestamp command
2455 if {(($optionData(time)) || ($configData(time))) && \
2456 ([catch {time}])} then {
2457 set TimeOk 1
2458 } else {
2459 set TimeOk 0
2460 }
2461
2462 # Load module database
2463 if {$Verbose >= 0} then {
2464 wpLog o * "Loading module database..."
2465 }
2466
2467 if {$TimeOk} then {
2468 set LoadTime [time {set LoadResult [loadModuleDatabase $Verbose]}]
2469 } else {
2470 set LoadResult [loadModuleDatabase $Verbose]
2471 }
2472
2473 if {$LoadResult != -1} then {
2474 if {$Verbose >= 0} then {
2475 if {[info exists LoadTime]} then {
2476 wpLog o * "Done. ([format "%.3f" [expr [lindex $LoadTime 0] / 1000000.0]] seconds elapsed)"
2477 } else {
2478 wpLog o * "Done."
2479 }
2480 }
2481 set CreateDatabase 0
2482 } else {
2483 if {$Verbose >= 0} then {
2484 wpLog o * "Warning: module database does not exist."
2485 }
2486 set CreateDatabase 1
2487 }
2488
2489 set NeedsRebuild 0
2490
2491 # Compare module database if we are not going to rebuild or create it
2492 if {(!$RebuildDatabase) && (!$CreateDatabase)} then {
2493 if {$Verbose >= 0} then {
2494 wpLog o * "Comparing module database..."
2495 }
2496 if {![file exists $configData(moddbfile)]} then {
2497 set NeedsRebuild 1
2498 } else {
2499 if {$TimeOk} then {
2500 set CompareTime [time {set CompareResult [compareModuleDatabase $Verbose]}]
2501 } else {
2502 set CompareResult [compareModuleDatabase $Verbose]
2503 }
2504 if {!$CompareResult} then {
2505 set NeedsRebuild 1
2506 }
2507 }
2508
2509 if {$Verbose >= 0} then {
2510 if {[info exists CompareTime]} then {
2511 wpLog o * "Done. ([format "%.3f" [expr [lindex $CompareTime 0] / 1000000.0]] seconds elapsed)"
2512 } else {
2513 wpLog o * "Done."
2514 }
2515 if {$NeedsRebuild} then {
2516 wpLog o * "Database is outdated."
2517 } else {
2518 wpLog o * "Database is current."
2519 }
2520 }
2521 }
2522
2523 # Create database if does not exist
2524 # Rebuild database if requested
2525 # Rebuild database if it's outdated and config permits
2526 if {($CreateDatabase) || ($RebuildDatabase) || \
2527 (($NeedsRebuild) && ($configData(rebuild)))} then {
2528
2529 if {$Verbose >= 0} then {
2530 if {$CreateDatabase} then {
2531 wpLog o * "Creating module database..."
2532 } else {
2533 wpLog o * "Rebuilding module database..."
2534 }
2535 }
2536
2537 # Rebuild module database
2538 if {$TimeOk} then {
2539 set RebuildTime [time {rebuildModuleDatabase $Verbose}]
2540 } else {
2541 rebuildModuleDatabase $Verbose
2542 }
2543
2544 if {$Verbose >= 0} then {
2545 if {[info exists RebuildTime]} then {
2546 wpLog o * "Done. ([format "%.3f" [expr [lindex $RebuildTime 0] / 1000000.0]] seconds elapsed)"
2547 } else {
2548 wpLog o * "Done."
2549 }
2550 }
2551
2552 # Save module database
2553 if {$Verbose >= 0} then {
2554 wpLog o * "Saving module database..."
2555 }
2556
2557 if {$TimeOk} then {
2558 set SaveTime [time {set SaveResult [saveModuleDatabase $Verbose]}]
2559 } else {
2560 set SaveResult [saveModuleDatabase $Verbose]
2561 }
2562
2563 if {$Verbose >= 0} then {
2564 if {$SaveResult} then {
2565 if {[info exists SaveTime]} then {
2566 wpLog o * "Done. ([format "%.3f" [expr [lindex $SaveTime 0] / 1000000.0]] seconds elapsed)"
2567 } else {
2568 wpLog o * "Done."
2569 }
2570 } else {
2571 wpLog o * "Error"
2572 }
2573 }
2574
2575 } elseif {($NeedsRebuild) && ($Verbose >= 0)} then {
2576 wpLog o * "Warning: not rebuilding outdated module database..."
2577 }
2578
2579 # Enter interactive configuration if loading with tclsh
2580 if {[info exists argv0]} then {
2581 if {$optionData(config)} then {
2582 Iconfig
2583 }
2584 }
2585
2586 # Build command table
2587 if {(![info exists argv0]) || ($optionData(debug))} then {
2588 if {$Verbose >= 0} then {
2589 wpLog o * "Building command table..."
2590 }
2591 if {$TimeOk} then {
2592 set CommandTime [time {buildCommandTable $Verbose}]
2593 } else {
2594 buildCommandTable $Verbose
2595 }
2596 if {$Verbose >= 0} then {
2597 if {[info exists CommandTime]} then {
2598 wpLog o * "Done. ([format "%.3f" [expr [lindex $CommandTime 0] / 1000000.0]] seconds elapsed)"
2599 } else {
2600 wpLog o * "Done."
2601 }
2602 }
2603
2604 set loadList ""
2605
2606 # Load debug module first if not in eggdrop mode
2607 if {([info exists argv0]) && ([moduleExists debug])} then {
2608 lappend loadList debug
2609 }
2610
2611 # Load other modules next, moduleLoad will preload additional modules
2612 foreach name [lsort [array names moduleDatabaseData]] {
2613 if {[getModuleDatabaseData $name load] == 1} then {
2614 lappend loadList $name
2615 }
2616 }
2617
2618 wpLog o * "Loading modules..."
2619
2620 # Load modules
2621 if {$TimeOk} then {
2622 set LoadTime [time {
2623 foreach module $loadList {
2624 if {[catch {moduleLoad $module $Verbose} result]} then {
2625 wpLog o * "Error: unable to load module `$module': $result"
2626 }
2627 }
2628 }]
2629 } else {
2630 foreach module $loadList {
2631 if {[catch {moduleLoad $module $Verbose} result]} then {
2632 wpLog o * "Error: unable to load module `$module': $result"
2633 }
2634 }
2635 }
2636
2637 if {[info exists LoadTime]} then {
2638 wpLog o * "Done. ([format "%.3f" [expr [lindex $LoadTime 0] / 1000000.0]] seconds elapsed)"
2639 } else {
2640 wpLog o * "Done."
2641 }
2642
2643 set ModuleLoadCount [llength [split $moduleLoadedList]]
2644 if {$ModuleLoadCount} then {
2645 set ModuleTotalCount [llength [array names moduleDatabaseData]]
2646 wpLog o * "Modules loaded ($ModuleLoadCount/$ModuleTotalCount): $moduleLoadedList"
2647 } else {
2648 wpLog o * "No modules loaded."
2649 }
2650
2651 # Unload modules if started in debug mode
2652 if {$optionData(debug)} then {
2653
2654 # FIXME: code module dependency stuff first
2655 # wpLog o * "Unloading modules..."
2656
2657 # Unload all modules except debug
2658 # foreach module $moduleLoadedList {
2659 # if {[string compare $module debug]} then {
2660 # if {[catch {moduleUnload $module $Verbose} result]} then {
2661 # wpLog o * "Error: unable to unload module `$module': $result"
2662 # }
2663 # }
2664 # }
2665
2666 # Unload debug module last to make it display stats
2667 if {[moduleLoaded debug]} then {
2668 if {[catch {moduleUnload debug} result]} then {
2669 wpLog o * "Error: unable to unload module `debug': $result"
2670 }
2671 }
2672 # wpLog o * "Done."
2673 }
2674 }
2675
2676 } ;# namespace ::wp
2677
2678 return

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23