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 

[SOLVED] Need help with file handling - thanks nml375!
Goto page 1, 2  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 6:21 pm    Post subject: [SOLVED] Need help with file handling - thanks nml375! Reply with quote

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:
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 Sad
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
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Mar 25, 2009 7:28 pm    Post subject: Reply with quote

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:
#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 ".
_________________
NML_375, idling at #eggdrop@IrcNET


Last edited by nml375 on Wed Mar 25, 2009 9:19 pm; edited 2 times in total
Back to top
View user's profile Send private message
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 8:14 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Mar 25, 2009 8:23 pm    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 8:29 pm    Post subject: Reply with quote

well,

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

hm?
Back to top
View user's profile Send private message MSN Messenger
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 8:38 pm    Post subject: Reply with quote

it was my mistake...
.set errorInfo worked

Code:
[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"
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Mar 25, 2009 8:57 pm    Post subject: Reply with quote

Ahh...
Guess I edited my code once too many, I'll update my previous post in a sec
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 9:11 pm    Post subject: Reply with quote

the error is now gone
this what happens

the file looks like
Code:
header goes here
<!-- post here --->
footer goes here


instead of putting the post between <!-- post here ---> and footer
it looks like
Code:
                  <!-- post here ---> http://www.google.com/2


it deletes the footer and the header too Sad

and just changes the current link to another after adding a new one...
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Mar 25, 2009 9:22 pm    Post subject: Reply with quote

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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 9:36 pm    Post subject: Reply with quote

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:
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:
header goes here
<!-- post here --->
footer goes here


and now
Code:

header goes here
 http://www.google.com/ ter
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Mar 25, 2009 9:54 pm    Post subject: Reply with quote

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:
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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 9:58 pm    Post subject: Reply with quote

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

Code:
header goes here
<!-- post here ---
http://www.google.com/
footer goes here


the last > from <!-- post here ---> is missing...
everything else is just fine Smile
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Mar 25, 2009 10:10 pm    Post subject: Reply with quote

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

Code:
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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Wed Mar 25, 2009 10:15 pm    Post subject: Reply with quote

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

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

thanks again!!
Back to top
View user's profile Send private message MSN Messenger
zerodtk
Voice


Joined: 25 Mar 2009
Posts: 20

PostPosted: Thu Mar 26, 2009 8:52 am    Post subject: Reply with quote

hey! all working! everything's just fine Smile

thanks again
Back to top
View user's profile Send private message 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
Goto page 1, 2  Next
Page 1 of 2

 
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