What I want to do is set up a bots listing service, for macro bots in games etc.
I've got the list to work with files, been good for some time, but I wanna covert it to INI files, just to clean things up a bit. Right now, I can !addbot <nickname> and it will add it to a file and set the line to None Nowhere None.
Which looks like this in the file:
BotName None Nowhere None
Now if it were in an INI file, it would look like this:
[botname]
charname=None
location=Nowhere
action=None
Now when a user types !bots, I want it to list all the bots in the ini file, this is where I get stuck, I have no idea how to get an undetermined amount of bots out of an INI file. If anyone can give me some help, would be greatly appreaciated.
proc parseIni {file} {
array set arr {}
set f [open $file]
while {[gets $f l]>-1} {
set l [string trim $l]
if {[string match {\[*\]} $l]} {
set e [string range $l 1 end-1]
} elseif {[info exists e]&&[string match *?=?* $l]} {
regexp {^([^=]+)=(.+)$} $l a a b
lappend arr($e) [string trim $a] [string trim $b]
} else {
# what to do? (invalid/empty line)
}
}
close $f
array get arr
}
# The list returned by this proc is really a multidimensional array
# Access settings like this:
array set myIni [parseIni your.ini]
array set tmpBot $myIni(botname)
# Now tmpBot is a array with element names like the ones in your ini file :)
# eg $tmpBot(charname)
proc parseIni {file} {
array set arr {}
set f [open $file]
while {[gets $f l]>-1} {
set l [string trim $l]
if {[string match {\[*\]} $l]} {
set e [string range $l 1 end-1]
} elseif {[info exists e]&&[string match *?=?* $l]} {
regexp {^([^=]+)=(.+)$} $l a a b
lappend arr($e) [string trim $a] [string trim $b]
} else {
#I'm stuck here, not exatly sure what needs to go here
}
}
close $f
array get arr
}
bind pub n "!test" mytestproc
proc mytestproc {nick uhost hand chan args} {
array set myIni [parseIni bots.ini]
## Don't know how to set all this up
array set tmpBot $myIni(botname)
putserv "PRIVMSG $chan :dunno what to put here"
return 0
}
If your .ini contains what you pasted, that code will not produce the error you pasted.
Let me explain what's happening in the code... The proc returns a list of lists...when you use 'array set' you create an array with element names being the botnames from the ini and the elements will contain the sublists with the botsettings. To retrieve the settings for a particular bot, just fetch the element with the name of that bot. If you want to list the botnicks from the ini, use [array names theArray]. To give you a better example I need to know what you want to do.
Darkj wrote:And i'm stuck on that invalid/empty line stuff.
You don't need to care about that part if you don't want error handling (you can remove the entire "else" part of that if statement. I only included it in case you needed/wanted something like that )