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.

Small socket script

Old posts that have not been replied to for several years.
Locked
D
DarkJFMan
Halfop
Posts: 85
Joined: Mon Dec 15, 2003 3:19 pm

Small socket script

Post by DarkJFMan »

I'm just testing out socket with eggdrop, and have not been successful at all. Can someone make a script real quick or has a link to one similar where, for example:

Someone types: -projects

It will go to http://www.tclscript.com/scripts.shtml and get how many Total Projects there is.

I have egghttp.tcl on, I looked at http://www.tclscript.com/egghttp_tut.shtml but still no luck getting the right script.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

there's no need of egghttp, TCL comes with built-in http package:

Code: Select all

package require http
bind pub - -projects aproc
proc aproc {n u h c t} {
  set token [::http::geturl http://www.tclscript.com/scripts.shtml]
  set data [::http::data $token]
  # parse $data, see how many project lines there is
  puthelp "privmsg $c :total number of projects: $total"
  ::http::cleanup $token
}
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

that depends if you want async connections or not cause if i recall well

that script uses async sockets..
XplaiN but think of me as stupid
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

for fetching a single, simple webpage you normally don't need async I/O; if you really need that, you can use -command option of [::http::geturl], which specifies a callback to be invoked when HTTP transaction is complete ([geturl] returns immediately)

I've never used egghttp, but would guess it's been written for older TCL versions which don't provide the http package
D
DarkJFMan
Halfop
Posts: 85
Joined: Mon Dec 15, 2003 3:19 pm

Post by DarkJFMan »

Thanks a lot for your reply demond but I got this for error
[22:53] Tcl error [aproc]: can't read "total": no such variable
What should I set it to?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

this is not a complete script, you need to code the commented part yourself:

Code: Select all

# parse $data, see how many project lines there is 
and calculate $total appropriately (search forum for "parse html" on info how to do that; hint: you may need to strip HTML tags first)

BTW, this isn't a forum for complete scripts also
D
DarkJFMan
Halfop
Posts: 85
Joined: Mon Dec 15, 2003 3:19 pm

Post by DarkJFMan »

I'm appreciating your help, the thing is I search "parse html" I wasn't able to find something on this forum, do you have a link where i could find how I can finish the script, I'm not asking you to make it, I just need few more hints.

The thing is, I have my script in mIRC and it works fine, I just need a small help in converting it into tcl. I know what I need to do, but not sure how to do it.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

well, basically you need to read through that webpage and count the lines which contain a project description; all you gotta do is:

Code: Select all

set total 0
foreach line [split [::http::data $token] \n] {
  if [string match *someprojectpattern* $line] {incr total}
}
D
DarkJFMan
Halfop
Posts: 85
Joined: Mon Dec 15, 2003 3:19 pm

Post by DarkJFMan »

For the example I'm using with website http://www.tclscript.com/scripts.shtml if you scroll to bottom you see " [ Total Projects: 35 ]". So I'm trying to make my bot say: "There are total of 35 projects. Of course 35 will be update with the site, if you get what I mean, so later if the site says "[ Total Projects: 36 ]", my bot will say: "There are total of 36 projects.

I will put the info you gave me together, and post more questions later, thanks.
D
DarkJFMan
Halfop
Posts: 85
Joined: Mon Dec 15, 2003 3:19 pm

Post by DarkJFMan »

The reason I wasn't able to get what you mean by the script you gave me, because you were thinking of a different thing, the script I have in mind isn't where it looks at each line to increase the value of $total, I'm really sorry to bother you, but the script I'm looking at is almost close to a google script for an example.


After typing -projects instead of counting the projects 1 by 1, I was looking for a script that msg the channel " [ Total Projects: 35 ]", which is found at the bottom of the page http://www.tclscript.com/scripts.shtml so it won't be adding them up, only reading the number "35" that's why I thought egghttp would help me because it has (.*) after looking at http://www.tclscript.com/egghttp_tut.shtml. I hope you know what I mean, and I'm not giving you too much trouble.

Looking at the tutorial, it's a DCC trigger, I think I want it to be a public trigger msged to the channel insstead of saved in the log.

Thanks again.
D
DarkJFMan
Halfop
Posts: 85
Joined: Mon Dec 15, 2003 3:19 pm

Post by DarkJFMan »

With the help of the tutorial...

Code: Select all

# egghttp_example.tcl

# Config
set url "http://www.tclscript.com/scripts.shtml"
set dcctrigger "example"
# End of config

if {![info exists egghttp(version)]} {
  putlog "egghttp.tcl was NOT successfully loaded."
  putlog "egghttp_example.tcl has not been loaded as a result."
} else {
  proc your_callbackproc {sock} {
    global url
    set headers [egghttp:headers $sock]
    set body [egghttp:data $sock]
  
    regsub -all "\n" $body "" body
    regsub -all -nocase {<br>} $body "<br>\n" body

    regexp {Total Projects: <u>(.*)</u>} $body - served

    puthelp "PRIVMSG #macked :There are total of $served projects so far."
  }

  bind dcc o|o $dcctrigger our:dcctrigger
  proc our:dcctrigger {hand idx text} {
    global url 
    set sock [egghttp:geturl $url your_callbackproc]
    return 1
  }  

  putlog "egghttp_example.tcl has been successfully loaded."
}
For testing I edited the tutorial of http://www.tclscript.com/egghttp_tut.shtml but still no luck, I couldn't change the DCC bind trigger to pub for some reason, it wasn't working right, and I kept getting "wrong # args".

Also, my bot is msging the channel

There are total of 35</u> ] - [ Total Projects For Download: <u>31</u> ] - [ Total Downloads: <u>117518 projects so far.

However all I want is "There are total of 35</u> ] - [ Total Projects For Download: <u>31</u> ] - [ Total Downloads: <u>117518 projects so far."

Also the reason I have <u>(.*)</u> because if I don't I get "There are total of <u>35</u> ] - [ Total Projects For Download: <u>31</u> ] - [ Total Downloads: <u>117518</u> ] </center> <font><br>" which is more messed up, I know it sounds weird but... :/

The underlined is completely unneeded, only the bolded part. Thanks for help.[/u]
User avatar
Robb
Voice
Posts: 1
Joined: Mon Jun 13, 2005 6:07 am

Post by Robb »

I prefer to use pure sockets.. :)

Try this one..

Code: Select all

set url "www.tclscript.com"

  proc get_projects {nick uhost handle channel text} {
    global url
    set sock [socket $url 80]
    fconfigure $sock -buffering line -buffersize 1000
    puts $sock "GET /scripts.shtml HTTP/1.0"
    puts $sock "Host: www.tclscript.com"
    puts $sock ""
    flush $sock
    
    while {![eof $sock]} {
    	gets $sock body
    	regexp -all {Total Projects: <u>(.*)</u>} $body _ served
    }
    
    if {[eof $sock]} { close $sock }
    
    regsub -all {(<.+?>)} $served "" served
    
    puthelp "PRIVMSG $channel :www.tclscript.com has a total of [lindex $served 0] projects so far."
    
}

bind pub - !projects get_projects
Locked