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.

Read from a TXT file in alternative lines...

Old posts that have not been replied to for several years.
Locked
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Read from a TXT file in alternative lines...

Post by Thunderdome »

Well... I have a txt file for a java applet news.... that goes like this:

##### bla bla bla code
text text text text
##### bla bla bla code
text text text text
##### bla bla bla code
text text text text
##### bla bla bla code
text text text text



I have a tcl script:


#**********#
# SETTINGS #
#**********#

# set this to the full location of the news file
set newsfile "scripts/NewsMaker.txt"

# want the answer to come out in notice (0) or privmsg (1)?
set tellwho 0

# what channels do you want the script to work on?
# write "*" to disable.
set newschan "#channel"

#*******#
# BINDS #
#*******#

bind pub - !news holm-pub_news

#************************#
# DO NOT EDIT UNDERNEATH #
#************************#

putlog "holm-news.tcl $ver by tw|ster (Stian Holm) loaded ..."

proc holm-pub_news {nick uhost handle channel text} {
global newsfile tellwho newschan
if {(([lsearch -exact [string tolower $newschan] [string tolower $channel]] != -1) || ($newschan == "*"))} {
if {![file exists "$newsfile"]} {
if {$tellwho} {
putserv "PRIVMSG $nick :.:error:. sorry, but i can't find the news file!"
} else {
putserv "NOTICE $nick :.:error:. sorry, but i can't find the news file!"
}
return 0
}
if {![file readable "$newsfile"]} {
if {$tellwho} {
putserv "PRIVMSG $nick :.:error:. sorry, but i'm not allowed to read that file!"
} else {
putserv "NOTICE $nick :.:error:. sorry, but i'm not allowed to read that file!"
}
return 0
}
set news [open "$newsfile" r]
while {![eof $news]} {
set line [gets $news]
if {$tellwho} {
putserv "PRIVMSG $nick :$line"
} else {
putserv "NOTICE $nick :$line"
}
}
close $news
}
}

I modified the tcl script a bit for my own purposes... but I dont want it to read the full text! I just want it to read the lines with no ###
So, either the script ignores lines that start with #### or...
it reads like "line no, line yes, line no, line yes"

Can you guys help me out?
I know this may seem simple but I am complete newbie....
Greetz and thanks for the page and forum! Really helpfull![/quote]
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Code: Select all

if ![string match ####* $line] {
  # parse it
}
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

sorry for the n00bity, but where do I place that code?
greetz and thanks for the help! :)
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Just replace that while loop in your code with this one

Code: Select all

while {![eof $news]} {
 set line [gets $news]
 if {![string match ####* $line]} {
  if {$tellwho} {
   putserv "PRIVMSG $nick :$line"
   } else {
   putserv "NOTICE $nick :$line"
  }
 }
}
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

works like a charm! :D :D :D
but.....


set newsfile "http://someurl.com/file.txt"


this does not work... only works for root directory or any shell directory and not a site outside the shell...
how do I manage to put a txt from an outside site?
greetz
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Downloading files off the internet and then processing them is obviously a whole different ballgame than just reading a local file.

Have a look at the http package that comes with tcl, or possibly egghttp.tcl.

There should be plenty of examples on how to use both on this forum, just search for it.
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

I've read all forum stuff that had the egghttp word... I see it's related (and I use the egghttp.tcl myself in my bot)... but I dunno really how to aplly it to this script... :|
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Code: Select all

# Set the chans you want to enable it in to +news
# .chanset #channel +news

# set this to the URL of the news file
set newsfile "http://someurl.com/file.txt" 

# want the answer to come out in notice (0) or privmsg (1)?
set tellwho 0 

setudef flag news

if ![info exists egghttp(version)] {
  putlog "egghttp.tcl was NOT successfully loaded."
  return
}

bind pub - !news getlist

proc getlist {nick uhost hand chan text} {
 if ![channel get $chan news] return
 set sock [egghttp:geturl $::newsfile [list processlist $nick]]
}

proc processlist {nick sock} {
 set data [egghttp:headers $sock]
 egghttp:cleanup $sock
 set parse 0
 foreach line [split $data \n] {
  if $parse&&![string match ####* $line] {
   if $::tellwho {
    putserv "NOTICE $nick :$line"
   } {
    putserv "PRIVMSG $nick :$line"
   }
  }
  if [string match -no Content-Type:* $line] {set parse 1}
 }
}

Last edited by greenbear on Fri Mar 18, 2005 12:44 am, edited 1 time in total.
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

Test Results:

well, the code seems fine... I used it and no problem with the bot... I also made the .chanset command....

but when I type !news.... nothing shows up! :?
What could it be :?:
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Turns out .txt files are sendt as part of the header...
I edited the code above, so go ahead and give it a try.
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

Thanks a LOT! :) Really... works very well! Exactly what I imagined...
You really know your way into TCL scripting! :)
If you come to Portugal say hello and I'll buy you a beer! :)
Greetz and Respect
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

gb wrote:Turns out .txt files are sendt as part of the header...
They're not... but egghttp.tcl looks for a "<" to detect the end of the header. (because connections made using "connect" can't recieve the empty line that denotes the end of the header)
If your text contains a "<" some where in the middle, the contents will be split at that point (every line including the first one with a "<" in it ends up in the body)
Have you ever read "The Manual"?
E
ElZeRo
Voice
Posts: 24
Joined: Mon Apr 11, 2005 12:04 pm
Contact:

Post by ElZeRo »

Hi there ...

Iwe Testet it too -... Working 100% with a txt file ... what changes are needed so that the script can read out "php" scripts --- i could use it togehter with my Ut2004 server Script .. (eggdrop dossn't support UDP direct) ... so i have "this" http://www.elzero.dk/query/index.php that has all INFO's that i need ... But how do i convert them into "eggdrop" ??? ... And I dont NEED everything there :-) ... Only "Map name", "server ip", "number of players" , "max players" and "server ip".

Regards
ElZeRo
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

I need exactly the same now... How can one do it?
Locked