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.

Timed Messages

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Timed Messages

Post by BigToe »

Hey all

I need your help.
I need a script that every X minutes will read according to the order the next line from Messages.TXT (in other words, it reads 1 line each time - not random lines, forever).

In addition, public commands including: !addmsg, !delmsg <line number>, and !playfile to play the whole file with line numbers before the messages

* Must support sending messages to more than 1 channel.

Please help - thanks!
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

You should search the egghelp script archive for the quote scripts, there are 40 in that catagory: http://www.egghelp.org/tclhtml/3478-4-3-0-1.htm
Two I find promising are:
  • CodeNinja Quote System ver 1.0.2 31/12/2010 by CodeNinja
    LegoMan's Quote Database Script ver 1.2.0 08/07/2007 by LegoMan
Looks to me like either of those scripts could manage your text file with !add and !delete commands.
If one of those scripts function properly, I could try to add the 'read lines on a timer' part for you.


You might also want to read this egghelp forum string: http://forum.egghelp.org/viewtopic.php?t=19139
Last edited by SpiKe^^ on Sat Dec 01, 2012 12:58 pm, edited 1 time in total.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Or maybe we could just start with this simple text file editing script i wrote a few days ago. It has !addline , !readline , and !editline already, and I'm sure it works as is. Would just need a little more work to do the rest of what you want:):

Code: Select all

# EditTextFile Version 0.3 #

# author:  SpiKe^^ #
# e-mail:  spike<at>mytclscripts<dot>com #
# webpage: http://mytclscripts.com/ #

# This file is Copyrighted under the GNU Public License. #
# http://www.gnu.org/copyleft/gpl.html #

###########  Begin Settings ###########

# set the full route & file name of the file to edit #
set eTxFile(file) {/usr/home/spike/eggdrop/scripts/filename.txt}

# set the channel for this script to run in #
set eTxFile(chan) {#yourchannel}

# set the public trigger for the add line command #
set eTxFile(add) {!addline}

# set the public trigger for the read line command #
set eTxFile(read) {!readline}

# set the public trigger for the edit line command #
set eTxFile(edit) {!editline}

# set the access flags to use the above public commands #
set eTxFile(flags) {o|o}

############  End Settings ############


bind pub $eTxFile(flags) $eTxFile(add) etfProcAdd
bind pub $eTxFile(flags) $eTxFile(read) etfProcRead
bind pub $eTxFile(flags) $eTxFile(edit) etfProcEdit

proc etfProcAdd {nk uh hn ch tx} {
  if {[string tolower $ch] ne [string tolower $::eTxFile(chan)]} { return }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"
    return
  }
  set tx [string trim $tx]
  if {$tx eq ""} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(add) text to add to the end of the file."
    return
  }
  puthelp "PRIVMSG $ch :Adding: $tx :to file: [file tail $tf]"
  set id [open $tf a]
  puts $id $tx
  close $id
  return
}

proc etfProcRead {nk uh hn ch tx} {
  if {[string tolower $ch] ne [string tolower $::eTxFile(chan)]} { return }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"
    return
  }
  set tx [string trim $tx]
  if {![string is digit -strict $tx]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(read) line#"
    return
  }
  set tid [open $tf]
  set lnum 0
  while {![eof $tid]} {
    set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$lnum==$tx} {
        puthelp "PRIVMSG $ch :Existing line $lnum text: $line"
        break
      }
    }
  }
  close $tid
  if {$tx>$lnum} {
    puthelp "PRIVMSG $ch :File line $tx doesn't exist ($lnum lines in the file)"
  }
  return
}

proc etfProcEdit {nk uh hn ch tx} {
  if {[string tolower $ch] ne [string tolower $::eTxFile(chan)]} { return }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"
    return
  }
  set tx [split [string trim $tx]]
  if {[llength $tx]<"2" || ![string is digit -strict [lindex $tx 0]]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(edit) line# text to replace original line."
    return
  }
  set find [lindex $tx 0]
  set tx [string trim [join [lrange $tx 1 end]]]
  set new [file dirname $tf]/newfile.tmp
  set nid [open $new w]
  set tid [open $tf]
  set lnum 0
  while {![eof $tid]} {
    set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$lnum==$find} {
        puthelp "PRIVMSG $ch :Replacing existing line $lnum text: $line"
        puthelp "PRIVMSG $ch :with the new text line: $tx"
        puts $nid $tx
      } else {  puts $nid $line  }
    }
  }
  close $nid  ;  close $tid
  if {$find>$lnum} {
    puthelp "PRIVMSG $ch :File line $find doesn't exist ($lnum lines in the file)"
    file delete $new
  } else {  file rename -force $new $tf  }
  return
}

Let me know what route you wish to pursue.
Think I prefer starting with my script, as it is simple to edit:)
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Post by BigToe »

Its really up to you :P
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Not had a chance to test this any, but here's what I have. Maybe you can try to load and test it some:).

Code: Select all

# EditTextFile+TimedReadLine Version 0.4 #

# author:  SpiKe^^ #
# e-mail:  spike<at>mytclscripts<dot>com #
# webpage: http://mytclscripts.com/ #

# This file is Copyrighted under the GNU Public License. #
# http://www.gnu.org/copyleft/gpl.html #

########### General Script Settings ###########

# Set the full route & file name of the file to edit #
set eTxFile(file) {/usr/home/spike/eggdrop/scripts/filename.txt}

# Set the channel(s) for this script to run in #
set eTxFile(chan) {#yourchannel #anotherchannel #someotherchan}

########### Public Command Settings ###########

# Set the public trigger for the add line command #
# to add a line at the end of the file: 
#    example:  !addline end The text to add at file end.
#    example:  !addline The text to add at file end.
# to add a line at a specific line position in the file:
#    example:  !addline 4 The text to add at file line 4.
#    note: this will renumber the original line 4 & all lines after it!
set eTxFile(add) {!addline}

# Set the public trigger for the read line command #
# to read a specific line (by line number) from the text file
#    example:  !readline 4
#    note: use to check for correct line before doing !editline or !delline
set eTxFile(read) {!readline}

# Set the public trigger for the delete line command #
# to delete a specific line (by line number) from the text file
#    example:  !delline 4
#    note: this will renumber all lines after line 4!
set eTxFile(del) {!delline}

# Set the public trigger for the edit line command #
# to edit a specific line (by line number) in the text file
#    example:  !editline 4 New text to replace file line 4.
set eTxFile(edit) {!editline}

# Set the public trigger for the read file command #
# to read all lines from the text file
#    example:  !readfile
set eTxFile(readf) {!readfile}

# Set the access flags to use the above public commands #
set eTxFile(flags) {o|o}

########### Timed Public Read Setting ###########

# Set number of minutes between each timed public read line #
# Set this to "0" to disable all timed public read lines
# Note: file errors will be sent to the first channel in eTxFile(chan)
set eTxFile(timed) "5"

################ End Settings ################


bind pub $eTxFile(flags) $eTxFile(add) etfProcAdd
bind pub $eTxFile(flags) $eTxFile(del) etfProcDel
bind pub $eTxFile(flags) $eTxFile(read) etfProcRead
bind pub $eTxFile(flags) $eTxFile(edit) etfProcEdit
bind pub $eTxFile(flags) $eTxFile(readf) etfProcReadF

set eTxFile(chan) [split $eTxFile(chan)]

if {$eTxFile(timed)>"0" && ![info exists eTxFile(tnxread)]} {
  if {$eTxFile(timed)>"3"} {  timer 3 [list etfProcTimed]
  } else {  timer $eTxFile(timed) [list etfProcTimed]  }
  set eTxFile(tnxread) 1
}

proc etfProcAdd {nk uh hn ch tx} {
  if {[lsearch -nocase $::eTxFile(chan) $ch]=="-1"} {  return  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [split [string trim $tx]]
  if {[lindex $tx 0] eq "end" || [string is digit -strict [lindex $tx 0]]} {
    set addat [lindex $tx 0] ; set tx [string trim [join [lrange $tx 1 end]]]
  } else {  set addat end  ;  set tx [join $tx]  }
  if {$tx eq ""} {  set add $::eTxFile(add)
    puthelp "PRIVMSG $ch :Correct syntax is: $add \[position\] <text to add>"
    puthelp "PRIVMSG $ch :Example: $add end Text to add at end of the file."
    puthelp "PRIVMSG $ch :Example: $add 4 Text to add at file line 4."
    puthelp "PRIVMSG $ch :Example: $add Text to add at end of the file."
    return
  }
  if {$addat ne "end"} {  set tx "$addat $tx"
    etfProcEdit $nk $uh $hn $ch $tx add  ;  return
  }
  puthelp "PRIVMSG $ch :Adding line at end of file: $tx"
  set id [open $tf a]  ;  puts $id $tx  ;  close $id  ;  return
}

proc etfProcReadF {nk uh hn ch tx} {
  etfProcRead $nk $uh $hn $ch $tx file  ;  return
}

proc etfProcTimed {} {
  if {$::eTxFile(timed)=="0"} {  unset ::eTxFile(tnxread)  ;  return  }
  set chan [lindex $::eTxFile(chan) 0]
  etfProcRead $::botnick Timed Line $chan $::eTxFile(tnxread)
  timer $::eTxFile(timed) [list etfProcTimed]
}

proc etfProcRead {nk uh hn ch tx {do line} } {
  if {[lsearch -nocase $::eTxFile(chan) $ch]=="-1"} {  return  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [string trim $tx]
  if {$do eq "line" && ![string is digit -strict $tx]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(read) <line#>"
    puthelp "PRIVMSG $ch :Example: $::eTxFile(read) 4"
    return
  }
  set tid [open $tf]  ;  set lnum 0
  while {![eof $tid]} {  set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$uh eq "Timed" && $lnum=="1"} {  set ln1 $line  }
      if {$do eq "file"} {  puthelp "PRIVMSG $ch :\[$lnum\]  $line"
      } elseif {$lnum==$tx} {
        if {$uh ne "Timed"} {  puthelp "PRIVMSG $ch :\[$lnum\]  $line"  }
        break
      }
    }
  }
  close $tid
  if {$lnum=="0"} {  puthelp "PRIVMSG $ch :Text file is empty: $tf" ;  return  }
  if {$uh eq "Timed"} {
    if {$lnum==$tx} {  incr ::eTxFile(tnxread)
    } else {  set line $ln1  ;  set lnum 1  ;  set ::eTxFile(tnxread) 2  }
    foreach ch $::eTxFile(chan) {  puthelp "PRIVMSG $ch :\[$lnum\]  $line"  }
  } elseif {$do eq "line" && $tx>$lnum} {
    puthelp "PRIVMSG $ch :File line $tx doesn't exist ($lnum lines in the file)"
  }
  return
}

proc etfProcDel {nk uh hn ch tx} {
  etfProcEdit $nk $uh $hn $ch $tx del  ;  return
}

proc etfProcEdit {nk uh hn ch tx {do edit} } {
  if {[lsearch -nocase $::eTxFile(chan) $ch]=="-1"} {  return  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [split [string trim $tx]]
  if {$do eq "edit"} {
    if {[llength $tx]<"2" || ![string is digit -strict [lindex $tx 0]]} {
      set edit $::eTxFile(edit)
      puthelp "PRIVMSG $ch :Correct syntax is: $edit <line#> <new edited text>"
      puthelp "PRIVMSG $ch :Example: $edit 4 New text to replace file line 4."
      return
    }
  } elseif {$do eq "del" && ![string is digit -strict [lindex $tx 0]]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(del) <line#>"
    puthelp "PRIVMSG $ch :Example: $::eTxFile(del) 4"
    return
  }
  set find [lindex $tx 0]  ;  set tx [string trim [join [lrange $tx 1 end]]]
  set new [file dirname $tf]/newfile.tmp  ;  set nid [open $new w]
  set tid [open $tf]  ;  set lnum 0
  while {![eof $tid]} {  set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$lnum==$find} {
        if {$do eq "edit"} {
          puthelp "PRIVMSG $ch :Replacing line $lnum text: $line"
          puthelp "PRIVMSG $ch :with the new text line: $tx"
          puts $nid $tx
        } elseif {$do eq "del"} {
          puthelp "PRIVMSG $ch :Deleting line $lnum text: $line"
        } elseif {$do eq "add"} {
          puthelp "PRIVMSG $ch :Adding new line $lnum text: $tx"
          puts $nid $tx
          puts $nid $line
        }
      } else {  puts $nid $line  }
    }
  }
  close $tid
  if {$find>$lnum} {
    if {$do eq "add"} {  incr lnum
      puthelp "PRIVMSG $ch :Adding new line $lnum text: $tx"
      puts $nid $tx  ;  close $nid  ;  file rename -force $new $tf
    } else {
      puthelp "PRIVMSG $ch :File line $find doesn't exist ($lnum lines in the file)"
      close $nid  ;  file delete $new
    }
  } else {  close $nid  ;  file rename -force $new $tf  }
  return
}

putlog "EditTextFile+TimedReadLine Ver. 0.4 by SpiKe^^ loaded."

Goodluck and let me know.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Post by BigToe »

Hello SpiKe^^!

Thank you for your time and work.

I am currently testing the script.

I have tried to add a line, it did not work and I received this error in the partyline:

Code: Select all

[09:27:01] Tcl error in script for 'timer752':
[09:27:01] bad option "-nocase": must be -all, -ascii, -decreasing, -dictionary, -exact, -glob, -increasing, -inline, -integer, -not, -real, -regexp, -sorted, or -start
None of the public commands work (I did not receive additional error messages)

Again, thanks!
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

OK, that answers the question of what version of tcl you are using :)
[lsearch -nocase] was added in tcl 8.5, you are using 8.4 or older.
I will fix it to work for tcl 8.4 and lower....
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Still untested, but this version removes the code that requires tcl 8.5

Code: Select all

# EditTextFile+TimedReadLine Version 0.5 #

# author:  SpiKe^^ #
# e-mail:  spike<at>mytclscripts<dot>com #
# webpage: http://mytclscripts.com/ #

# This file is Copyrighted under the GNU Public License. #
# http://www.gnu.org/copyleft/gpl.html #

########### General Script Settings ###########

# Set the full route & file name of the file to edit #
set eTxFile(file) {/usr/home/spike/eggdrop/scripts/filename.txt}

# Set the channel(s) for this script to run in #
set eTxFile(chan) {#yourchannel #anotherchannel #someotherchan}

########### Public Command Settings ###########

# Set the public trigger for the add line command #
# to add a line at the end of the file: 
#    example:  !addline end The text to add at file end.
#    example:  !addline The text to add at file end.
# to add a line at a specific line position in the file:
#    example:  !addline 4 The text to add at file line 4.
#    note: this will renumber the original line 4 & all lines after it!
set eTxFile(add) {!addline}

# Set the public trigger for the read line command #
# to read a specific line (by line number) from the text file
#    example:  !readline 4
#    note: use to check for correct line before doing !editline or !delline
set eTxFile(read) {!readline}

# Set the public trigger for the delete line command #
# to delete a specific line (by line number) from the text file
#    example:  !delline 4
#    note: this will renumber all lines after line 4!
set eTxFile(del) {!delline}

# Set the public trigger for the edit line command #
# to edit a specific line (by line number) in the text file
#    example:  !editline 4 New text to replace file line 4.
set eTxFile(edit) {!editline}

# Set the public trigger for the read file command #
# to read all lines from the text file
#    example:  !readfile
set eTxFile(readf) {!readfile}

# Set the access flags to use the above public commands #
set eTxFile(flags) {o|o}

########### Timed Public Read Setting ###########

# Set number of minutes between each timed public read line #
# Set this to "0" to disable all timed public read lines
# Note: file errors will be sent to the first channel in eTxFile(chan)
set eTxFile(timed) "5"

################ End Settings ################


bind pub $eTxFile(flags) $eTxFile(add) etfProcAdd
bind pub $eTxFile(flags) $eTxFile(del) etfProcDel
bind pub $eTxFile(flags) $eTxFile(read) etfProcRead
bind pub $eTxFile(flags) $eTxFile(edit) etfProcEdit
bind pub $eTxFile(flags) $eTxFile(readf) etfProcReadF

set eTxFile(chan) [split [string tolower $eTxFile(chan)]]

if {$eTxFile(timed)>"0" && ![info exists eTxFile(tnxread)]} {
  if {$eTxFile(timed)>"3"} {  timer 3 [list etfProcTimed]
  } else {  timer $eTxFile(timed) [list etfProcTimed]  }
  set eTxFile(tnxread) 1
}

proc etfProcAdd {nk uh hn ch tx} {
  set ch [string tolower $ch]
  if {[lsearch -exact $::eTxFile(chan) $ch]=="-1"} {  return  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [split [string trim $tx]]
  if {[lindex $tx 0] eq "end" || [string is digit -strict [lindex $tx 0]]} {
    set addat [lindex $tx 0] ; set tx [string trim [join [lrange $tx 1 end]]]
  } else {  set addat end  ;  set tx [join $tx]  }
  if {$tx eq ""} {  set add $::eTxFile(add)
    puthelp "PRIVMSG $ch :Correct syntax is: $add \[position\] <text to add>"
    puthelp "PRIVMSG $ch :Example: $add end Text to add at end of the file."
    puthelp "PRIVMSG $ch :Example: $add 4 Text to add at file line 4."
    puthelp "PRIVMSG $ch :Example: $add Text to add at end of the file."
    return
  }
  if {$addat ne "end"} {  set tx "$addat $tx"
    etfProcEdit $nk $uh $hn $ch $tx add  ;  return
  }
  puthelp "PRIVMSG $ch :Adding line at end of file: $tx"
  set id [open $tf a]  ;  puts $id $tx  ;  close $id  ;  return
}

proc etfProcReadF {nk uh hn ch tx} {
  etfProcRead $nk $uh $hn $ch $tx file  ;  return
}

proc etfProcTimed {} {
  if {$::eTxFile(timed)=="0"} {  unset ::eTxFile(tnxread)  ;  return  }
  set chan [lindex $::eTxFile(chan) 0]
  etfProcRead $::botnick Timed Line $chan $::eTxFile(tnxread)
  timer $::eTxFile(timed) [list etfProcTimed]  ;  return
}

proc etfProcRead {nk uh hn ch tx {do line} } {
  set ch [string tolower $ch]
  if {[lsearch -exact $::eTxFile(chan) $ch]=="-1"} {  return  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [string trim $tx]
  if {$do eq "line" && ![string is digit -strict $tx]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(read) <line#>"
    puthelp "PRIVMSG $ch :Example: $::eTxFile(read) 4"
    return
  }
  set tid [open $tf]  ;  set lnum 0
  while {![eof $tid]} {  set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$uh eq "Timed" && $lnum=="1"} {  set ln1 $line  }
      if {$do eq "file"} {  puthelp "PRIVMSG $ch :\[$lnum\]  $line"
      } elseif {$lnum==$tx} {
        if {$uh ne "Timed"} {  puthelp "PRIVMSG $ch :\[$lnum\]  $line"  }
        break
      }
    }
  }
  close $tid
  if {$lnum=="0"} {
    if {[info exists ::eTxFile(tnxread)]} {  set ::eTxFile(tnxread) 1  }
    puthelp "PRIVMSG $ch :Text file is empty: $tf" ;  return
  }
  if {$uh eq "Timed"} {
    if {$lnum==$tx} {  incr ::eTxFile(tnxread)
    } else {  set line $ln1  ;  set lnum 1  ;  set ::eTxFile(tnxread) 2  }
    foreach ch $::eTxFile(chan) {  puthelp "PRIVMSG $ch :\[$lnum\]  $line"  }
  } elseif {$do eq "line" && $tx>$lnum} {  set tl line
    if {$lnum>"1"} {  set tl lines  }
    puthelp "PRIVMSG $ch :File line $tx doesn't exist ($lnum $tl in the file)"
  }
  return
}

proc etfProcDel {nk uh hn ch tx} {
  etfProcEdit $nk $uh $hn $ch $tx del  ;  return
}

proc etfProcEdit {nk uh hn ch tx {do edit} } {
  set ch [string tolower $ch]
  if {[lsearch -exact $::eTxFile(chan) $ch]=="-1"} {  return  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [split [string trim $tx]]
  if {$do eq "edit"} {
    if {[llength $tx]<"2" || ![string is digit -strict [lindex $tx 0]]} {
      set edit $::eTxFile(edit)
      puthelp "PRIVMSG $ch :Correct syntax is: $edit <line#> <new edited text>"
      puthelp "PRIVMSG $ch :Example: $edit 4 New text to replace file line 4."
      return
    }
  } elseif {$do eq "del" && ![string is digit -strict [lindex $tx 0]]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(del) <line#>"
    puthelp "PRIVMSG $ch :Example: $::eTxFile(del) 4"
    return
  }
  set find [lindex $tx 0]  ;  set tx [string trim [join [lrange $tx 1 end]]]
  set new [file dirname $tf]/newfile.tmp  ;  set nid [open $new w]
  set tid [open $tf]  ;  set lnum 0
  while {![eof $tid]} {  set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$lnum==$find} {
        if {$do eq "edit"} {
          puthelp "PRIVMSG $ch :Replacing line $lnum text: $line"
          puthelp "PRIVMSG $ch :with the new text line: $tx"
          puts $nid $tx
        } elseif {$do eq "del"} {
          puthelp "PRIVMSG $ch :Deleting line $lnum text: $line"
        } elseif {$do eq "add"} {
          puthelp "PRIVMSG $ch :Adding new line $lnum text: $tx"
          puts $nid $tx  ;  puts $nid $line
        }
      } else {  puts $nid $line  }
    }
  }
  close $tid
  if {$find>$lnum} {
    if {$do eq "add"} {  incr lnum
      puthelp "PRIVMSG $ch :Adding new line $lnum text: $tx"
      puts $nid $tx  ;  close $nid  ;  file rename -force $new $tf
    } else {
      if {$lnum>"0"} {  set tl line
        if {$lnum>"1"} {  set tl lines  }
        puthelp "PRIVMSG $ch :File line $find doesn't exist ($lnum $tl in the file)"
      } else {  puthelp "PRIVMSG $ch :Text file is empty: $tf"  }
      close $nid  ;  file delete $new
    }
  } else {  close $nid  ;  file rename -force $new $tf  }
  return
}

putlog "EditTextFile+TimedReadLine Ver. 0.5 by SpiKe^^ loaded."

Try again and let me know.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Installed the script and fully tested it, All looks good:)
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Post by BigToe »

Hey SpiKe^^,

I would like to thank you from the bottom of my heart for your time and effort - the script works lovely, exactly like I wanted, all the functionality is there and its good!

I would like to ask though, if possible, can you remove the [number-of-line-here] that appears before each message when displayed to the channel?

And, is there any chance you can modify the script so I can add messages to Messages.txt without being on eTxFile(chan) - meaning I can edit the messages relied to a certain channel (or channels) even if I`m not on it?

Thanks again!
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Here is the script with the new requested options.

Code: Select all

# EditTextFile+TimedReadLine Version 1.0 #

# author:  SpiKe^^ #
# e-mail:  spike<at>mytclscripts<dot>com #
# webpage: http://mytclscripts.com/ #

# This file is Copyrighted under the GNU Public License. #
# http://www.gnu.org/copyleft/gpl.html #

########### General Text File Settings ###########

# Set the full route & file name of the file to edit #
# Note: The !addline command will attempt to make this file if needed.
set eTxFile(file) {/usr/home/spike/eggdrop/scripts/filename.txt}

########### Public 'Line' Command Settings ###########
## settings for: !addline !readline !delline & !editline ##

# Set the listen channel(s) for all public Line commands #
set eTxFile(lchan) {#yourchannel #anotherchannel #someotherchan}

# Set the access flags to use the public Line commands #
set eTxFile(lflags) {o|o}

# Set the public trigger for the add line command #
# to add a line at the end of the file: 
#    example:  !addline end The text to add at file end.
#    example:  !addline The text to add at file end.
# to add a line at a specific line position in the file:
#    example:  !addline 4 The text to add at file line 4.
#    note: this will renumber the original line 4 & all lines after it!
set eTxFile(add) {!addline}

# Set the public trigger for the read line command #
# to read a specific line (by line number) from the text file
#    example:  !readline 4
#    note: use to check for correct line before doing !editline or !delline
set eTxFile(read) {!readline}

# Set the public trigger for the delete line command #
# to delete a specific line (by line number) from the text file
#    example:  !delline 4
#    note: this will renumber all lines after line 4!
set eTxFile(del) {!delline}

# Set the public trigger for the edit line command #
# to edit a specific line (by line number) in the text file
#    example:  !editline 4 New text to replace file line 4.
set eTxFile(edit) {!editline}

########### Public 'ReadFile' Command Settings ###########
## settings for the public !readfile command ##

# Set the listen channel(s) for the public !readfile command #
set eTxFile(rchan) {#yourchannel #anotherchannel #someotherchan}

# Set the access flags to use the public read file command #
set eTxFile(rflags) {o|o}

# Set the public trigger for the read file command #
# to read all lines from the text file
#    example:  !readfile
set eTxFile(readf) {!readfile}

########### Timed Public Read Setting ###########

# Set the channel(s) for the timed public read line #
set eTxFile(tchan) {#yourchannel #anotherchannel #someotherchan}

# Set number of minutes between each timed public read line #
# Set this to "0" to disable all timed public read lines
# Note: file errors will be sent to the first channel in eTxFile(tchan)
set eTxFile(timed) "5"

# show file line number before each timed public read line? (0=no | 1=yes) #
set eTxFile(tnum) "1"

################ End Settings ################


bind pub $eTxFile(lflags) $eTxFile(add) etfProcAdd
bind pub $eTxFile(lflags) $eTxFile(del) etfProcDel
bind pub $eTxFile(lflags) $eTxFile(read) etfProcRead
bind pub $eTxFile(lflags) $eTxFile(edit) etfProcEdit
bind pub $eTxFile(rflags) $eTxFile(readf) etfProcReadF

set eTxFile(lchan) [split [string tolower $eTxFile(lchan)]]
set eTxFile(rchan) [split [string tolower $eTxFile(rchan)]]
set eTxFile(tchan) [split [string tolower $eTxFile(tchan)]]

if {$eTxFile(timed)>"0" && ![info exists eTxFile(tnxread)]} {
  if {$eTxFile(timed)>"3"} {  timer 3 [list etfProcTimed]
  } else {  timer $eTxFile(timed) [list etfProcTimed]  }
  set eTxFile(tnxread) 1
}

proc etfProcAdd {nk uh hn ch tx} {
  set ch [string tolower $ch]
  if {[lsearch -exact $::eTxFile(lchan) $ch]=="-1"} {  return  }
  set tx [split [string trim $tx]]
  if {[lindex $tx 0] eq "end" || [string is digit -strict [lindex $tx 0]]} {
    set addat [lindex $tx 0] ; set tx [string trim [join [lrange $tx 1 end]]]
  } else {  set addat end  ;  set tx [join $tx]  }
  if {$tx eq ""} {  set add $::eTxFile(add)
    puthelp "PRIVMSG $ch :Correct syntax is: $add \[position\] <text to add>"
    puthelp "PRIVMSG $ch :Example: $add end Text to add at end of the file."
    puthelp "PRIVMSG $ch :Example: $add 4 Text to add at file line 4."
    puthelp "PRIVMSG $ch :Example: $add Text to add at end of the file."
    return
  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {  set addat end  ;  set nofile 1  }
  if {$addat ne "end"} {  set tx "$addat $tx"
    etfProcEdit $nk $uh $hn $ch $tx add  ;  return
  }
  set id [open $tf a]  ;  puts $id $tx  ;  close $id
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Unable to find or make text file: $tf"
  } elseif {[info exists nofile]} {
    puthelp "PRIVMSG $ch :Added line to new text file: $tx"
  } else {
    puthelp "PRIVMSG $ch :Added line at end of text file: $tx"
  }
  return
}

proc etfProcReadF {nk uh hn ch tx} {
  etfProcRead $nk $uh $hn $ch $tx file  ;  return
}

proc etfProcTimed {} {
  if {$::eTxFile(timed)=="0"} {  unset ::eTxFile(tnxread)  ;  return  }
  timer $::eTxFile(timed) [list etfProcTimed]
  set chan [lindex $::eTxFile(tchan) 0]
  etfProcRead $::botnick Timed Line $chan $::eTxFile(tnxread)  ;  return
}

proc etfProcRead {nk uh hn ch tx {do line} } {
  set ch [string tolower $ch]
  if {$do eq "file"} {
    if {[lsearch -exact $::eTxFile(rchan) $ch]=="-1"} {  return  }
  } elseif {$uh eq "Timed"} {
    if {[lsearch -exact $::eTxFile(tchan) $ch]=="-1"} {  return  }
  } else {
    if {[lsearch -exact $::eTxFile(lchan) $ch]=="-1"} {  return  }
  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [string trim $tx]
  if {$do eq "line" && ![string is digit -strict $tx]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(read) <line#>"
    puthelp "PRIVMSG $ch :Example: $::eTxFile(read) 4"  ;  return
  }
  set tid [open $tf]  ;  set lnum 0
  while {![eof $tid]} {  set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$uh eq "Timed" && $lnum=="1"} {  set ln1 $line  }
      if {$do eq "file"} {  puthelp "PRIVMSG $ch :\[$lnum\] $line"
      } elseif {$lnum==$tx} {
        if {$uh ne "Timed"} {  puthelp "PRIVMSG $ch :\[$lnum\] $line"  }
        break
      }
    }
  }
  close $tid
  if {$lnum=="0"} {
    if {[info exists ::eTxFile(tnxread)]} {  set ::eTxFile(tnxread) 1  }
    puthelp "PRIVMSG $ch :Text file is empty: $tf" ;  return
  }
  if {$uh eq "Timed"} {  set pre ""
    if {$lnum==$tx} {  incr ::eTxFile(tnxread)
    } else {  set line $ln1  ;  set lnum 1  ;  set ::eTxFile(tnxread) 2  }
    if {$::eTxFile(tnum)!="0"} {  set pre "\[$lnum\] "  }
    foreach ch $::eTxFile(tchan) {  puthelp "PRIVMSG $ch :$pre$line"  }
  } elseif {$do eq "line" && $tx>$lnum} {  set tl line
    if {$lnum>"1"} {  set tl lines  }
    puthelp "PRIVMSG $ch :File line $tx doesn't exist ($lnum $tl in the file)"
  }
  return
}

proc etfProcDel {nk uh hn ch tx} {
  etfProcEdit $nk $uh $hn $ch $tx del  ;  return
}

proc etfProcEdit {nk uh hn ch tx {do edit} } {
  set ch [string tolower $ch]
  if {[lsearch -exact $::eTxFile(lchan) $ch]=="-1"} {  return  }
  set tf $::eTxFile(file)
  if {![file exists $tf]} {
    puthelp "PRIVMSG $ch :Text file does not exist: $tf"  ;  return
  }
  set tx [split [string trim $tx]]
  if {$do eq "edit"} {
    if {[llength $tx]<"2" || ![string is digit -strict [lindex $tx 0]]} {
      set edit $::eTxFile(edit)
      puthelp "PRIVMSG $ch :Correct syntax is: $edit <line#> <new edited text>"
      puthelp "PRIVMSG $ch :Example: $edit 4 New text to replace file line 4."
      return
    }
  } elseif {$do eq "del" && ![string is digit -strict [lindex $tx 0]]} {
    puthelp "PRIVMSG $ch :Correct syntax is: $::eTxFile(del) <line#>"
    puthelp "PRIVMSG $ch :Example: $::eTxFile(del) 4"  ;  return
  }
  set find [lindex $tx 0]  ;  set tx [string trim [join [lrange $tx 1 end]]]
  set new [file dirname $tf]/newfile.tmp  ;  set nid [open $new w]
  set tid [open $tf]  ;  set lnum 0
  while {![eof $tid]} {  set line [gets $tid]
    if {$line ne ""} {  incr lnum
      if {$lnum==$find} {
        if {$do eq "edit"} {
          puthelp "PRIVMSG $ch :Replaced line $lnum text: $line"
          puthelp "PRIVMSG $ch :with the new text line: $tx"
          puts $nid $tx
        } elseif {$do eq "del"} {
          puthelp "PRIVMSG $ch :Deleted line $lnum text: $line"
        } elseif {$do eq "add"} {
          puthelp "PRIVMSG $ch :Added new line $lnum text: $tx"
          puts $nid $tx  ;  puts $nid $line
        }
      } else {  puts $nid $line  }
    }
  }
  close $tid
  if {$find>$lnum} {
    if {$do eq "add"} {  incr lnum
      puthelp "PRIVMSG $ch :Added new line $lnum text: $tx"
      puts $nid $tx  ;  close $nid  ;  file rename -force $new $tf
    } else {
      if {$lnum>"0"} {  set tl line
        if {$lnum>"1"} {  set tl lines  }
        puthelp "PRIVMSG $ch :File line $find doesn't exist ($lnum $tl in the file)"
      } else {  puthelp "PRIVMSG $ch :Text file is empty: $tf"  }
      close $nid  ;  file delete $new
    }
  } else {  close $nid  ;  file rename -force $new $tf  }
  return
}

putlog "EditTextFile+TimedReadLine Ver. 1.0 by SpiKe^^ loaded."

Let me know if that's what you were looking for.

EDITED: Added code to make the text file if it doesn't exist.
Last edited by SpiKe^^ on Mon Dec 17, 2012 3:50 pm, edited 2 times in total.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Post by BigToe »

YES! FANTASTIC! THIS IS IT!!

THANK YOU SPIKE^^!!!!!
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Edited the above script, to have it make the text file, if it does not exist.
Corrected some of the script documentation.
Changed it's version number to ver. 1.0
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Post Reply