This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Unable to find fault with the code. ini writing

Help for those learning Tcl or writing their own scripts.
Post Reply
W
Wannabe
Voice
Posts: 17
Joined: Fri Feb 10, 2006 1:02 pm

Unable to find fault with the code. ini writing

Post by Wannabe »

Ok, ive downloaded the ini database script written by mamaKIN, who i cant seem to contact (e-mail bounces, never on irc)

using his script (which ill post here too) ive updated my own script to use ini files. but now i get an odd result when running the !add command. ill post an example of the ini file after only ONE add.

ignore the # on all the blank lines, its from the pastebin i copied this code back from.

Anyone able to work out why it stores multiple times and suggest a fix. i will be very happy with :)

Code: Select all


#bind msg - !help addme_help
#bind msg - help addme_help
#bind pub - !help addme_help_pub
#bind msg - !removeall addme_removeall
#bind msg - removeall addme_removeall
#bind pub - !removeall addme_removeall_pub
bind pub - !add addg_addguild
bind pub - !addme addg_addguild
bind pub - !clearall clearall
bind pub - !match addg_match
bind pub - !readall readall
bind pub - !remove addg_removename
bind pub - !removeme addg_removename
#bind pub - !rappel addme_recall
#bind pub - !recall addme_recall
bind pub - !motd addme_updatemotd
#bind pub - !stats addme_gamecount

#bind join - * addme_welcome
# on very populated channels, you may want to comment out this line,
# so the bot wont keep looking at people joining.
# If so, add a static welcome message to another bot, or to channel service
# (ie X on undernet, Q or L on quakenet ...)
bind part - * addg_part
bind sign - * addg_part
bind kick - * addg_kick
bind nick - * addg_nick
#bind msg - addme_version addme_version



#init
set addg_channels "#guildwars.pvp"
set addg_motd "Guildwars PvP gather Beta 1"
set addg_number 0
set inifile "data/guild.ini"





proc addg_addguild {nick mask hand chan text} {
putlog "In the proc addguild"
#add player to array and incr counter is supposed to do so

       	global inifile
	global addg_channels
	global addg_motd
	global addg_number
putlog "pasted the globals, now checking channel"
# if {![regexp $chan $addg_channels]} {
#		putlog "Wrong chan"
#               return 0
#	}

	if {$text == "" || $text == "{}" || ![info exists text]} {putquick "NOTICE $nick :Invalid Syntax, See !help command \002$nick\002 Error Number 1"}
        set leader $nick
        putlog "leader set to $leader"
        set guild [lindex $text 0]
        putlog "guild set to $guild"
        set rank [lindex $text 1]
        putlog "rank set to $rank"
        if {![info exist leader] || ![info exist guild] || ![info exists rank]} {putquick "NOTICE $nick :Invalid Syntax, See !help command \002$nick\002 Error Number 2"}
        
        if {$rank < 10 } {
           set bracket 1
           putlog "Bracket 1"
        } elseif { $rank > 10 && $rank < 50 } {
           set bracket 2
           putlog "Bracket 2"
        } elseif { $rank > 50 && $rank < 100 } {
           set bracket 3
           putlog "Bracket 3"
        } elseif { $rank > 100 && $rank < 500 } {
           set bracket 4
           putlog "Bracket 4"
        } elseif { $rank > 500 && $rank < 1000 } {
           set bracket 5
           putlog "Bracket 5"
        } elseif { $rank > 1000 && $rank < 5000 } {
           set bracket 6
           putlog "Bracket 6"
        } elseif { $rank > 5000 } {
           set bracket 7
           putlog "Bracket 7"
        }


        if {![nickfind $nick]} {
           putlog "storing..."
           lappend nicklist $nick
           ini_write $inifile $nick rank $rank
           ini_write $inifile $nick guild $guild
           ini_write $inifile $nick bracket $bracket
           ini_write $inifile added leaders $nicklist
           #set guildinfo($leader,rank) $rank
           #set guildinfo($leader,guild) $guild
           #set guildinfo($leader,bracket) $bracket
           putlog "done, now changing topic"
           incr addg_number
           addg_updatetopic $addg_number $addg_motd
        } else {
           putlog "the value of is nick [isnick $nick]"
           putquick "NOTICE $nick :Your already stored"
        }
}

proc addg_removename  {nick mask hand chan text} {
# Call the internal proc remove when someone types !remove, if supposed to do so

	global botnick
	global inifile

	if {$text == "" || $text == "{}" || ![info exists text]} {set text $nick}

		if {![isop $nick $chan]} {
			putquick "NOTICE $nick :Sorry \002$nick\002, admin command : you need to be op"
			return 0
		} else {
                       addg_remove $nick
                }
}

proc addg_remove {nick} {
global addg_motd
global addg_number
global inifile
       if {[file exist $inifile]} {
           ini_remove $inifile $nick ""
           set ulist [ini_read $inifile added leaders]
           set uindex [lsearch $ulist $nick]
           set newlist [lreplace $ulist $uindex $uindex]
           ini_write $inifile added leaders $newlist
           set newnum [$addg_number - 1]
           addg_updatetopic $newnum $addg_motd
       } else {
           putquick "NOTICE $nick : Your not added, please view !help"
       }
}

proc nickfind {nick} {
global inifile

set isthere [ini_read $inifile added leaders]
foreach name $isthere {
if {$name == $nick} {
 return 1
} else {
 return 0
}
}

}




proc isnick {nick} {
     putlog "nick is $nick"
     putlog "checking if nick is added or file exists"
     global inifile

     if {[file exist $inifile]} {
       if {[ini_read $inifile $nick rank] == ";"} {
           putlog "was not found"
           return 0
        } else {
           putlog "Found"
           return 1
        }
     } else {
     return 0
     }
}

proc addg_nick {nick host hand chan newnick} {
# When someone listed in topic changes nick (Tom -> Tom|AFK), changes topic to match new nick

	global botnick
	global addg_channels

	#if {[regexp $chan $addg_channels]} { }
	#addg_getvars $chan

		if {[isnick $nick] == 1} {
			remove($nick)
			putquick "NOTICE $nick : Changing your nickname has removed you from the list, please re-add"
		}
}


proc addg_part {nick host hand chan text} {
# Remove nick from players list if they leave channel or irc server

	global botnick
	global addg_channels

        if {[regexp $chan $addg_channels]} {
		addg_getvars $chan

		if {[isnick $nick] == 1} {
			remove($nick)
		}
	}
}

proc addg_kick {nick host hand chan target reason} {
# Remove nick from players list if they leave channel or irc server

	global botnick
	global addg_channels

	        if {[regexp $chan $addg_channels]} {
		addg_getvars $chan

		if {[isnick $nick] == 1} {
			remove($nick)
		}
	}
}

proc addg_updatetopic {number motd} {
# Internal proc modify topic with new arguments (chan, Guild number and motd)


	set newtopic "Currently $number of guilds waiting for a game \0037 $motd"
	puthelp "TOPIC #guildwars.pvp : $newtopic"
}

proc addg_match { nick host hand chan text } {

global inifile
set randum 0
set found 0
    if {[file exist $inifile]} {
       if {[ini_read $inifile $nick rank] == ";"} {
          putquick "NOTICE $nick :Please add yourself before attempting the !match command, see !help"
       } else {
          putlog "Found"
          set matchrank [ini_read $inifile $nick rank]
          set $found 1
       }
    }

set ulist [ini_read $inifile added leaders]
set num 0
foreach name $ulist {
if{[ini_read $inifile $name bracket] == $matchrank} {
             set unames($num) $name
}
set num $num+1
}


set randem [rand [array size unames]]
set result $unames($randem)
if {$randem == 0} {
putquick "NOTICE $nick : Sorry An error has accured please contact Syntax-error Error invalid Array at rand"
} else {
putquick "NOTICE $nick : Match found against $result"
putquick "NOTICE $result : Match found against $nick"
}
}

proc readall { nick host hand chan text } {
global inifile

foreach guild [array names guildinfo] {
 putquick "NOTICE $nick : $guildinfo($guild,rank)"
 putquick "NOTICE $nick : $guildinfo($guild,guild)"
 putquick "NOTICE $nick : $guildinfo($guild,bracket)"
 }
}

proc clearall { nick host hand chan text } {
global inifile
unset guildinfo
putlog "Array is gone"
}




Code: Select all

####################
## INI Database v0.4.2 [ DEC/16/1999 ]
##  By mamaKIN (mamakin@mailandnews.com)
##
##  Thanks to Amadeus` for spotting some important issues
##
## Visit http://mort.level5.net/~mamakin/scripts/eggdrop/inidb.html for updates
##
## Description:
##   INI Datbase allows people to easily store settings and easily access them.
##   * INI Database is for TCL scripters only *
##
##-----------------------------------------------------------------------------
##   PROCEDURE:   ini_read <ini file> <section> <item>
##   DESCRIPTION: Reads the value of an item from the specified section in the
##                ini file.
##   RETURNS:     the value of the item if successful; semi-colon ';' otherwise
##                (semi-colon because any other characters could be used as the
##                  value, so use "\;" when comparing ini_read's return value)
##-----------------------------------------------------------------------------
##   PROCEDURE:   ini_write <ini file> <section> <item> [value]
##   DESCRITPION: Sets the value of item in the specified section in the ini
##                file.
##   RETURNS:     TRUE (1) if successful; FALSE (0) otherwise
##-----------------------------------------------------------------------------
##   PROCEDURE:   ini_remove <ini file> <section> [item]
##   DESCRIPTION: Removes a section of the ini file or an item of the section.
##                Use "" in place of the item variable if you wish to
##                exclude it.
##                ( Use "file delete" if you want to remove the ini file )
##   RETURNS:     TRUE (1) if successful; FALSE (0) otherwise
##-----------------------------------------------------------------------------
####################

set inidb_ver "{INI Database v0.4.2} {04200!00} {6447b} {945384656} {mamaKIN}"

proc ini_read {inifile section item} {
  set item [lindex $item 0]
  if {[lindex $inifile 0] == "" || [lindex $section 0] == "" || [lindex $item 0] == ""} { return "\;" }
  if {![file exists $inifile]} { return "\;" }
  set fileo [open $inifile r]
  set sect ""
  while {![eof $fileo]} {
    set rline [gets $fileo]
    set rline [string trim $rline]
    if {$rline != "" || [string index $rline 0] != "\;"} {
      if {[string index $rline 0] == "\[" && [string index $rline [expr [string length $rline] - 1]] == "\]"} {
        set sect [string range $rline 1 [expr [string length $rline] - 2]]
      } elseif {[string tolower $sect] == [string tolower $section]} {
        set im [string tolower [string range $rline 0 [expr [string first = $rline] - 1]]]
        set va [string range $rline [expr [string first = $rline] + 1] end]
        set itm(${im}) $va
      }
    }
  }
  if {[array names itm [string tolower $item]] == ""} { return "\;" }
  set rtrn [set itm([string tolower $item])]
  return $rtrn
}

proc ini_write {inifile section item value} {
  set section [lindex [string tolower $section] 0]
  if {[lindex $inifile 0] == "" || [lindex $section 0] == "" || [lindex $item 0] == ""} { return 0 }
  if {![file exists $inifile] || [file size $inifile] == 0} {
    set filew [open $inifile w]
    puts $filew "\[$section\]"
    puts $filew "[string tolower $item]=$value"
    close $filew; return 1
  }
  set fileo [open $inifile r]
  set cursect ""; set sect ""
  while {![eof $fileo]} {
    set rline [string trim [gets $fileo]]
    if {$rline != "" || [string index $rline 0] != "\;"} {
      if {[string index $rline 0] == "\[" && [string index $rline [expr [string length $rline] - 1]] == "\]"} {
        set cursect [string tolower [string range $rline 1 [expr [string length $rline] - 2]]]
        lappend sect $cursect
      } {
        set im [string tolower [string range $rline 0 [expr [string first = $rline] - 1]]]
        set vl [string range $rline [expr [string first = $rline] + 1] end]
        lappend [join "ini $cursect" ""]($im) $vl
      }
    }
  }
  close $fileo; unset fileo
  if {[lsearch $sect $section] == -1} { lappend sect $section }
  set [join "ini $section" ""]([string tolower $item]) $value
  set fileo [open $inifile w]
  foreach sct $sect {
    puts $fileo "\[$sct\]"
    foreach ite [array names [join "ini $sct" ""]] {
      set ite [lindex $ite 0]
      set valu [set [join "ini $sct" ""]($ite)]
      if {$ite != ""} {
        puts $fileo "$ite=[join $valu]"
      }
    }
    puts $fileo ""
  }
  close $fileo
  return 1
}

proc ini_remove { inifile section item } {
  set section [lindex [string tolower $section] 0]
  set item [lindex [string tolower $item] 0]
  if {[lindex $inifile 0] == ""} { return 0 }
  if {![file exists $inifile]} { return 0 }
  if {$section == ""} { return 0 }
  set fileo [open $inifile r]
  set cursect ""; set sect ""
  while {![eof $fileo]} {
    set rline [string trim [gets $fileo]]
    if {$rline != "" || [string index $rline 0] != "\;"} {
      if {[string index $rline 0] == "\[" && [string index $rline [expr [string length $rline] - 1]] == "\]"} {
        set cursect [string tolower [string range $rline 1 [expr [string length $rline] - 2]]]
        lappend sect $cursect
      } {
        set im [string tolower [string range $rline 0 [expr [string first = $rline] - 1]]]
        set vl [string range $rline [expr [string first = $rline] + 1] end]
        lappend [join "ini $cursect" ""]($im) $vl
      }
    }
  }
  close $fileo; unset fileo
  set sesect [lsearch $sect $section]
  if {$sesect == -1} {
    return 0
  } {
    if {$item == ""} { set sect [lreplace $sect $sesect $sesect] }
  }
  set seitem [lsearch [array names [join "ini $section" ""]] $item]
  if {$seitem != -1} {
    unset [join "ini $section" ""]($item)
    if {[llength [array names [join "ini $section" ""]]] == 1} {
      set sect [lreplace $sect $sesect $sesect]
    }
  }
  if {[llength $sect] == 0} { file delete $inifile; return 1 }
  set fileo [open $inifile w]
  foreach sct $sect {
    puts $fileo "\[$sct\]"
    foreach ite [array names [join "ini $sct" ""]] {
      set ite [lindex $ite 0]
      set valu [set [join "ini $sct" ""]($ite)]
      if {$ite != "" && [lindex $valu 0] != ""} {
        puts $fileo "$ite=[join $valu]"
      }
    }
    puts $fileo ""
  }
  close $fileo
  return 1
}

if {[info exists version]} {
  putlog "Loaded [lindex $inidb_ver 0] by [lindex $inidb_ver 4]"
}


And this is the ini file after a single add

Code: Select all


   1.
      [syn[coding]]
   2.
      rank=1234 1234 1234 1234 1234 1234
   3.
      bracket=6 6 6
   4.
      guild=myguild myguild myguild myguild myguild myguild
   5.
       
   6.
      [syn[coding]]
   7.
      rank=1234 1234 1234 1234 1234 1234
   8.
      bracket=6 6 6
   9.
      guild=myguild myguild myguild myguild myguild myguild
  10.
       
  11.
      [syn[coding]]
  12.
      rank=1234 1234 1234 1234 1234 1234
  13.
      bracket=6 6 6
  14.
      guild=myguild myguild myguild myguild myguild myguild
  15.
       
  16.
      [added]
  17.
      leaders=syn[coding] 

Last edited by Wannabe on Fri Feb 10, 2006 7:36 pm, edited 2 times in total.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

I don't know about the others, but the code looks ugly with all those #'s. So I suggest you fix it.
W
Wannabe
Voice
Posts: 17
Joined: Fri Feb 10, 2006 1:02 pm

Post by Wannabe »

Ok fixed sorry about that
O
OpEn_FiRe
Voice
Posts: 2
Joined: Sun Jan 29, 2006 10:10 am

Post by OpEn_FiRe »

I have a found a version newer than your for inidb.tcl.
I didnt really take a look at your scripts or even checked if the inidb you have to see if it is differecnt from this version i found. but u can always give it a try.

Code: Select all

####################
## INI Database v0.4.3 [ JUN/29/2001 ]
##  By mamaKIN (mamakin@mailandnews.com)
##  By Ze (mgm@ifrance.com) (v0.4.3)
##  $Id: inidb.tcl,v 1.2 2001/09/29 18:22:39 Ze Exp $
##
##  Thanks to Amadeus` for spotting some important issues
##
## Visit http://mort.level5.net/~mamakin/scripts/eggdrop/inidb.html for updates
##
## Description:
##   INI Datbase allows people to easily store settings and easily access them.
##   * INI Database is for TCL scripters only *
##
##-----------------------------------------------------------------------------
##   PROCEDURE:   ini_read <ini file> <section> <item>
##   DESCRIPTION: Reads the value of an item from the specified section in the
##                ini file.
##   RETURNS:     the value of the item if successful; semi-colon ';' otherwise
##                (semi-colon because any other characters could be used as the
##                  value, so use "\;" when comparing ini_read's return value)
##-----------------------------------------------------------------------------
##   PROCEDURE:   ini_write <ini file> <section> <item> [value]
##   DESCRITPION: Sets the value of item in the specified section in the ini
##                file.
##   RETURNS:     TRUE (1) if successful; FALSE (0) otherwise
##-----------------------------------------------------------------------------
##   PROCEDURE:   ini_remove <ini file> <section> [item]
##   DESCRIPTION: Removes a section of the ini file or an item of the section.
##                Use "" in place of the item variable if you wish to
##                exclude it.
##                ( Use "file delete" if you want to remove the ini file )
##   RETURNS:     TRUE (1) if successful; FALSE (0) otherwise
##-----------------------------------------------------------------------------
##   PROCEDURE:   ini_get_section_list <ini file>
##   DESCRIPTION: Return the list of all section who are present in 'ini file'
##   RETURNS:     A list. "" if no section was found.
####################

set inidb_ver "{INI Database v0.4.2} {04200!00} {6447b} {945384656} {mamaKIN}"

proc ini_get_section_list {inifile} {
    if { [lindex $inifile 0] == "" } {
	return ""
    }
    if {![file exists $inifile]} { return "\;" }
    set fileo [open $inifile r]
    set sect_list ""
    while {![eof $fileo]} {
	set rline [gets $fileo]
	set rline [string trim $rline]
	if {[string index $rline 0] == "\[" && [string index $rline [expr [string length $rline] - 1]] == "\]"} {
	    lappend sect_list [string range $rline 1 [expr [string length $rline] - 2]]
	}
    }
    close $fileo
    return sect_list
}

proc ini_read {inifile section item} {
  set item [lindex $item 0]
  if {[lindex $inifile 0] == "" || [lindex $section 0] == "" || [lindex $item 0] == ""} { return "\;" }
  if {![file exists $inifile]} { return "\;" }
  set fileo [open $inifile r]
  set sect ""
  while {![eof $fileo]} {
    set rline [gets $fileo]
    set rline [string trim $rline]
    if {$rline != "" || [string index $rline 0] != "\;"} {
      if {[string index $rline 0] == "\[" && [string index $rline [expr [string length $rline] - 1]] == "\]"} {
        set sect [string range $rline 1 [expr [string length $rline] - 2]]
      } elseif {[string tolower $sect] == [string tolower $section]} {
        set im [string tolower [string range $rline 0 [expr [string first = $rline] - 1]]]
        set va [string range $rline [expr [string first = $rline] + 1] end]
        set itm(${im}) $va
      }
    }
  }
  if {[array names itm [string tolower $item]] == ""} { 
      close $fileo
      return "\;" 
  }
  set rtrn [set itm([string tolower $item])]
  close $fileo
  return $rtrn
}

proc ini_write {inifile section item value} {
  set section [lindex [string tolower $section] 0]
  if {[lindex $inifile 0] == "" || [lindex $section 0] == "" || [lindex $item 0] == ""} { return 0 }
  if {![file exists $inifile] || [file size $inifile] == 0} {
    set filew [open $inifile w]
    puts $filew "\[$section\]"
    puts $filew "[string tolower $item]=$value"
    close $filew; return 1
  }
  set fileo [open $inifile r]
  set cursect ""; set sect ""
  while {![eof $fileo]} {
    set rline [string trim [gets $fileo]]
    if {$rline != "" || [string index $rline 0] != "\;"} {
      if {[string index $rline 0] == "\[" && [string index $rline [expr [string length $rline] - 1]] == "\]"} {
        set cursect [string tolower [string range $rline 1 [expr [string length $rline] - 2]]]
        lappend sect $cursect
      } {
        set im [string tolower [string range $rline 0 [expr [string first = $rline] - 1]]]
        set vl [string range $rline [expr [string first = $rline] + 1] end]
        lappend [join "ini $cursect" ""]($im) $vl
      }
    }
  }
  close $fileo; unset fileo
  if {[lsearch $sect $section] == -1} { lappend sect $section }
  set [join "ini $section" ""]([string tolower $item]) $value
  set fileo [open $inifile w]
  foreach sct $sect {
    puts $fileo "\[$sct\]"
    foreach ite [array names [join "ini $sct" ""]] {
      set ite [lindex $ite 0]
      set valu [set [join "ini $sct" ""]($ite)]
      if {$ite != ""} {
        puts $fileo "$ite=[join $valu]"
      }
    }
    puts $fileo ""
  }
  close $fileo
  return 1
}

proc ini_remove { inifile section item } {
  set section [lindex [string tolower $section] 0]
  set item [lindex [string tolower $item] 0]
  if {[lindex $inifile 0] == ""} { return 0 }
  if {![file exists $inifile]} { return 0 }
  if {$section == ""} { return 0 }
  set fileo [open $inifile r]
  set cursect ""; set sect ""
  while {![eof $fileo]} {
    set rline [string trim [gets $fileo]]
    if {$rline != "" || [string index $rline 0] != "\;"} {
      if {[string index $rline 0] == "\[" && [string index $rline [expr [string length $rline] - 1]] == "\]"} {
        set cursect [string tolower [string range $rline 1 [expr [string length $rline] - 2]]]
        lappend sect $cursect
      } {
        set im [string tolower [string range $rline 0 [expr [string first = $rline] - 1]]]
        set vl [string range $rline [expr [string first = $rline] + 1] end]
        lappend [join "ini $cursect" ""]($im) $vl
      }
    }
  }
  close $fileo; unset fileo
  set sesect [lsearch $sect $section]
  if {$sesect == -1} {
    return 0
  } {
    if {$item == ""} { set sect [lreplace $sect $sesect $sesect] }
  }
  set seitem [lsearch [array names [join "ini $section" ""]] $item]
  if {$seitem != -1} {
    unset [join "ini $section" ""]($item)
    if {[llength [array names [join "ini $section" ""]]] == 1} {
      set sect [lreplace $sect $sesect $sesect]
    }
  }
  if {[llength $sect] == 0} { file delete $inifile; return 1 }
  set fileo [open $inifile w]
  foreach sct $sect {
    puts $fileo "\[$sct\]"
    foreach ite [array names [join "ini $sct" ""]] {
      set ite [lindex $ite 0]
      set valu [set [join "ini $sct" ""]($ite)]
      if {$ite != "" && [lindex $valu 0] != ""} {
        puts $fileo "$ite=[join $valu]"
      }
    }
    puts $fileo ""
  }
  close $fileo
  return 1
}

if {[info exists version]} {
  putlog "Loaded [lindex $inidb_ver 0] by [lindex $inidb_ver 4]"
}
W
Wannabe
Voice
Posts: 17
Joined: Fri Feb 10, 2006 1:02 pm

Post by Wannabe »

Still have the same problem i just cant see why,
p
poppabear
Voice
Posts: 2
Joined: Fri Dec 22, 2006 8:06 pm

Post by poppabear »

Wannabe wrote:Still have the same problem i just cant see why,
Well i could tell you one of the problems with the ini_functions are that, if you try and add a "nick" or "section" for the section part and the nick say for exaple has any [] in it then it tries and add to the data base for example: [poppabear] will be written to the database as [[poppabear]] then it tries to interpret the extra [] so basically what i am saying is that the ini_functions script is not special char friendly.

i currently am having the same issues myself, would be nice if someone was able to come out with a new version with this bug fix.
Post Reply