It isn't clear whether this is a core Tcl project or something written for an Eggdrop bot, nevertheless I've provided an Eggdrop IRC public command !interface in the script below together with suitable output statements so that I can test it.
No directory/path is provided with the GLOB, FILE and OPEN commands, hence they default to the bots root directory.
The script searches all .txt files for the text 'interface xxxx' without regard to case, where xxxx is a four digit hexadecimal number.
Interface details are held in an array named iarray, where each array element name is the four digit hexadecimal number and the corresponding array element value is the frequency it occurs in the .txt files.
Code: Select all
# interface.tcl
bind PUB n !interface pPubCommand
proc pPubCommand {nick uhost hand channel txt} {
if {[llength [split [string trim $txt]]] == 0} {
pInterface
pOutput $channel
} else {putserv "PRIVMSG $channel :correct syntax is !interface without additional arguments"}
return 0
}
proc pInterface {} {
global flist iarray
unset -nocomplain iarray
set flist [lsort [glob -nocomplain -- *.txt]]
foreach item $flist {
set fp [open $item r]
set fdata [read $fp]
close $fp
while {[regexp -nocase -- {interface ([0-9a-f]{4})} $fdata -> found]} {
set fdata [regsub -nocase -- $found $fdata ""]
set found [string tolower $found]
if {[info exists iarray($found)]} {incr iarray($found)} else {set iarray($found) 1}
}
}
return 0
}
proc pOutput {channel} {
global flist iarray
putserv "PRIVMSG $channel :\037files\037"
if {[llength $flist] != 0} {
foreach item $flist {
putserv "PRIVMSG $channel :$item [file size $item] bytes"
}
putserv "PRIVMSG $channel :\037interfaces\037"
if {[array size iarray] != 0} {
foreach item [lsort [array names iarray]] {
putserv "PRIVMSG $channel :interface $item frequency $iarray($item)"
}
} else {putserv "PRIVMSG $channel :no interfaces found"}
} else {putserv "PRIVMSG $channel :no files found"}
return 0
}
This is the original output (there are .txt files in the bot's root directory, but no text that matches the regular expression) :-
[09:54] <@arfer> !interface
[09:54] <@osmosis>
files
[09:54] <@osmosis> abuseat.txt 252 bytes
[09:54] <@osmosis> access.txt 12076 bytes
[09:54] <@osmosis> chanstats.txt 53 bytes
[09:54] <@osmosis> url.txt 0 bytes
[09:54] <@osmosis>
interfaces
[09:54] <@osmosis> no interfaces found
I created two .txt files in the bot's root directory as follows :-
test1.txt
this is a test
my Interface ac67 and my interface 458A
did i say InterFace AC67?
test2.txt
interface 432b
Interface 675c and InterfaCE AAAb
i think i said interface 458a in test1.txt
try 3 times ... interface bbb4, INTERFACE BBB4, inTERface bBb4
This is the script output after creating the two files :-
[10:13] <@arfer> !interface
[10:13] <@osmosis>
files
[10:13] <@osmosis> abuseat.txt 252 bytes
[10:13] <@osmosis> access.txt 12076 bytes
[10:13] <@osmosis> chanstats.txt 53 bytes
[10:13] <@osmosis> status.txt 371 bytes
[10:13] <@osmosis> test1.txt 84 bytes
[10:13] <@osmosis> test2.txt 159 bytes
[10:13] <@osmosis> url.txt 0 bytes
[10:13] <@osmosis>
interfaces
[10:13] <@osmosis> interface 432b frequency 1
[10:13] <@osmosis> interface 458a frequency 2
[10:13] <@osmosis> interface 675c frequency 1
[10:13] <@osmosis> interface aaab frequency 1
[10:13] <@osmosis> interface ac67 frequency 2
[10:13] <@osmosis> interface bbb4 frequency 3