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.

Help with logging script that dupe checks

Help for those learning Tcl or writing their own scripts.
Post Reply
F
Felix2003
Voice
Posts: 24
Joined: Fri Feb 06, 2009 8:19 pm

Help with logging script that dupe checks

Post by Felix2003 »

i was wondering if anybody could help me with a script i found many moons ago

i am currently on a forums that has a private radio for members and we also have an irc server

i wanted to have a channel were users could connect say add song and it gets written to a file i can upload to the djs so they can build a playlist from the songs suggested

i ask members when requesting to say add Artist - song after testing and trying to edit it myself i have failed :( (not the best with tcl)

it writes to the file fine but when checking to see if the artist - song is already in the file it fails unless its one word example song-artists with no spaces

an example of what the script does now

User1: add Mariah Carey - Without You
Bot: Added Mariah Carey - Without You to file
User2: add Mariah Carey - Without You
Bot: Added Mariah Carey - Without You to file

User1: add Mariah.Carey-Without.You
Bot: Added Mariah.Carey-Without.You to file
User2: add Mariah.Carey-Without.You
Bot: Song already in the list please choose another

#################

an example of what i want it to do

User1: add Mariah Carey - Without You
Bot: Added Mariah Carey - Without You to file
User2: add Mariah Carey - Without You
Bot: Song already in the list please choose another




Code: Select all

set datafile "/usr/home/Felix2003/Eggdrop/scripts/data.txt" 

bind pub - add procadd 
bind pub - del procdel 

proc procadd {nick uhost hand chan text} { 
        global datafile 
        set text [split $text] 
        set ahand [lindex $text 0] 

        # check for duplicate songs 
        if {[file exists $datafile]} { 
                set input [open $datafile r] 
                while {![eof $input]} { 
                        set curline [gets $input];set curline [split $curline] 
                        if {[lindex $curline 0] == $text} { 
                                puthelp "PRIVMSG $chan :$text already exists.. Pick another song" 
                                catch {close $input} 
                                return 
                        } 
                } 
                catch {close $input} 
        } 
        # no dupes found, so add the new line 
        set output [open $datafile a] 
        puts $output "$text" 
        flush $output 
        catch {close $output} 
        puthelp "PRIVMSG $chan :Wrote $text to file.." 
}                
        
proc procdel {nick uhost hand chan text} { 
        global datafile 
        set text [split $text] 

        # check for valid input 
        if {$text == ""} { 
                puthelp "PRIVMSG $chan :meh" 
                return 
        } 

        # check for data file 
        if {![file exists $datafile]} { 
                puthelp "PRIVMSG $chan :No data file, nothing to delete!" 
                return 
        } 

        # check for requested handle 
        set data "" 
        set input [open $datafile r] 
        while {![eof $input]} { 
                set curline [gets $input];set curline [split $curline] 
                if {$curline != ""} { 
                        set data [linsert $data end $curline] 
                } 
        } 
        catch {close $input} 

        set mark -1;set match "" 
        foreach line $data { 
                incr mark 
                if {[lindex $line 0] == $text} { 
                        set match $mark 
                        break 
                } 
        } 
        if {$match == ""} { 
                puthelp "PRIVMSG $chan :No handle matching $text found.." 
                return 
        } 
        set newdata [lreplace $data $mark $mark] 
        set output [open $datafile w] 
        foreach newline $newdata { 
                if {$newline != ""} { 
                        puts $output $newline 
                } 
        } 
        flush $output 
        catch {close $output} 
        puthelp "PRIVMSG $chan :Deleted $text" 
        return                  
}
        global datafile 
        if {![file exists $datafile]} { 
                puthelp "PRIVMSG $nick :No data file.." 
                return 
        } 
        set input [open $datafile r] 
        set lines [split [read $input] \n] 
        catch {close $input} 
        set cnt 0 
        foreach line $lines { 
                if {$line != ""} { 
                        puthelp "PRIVMSG $nick :$line" 
                        incr cnt 
                } 
        } 
        if {$cnt == 0} { 
                puthelp "PRIVMSG $nick :No saved data";return 
        } else { 
                puthelp "PRIVMSG $nick :End of list.." 
        } 
##################################################################################################################################### 
putlog "add/del script loaded.. Orig file created by rosc2112" 
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

add/del/list script

Post by SpiKe^^ »

Try this....
Also fixed the del and list procs.

Code: Select all

set datafile "/usr/home/Felix2003/Eggdrop/scripts/data.txt" 

bind pub - add procadd 
bind pub - del procdel 
bind pub - list proclist

proc procadd {nick uhost hand chan text} { 
  global datafile 
  set text [string trim $text] 

  if {$text eq "" || ![string match "*?-?*" $text]} {
    puthelp "PRIVMSG $chan :Correct syntax: add <Artist> - <Song>"
    return 0
  } 

  # check for duplicate songs 
  if {[file exists $datafile]} { 
    set input [open $datafile r] 
    while {![eof $input]} { 
      set curline [gets $input]
      if {[string match -nocase $text $curline]} { 
        puthelp "PRIVMSG $chan :$text already exists.. Pick another song" 
        close $input
        return 0
      } 
    } 
    close $input
  } 
  # no dupes found, so add the new line 
  set output [open $datafile a] 
  puts $output "$text" 
  close $output
  puthelp "PRIVMSG $chan :Wrote $text to file.." 
  return 0
}

proc procdel {nick uhost hand chan text} { 
  global datafile 
  set text [string trim $text] 

  # check for valid input 
  if {$text eq ""} { 
    puthelp "PRIVMSG $chan :Correct syntax: del <Artist> - <Song>"
    return 0
  } 

  # check for data file 
  if {![file exists $datafile]} { 
    puthelp "PRIVMSG $chan :No data file, nothing to delete!" 
    return 0
  } 

  # check for requested song
  set data ""  ;  set found 0
  set input [open $datafile r] 
  while {![eof $input]} { 
    set curline [gets $input]
    if {$curline eq ""} {  continue  }
    if {[string match -nocase $text $curline]} {  set found 1
    } else {  lappend data $curline  }
  } 
  close $input

  # if request not found
  if {$found == 0} { 
    puthelp "PRIVMSG $chan :No request matching $text found.." 
    return 0
  } 

  # if request is found
  if {$data eq ""} {  file delete $datafile
  } else {
    set output [open $datafile w] 
    foreach newline $data { 
      puts $output $newline 
    }
    close $output
  } 
  puthelp "PRIVMSG $chan :Deleted $text" 
  return 0
}

proc proclist {nick uhost hand chan text} { 
  global datafile 
  if {![file exists $datafile]} { 
    puthelp "PRIVMSG $nick :No data file.." 
    return 0
  } 
  set input [open $datafile r] 
  set lines [split [read $input] \n] 
  close $input
  set cnt 0 
  foreach line $lines { 
    if {$line ne ""} { 
      puthelp "PRIVMSG $nick :$line" 
      incr cnt 
    } 
  } 
  if {$cnt == 0} { 
    puthelp "PRIVMSG $nick :No saved data."
  } else { 
    puthelp "PRIVMSG $nick :End of list.." 
  } 
  return 0
}

#######################################################
putlog "add/del/list script loaded.. Orig file created by rosc2112" 

SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
F
Felix2003
Voice
Posts: 24
Joined: Fri Feb 06, 2009 8:19 pm

Post by Felix2003 »

Thank you spike for having a look :)

tested your version and isn't doing exactly what i needed

User1: add Mariah Carey - Infinity
Bot: added song to file
User2: add Mariah Carey - Without you
Bot: handle Mariah already exists

seems it just reading the 1st word when searching not the entire line
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Felix2003 wrote:tested your version and isn't doing exactly what i needed
You did not use my edited script as it is posted above.
The original script did use just the first word for matching, mine does not.
If you are just going to pick a line or 2 of my code, and edit them back into your copy you will get strange results.
You MUST use my edited script as it is wrote above for it to work as you want it to.

Please test my edited script as I wrote it.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
F
Felix2003
Voice
Posts: 24
Joined: Fri Feb 06, 2009 8:19 pm

Post by Felix2003 »

SpiKe your correct i loaded the wrong script rushing this morning before heading to work

i loaded your modified file correctly this time and it works perfectly :D

Thank you man you made my life that little bit easier :D

one more question which i forgot to say is there a way to make it channel specific? like setting a channel mode or just writing a channel in the tcl?
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Channels specific version...

Do multiple channels space separated... {#chan1 #chan2}

Code: Select all

# set the channel(s) for the script to run in #
set adddelchan {#yourchan}

set datafile "/usr/home/Felix2003/Eggdrop/scripts/data.txt" 

############ END SETTINGS ############

bind pub - add procadd 
bind pub - del procdel 
bind pub - list proclist

set adddelchan [split [string trim $adddelchan]]

proc procadd {nick uhost hand chan text} { 
  global datafile adddelchan
  set text [string trim $text] 

  if {[lsearch -nocase $adddelchan $chan] == -1} { return 0 }

  if {$text eq "" || ![string match "*?-?*" $text]} {
    puthelp "PRIVMSG $chan :Correct syntax: add <Artist> - <Song>"
    return 0
  } 

  # check for duplicate songs 
  if {[file exists $datafile]} { 
    set input [open $datafile r] 
    while {![eof $input]} { 
      set curline [gets $input]
      if {[string match -nocase $text $curline]} { 
        puthelp "PRIVMSG $chan :$text already exists.. Pick another song" 
        close $input
        return 0
      } 
    } 
    close $input
  } 
  # no dupes found, so add the new line 
  set output [open $datafile a] 
  puts $output "$text" 
  close $output
  puthelp "PRIVMSG $chan :Wrote $text to file.." 
  return 0
}

proc procdel {nick uhost hand chan text} { 
  global datafile adddelchan
  set text [string trim $text] 

  if {[lsearch -nocase $adddelchan $chan] == -1} { return 0 }

  # check for valid input 
  if {$text eq ""} { 
    puthelp "PRIVMSG $chan :Correct syntax: del <Artist> - <Song>"
    return 0
  } 

  # check for data file 
  if {![file exists $datafile]} { 
    puthelp "PRIVMSG $chan :No data file, nothing to delete!" 
    return 0
  } 

  # check for requested song
  set data ""  ;  set found 0
  set input [open $datafile r] 
  while {![eof $input]} { 
    set curline [gets $input]
    if {$curline eq ""} {  continue  }
    if {[string match -nocase $text $curline]} {  set found 1
    } else {  lappend data $curline  }
  } 
  close $input

  # if request not found
  if {$found == 0} { 
    puthelp "PRIVMSG $chan :No request matching $text found.." 
    return 0
  } 

  # if request is found
  if {$data eq ""} {  file delete $datafile
  } else {
    set output [open $datafile w] 
    foreach newline $data { 
      puts $output $newline 
    }
    close $output
  } 
  puthelp "PRIVMSG $chan :Deleted $text" 
  return 0
}

proc proclist {nick uhost hand chan text} { 
  global datafile adddelchan

  if {[lsearch -nocase $adddelchan $chan] == -1} { return 0 }

  if {![file exists $datafile]} { 
    puthelp "PRIVMSG $nick :No data file.." 
    return 0
  } 
  set input [open $datafile r] 
  set lines [split [read $input] \n] 
  close $input
  set cnt 0 
  foreach line $lines { 
    if {$line ne ""} { 
      puthelp "PRIVMSG $nick :$line" 
      incr cnt 
    } 
  } 
  if {$cnt == 0} { 
    puthelp "PRIVMSG $nick :No saved data."
  } else { 
    puthelp "PRIVMSG $nick :End of list.." 
  } 
  return 0
}

#######################################################
putlog "add/del/list script loaded.. Orig file created by rosc2112" 

SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
F
Felix2003
Voice
Posts: 24
Joined: Fri Feb 06, 2009 8:19 pm

Post by Felix2003 »

Spike you are amazing thank you so much for your help on this :D
Post Reply