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.

[SOLVED] Need help with file handling - thanks nml375!

Help for those learning Tcl or writing their own scripts.
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

[SOLVED] Need help with file handling - thanks nml375!

Post by zerodtk »

hello there eggdrop users
i need a little help with this file handling stuff
i'm working on a url catcher script, which catches every said urls on the channel, and makes them shown on a webpage
there's one script like this out there which uses mysqltcl and stores the data into a mysql database
what i am doing is really simple
catches all said urls with *http* and puts them in index.html
my problem that the bot puts every url after the latest one
so if there is a link on the page, the second one goes after

what i actually want is to put a pre defined line into the html
for example <---post here--->
and to make the bot put every post after this line and before the old posts
so every new post will be on the first place

Code: Select all

proc catch_url_pubm {nick host hand chan arg} {
  if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
    if {![catch {open /home/zerodtk/public_html/log/index.php "a+"} af]} {
      set type "unknown"
      if {![catch {::http::geturl $url} http]} {
        if {[regexp -nocase {content-type image|content-type .?image} [::http::meta $http]]} {
          set type "image"
	puts $af "$nick added a new image $url"
	close $af
 } else


etc etc
i think an IF command would do the job, but i can't get it work :(
let me know if you know any solution for this
thanks
Last edited by zerodtk on Sat Mar 28, 2009 8:56 am, edited 3 times in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Actually, this will take a little more than a simple if-conditional.
The problem at hand, is that when you write to a file, by default the file pointer will be at the end. If you move it to an earlier point within the file (using seek), you'll instead overwrite whatever was there before.

To make this work, you'll need to read the whole file into memory, split the file in two (at the point you wish to insert the text), and combine your new url and the two halves into a new string. This you then write to your file.

Below you'll see an example on how to do this. Implementing/adapting this to suite your code shouldn't be too hard. Feel free to ask if anything is unclear or confusing.

Code: Select all

#string contains the whole file content
#url contains the whole line to insert into the file
#af is the handle of the opened file. File must be opened with read&write permissions.
set string [read $af]
set key "<!-- post here --->"
set j [expr [string first $key $string] + [string length $key]]
set i [expr $j - 1]
set first [string range $string 0 $i]
set last [string range $string $j end]
seek $af $j
puts -nonewline $af "${first}${url}${last}"
Edit: Added missing "[expr ".
Last edited by nml375 on Wed Mar 25, 2009 9:19 pm, edited 2 times in total.
NML_375
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

whenever i want to put in a set
f.e.
set string [read $af]

it says
Tcl error [catch_url_pubm]: wrong # args: should be "set varName ?newValue?"

any advice?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

That looks very odd. The syntax is correct.
You could try and enable the .set command (see your configfile on how to do this), and type ".set errorInfo" on the dcc partyline right after the error occurs.
NML_375
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

well,

[01:28:41] [(Angela]: [01:26] #z3r0# set errorinfo
[01:28:41] [(Angela]: Error: can't read "errorinfo": no such variable

hm?
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

it was my mistake...
.set errorInfo worked

Code: Select all

[01:37:46] [(Angela]: [01:35] #z3r0# set errorInfo
[01:37:46] [(Angela]: Currently: wrong # args: should be "set varName ?newValue?"
[01:37:46] [(Angela]: Currently: while executing
[01:37:46] [(Angela]: Currently: "set j [string first $key $string] + [string length $key]]"
[01:37:46] [(Angela]: Currently: (procedure "catch_url_pubm" line 6)
[01:37:46] [(Angela]: Currently: invoked from within
[01:37:46] [(Angela]: Currently: "catch_url_pubm $_pubm1 $_pubm2 $_pubm3 $_pubm4 $_pubm5"
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Ahh...
Guess I edited my code once too many, I'll update my previous post in a sec
NML_375
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

the error is now gone
this what happens

the file looks like

Code: Select all

header goes here
<!-- post here --->
footer goes here
instead of putting the post between <!-- post here ---> and footer
it looks like

Code: Select all

                  <!-- post here ---> http://www.google.com/2
it deletes the footer and the header too :(

and just changes the current link to another after adding a new one...
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

That's odd..
I missed including the file pointer in the puts-line, but that shouldn't wipe your file.. (updated post to fix that though)

Do you think you could post your implementation of my code?
NML_375
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

it was set to w+ not a+
actually its not deleting the header now
but deletes the <!-- post here ---> and the first few characters of the footer..

Code: Select all

proc catch_url_pubm {nick host hand chan arg} {
  if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
    if {![catch {open scripts/urllogger.txt "a+"} af]} {
	set string [read $af]
	set key "<!-- post here --->"
	set j [expr [string first $key $string] + [string length $key]]
	set i [expr $j - 1]
	set first [string range $string 0 $i]
	set last [string range $string $j end]
	seek $af $j
puts -nonewline $af "$first $url $last"
#	puts $af "$url"
 close $af
} } }

bind pubm -|- *http* catch_url_pubm

it was

Code: Select all

header goes here
<!-- post here --->
footer goes here
and now

Code: Select all

header goes here
 http://www.google.com/ ter
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Actually, I do spot a flaw in my code. But it should produce an entirely different behaviour to what you are describing.

'k.. after a few minutes of thinking.. the problem at hand, is that you're opening the file in append-mode. That basically means that the file pointer is set to the end of the file... reading the file thus returns nothing. Opening the file in normal read/write mode solves that issue. I also removed some double-work I did in my first post.

The code below should behave properly.

Code: Select all

proc catch_url_pubm {nick host hand chan arg} {
  if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
    if {![catch {open scripts/urllogger.txt "RDWR CREAT"} af]} {
      set string [read $af]
      set key "<!-- post here --->"
      set j [expr [string first $key $string] + [string length $key]]
      set last [string range $string $j end]
      seek $af $j start
      puts -nonewline $af "\n${url}${last}"
      close $af
    }
  }
}

bind pubm -|- *http* catch_url_pubm
NML_375
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

i think we are in the finish..
but there's one more thing

Code: Select all

header goes here
<!-- post here ---
http://www.google.com/
footer goes here
the last > from <!-- post here ---> is missing...
everything else is just fine :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

'k.. ohh, forgot, seek uses bytes, not characters.
Guess we'll have to do it the other way *sigh*

Code: Select all

proc catch_url_pubm {nick host hand chan arg} {
  if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
    if {![catch {open scripts/urllogger.txt "RDWR CREAT"} af]} {
      set string [read $af]
      set key "<!-- post here --->"
      set j [expr [string first $key $string] + [string length $key]]
      set i [expr $j - 1]
      set first [string range $string 0 $i]
      set last [string range $string $j end]
      seek $af 0 start
      puts -nonewline $af "${first}\n${url}\n${last}"
      close $af
    }
  }
}

bind pubm -|- *http* catch_url_pubm
NML_375
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

it works!!! thank you very much!!!
i can't believe it all works now :)

i will put it in the full script tomorrow morning and will let you know :)

thanks again!!
z
zerodtk
Voice
Posts: 20
Joined: Wed Mar 25, 2009 6:06 pm

Post by zerodtk »

hey! all working! everything's just fine :)

thanks again
Post Reply