mIRC Home    About    Download    Register    News    Help

Print Thread
#190309 18/11/07 11:37 PM
Joined: Nov 2007
Posts: 9
S
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
S
Joined: Nov 2007
Posts: 9
I'm trying to learn how to script sockets, and it isnt going well. I dont know how close I am, but heres my code:

Code:
on *:TEXT:!bash:#:{
  if ($me != Prometheus) { halt }
  else { echo triggered | sockopen bsocket bash.org 80 | set %nick $nick }
}

on *:SOCKOPEN:BSOCKET:{
  sockwrite -nt $sockname GET http://bash.org/?random
  sockwrite -nt $sockname Host: bash.org
  sockwrite -nt $sockname $crlf
  echo opened
}

on *:SOCKREAD:BSOCKET:{
  echo reading
  var %bash
  sockread %bash
  notice %nick %bash
  sockclose $sockname
  unset %nick
  echo closed
}


It returns a line of HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

I dont know what that means. Can anyone help?

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
Use a while loop to read the entire document.

Code:
sockread %bash
while ($sockbr) {
sockread %bash
}


Fool around with this.

Joined: Nov 2007
Posts: 9
S
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
S
Joined: Nov 2007
Posts: 9
I still can't make it work. Example?

Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
If you read the help file a bit more thoroughly, you'll read two things:

1) ON SOCKREAD will trigger every time something new comes into that socket, or something remains in the socket's queue, and will re-trigger if there is still things to read.

2) A /sockread command will read into a non-binary variable up until the next carriage-return/line-feed/crlf. You can also see if there's more to read by checking the $sockbr identifier's state.

Take these two together, and you can make some assumptions:

- Let ON SOCKREAD trigger many times, reading from the socket one line at a time.

- Set up a loop of some kind (while/goto) and read until $sockbr is no longer 0.

Now, the code as it stands reads one line, then closes the socket, forcing mIRC to discard the remaining data in the socket connection.

Don't close the socket in your ON SOCKREAD, leave it until an ON SOCKCLOSE event is triggered, or set up a timer and make your own connection timeout.

What does this all translate to?

Code:
ON *:SOCKREAD:bsocket: {
  var %bash
  while ($sockbr != 0) {
    sockread $sockname %bash
    <manipulate the data here>
  }
}
ON *:SOCKCLOSE:bsocket: {
  sockclose $sockname
}


.. Roughly ..

Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
In mind of making it a bit more resilient to multiple users, you may want to look at the /sockmark command, and it's associated identifers.

Example code to look at can be found here.

Joined: Feb 2006
Posts: 546
J
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
in the sockread event, $sockbr is equal to 0 initially (since /sockread hasn't been used yet) so that while loop is never entered :P that's why Loki included the extra /sockread above the loop

an alternative to that would've been:

Code:
while ($sock($sockname).rq) {
  sockread -f %var
  ; commands with %var
}


i think you made a typo! /sockread $sockname %bash? :S


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
Yeah.. was at work, no help file to refer to smile


Link Copied to Clipboard