mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jan 2016
Posts: 2
D
Bowl of petunias
OP Offline
Bowl of petunias
D
Joined: Jan 2016
Posts: 2
Hi All, I'm trying to create a script in mirc that checks a value from a external page.

I start with open a socket to the page and read the value, and end with a if condition.

The problem is that the parameter %follow is set to late. I have tried to use a timer to delay the if condition but I can't make it work. Any suggestion?

Here is my script:
Code:
alias follower {
  sockclose follower
  sockopen follower www.albinstuff.net 80
}
on *:sockopen:follower: {
  sockwrite -n follower GET /twitch2.php?id=user1&follows=user2 HTTP/1.0
  sockwrite -n follower Host: www.albinstuff.net
  sockwrite -n follower
}
on *:sockread:follower: {
  var %read
  sockread -f %read
}
on *:sockclose:follower: {
  var %read
  sockread -f %read
  set %follow %read
  echo -a %follow
}

on *:text:!follow:#: { 
  set %follow 0
  follower
  if (%follow = true) {
    msg $chan You are a follower $nick
  }
  elseif (%follow = false) {
    msg $chan You are not a follower $nick
  }
  else {
    msg $chan Sorry $nick, a error happen
  }
}


Output now:
Code:
[13:04] <user1> !follow
[13:04] <user2> Sorry user1, a error happen
false


Want output:
Code:
[13:04] <user1> !follow
false
[13:04] <user2> You are not a follower user1

Joined: Jul 2006
Posts: 4,202
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,202
That is because socket are asynchronous, they do not 'block' the execution of the script, when you call /follower inside the on text event, mIRC execute that alias and once that alias is done, moves on to the next thing, and the alias is only calling /sockclose & /sockopen, it's not setting the %follow variable, which is why %follow is empty.
You don't want to delay, you want to check for %follow once %follow is set.
The issue for beginner is "but how am I going to send a message to the channel from where %follow is set since i don't have $chan available?", and the answer is to just store the channel name.
There is, basically, a variable tied to the socket, you can use /sockmark <socket> <data> to store data tied to the socket.

Also, it looks like you only want the last line, you're using the on sockclose event for that, which is correct, but you should not use -f inside the on sockread, since it would read anything, even if it's not a line.
Try this:

Code:
alias follower {
  sockclose follower
  sockopen follower www.albinstuff.net 80
  ;$1 is $chan from the on text event, $2 is the nickname
  sockmark follower $1-
}
on *:sockopen:follower: {
  sockwrite -n follower GET /twitch2.php?id=user1&follows=user2 HTTP/1.0
  sockwrite -n follower Host: www.albinstuff.net
  sockwrite -n follower
}
on *:sockread:follower: {
  var %read
  sockread %read
}
on *:sockclose:follower: {
  var %read
  ;makes $1 the channel and $2 the nickname
  tokenize 32 $sock($sockname).mark
  sockread -f %read
  if (%read == true) {
    msg $1 You are a follower $2
  }
  elseif (%read == false) {
    msg $1 You are not a follower $2
  }
  else {
    ;you had $nick, previously, this is incorrect, mirc would try to evaluate "$nick,", you need $+ to concatenate
    msg $1 Sorry $2 $+ , a error happen
  }
}

on *:text:!follow:#: { 
  follower $chan $nick
}


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jan 2016
Posts: 2
D
Bowl of petunias
OP Offline
Bowl of petunias
D
Joined: Jan 2016
Posts: 2
It works perfectly! Very good explained, thank you Wims!!


Link Copied to Clipboard