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.

File operations

Help for those learning Tcl or writing their own scripts.
Post Reply
l
loulou7593
Voice
Posts: 12
Joined: Tue Apr 08, 2008 12:52 pm

File operations

Post by loulou7593 »

Good evening, I do not know how to replace a precise line, either by his number of line, or by detecting a word which appears on this line.
I explain: I have a script of jokes which can send the jokes all X minutes but which it is necessary to adjust in the blague.tcl file
I wanted for example to create a command with a bind pub that when I bang !Addtemp X (X being a figure in minutes) that replaces me in the blague.tcl file the following line

Code: Select all

set blagues(time) 5
For the moment I put him 5 minutes, but afterward to be able to modify her by banging the chan !Addtemp 10 and what that changes in the file 5 there 10.

If somebody had an idea, I am interested.
Thank you in advance.

___________

Here is the code

Code: Select all

#####################################################################
## Configuration du Script ##########################################
#####################################################################

# Anti flood sur les commandes du Bot x:y (x=nombre de fois, y=secondes) :
set fun(flood) 5:10

# Ignorer l'utilisateur après son flood (0=non, 1=oui) :
set fun(ignore) 0

# Si oui, combien de temps (en minutes) :
set fun(ignoretime) 0

# Laisser faire les utilisateurs ayant certains flags :
set fun(ignflags) "-|-"

## Blagues ##########################################################
#####################################################################

# Channels où seront envoyés les blagues :
set blagues(chans) "#chan"

# Intervalle de temps entre chaque blague en minutes (0=désactivé) :
set blagues(time) 5

# Editer ici les blagues :
set blagues(msgs) {
   "Citation : C'est parce que la vitesse de la lumière est supérieure à celle du son, que beaucoup de gens paraissent brillants avant d'avoir l'air con."
   "Citation : La main droite c'est celle où tu as le pouce à gauche."
   "Citation : Je marche pieds nus pour ne plus péter plus haut que mon cul."
   "Citation : La dictature, c'est ferme ta gueule, et la démocatie, c'est cause toujours (Woody Allen)."
   "Citation : Avec les capotes Nestor, t'es pas né, t'es pas mort (Coluche)."
   "Citation : Je ne parle pas aux cons, ça les instruit (Audiard)."
   "Citation : Les hommes préfèrent les blondes parce que les blondes savent ce que les hommes préfèrent (Marilyn Monroe)"
   "Citation : Le travail est l'opium du peuple et je ne veux pas mourir drogue (Boris Vian)"
   "Citation : Les meilleurs moments de la vie à deux, c'est quand on est tout seul (Pierre Dac)"
   
}

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

proc fun:flood:init {} {
global fun funflood
set fun(floodnum) [lindex [split $fun(flood) :] 0]
set fun(floodtime) [lindex [split $fun(flood) :] 1]
set i [expr $fun(floodnum) - 1]
while {$i >= 0} {
   set funflood($i) 0
   incr i -1
}
}
fun:flood:init

proc fun:flood {nick uhost} {
global fun funflood botnick
if {$fun(floodnum) == 0} {
   return 0
}
set i [expr $fun(floodnum) - 1]
while {$i >= 1} {
   set funflood($i) $funflood([expr $i - 1])
   incr i -1
}
set funflood(0) [unixtime]
if {[expr [unixtime] - $funflood([expr $fun(floodnum) - 1])] <= $fun(floodtime)} {
   if {$fun(ignore)} {
      newignore [join [maskhost *!*[string trimleft $uhost ~]]] $botnick "Flood" $fun(ignoretime)
   }
   return 1
} {
   return 0
}
}

proc fun:pub:bot {nick uhost hand channel arg} {
global fun bot botnick
if ![matchattr $nick $fun(ignflags) $channel] {
   if {[fun:flood $nick $uhost]} {
      return 0
   }
}
if {(([lsearch -exact [string tolower $bot(chans)] [string tolower $channel]] != -1) || ($bot(chans) == "*")) && (![matchattr $hand b]) && ($nick != $botnick)} {
   putserv "PRIVMSG $channel :[lindex $bot(msgs) [rand [llength $bot(msgs)]]]"
   return 1
}
}

if {(($blagues(time) != 0) && (![string match "*fun:blagues*" [timers]]))} {
 timer $blagues(time) fun:blagues
}

proc fun:blagues {} {
global blagues
foreach channel $blagues(chans) {
   puthelp "PRIVMSG $channel :[lindex $blagues(msgs) [rand [llength $blagues(msgs)]]]"
}
if {(($blagues(time) != 0) && (![string match "*fun:blagues*" [timers]]))} {
   timer $blagues(time) fun:blagues
}
return 1
}

I would like to create a command !addtemp X ( X= temp ) And what it modifies the time which is situated on the line.

Example : At present the line is

Code: Select all

set blagues(time) 5
I would want that when I write on the channel !addtemp 10
the line becomes

Code: Select all

set blagues(time) 10
Last edited by loulou7593 on Wed Oct 30, 2013 7:25 pm, edited 1 time in total.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Please avoid reviving old topics. Just create your own and link to the old one if you really need to.

Edit: What exactly you want to to? Adjust the time the bot parses the jokes file or want to read a line with a certain line number?

If you could paste the script you want to adjust, we have a lot of members that are more than willing to help out.
Last edited by caesar on Wed Oct 30, 2013 2:39 pm, edited 2 times in total.
Once the game is over, the king and the pawn go back in the same box.
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Re: File operations

Post by willyw »

loulou7593 wrote:Good evening, I do not know how to replace a precise line,
...

Perhaps some of the info found here:
http://forum.egghelp.org/viewtopic.php?t=6885
would be helpful.
l
loulou7593
Voice
Posts: 12
Joined: Tue Apr 08, 2008 12:52 pm

Post by loulou7593 »

Thx Willyw

I tested this

Code: Select all

set chanback "#chan"
bind pub n !addtemp addtemp
proc addtemp {nick host hand chan arg} { 
global botnick
# File name to read.
set fname "scripts/blagues.tcl"

# Open file for read access (note we're not catching errors, you might
# want to use catch {} if the file might not exist.
set fp [open $fname "r"]

# Here we read in all of the data.
set data [read -nonewline $fp]

# Close the file, since we're done reading.
close $fp


# Use the code from above (1.) to read in all the lines from the file.
# We continue right after: 
set lines [split $data "\n"]

# For the beginning, use line 0, since tcl starts counting at 0. If you want
# to append the line to the end, see the next question for a better way.
set temp [lindex [split $arg " "] 0]
   if {$temp == ""} {
		puthelp "PRIVMSG $::chanback :Time Please specify the time."
		return 0
	}
	

# delete ALL lines ending with " user8"
set match "*set*blagues(time)*"
while {[set i [lsearch -glob $lines $match]]>-1} {
   set set "set"
   set bla "blagues(time)"
   set lines "[lreplace $lines $i $i $set $bla $temp]"
}

 

# Now we insert the line into our list in memory.
#set lines [linsert $lines $where_to_insert $var $arq $temp]

# And now we re-write the file, just like in 3.
set fp [open $fname "w"]
puts $fp [join $lines "\n"]
close $fp }
It deletes me well the line which I want but that writes it to me on several lines

Code: Select all

# Intervalle de temps entre chaque blague en minutes (0=désactivé) :
set
blagues(time)
10


I do not manage to have all on the same line
User avatar
CrazyCat
Revered One
Posts: 1303
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

As replyed to your french similar post, modifying the source of the script is dummy. It'll not takes effect until you rehash or restart the eggdrop.
Doing a set ::blagues(time) XX is better.

btw, here is a small modification of your (silly) script:

Code: Select all

set chanback "#chan"
bind pub n !addtemp addtemp
proc addtemp {nick host hand chan arg} {
   if {[llength $arg]!=1} { return 0; }
   # File name to read.
   set fname "scripts/blagues.tcl"
   # Open file for read access (note we're not catching errors, you might
   # want to use catch {} if the file might not exist.
   set fp [open $fname "r"]
   # Here we read in all of the data.
   set data [read -nonewline $fp]
   # Close the file, since we're done reading.
   close $fp

   set fo [open $fname "w"]
   set match "*set*blagues\(time\)*"
   foreach line [split $data "\n"]
      if {[string match $match $line]} {
         set line "set blagues(time) $arg"
      }
      puts $fo $line
   }
   close $fo
}
Post Reply