egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Deleting a line from a file - problem with special chars

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Fri Jun 15, 2007 7:02 am    Post subject: Deleting a line from a file - problem with special chars Reply with quote

Here is a script I coded recently, but I'm having problems for nicks with special characters. When I delete a nick with a special character:

1) It says nick not found in the database (text file)
2) The database file containing all the nicks becomes empty.

I need these two errors to be fixed if possible, for the script to work fine. Can anyone help me to rectify these errors if possible?

Here is the testing I did:
Quote:

<awyeah> .aovadd AhB
<adapter> AhB has been successfully added to the AOv list.
* adapter sets mode: +v-k AhB Added.to.AOv.list

<awyeah> .aovdel AhB
<adapter> AhB has been successfully removed from the AOv list.
* adapter sets mode: -vk AhB Removed.from.AOv.list


Quote:

<awyeah> .aovadd [Nadia]
* adapter sets mode: +v-k [Nadia] Added.to.AOv.list

<awyeah> .aovdel [Nadia]
<adapter> [Nadia] was not found in the AOv list.

<awyeah> .aovlist
-adapter- AOv list for #pak
-adapter- 1 - \[Nadia\] (aov@pak.dal.net)
-adapter- End of list.

Text file contains this entry:
[Nadia]:#pak


Quote:

adapter- AOv list for #pak
-adapter- 1 - \[Nadia\] (aov@klsex.dal.net)
-adapter- 2 - \[DeV-Kl\] (aov@klsex.dal.net)
-adapter- End of list.

<awyeah> .delaov [Nadia]
<adapter> [Nadia] was not found in the AOv list of #pak.

<awyeah> .listaov
-adapter- AOv list for #pak
-adapter- End of list.


Is my code okay and proper for deleting a specified nick from the database file? Also and suggestions on how to fix the script, so it deletes nicks with special characters from the database file??

Here is my code:
Code:

proc aov:add {nick uhost hand chan arg} {
 global aov sov
 set arg [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $arg]
 if {$arg != ""} {
  set fp [open $sov r+]
  set data [read -nonewline $fp]
  close $fp
  set list [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $data]]
  if {[lsearch -exact [string tolower $list] [string tolower [lindex $arg 0]]:[string tolower $chan]] != -1} {
   putserv "PRIVMSG $chan :You are already a SOv on [string tolower $chan]. You cannot add yourself to the AOv list of [string tolower $chan]."
   return 0
  }
  if {([lsearch -exact [string tolower $list] [string tolower $nick]:[string tolower $chan]] != -1) || [matchattr $hand mn|mn $chan]} {
   set user [lindex $arg 0]:[string tolower $chan]
   set fp [open $aov r+]
   set data [read -nonewline $fp]
   close $fp
   set list2 [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $data]]
   if {[lsearch -exact [string tolower $list2] [string tolower $user]] == -1} {
    set file [open $aov a+]
    puts $file $user
    close $file
    putserv "PRIVMSG $chan :[lindex $arg 0] has been successfully added to the AOv list of [string tolower $chan]."
    utimer 3 [list aov:on:add [lindex $arg 0] $chan]
    return 0
   } else {
     putserv "PRIVMSG $chan :[lindex $arg 0] already exists on the AOv list of [string tolower $chan]."
     return 0
     }
   } else {
      putserv "PRIVMSG $chan :You do not have access to modify the AOv list of [string tolower $chan]."; return 0
      }
   }
}

proc aov:delete {nick uhost hand chan arg} {
 global aov sov
 set arg [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $arg]
 if {[info exists list3]} { unset list3 }
 if {$arg != ""} {
  set fp [open $sov r+]
  set data [read -nonewline $fp]
  close $fp
  set list [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $data]]
  set fp [open $aov r+]
  set data [read -nonewline $fp]
  close $fp
  set list2 [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $data]]
  if {([lsearch -exact [string tolower $list] [string tolower $nick]:[string tolower $chan]] != -1) || ([lsearch -exact [string tolower $list2] [string tolower $nick]:[string tolower $chan]] != -1) || [matchattr $hand mn|mn $chan]} {
   set user [lindex $arg 0]:[string tolower $chan]
   set fp [open $aov r+]
   set data [read -nonewline $fp]
   close $fp
   set list3 [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $data]]
   set fd [open $aov w+]
   if {[set i [lsearch -exact [string tolower $list3] [string tolower $user]]] != -1} {
     set fd [open $aov w+]
     puts -nonewline $fd [join [lreplace $list3 $i $i] \n]
     close $fd
     putserv "PRIVMSG $chan :[lindex $arg 0] has been successfully removed from the AOv list of [string tolower $chan]."
     utimer 3 [list aov:on:del [lindex $arg 0] $chan]
   } else {
     putserv "PRIVMSG $chan :[lindex $arg 0] was not found in the AOv list of [string tolower $chan]."
     return 0
     }
   } else {
     putserv "PRIVMSG $chan :You do not have access to modify the AOv list of [string tolower $chan]."
    }
   if {[info exists list3]} { close $fd; unset list3; return 0 }
  }
}


Also a similar problem here, when I try to delete more than 1 line from the text file, the whole file gets erased. This is the code for deleting more than 1 line from the database:

Code:

proc sov:wipe {nick uhost hand chan arg} {
 global aov sov
  set fp [open $sov r+]
  set data [read -nonewline $fp]
  close $fp
  set list [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $data]]
  if {[matchattr $hand mn|mn $chan]} {
  set fd [open $sov w+]
  set list3 [list]
   foreach user $list {
    if {[set i [lsearch -exact [string tolower $list] [string tolower $user]]] != -1} {
     set list3 [lreplace $list3 $i $i]
     }
   }
   puts -nonewline $fd [join $list3 "\n"]
   putserv "PRIVMSG $chan :The SOv list of [string tolower $chan] has been wiped successfully."
   close $fd
  }
  set fp [open $aov r+]
  set data [read -nonewline $fp]
  close $fp
  set list2 [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} $data]]
  if {([lsearch -exact [string tolower $list] [string tolower $nick]:[string tolower $chan]] != -1) || ([lsearch -exact [string tolower $list2] [string tolower $nick]:[string tolower $chan]] != -1)} {
   putserv "PRIVMSG $chan :You do not have access to wipe the SOv list of [string tolower $chan]."
   return 0
   }
}

_________________
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Fri Jun 15, 2007 10:06 pm    Post subject: Reply with quote

Get rid of the string map kludge stuff and use [split] and [join] properly like you should. There is a file that explains how to make scripts tcl-special-char safe, check around the forum or google it.
Back to top
View user's profile Send private message
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Sat Jun 16, 2007 5:42 am    Post subject: Reply with quote

So all the elements in the database are deleted due to the special character thing? It's not cause of a faulty coding in the delete proc? Hmm okay, i'll look about split and join, thanks!
_________________
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Sat Jun 16, 2007 4:19 pm    Post subject: Reply with quote

1) First I used split on $arg to convert it into a list.
2) Then and the user i want to add, delete or even match in the database (list), i convert that into a string either by using split or just even by string tolower (when im using lsearch with the option -exact to match the element in the list -- so should be in the same case).

But it still doesn't seem to work. When I try to delete a nick with a special character, say [, ] etc it will just delete all the users in the database and generate and empty text file.

I think maybe there is a problem maybe with my proc which is deleting the users from the database then maybe (aov:del). Can anyone have a look and tell me where the problem might be maybe, or is it still the special characters? I've used join and split as instructed and even user a filter before for special characters (string map, regsub). Thanks!

Here is the code:
Code:

proc aov:add {nick uhost hand chan arg} {
 global aov sov
 set arg [split $arg]
 if {$arg != ""} {
  set fp [open $sov r+]
  set data [read -nonewline $fp]
  close $fp
  set list [split $data "\n"]
  if {[lsearch -exact [split [string tolower $list]] [string tolower [lindex $arg 0]:$chan]] != -1} {
   putserv "PRIVMSG $chan :You are already a SOv on \002[string tolower $chan]\002. You cannot add yourself to the AOv list of \002[string tolower $chan]\002."
   return 0
  }
  if {([lsearch -exact [split [string tolower $list]] [string tolower $nick:$chan]] != -1) || [matchattr $hand mn|mn $chan]} {
   set user [string tolower [lindex $arg 0]:$chan]
   set fp [open $aov r+]
   set data [read -nonewline $fp]
   close $fp
   set list2 [split $data "\n"]
   if {[lsearch -exact [split [string tolower $list2]] $user] == -1} {
    set file [open $aov a+]
    puts $file $user
    close $file
    putserv "PRIVMSG $chan :\002[lindex [split $arg] 0]\002 has been successfully added to the AOv list of \002[string tolower $chan]\002."
    utimer 3 [list aov:on:add [lindex [split $arg] 0] $chan]
    return 0
   } else {
     putserv "PRIVMSG $chan :\002[lindex [split $arg] 0]\002 already exists on the AOv list of \002[string tolower $chan]\002."
     return 0
     }
   } else {
      putserv "PRIVMSG $chan :You do not have access to modify the AOv list of \002[string tolower $chan]\002."; return 0
      }
   }
}

proc aov:delete {nick uhost hand chan arg} {
 global aov sov
 set arg [split $arg]
 if {$arg != ""} {
  set fp [open $sov r+]
  set data [read -nonewline $fp]
  close $fp
  set list [split $data "\n"]
  set fp [open $aov r+]
  set data [read -nonewline $fp]
  close $fp
  set list2 [split $data "\n"]
  if {([lsearch -exact [split [string tolower $list]] [string tolower $nick:$chan]] != -1) || ([lsearch -exact [split [string tolower $list2]] [string tolower $nick:$chan]] != -1) || [matchattr $hand mn|mn $chan]} {
   set user [string tolower [lindex $arg 0]:$chan]
   set fd [open $aov w+]
   if {[set i [lsearch -exact [split [string tolower $list2]] $user]] != -1} {
     set fd [open $aov w+]
     puts -nonewline $fd [join [lreplace $list2 $i $i] \n]
     close $fd
     putserv "PRIVMSG $chan :\002[lindex [split $arg] 0]\002 has been successfully removed from the AOv list of \002[string tolower $chan]\002."
     utimer 3 [list aov:on:del [lindex $arg 0] $chan]
   } else {
     putserv "PRIVMSG $chan :\002[lindex [split $arg] 0]\002 was not found in the AOv list of \002[string tolower $chan]\002."
     return 0
     }
   } else {
     putserv "PRIVMSG $chan :You do not have access to modify the AOv list of \002[string tolower $chan]\002."
    }
  }
}


Tried this also:
Code:

set arg [split $arg]
set user [join [string tolower [lindex $arg 0]:$chan]]
###OR###
set user [join [lindex $arg 0]:$chan]

Since I think when using lsearch $list should be split, but the *pattern* can be a string, as I checked in the tcl manual, hence [join] and [string tolower] both produce a string.

_________________
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sat Jun 16, 2007 9:07 pm    Post subject: Reply with quote

You have problem with using strings and lists. You're creating a list called $list and then you apply [string tolower] over it - bad idea. How about:
Code:
set list [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} [string tolower $data]]]

and now you have a list of lower-case elements.

Hint: Apply the string match over the elements you want to use in the <match> and not over the string that you're matching from. (i.e. string match <match> <match-string>) apply the string map on <match>.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Sun Jun 17, 2007 3:59 am    Post subject: Reply with quote

But here I am not using string match, I am using lsearch. So u mean instead of lsearch I should use string match/equal with a foreach loop?

Something like this is it?
Code:

set arg [split $arg]
set fp [open $sov r+]
set data [read -nonewline $fp]
close $fp
set list [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} [string tolower $data]]]
foreach person $list {
if {[string match -nocase *$person* [lindex $arg 0]:$chan]} {
 set matchfound 1; break
 }
}

###OR###

set arg [split $arg]
set fp [open $sov r+]
set data [read -nonewline $fp]
close $fp
set list [split [string map {\\ \\\\ [ \\\[ ] \\\] \{ \\\{ \} \\\}} [string tolower $data]]]
if {[string match -nocase *[lindex $arg 0]:$chan* $list]} {
 #do stuff
}


Okay, I'll give it a go, thanks!
_________________
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Sun Jun 24, 2007 7:08 am    Post subject: Reply with quote

Thanks for the help. I was able to solve the problem, using string match instead of lsearch.

The only thing to be done was to split the input argument and to use lindex for string equal with the nick:chan variable.

For the second part to delete a line, lists have to be used. So I converted the match into a string, and then split the list and also split the match when using lsearch.

Basically the filter was causing the problem, which I was able to solve using the split, join and string tolower commands.

The code to one of the working procs:

Code:

proc aov:delete {nick uhost hand chan arg} {
 global aov sov
 set arg [split $arg]
 set chan [string tolower $chan]
 if {$arg == ""} { putserv "PRIVMSG $chan :Usage: !delaov <nick>"; return 0 }
  set fp [open $sov r+]
  set data [read -nonewline $fp]
  close $fp
  set sovlist [split $data "\n"]
  set fp [open $aov r+]
  set data [read -nonewline $fp]
  close $fp
  set aovlist [split $data "\n"]

  #check if user has access level sov or founder
  if {[matchattr $hand mn|mn $chan]} { set founder:ok 1 }
  foreach person $sovlist {
   if {[string equal -nocase "$nick:$chan" $person]} {
    set sov:ok 1; break
    }
  }
  foreach person $aovlist {
   if {[string equal -nocase "$nick:$chan" $person]} {
    set aov:ok 1; break
    }
  }
  #if access level not sov or founder stop
  if {![info exists founder:ok] || ![info exists sov:ok] || ![info exists aov:ok]} {
   putserv "NOTICE $nick :You do not have access to modify the AOv list of $chan."
   return 0
  }

  #check if access level is aov
  foreach person $aovlist {
   if {[string equal -nocase "$nick:$chan" $person]} {
    set aov:user 1; break
    }
  }
  #user has aov access
  if {[info exists aov:user]} {
   #if argument contains same nick as user
   if {[string equal -nocase "[lindex $arg 0]:$chan" $nick:$chan]} {
    #del nick from aov list
    set user [string tolower [lindex $arg 0]]:$chan
    set i [lsearch -exact [split [string tolower $aovlist]] [split $user]]
    set fd [open $aov w+]
    puts -nonewline $fd [join [lreplace $aovlist $i $i] "\n"]
    close $fd
    putserv "PRIVMSG $chan :[lindex $arg 0] has been successfully removed from the AOv list of $chan."
    utimer 2 [list aov:on:del [lindex $arg 0] $chan]
    return 0
   } else {
    putserv "NOTICE $nick :You do not have access to modify the AOv list of $chan."
    return 0
    }
  }

  #check if user is in sov or founder
  foreach person $sovlist {
   if {[string equal -nocase "$nick:$chan" $person]} {
    set sov:found 1; break
    }
  }
  #if user has sov or founder access level
  if {[info exists sov:found] || [matchattr $hand mn|mn $chan]} {
  #check if aov is in list or not
   foreach person $aovlist {
    if {[string equal -nocase "[lindex $arg 0]:$chan" $person]} {
    set aov:found 1; break
    }
   }
  #if aov is not in list stop
  if {![info exists aov:found]} {
   putserv "PRIVMSG $chan :[lindex $arg 0] was not found in the AOv list of $chan."
   return 0
  #if aov is in list then delete
  } else {
   set user [string tolower [lindex $arg 0]]:$chan
   set i [lsearch -exact [split [string tolower $aovlist]] [split $user]]
   set fd [open $aov w+]
   puts -nonewline $fd [join [lreplace $aovlist $i $i] "\n"]
   close $fd
   putserv "PRIVMSG $chan :[lindex $arg 0] has been successfully removed from the AOv list of $chan."
   utimer 2 [list aov:on:del [lindex $arg 0] $chan]
   return 0
  }
 }
}

_________________
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber