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

Annotation of /wolfpack/wolfpack.tcl

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


Revision 1.24 - (hide annotations) (download) (as text)
Tue Jan 14 10:34:51 2003 UTC (16 years, 5 months ago) by tothwolf
Branch: MAIN
Changes since 1.23: +5 -1 lines
File MIME type: application/x-tcl
*** empty log message ***

1 guppy 1.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 tothwolf 1.9 ## Copyright (C) 1998-2003 Tothwolf <tothwolf@concentric.net>
41 guppy 1.1 ##
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 tothwolf 1.9 ## './wolfpack.tcl -c' may be used to configure initial settings.
61 guppy 1.1 ## './wolfpack.tcl --help' will list all avaliable options.
62     ##
63 tothwolf 1.7 ## Use '.wpconf' in dcc chat with the bot to configure everything else.
64 guppy 1.1 ##
65     ###############################################################################
66     ##
67 tothwolf 1.24 ## $Id: wolfpack.tcl,v 1.23 2003/01/14 10:18:28 tothwolf Exp $
68 guppy 1.1 ##
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 tothwolf 1.7 # HACK: quick fix for buggy tcl variables in older bots
87 guppy 1.1 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 tothwolf 1.7 set IconfigDefaults(cfgfile) wolfpack.conf
165 guppy 1.1
166     # Interactive configuration prompt
167 tothwolf 1.7 set IconfigDefaults(prompt) "wolfpack> "
168 guppy 1.1
169 tothwolf 1.9 # 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 guppy 1.1 }
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 tothwolf 1.2 splitList
213 guppy 1.1 arraySearch
214 tothwolf 1.9 commandChar
215 guppy 1.1 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 tothwolf 1.15 moduleBindUnbind
231 guppy 1.1 moduleLoad
232     moduleUnload
233 tothwolf 1.9 moduleConfigSave
234     moduleConfigLoad
235     moduleConfigCheckdefs
236 guppy 1.1 moduleConfig
237 tothwolf 1.9 moduleDataSave
238     moduleDataLoad
239     moduleDataBackup
240 guppy 1.1 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 tothwolf 1.5 ## Returns: 1 if successful
492     ## -1 if file is 0 in size
493     ## 0 otherwise
494 guppy 1.1 ##
495     proc backupFile {file {verbose 0} {suffix ~bak}} {
496     variable NamespaceCurrent
497    
498     if {[string compare "" $suffix]} then {
499 tothwolf 1.6 if {[file size $file]} then {
500     if {[catch {
501 guppy 1.1 file copy -force $file $file${suffix}
502 tothwolf 1.6 } result]} then {
503     if {$verbose >= 0} then {
504     wpLog o * $NamespaceCurrent "Error: unable to create backup file for `[file tail $file]': $result"
505     }
506 tothwolf 1.5 } else {
507 tothwolf 1.6 return 1
508 guppy 1.1 }
509     } else {
510 tothwolf 1.6 return -1
511 guppy 1.1 }
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 tothwolf 1.9 ## Return length of longest data in an array at index
677 guppy 1.1 ##
678     ## Args: array name
679     ## Returns: length of longest name in an array
680     ##
681 tothwolf 1.9 proc arrayMaxElementDataLength {arrayName index} {
682 guppy 1.1 upvar 1 $arrayName array
683    
684     set maxlength 0
685     foreach {name data} [array get array] {
686 tothwolf 1.9 set length [string length [lindex $data $index]]
687 guppy 1.1 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 tothwolf 1.2 ## Split up a list into multiple elements
713     ##
714     ## Args: text, max list length, split char, trim chars
715     ## Returns: split list
716     ##
717 tothwolf 1.9 # FIXME: improve this
718 tothwolf 1.2 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 guppy 1.1 ## 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 tothwolf 1.9 ## 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 guppy 1.1 ## 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 tothwolf 1.2 ## Create a data format list for a given data format and options
873 guppy 1.1 ##
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 tothwolf 1.2 if {(![info exists $option]) || \
978     (![string compare "" [set $option]])} then {
979 guppy 1.1 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 tothwolf 1.3 variable moduleDatabaseDataChanged
1044 guppy 1.1
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 tothwolf 1.3 set ret [arraySave moduleDatabaseData $configData(moddbfile) $verbose "module database file " a]
1054     if {$ret} then {
1055     set moduleDatabaseDataChanged 0
1056     }
1057     return $ret
1058 guppy 1.1 }
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 tothwolf 1.3 variable moduleDatabaseDataChanged
1076 guppy 1.1
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 tothwolf 1.3 set moduleDatabaseDataChanged 0
1104 guppy 1.1 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 tothwolf 1.15 ## Add/remove bindings for a given module
1268 tothwolf 1.7 ##
1269 tothwolf 1.16 ## Args: mode {bind|unbind}, module,
1270 tothwolf 1.15 ## args {{type ...} {option ...} {regsub ... ...}}
1271 tothwolf 1.7 ## Returns: nothing
1272     ##
1273 tothwolf 1.16 proc moduleBindUnbind {mode module args} {
1274 tothwolf 1.7 variable NamespaceCurrent
1275    
1276     if {[info exists ${NamespaceCurrent}::${module}::bindDefaults]} then {
1277 tothwolf 1.15 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 tothwolf 1.9 }
1286     }
1287 tothwolf 1.15 option {
1288     # commandchar noauto ...
1289     foreach opt [lrange $option 1 end] {
1290     lappend bindoptions $opt
1291 tothwolf 1.7 }
1292     }
1293 tothwolf 1.15 regsub {
1294     # replace regexp 'replacewhat' with 'replacewith'
1295 tothwolf 1.17 foreach opt [lrange $option 1 end] {
1296     foreach {rwhat rwith} $opt {break}
1297 tothwolf 1.24 # FIXME: better fix for leading '+'
1298     if {[regexp -- \\+ $rwhat]} then {
1299     regsub -- "\\+" $rwhat "\\\\+" rwhat
1300     }
1301 tothwolf 1.17 set replace($rwhat) $rwith
1302     }
1303 tothwolf 1.15 }
1304     }
1305 tothwolf 1.7 }
1306 tothwolf 1.15 foreach {proc data} [array get ${NamespaceCurrent}::${module}::bindDefaults] {
1307     foreach bind $data {
1308     foreach {type flags mask options help} $bind {break}
1309     if {([string compare "" $bindtypes]) && \
1310     ([lsearch -exact $bindtypes $type] == -1)} then {
1311     continue
1312     }
1313 tothwolf 1.18 if {[string compare "" $bindoptions]} then {
1314     set found 0
1315     foreach opt $options {
1316     if {[lsearch -exact $bindoptions $opt] != -1} then {
1317     set found 1
1318     break
1319     }
1320     }
1321     if {!$found} then {
1322     continue
1323     }
1324 tothwolf 1.15 }
1325     if {[lsearch -exact $options commandchar] != -1} then {
1326     set mask "[commandChar pub]$mask"
1327 tothwolf 1.9 }
1328 tothwolf 1.15 if {[array exists replace]} then {
1329 tothwolf 1.17 foreach {replacewhat replacewith} [array get replace] {
1330 tothwolf 1.15 regsub -all -- $replacewhat $mask $replacewith mask
1331 tothwolf 1.9 }
1332 tothwolf 1.7 }
1333 tothwolf 1.15 # bind/unbind
1334     $mode $type $flags $mask ${NamespaceCurrent}::${module}::$proc
1335 tothwolf 1.7 }
1336     }
1337     }
1338     return
1339     }
1340    
1341     ##
1342 guppy 1.1 ## Load a module
1343     ##
1344     ## Args: module name, verbose {-1,0,1}, args {loop detection}
1345     ## Returns: nothing
1346     ## Errors: if unable to load module
1347     ##
1348     proc moduleLoad {module {verbose 0} args} {
1349     variable NamespaceCurrent
1350     variable moduleLoadedList
1351    
1352     if {[lsearch -exact [set loop [lindex $args 0]] $module] == -1} then {
1353     if {[moduleExists $module]} then {
1354     set preload ""
1355     set requires [getModuleDatabaseData $module requires]
1356     foreach required $requires {
1357     set preloadModule [whichModuleCommand $required]
1358     if {[string compare "" $preloadModule]} then {
1359     if {([lsearch -exact $preload $preloadModule] == -1) && \
1360     ([string compare wp $preloadModule]) && \
1361     (![moduleLoaded $preloadModule])} then {
1362     lappend preload $preloadModule
1363     }
1364     } else {
1365     error "required command `$required' not found."
1366     }
1367     }
1368     if {[string compare "" $preload]} then {
1369     foreach premod $preload {
1370     if {[catch {moduleLoad $premod $verbose [concat $loop $module]} result]} then {
1371     error $result
1372     }
1373     }
1374     }
1375     if {[catch {source [getModuleDatabaseData $module file]} result]} then {
1376     error $result
1377     } else {
1378     package forget $module
1379     package provide ${NamespaceCurrent}::${module} [getModuleDatabaseData $module version]
1380 tothwolf 1.9 moduleConfigLoad $module 1
1381     moduleConfigCheckdefs $module 1
1382     moduleDataLoad $module 1
1383 guppy 1.1 # Imported commands '# requires: ...'
1384     if {[string compare "" $requires]} then {
1385     set Eval "namespace eval ${NamespaceCurrent}::${module} \{\n namespace forget *\n namespace import"
1386     foreach required $requires {
1387     if {[string compare "" [set command [whichCommand $required]]]} then {
1388     append Eval " $command"
1389     }
1390     }
1391     append Eval "\n\}"
1392     eval $Eval
1393     }
1394     # Exported commands '# provides: ...'
1395     if {[string compare "" [set provides [getModuleDatabaseData $module provides]]]} then {
1396     set Eval "namespace eval ${NamespaceCurrent}::${module} \{\n namespace export"
1397     foreach provided $provides {
1398     append Eval " $provided"
1399     }
1400     append Eval "\n\}"
1401     eval $Eval
1402     }
1403 tothwolf 1.7 if {[string compare "" [info commands ${NamespaceCurrent}::${module}::ModuleInit]]} then {
1404     ${NamespaceCurrent}::${module}::ModuleInit
1405 guppy 1.1 }
1406 tothwolf 1.7 # FIXME: check for bindings? duplicates?
1407 tothwolf 1.15 moduleBindUnbind bind $module
1408 guppy 1.1 if {![getModuleDatabaseData $module load]} then {
1409     setModuleDatabaseData $module load 1
1410     }
1411     if {[lsearch -exact $moduleLoadedList $module] == -1} then {
1412     lappend moduleLoadedList $module
1413     if {$verbose >= 1} then {
1414     wpLog o * "Module loaded: $module"
1415     }
1416     }
1417     }
1418     } else {
1419     error "No such module: $module"
1420     }
1421     } else {
1422     regsub -all -- " " $loop " -> " loop
1423     error "Preload endless loop: $loop -> $module"
1424     }
1425     return
1426     }
1427    
1428     ##
1429     ## Unload a module
1430     ##
1431     ## Args: module name, verbose {-1,0,1}
1432     ## Returns: nothing
1433     ## Errors: if unable to completely unload module
1434     ##
1435     proc moduleUnload {module {verbose 0}} {
1436     variable NamespaceCurrent
1437     variable moduleLoadedList
1438    
1439 tothwolf 1.9 # FIXME: handle dependant modules and modules that can't be unloaded
1440 guppy 1.1 if {[moduleExists $module]} then {
1441 tothwolf 1.7 if {[string compare "" [info commands ${NamespaceCurrent}::${module}::ModuleDestroy]]} then {
1442     ${NamespaceCurrent}::${module}::ModuleDestroy
1443 guppy 1.1 }
1444 tothwolf 1.7 # FIXME: check for bindings?
1445 tothwolf 1.15 moduleBindUnbind unbind $module
1446 tothwolf 1.9 moduleConfigSave $module 1
1447     moduleDataSave $module 1
1448 guppy 1.1 if {[catch {namespace delete ${NamespaceCurrent}::${module}} result]} then {
1449     error $result
1450     } else {
1451     package forget ${NamespaceCurrent}::${module}
1452     if {[getModuleDatabaseData $module load] == 1} then {
1453     setModuleDatabaseData $module load 0
1454     }
1455     set index [lsearch -exact $moduleLoadedList $module]
1456     if {$index != -1} then {
1457     set moduleLoadedList [lreplace $moduleLoadedList $index $index]
1458     if {$verbose >= 1} then {
1459     wpLog o * "Module unloaded: $module"
1460     }
1461     }
1462     }
1463     } else {
1464     error "No such module: $module"
1465     }
1466     return
1467     }
1468    
1469     ##
1470 tothwolf 1.9 ## Save configuration settings for the given module
1471 guppy 1.1 ##
1472 tothwolf 1.9 ## Args: module, force {0,1}, verbose {-1,0,1}
1473     ## Returns: 1 if settings saved
1474 guppy 1.1 ## 0 otherwise
1475     ##
1476 tothwolf 1.9 proc moduleConfigSave {module {force 0} {verbose 0}} {
1477 guppy 1.1 variable NamespaceCurrent
1478     variable configData
1479    
1480     if {([string compare "" \
1481     [set file [getModuleDatabaseData $module config]]]) && \
1482     ([createDir $configData(configpath)])} then {
1483     set cfgfile [file join $configData(configpath) $file]
1484 tothwolf 1.9 if {([getModuleDatabaseData $module load]) && \
1485     (($force) || \
1486     (![info exists ${NamespaceCurrent}::${module}::configDataChanged]) || \
1487     ([set ${NamespaceCurrent}::${module}::configDataChanged]))} then {
1488     if {[arraySave ${NamespaceCurrent}::${module}::configData $cfgfile 0 "$module configuration file "]} then {
1489     if {$verbose >= 1} then {
1490     wpLog o * "Writing $module config file..."
1491 guppy 1.1 }
1492 tothwolf 1.9 set ${NamespaceCurrent}::${module}::configDataChanged 0
1493     return 1
1494     } elseif {$verbose >= 0} then {
1495     wpLog o * "Error writing $module config file."
1496 guppy 1.1 }
1497 tothwolf 1.9 }
1498     }
1499     return 0
1500     }
1501    
1502     ##
1503     ## Load configuration settings for the given module
1504     ##
1505     ## Args: module, force {0,1}, verbose {-1,0,1}
1506     ## Returns: 1 if settings loaded
1507     ## 0 otherwise
1508     ##
1509     proc moduleConfigLoad {module {force 0} {verbose 0}} {
1510     variable NamespaceCurrent
1511     variable configData
1512    
1513     if {([string compare "" \
1514     [set file [getModuleDatabaseData $module config]]]) && \
1515     ([createDir $configData(configpath)])} then {
1516     set cfgfile [file join $configData(configpath) $file]
1517     if {($force) || \
1518     (![info exists ${NamespaceCurrent}::${module}::configDataChanged]) || \
1519     (![set ${NamespaceCurrent}::${module}::configDataChanged])} then {
1520     if {[arrayLoad ${NamespaceCurrent}::${module}::configData $cfgfile 0 "$module configuration file "]} then {
1521     if {$verbose >= 1} then {
1522     wpLog o * "Loading $module config file..."
1523 guppy 1.1 }
1524 tothwolf 1.9 set ${NamespaceCurrent}::${module}::configDataChanged 0
1525     return 1
1526     } elseif {$verbose >= 0} then {
1527     wpLog o * "Error loading $module config file."
1528 guppy 1.1 }
1529     }
1530     }
1531     return 0
1532     }
1533    
1534     ##
1535 tothwolf 1.9 ## Check and set default configuration settings for the given module
1536 guppy 1.1 ##
1537 tothwolf 1.9 ## Args: module, force {0,1}, verbose {-1,0,1}
1538 guppy 1.1 ## Returns: nothing
1539     ##
1540 tothwolf 1.9 proc moduleConfigCheckdefs {module {force 0} {verbose 0}} {
1541     variable NamespaceCurrent
1542    
1543     if {([array exists ${NamespaceCurrent}::${module}::configDefaults]) && \
1544     ([string compare "" [getModuleDatabaseData $module config]])} then {
1545     set Changed 0
1546     # Unset unknown variables
1547     foreach name [array names ${NamespaceCurrent}::${module}::configData] {
1548     if {![info exists ${NamespaceCurrent}::${module}::configDefaults($name)]} then {
1549     unset ${NamespaceCurrent}::${module}::configData($name)
1550     set Changed 1
1551     }
1552     }
1553     # Set missing variables to defaults
1554     foreach {name data} [array get ${NamespaceCurrent}::${module}::configDefaults] {
1555     if {![info exists ${NamespaceCurrent}::${module}::configData($name)]} then {
1556     set ${NamespaceCurrent}::${module}::configData($name) [lindex $data 1]
1557     set Changed 1
1558     }
1559     }
1560     # FIXME: do this with a trace?
1561     if {$Changed} then {
1562     set ${NamespaceCurrent}::${module}::configDataChanged 1
1563     }
1564     }
1565     return
1566     }
1567 guppy 1.1
1568 tothwolf 1.9 ##
1569     ## Handle config data for a list of modules
1570     ##
1571     ## Args: action {load|save|checkdefs}, module list, force {0,1},
1572     ## verbose {-1,0,1}
1573     ## Returns: nothing
1574     ##
1575     proc moduleConfig {action modules {force 0} {verbose 0}} {
1576 guppy 1.1 if {![string compare * $modules]} then {
1577     set modules [listModules 1]
1578     }
1579 tothwolf 1.9 switch -exact -- $action {
1580     save {
1581     foreach module $modules {
1582     if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1583     moduleConfigSave $module $force $verbose
1584     }
1585     }
1586     }
1587     load {
1588     foreach module $modules {
1589     if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1590     moduleConfigLoad $module $force $verbose
1591     }
1592     }
1593     }
1594     checkdefs {
1595     foreach module $modules {
1596     if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1597     moduleConfigCheckdefs $module $force $verbose
1598     }
1599     }
1600 guppy 1.1 }
1601     }
1602     return
1603     }
1604    
1605     ##
1606 tothwolf 1.9 ## Save data for the given module
1607 guppy 1.1 ##
1608 tothwolf 1.9 ## Args: module, force {0,1}, verbose {-1,0,1}
1609 guppy 1.1 ## Returns: nothing
1610     ##
1611 tothwolf 1.9 proc moduleDataSave {module {force 0} {verbose 0}} {
1612 guppy 1.1 variable NamespaceCurrent
1613     variable configData
1614    
1615     if {([info exists ${NamespaceCurrent}::${module}::dataOptions]) && \
1616     ([createDir $configData(datapath)])} then {
1617 tothwolf 1.9 foreach {name data} [array get ${NamespaceCurrent}::${module}::dataOptions] {
1618     foreach {type file desc} $data {break}
1619     if {([info exists type]) && ([info exists file]) && \
1620     ([info exists desc])} then {
1621     set Changed ${NamespaceCurrent}::${module}::${name}Changed
1622     if {($force) || ((![info exists $Changed]) || ([set $Changed]))} then {
1623     if {[${type}Save ${NamespaceCurrent}::${module}::$name [file join $configData(datapath) $file]]} then {
1624     if {$verbose >= 1} then {
1625     wpLog o * $NamespaceCurrent "Writing $desc data file..."
1626 guppy 1.1 }
1627 tothwolf 1.9 set $Changed 0
1628     } elseif {$verbose >= 0} then {
1629     wpLog o * $NamespaceCurrent "Error writing $desc data file!"
1630 guppy 1.1 }
1631     }
1632     }
1633 tothwolf 1.9 }
1634     }
1635     return
1636     }
1637    
1638     ##
1639     ## Load data for the given module
1640     ##
1641     ## Args: module, force {0,1}, verbose {-1,0,1}
1642     ## Returns: nothing
1643     ##
1644     proc moduleDataLoad {module {force 0} {verbose 0}} {
1645     variable NamespaceCurrent
1646     variable configData
1647    
1648     if {([info exists ${NamespaceCurrent}::${module}::dataOptions]) && \
1649     ([createDir $configData(datapath)])} then {
1650     foreach {name data} [array get ${NamespaceCurrent}::${module}::dataOptions] {
1651     foreach {type file desc} $data {break}
1652     if {([info exists type]) && ([info exists file]) && \
1653     ([info exists desc])} then {
1654     set Changed ${NamespaceCurrent}::${module}::${name}Changed
1655     if {($force) || ((![info exists $Changed]) || ([set $Changed]))} then {
1656     if {[${type}Load ${NamespaceCurrent}::${module}::$name [file join $configData(datapath) $file]]} then {
1657     if {$verbose >= 1} then {
1658     wpLog o * $NamespaceCurrent "Reloading $desc data file..."
1659 guppy 1.1 }
1660 tothwolf 1.9 set $Changed 0
1661     } elseif {$verbose >= 0} then {
1662     wpLog o * $NamespaceCurrent "Error reloading $desc data file!"
1663 guppy 1.1 }
1664     }
1665     }
1666 tothwolf 1.9 }
1667     }
1668     return
1669     }
1670    
1671     ##
1672     ## Backup data for the given module
1673     ##
1674     ## Args: module, force {0,1}, verbose {-1,0,1}
1675     ## Returns: nothing
1676     ##
1677     proc moduleDataBackup {module {force 0} {verbose 0}} {
1678     variable NamespaceCurrent
1679     variable configData
1680    
1681     if {([info exists ${NamespaceCurrent}::${module}::dataOptions]) && \
1682     ([createDir $configData(datapath)])} then {
1683     foreach {name data} [array get ${NamespaceCurrent}::${module}::dataOptions] {
1684     foreach {type file desc} $data {break}
1685     if {([info exists type]) && ([info exists file]) && \
1686     ([info exists desc])} then {
1687     if {[set result [backupFile [file join $configData(datapath) $file] $verbose]]} then {
1688     if {($result >= 1) && ($verbose >= 1)} then {
1689     wpLog o * $NamespaceCurrent "Backing up $desc data file..."
1690 guppy 1.1 }
1691 tothwolf 1.9 } elseif {$verbose >= 0} then {
1692     wpLog o * $NamespaceCurrent "Error backing up $desc data file!"
1693 guppy 1.1 }
1694     }
1695     }
1696     }
1697     return
1698     }
1699    
1700     ##
1701 tothwolf 1.9 ## Handle data for a list of modules
1702 guppy 1.1 ##
1703 tothwolf 1.9 ## Args: action {load|save|backup}, module list, force {0,1},
1704     ## verbose {-1,0,1}
1705 guppy 1.1 ## Returns: nothing
1706     ##
1707 tothwolf 1.9 proc moduleData {action modules {force 0} {verbose 0}} {
1708 guppy 1.1 if {![string compare * $modules]} then {
1709     set modules [listModules 1]
1710     }
1711 tothwolf 1.9 switch -exact -- $action {
1712     save {
1713     foreach module $modules {
1714     if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1715     moduleDataSave $module $force $verbose
1716     }
1717     }
1718     }
1719     load {
1720     foreach module $modules {
1721     if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1722     moduleDataLoad $module $force $verbose
1723     }
1724     }
1725     }
1726     backup {
1727     foreach module $modules {
1728     if {([moduleExists $module]) && ([moduleLoaded $module])} then {
1729     moduleDataBackup $module $force $verbose
1730     }
1731     }
1732 guppy 1.1 }
1733     }
1734     return
1735     }
1736    
1737     ##
1738     ## Builds command matching table from module database
1739     ##
1740     ## Args: none
1741     ## Returns: nothing
1742     ##
1743     proc buildCommandTable {{verbose 0}} {
1744     variable NamespaceCurrent
1745     variable ExportList
1746     variable commandTable
1747    
1748     foreach command $ExportList {
1749     if {![info exists tmp($command)]} then {
1750     if {$verbose >= 2} then {
1751     wpLog o * "Adding command: `$command' as `${NamespaceCurrent}::$command'"
1752     }
1753     set tmp($command) ${NamespaceCurrent}::$command
1754     } elseif {$verbose >= 0} then {
1755     wpLog o * "Warning: ignoring duplicate command `$command', conflicts with `$tmp($command)'"
1756     }
1757     }
1758     foreach module [listModules] {
1759     if {[string compare "" [set provides [getModuleDatabaseData $module provides]]]} then {
1760     foreach command $provides {
1761     if {![info exists tmp($command)]} then {
1762     if {$verbose >= 2} then {
1763     wpLog o * "Adding command: `$command' as `${NamespaceCurrent}::${module}::$command'"
1764     }
1765     set tmp($command) ${NamespaceCurrent}::${module}::$command
1766     } elseif {$verbose >= 0} then {
1767     wpLog o * "Warning: ignoring duplicate command `$command', conflicts with `$tmp($command)'"
1768     }
1769     }
1770     }
1771     }
1772     if {[info exists commandTable]} then {
1773     unset commandTable
1774     }
1775     array set commandTable [array get tmp]
1776     return
1777     }
1778    
1779     ##
1780     ## Return full namespace path for the given command
1781     ##
1782     ## Args: command
1783     ## Returns: full namespace path for the given command if it exists
1784     ## nothing otherwise
1785     ##
1786     proc whichCommand {command} {
1787     variable commandTable
1788    
1789     if {[info exists commandTable($command)]} then {
1790     return $commandTable($command)
1791     }
1792     return
1793     }
1794    
1795     ##
1796     ## Return full namespace path for the given module
1797     ##
1798     ## Args: module
1799     ## Returns: full namespace path for the given module if it's loaded
1800     ## nothing otherwise
1801     ##
1802     proc whichModule {module} {
1803     variable NamespaceCurrent
1804    
1805     if {![string compare $module [namespace tail $NamespaceCurrent] $module]} then {
1806     return $NamespaceCurrent
1807     } elseif {[moduleLoaded $module]} then {
1808     return ${NamespaceCurrent}::$module
1809     }
1810     return
1811     }
1812    
1813     ##
1814     ## Return module name that provides the given command
1815     ##
1816     ## Args: command
1817     ## Returns: name of module that provides the given command
1818     ## nothing otherwise
1819     ##
1820     proc whichModuleCommand {command} {
1821     variable NamespaceCurrent
1822     variable commandTable
1823    
1824     if {[info exists commandTable($command)]} then {
1825     if {![string compare ${NamespaceCurrent}::$command \
1826     $commandTable($command)]} then {
1827     return [namespace tail $NamespaceCurrent]
1828     }
1829     return [namespace tail [namespace qualifiers $commandTable($command)]]
1830     }
1831     return
1832     }
1833    
1834     ##
1835     ## Compare the given version to eggdrop's version
1836     ##
1837     ## Args: version
1838     ## Returns: 0 if eggdrop's version is older then the given version
1839     ## 1 if eggdrop's version matches the given version
1840     ## 2 if eggdrop's version is newer then the given version
1841     ##
1842     proc compareVersion {version} {
1843     global numversion
1844    
1845     if {[string compare "" $version]} then {
1846     if {([info exists numversion]) || \
1847     (![catch {set numversion}]) || \
1848     ([info exists numversion])} then {
1849     if {[regexp -- \\. $version]} then {
1850     regsub -all -- \\. $version 0 version
1851     set version ${version}00
1852     }
1853     if {![regexp -- \[^0-9\] $version]} then {
1854     if {$numversion > $version} then {
1855     return 2
1856     } elseif {$numversion == $version} then {
1857     return 1
1858     }
1859     }
1860     }
1861     }
1862     return 0
1863     }
1864    
1865     ##
1866     ## Log module information
1867     ##
1868     ## Args: level, channel, args ({<namespace>} {<text>} | {<text>})
1869     ## Returns: nothing
1870     ##
1871     proc wpLog {level channel args} {
1872     if {[llength $args] == 2} then {
1873     if {[string compare wp [set namespace [namespace tail [lindex $args 0]]]]} then {
1874     putloglev $level $channel "Wolfpack: \[$namespace\] [lindex $args 1]"
1875     } else {
1876     putloglev $level $channel "Wolfpack: [lindex $args 1]"
1877     }
1878     } else {
1879     putloglev $level $channel "Wolfpack: [join $args]"
1880     }
1881     return
1882     }
1883    
1884     ##
1885 tothwolf 1.9 ## Trace configuration changes
1886     ##
1887     ## Args: name1, name2, operation
1888     ## Returns: nothing
1889     ##
1890     proc TraceConfigData {name1 name2 op} {
1891     variable configData
1892     variable oldConfigData
1893    
1894     if {[string compare $oldConfigData($name2) $configData($name2)]} then {
1895     switch -exact -- $name2 {
1896     commandchar {
1897     foreach module [listModules 1] {
1898 tothwolf 1.23 moduleBindUnbind unbind $module "type pub" "option commandchar" [list regsub "^[commandChar pub] $oldConfigData(commandchar)"]
1899 tothwolf 1.15 moduleBindUnbind bind $module "type pub" "option commandchar"
1900 tothwolf 1.9 }
1901     }
1902     }
1903     set oldConfigData($name2) $configData($name2)
1904     }
1905     return
1906     }
1907    
1908     ##
1909 guppy 1.1 ## Evaluate command line arguments
1910     ##
1911     ## Args: none
1912     ## Returns: nothing
1913     ##
1914     proc EvalArgs {argc argv argv0} {
1915     variable NamespaceCurrent
1916     variable optionData
1917    
1918     # Make sure defaults are sane
1919     arraySetAll optionData 0
1920     for {set index 0} {$index < $argc} {incr index} {
1921     set option [lindex $argv $index]
1922     switch -regexp -- $option {
1923     (^--config$) {
1924     set optionData(config) 1
1925     }
1926     (^--rebuild$) {
1927     set optionData(rebuild) 1
1928     }
1929     (^--norebuild$) {
1930     set optionData(norebuild) 1
1931     }
1932     (^--time$) {
1933     set optionData(time) 1
1934     }
1935     (^--verbose$) {
1936     incr optionData(verbose)
1937     }
1938     (^--quiet$) {
1939     incr optionData(quiet) -1
1940     }
1941     (^--debug$) {
1942     set optionData(debug) 1
1943     }
1944     (^--help$) {
1945     ShowUsage $argv0
1946     exit
1947     }
1948     (^--version$) {
1949     puts "[file tail $argv0] version [package require ${NamespaceCurrent}]"
1950     exit
1951     }
1952     (^-\[^-\]*$) {
1953     set suboptions [split $option ""]
1954     set sublength [llength [split $suboptions]]
1955     for {set subindex 0} {$subindex < $sublength} {incr subindex} {
1956     set suboption [lindex $suboptions $subindex]
1957     switch -exact -- $suboption {
1958     - {
1959     continue
1960     }
1961     c {
1962     set optionData(config) 1
1963     }
1964     r {
1965     set optionData(rebuild) 1
1966     }
1967     n {
1968     set optionData(norebuild) 1
1969     }
1970     t {
1971     set optionData(time) 1
1972     }
1973     v {
1974     incr optionData(verbose)
1975     }
1976     q {
1977     incr optionData(quiet) -1
1978     }
1979     d {
1980     set optionData(debug) 1
1981     }
1982     default {
1983     if {(![info exists invalidopt]) || \
1984     ($subindex == 1) || \
1985     ([lsearch -exact $invalidopt $option] == -1)} then {
1986     lappend invalidopt $option
1987     }
1988     }
1989     }
1990     }
1991     }
1992     default {
1993     lappend invalidopt $option
1994     }
1995     }
1996     }
1997    
1998     # complain about invalid command line arguments
1999     if {[info exists invalidopt]} then {
2000     foreach option $invalidopt {
2001     puts stderr "[file tail $argv0]: unrecognized option `$option'"
2002     }
2003     exit 1
2004     }
2005     }
2006    
2007     ##
2008     ## Show usage information
2009     ##
2010     ## Args: none
2011     ## Returns: nothing
2012     ##
2013     proc ShowUsage {argv0} {
2014 tothwolf 1.9 # FIXME: code missing options
2015 guppy 1.1 puts "Usage: [file tail $argv0] <options>"
2016     puts " Valid options:"
2017     puts " -c, --config start interactive configuration"
2018     #puts " -u, --update update module database"
2019     puts " -r, --rebuild force rebuild of module database"
2020     puts " -n, --norebuild don't rebuild module database even if it's outdated"
2021     puts " -t, --time time compare and rebuild of module database"
2022     #puts " -i, --include <mod> include `module' when building database"
2023     #puts " -e, --exclude <mod> exclude `module' when building database"
2024     #puts " -m, --module <mod> only update database for `module'"
2025     puts " -v, --verbose use more than once for more verbose operation"
2026     puts " -q, --quiet use more than once for quieter operation"
2027     puts " -d, --debug start in debug mode with tclsh"
2028     puts " --help show this help"
2029     puts " --version show version information"
2030     }
2031    
2032     ##
2033     ## Enter interactive configuration
2034     ##
2035     ## Args: none
2036     ## Returns: nothing
2037     ##
2038     proc Iconfig {} {
2039     variable NamespaceCurrent
2040 tothwolf 1.7 variable IconfigDefaults
2041 guppy 1.1
2042     fileevent stdin readable ${NamespaceCurrent}::IconfigReadStdin
2043     puts "Entering wolfpack configuration system..."
2044     puts "Type 'help' for help."
2045 tothwolf 1.7 puts -nonewline $IconfigDefaults(prompt)
2046 guppy 1.1 flush stdout
2047     vwait forever
2048     }
2049    
2050     ##
2051     ## Read stdin and process commands
2052     ##
2053     ## Args: none
2054     ## Returns: nothing
2055     ##
2056 tothwolf 1.10 # FIXME: readline-like support?
2057 guppy 1.1 proc IconfigReadStdin {} {
2058 tothwolf 1.7 variable IconfigDefaults
2059 guppy 1.1 variable configData
2060    
2061     set exit 0
2062     if {[eof stdin]} {
2063     set exit 1
2064     }
2065     set stdin [string trimright [gets stdin]]
2066     set data [join [lrange [split $stdin] 1 end]]
2067     if {[string compare "" $stdin]} then {
2068     switch -exact -- [lindex [split $stdin] 0] {
2069     set {
2070     IconfigSet $data
2071     }
2072     enable {
2073     IconfigEnable $data
2074     }
2075     disable {
2076     IconfigDisable $data
2077     }
2078     modules {
2079     IconfigModules $data
2080     }
2081 tothwolf 1.14 info {
2082     IconfigInfo $data
2083     }
2084 guppy 1.1 help {
2085     IconfigHelp $data
2086     }
2087     quit {
2088     set exit 1
2089     }
2090     default {
2091     puts "What? You need 'help'"
2092     }
2093     }
2094     }
2095     if {(!$exit) && (![eof stdin])} then {
2096 tothwolf 1.7 puts -nonewline $IconfigDefaults(prompt)
2097 guppy 1.1 flush stdout
2098     } else {
2099     # Save configuration data
2100 tothwolf 1.7 arraySave configData $IconfigDefaults(cfgfile) 0 "configuration file "
2101 guppy 1.1 # Save module database
2102     saveModuleDatabase
2103     puts ""
2104     flush stdout
2105     exit
2106     }
2107     return
2108     }
2109    
2110     ##
2111     ## Set configuration settings
2112     ##
2113     ## Args: data
2114     ## Returns: nothing
2115     ##
2116     proc IconfigSet {data} {
2117     variable configData
2118 tothwolf 1.9 variable configDefaults
2119 guppy 1.1
2120     if {![string compare "" $data]} then {
2121 tothwolf 1.9 set fmtlen1 [arrayMaxElementDataLength configDefaults 3]
2122 guppy 1.1 if {$fmtlen1 < 13} then {
2123     set fmtlen1 13 ;# 'Description: '
2124     }
2125     set names [array names configData]
2126     set fmtlen2 [listMaxElementLength $names]
2127     if {$fmtlen2 < 8} then {
2128     set fmtlen2 8 ;# 'Option: '
2129     }
2130     puts ""
2131     puts "Current settings:"
2132     puts ""
2133 tothwolf 1.10 # FIXME: this needs improvement
2134 guppy 1.1 puts [format " %-${fmtlen1}s %-${fmtlen2}s %s" Description: Option: Value:]
2135     foreach option [lsort $names] {
2136 tothwolf 1.9 if {[info exists configDefaults($option)]} then {
2137     puts [format " %-${fmtlen1}s %-${fmtlen2}s %s" [lindex $configDefaults($option) 3] $option $configData($option)]
2138 guppy 1.1 } else {
2139     puts [format " %-${fmtlen1}s %-${fmtlen2}s %s" "" $option $configData($option)]
2140     }
2141     }
2142     puts ""
2143     } else {
2144     set option [lindex [split $data] 0]
2145     if {![info exists configData($option)]} then {
2146     puts "Invalid option: $option"
2147     } else {
2148     set value [join [lrange [split $data] 1 end]]
2149     if {![string compare "" $value]} then {
2150     puts "Currently: $configData($option)"
2151     } else {
2152     set configData($option) $value
2153     puts "Set $option to: $value"
2154     }
2155     }
2156     }
2157     return
2158     }
2159    
2160     ##
2161     ## Enable a module
2162     ##
2163     ## Args: data
2164     ## Returns: nothing
2165     ##
2166     proc IconfigEnable {data} {
2167     set module [lindex [split $data] 0]
2168     if {![string compare "" $module]} then {
2169     puts "Usage: enable <module>"
2170     } elseif {[moduleExists $module]} then {
2171     if {![getModuleDatabaseData $module load]} then {
2172     setModuleDatabaseData $module load 1
2173     puts "Enabled module: $module"
2174     } else {
2175     puts "Module `$module' is already enabled."
2176     }
2177     } else {
2178     puts "Invalid module: $module"
2179     }
2180     return
2181     }
2182    
2183     ##
2184     ## Disable a module
2185     ##
2186     ## Args: data
2187     ## Returns: nothing
2188     ##
2189     proc IconfigDisable {data} {
2190     set module [lindex [split $data] 0]
2191     if {![string compare "" $module]} then {
2192     puts "Usage: disable <module>"
2193     } elseif {[moduleExists $module]} then {
2194     if {[getModuleDatabaseData $module load] == 1} then {
2195     setModuleDatabaseData $module load 0
2196     puts "Disabled module: $module"
2197     } else {
2198     puts "Module `$module' is already disabled."
2199     }
2200     } else {
2201     puts "Invalid module: $module"
2202     }
2203     return
2204     }
2205    
2206     ##
2207     ## List modules
2208     ##
2209     ## Args: data
2210     ## returns: nothing
2211     ##
2212 tothwolf 1.14 # FIXME: format the list of modules better (proc from texttools?)
2213 guppy 1.1 proc IconfigModules {data} {
2214 tothwolf 1.14 if {[llength [set modules [listModules]]]} then {
2215     set what [lindex [split $data] 0]
2216     if {![string compare "" $what]} then {
2217     set what "all"
2218     }
2219     switch -exact -- $what {
2220     * -
2221     all {
2222     puts "Available modules:"
2223     foreach line [splitList $modules 65 " " " "] {
2224     puts " $line"
2225     }
2226     }
2227     enabled {
2228     set list ""
2229     foreach module $modules {
2230     if {[getModuleDatabaseData $module load]} {
2231     lappend list $module
2232     }
2233     }
2234     if {[llength $list]} then {
2235     puts "Enabled modules:"
2236     foreach line [splitList $list 65 " " " "] {
2237     puts " $line"
2238     }
2239     } else {
2240     puts "No modules enabled"
2241     }
2242     }
2243     disabled {
2244     set list ""
2245     foreach module $modules {
2246     if {![getModuleDatabaseData $module load]} {
2247     lappend list $module
2248     }
2249     }
2250     if {[llength $list]} then {
2251     puts "Disabled modules:"
2252     foreach line [splitList $list 65 " " " "] {
2253     puts " $line"
2254     }
2255     } else {
2256     puts "No modules disabled"
2257     }
2258     }
2259     default {
2260     puts "Error: 'option' must be one of: all, enabled, disabled"
2261     }
2262     }
2263     } else {
2264     puts "Error: No modules available."
2265     }
2266     return
2267     }
2268    
2269     ##
2270     ## Show info for the given module
2271     ##
2272     ## Args: data {*|all|enabled|disabled}
2273     ## returns: nothing
2274     ##
2275     # FIXME: add multiple module support as in config.tcl and or integrate?
2276     proc IconfigInfo {data} {
2277     set module [lindex [split $data] 0]
2278     if {![string compare "" $module]} then {
2279     puts "Usage: info <module>"
2280     } elseif {[moduleExists $module]} then {
2281     puts "Info for module: $module"
2282     puts ""
2283     puts "Filename: [getModuleDatabaseData $module file]"
2284     puts "MD5: [getModuleDatabaseData $module md5sum]"
2285     puts "Version: [getModuleDatabaseData $module version]"
2286     if {[getModuleDatabaseData $module load]} then {
2287     puts "Enabled: yes"
2288     } else {
2289     puts "Enabled: no"
2290     }
2291     if {[string compare "" [set config [getModuleDatabaseData $module config