mIRC Home    About    Download    Register    News    Help

Print Thread
#271775 12/06/23 08:43 AM
Joined: Apr 2022
Posts: 25
Z
Zenti Offline OP
Ameglian cow
OP Offline
Ameglian cow
Z
Joined: Apr 2022
Posts: 25
hy i'm new with sockopen etc..

i try to make a script that connects to www.github.com

but it says not connected after i try gitpush so whats wrong?

Code
alias gitpush { 
  sockopen gitpush http://github.com 80
}

on *:SOCKOPEN:gitpush: {
  sockwrite -n $sockname GET /svdermant/ HTTP/1.0
}

on *:SOCKREAD:gitpush : {
  sockread %gitpush
}

here that is my error..
* /sockwrite: 'gitpush' not connected (line 6, remote.ini)

Zenti #271778 12/06/23 06:32 PM
Joined: Jul 2006
Posts: 4,153
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,153
You get this error because your socket did not connect successfully, and the reason why is because the name of the server here is github.com, http or https are just the protocol, you need to use "/sockopen gitpush github.com 80" at the very least.
Inside the on sockopen event, you can use $sockerr to know if the connection was successful.
Inside the on sockread event, you're reading into a global variable, you should probably make that variable local by using "var %gitpush" before /sockread %gitpush, and if you're looking to read line by line, you should check that data was read by checking $sockbr. if $sockbr is 0, no data was read and you should stop doing anything with a /return or something.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Wims #271780 12/06/23 07:06 PM
Joined: Apr 2022
Posts: 25
Z
Zenti Offline OP
Ameglian cow
OP Offline
Ameglian cow
Z
Joined: Apr 2022
Posts: 25
I've tried it this way, but mirc returns a 408 error request timeout.
Code
alias Example {
  /sockopen Example github.com 80

  ; Check if there was an error.
  if ($sockerr) {
    echo -ag There was an error: $sockerr
    halt
  }
}

; Listen for the on SOCKOPEN event.
on *:SOCKOPEN:Example:{
  ; Write a GET request.
  sockwrite -nt $sockname GET / HTTP/1.1
  ; Check if there was an error.
  if ($sockerr) {
    echo -ag There was an error: $sockerr
    halt
  }
}

; Listen for the on SOCKREAD event.
on *:SOCKREAD:Example:{
  ; Check if there was an error.
  if ($sockerr) {
    echo -ag There was an error: $sockerr
    halt
  }

  ; Create a local variable to hold the data.
  var %i

  ; Read the data waiting into the local variable.
  sockread %i

  ; Print data read.
  echo -ag Data = %i
  %a = %i
}


Output from mirc:
Code
Data = HTTP/1.1 408 Request Time-out
Data = Content-length: 110
Data = Cache-Control: no-cache
Data = Connection: close
Data = Content-Type: text/html
Data =
Data = <html><body><h1>408 Request Time-out</h1>
Data = Your browser didn't send a complete request in time.
Data = </body></html>

If I open github.com with my browser e.g. Firefox
then I don't get a 408 error so why with mirc?

Zenti #271781 12/06/23 07:37 PM
Joined: Jul 2006
Posts: 4,153
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,153
That's because your browser speaks the http protocol language a bit more in depth than your script.

Your request is timing out because you're not sending an empty crlf at the end, try sending it.

Note and tips: typically people /sockclose the socket name before attempting to /sockopen as that will create an error if the socket name is already in use, $sockerr should be checked prior any /sockwrite inside on sockopen, not after.
Inside on sockread, in practice people check $sockerr once before any /sockread, but won't check $sockerr again if they use /sockread again later on.
The help file says that $sockerr should be checked after any socket command and before processing events, but in practice scripts are typically only checking $sockerr before processing events, there are no known cases of $sockerr being UNset before executing a command and set after.
You can get more info about socket error with $sock(socketname).wsmsg

Try this

alias Example {
sockclose example
sockopen Example github.com 80

; Check if there was an error.
; in practice this check is not required, if anyone know how to get $sockerr set after executing a /sockopen command that did not result in a script error, let me know
if ($sockerr) {
echo -ag There was an error: $sockerr
halt
}
}

; Listen for the on SOCKOPEN event.
on *:SOCKOPEN:Example:{
if ($sockerr) {
echo -ag there was an error: $sockerr : $sock($sockname).wsmsg
; we usually /return event, not halt them unless you specifically want to halt, when we want to stop processing)
return
}

; Write a GET request.
sockwrite -nt $sockname GET / HTTP/1.1
sockwrite -n $sockname
; Check if there was an error. again this check is not required in practice, same as above
if ($sockerr) {
echo -ag There was an error: $sockerr
halt
}
}

; Listen for the on SOCKREAD event.
on *:SOCKREAD:Example:{
; Check if there was an error.
if ($sockerr) {
echo -ag There was an error: $sockerr
halt
}

; Create a local variable to hold the data.
var %i

; Read exactly one line from the received buffer, the data being read is put into the local variable.
sockread %i
;if data was received but does not contain a new line, sockread won't read anything and $sockbr is 0, you need to stop processing and wait for the event to trigger again with more data, hopefully with a newline in it.
if ($sockbr == 0) return
; if a line has been found, Print it
echo -ag Data = %i
;not sure what you're trying to do with %a = %i here.
%a = %i
}



Note that reading line by line like this will not read the last line if it does not end with a line ending sequence (crlf or lf), in this case you can either use the content length header to figure out if you need to read by forcing the data to be read with /sockread -f, or if your socket is not being kept alive and is closing itself after the request is done, you can just read with sockread -f from the on sockclose event.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Wims #271782 13/06/23 01:23 AM
Joined: Apr 2022
Posts: 25
Z
Zenti Offline OP
Ameglian cow
OP Offline
Ameglian cow
Z
Joined: Apr 2022
Posts: 25
thanks for you now ... it works at last i want to get the latest commit from my projekt but i dont know the html tags or so..

my code is now:

Code
alias Example {
  sockclose example
  sockopen -e Example github.com 443

  ; Check if there was an error.
  ; in practice this check is not required, if anyone know how to get $sockerr set after executing a /sockopen command that did not result in a script error, let me know
  if ($sockerr) {
    echo -ag There was an error: $sockerr
    halt
  }
}

; Listen for the on SOCKOPEN event.
on *:SOCKOPEN:Example:{
  if ($sockerr) {
    echo -ag there was an error: $sockerr : $sock($sockname).wsmsg
    ; we usually /return event, not halt them unless you specifically want to halt, when we want to stop processing)
    return
  }

  ; Write a GET request.
  sockwrite -nt $sockname GET /svdermant/MinecraftBot/commits/master HTTP/1.1
  sockwrite -n $sockname Host: github.com
  sockwrite -nt $sockname $crlf
  ; Check if there was an error. again this check is not required in practice, same as above
  if ($sockerr) {
    echo -ag There was an error: $sockerr
    halt
  }
}

; Listen for the on SOCKREAD event.
on *:SOCKREAD:Example:{
  ; Check if there was an error.
  if ($sockerr) {
    echo -ag There was an error: $sockerr
    halt
  }

  ; Create a local variable to hold the data.
  set %gitpush
  set %c >
  set %p </p>

  ; Read exactly one line from the received buffer, the data being read is put into the local variable.
  sockread %gitpush
  ;if data was received but does not contain a new line, sockread won't read anything and $sockbr is 0, you need to stop processing and wait for the event to trigger again with more data, hopefully with a newline in it.
  if ($sockbr == 0) return
  ; if a line has been found, Print it
  echo -ag Data = %gitpush
  if (*%c* isin %gitpush) && (*%p* isin %gitpush { set %git %gitpush }
  ;;write info.txt $noHTML(%gitpush)
  echo -a %git
  ;not sure what you're trying to do with %a = %i here.
}

The alias is supposed to work in such a way that when a new commit is made, I always get the latest one displayed... but I don't know how?

Zenti #271784 13/06/23 10:55 AM
Joined: Feb 2015
Posts: 138
kap Offline
Vogon poet
Offline
Vogon poet
Joined: Feb 2015
Posts: 138
Would you consider using SReject's JSON-For-mIRC?

This works:

Code
; First type /getJFM to load SReject's JSON-4-mIRC
; Afterwards you can type /getInfo as many times as you like

alias getInfo {
  var %url = https://api.github.com/repos/svdermant/MinecraftBot/commits/master
  var %jsonname = lcommit
  JSONOPEN -u %jsonname %url
  if ($json(%jsonname).httpStatus == 200) {
    echo -ag Latest commit information:
    echo -ag Author: $json(%jsonname,commit,author,name).value - Date: $json(%jsonname,commit,author,date).value
    echo -ag Additions: $json(%jsonname,stats,additions).value - Deletions: $json(%jsonname,stats,deletions).value
    echo -ag Commit message: $json(%jsonname,commit,message).value
  }
}
alias getJFM {

  ; sha-512 of JSON For mIRC.mrc v1.0.4000
  var %sha512-1 b29de8c964b002d089fa030a9a5357e3c737f17cd16309c0fa82142120a590db2223d4703cafe97f37b65de170bb4d9dbf7bb508f4052a47a2127fc74c10a6d6
  ; group enabled hash
  var %sha512-2 183153806705c99fce8fb3003ea7e5522e9f67c40b3e064280ad83111e3d8f92b4cfae106b8827a9d99c2377c32d6d6892fe878a027bcaea24a466450cfeead8

  ; check if JSON For mIRC is loaded
  if ($isalias(JSONVersion)) {
    ; script is loaded, but what version?
    var %file $isalias(JSONVersion).fname
    if (!$istok(%sha512-1 %sha512-2,$sha512(%file,2),32)) {
      echo -ag Unloading $JSONVersion ...
      .unload -rs $qt(%file) | .timer -mio 1 0 getJFM 
    }
    else echo -ag JFM already loaded!
  }
  elseif ($1 isnum) {
    if ($urlget($1).state == fail) { echo 4 -ag Downloading JSON For mIRC failed. Are you connected to the internet? | halt }
    elseif ($zip($urlget($1).target,eo,scripts)) { 
      var %file $qt($findfile(scripts\JSON-For-Mirc-1.0.4000,JSON For mIRC.mrc,1))
      if ($sha512(%file,2) == %sha512-1) echo -ag Loading JSON For mIRC ver1.0.4000 | .load -rs %file
      else echo 4 -ag Wrong sha512 hash for %file $+ !      
      .remove $urlget($1).target
    }
    else { echo 4 -ag Something went wrong unzipping $:t($urlget($1).target) }
  }
  else { 
    var %url https://github.com/SReject/JSON-For-Mirc/archive/v1.0.4000.zip
    var %target JFM.zip
    echo -ag Obtaining JSON For mIRC...
    return $urlget(%url,gf,%target,getJFM)
  }
}


Quote
Latest commit information:
Author: svdermant - Date: 2023-06-11T12:16:17Z
Additions: 2 - Deletions: 1
Commit message: Update README.mdHinweis zur Ramangabe...

Note: This uses an older version of JFM. Might want to use a newer version.


GNU Terry Pratchett - Looking for a mIRC help channel -> Check #mircscripting @ irc.swiftirc.net
kap #271786 13/06/23 03:21 PM
Joined: Apr 2022
Posts: 25
Z
Zenti Offline OP
Ameglian cow
OP Offline
Ameglian cow
Z
Joined: Apr 2022
Posts: 25
if that works that is what i want i dont need to selfcoded..

Question if i time /getInfo i always get the latest commit ?
Wich params can i add after the variable %jsonname in $json(%jsonname,commit,author,name).value ?
Does a list exist?

Zenti #271787 13/06/23 04:01 PM
Joined: Feb 2015
Posts: 138
kap Offline
Vogon poet
Offline
Vogon poet
Joined: Feb 2015
Posts: 138
You can navigate the JSON. Open this url in your browser and have a look at what it returns:
https://api.github.com/repos/svdermant/MinecraftBot/commits/master

I got the idea from this SO post: https://stackoverflow.com/questions/45726013/how-can-i-get-last-commit-from-github-api


GNU Terry Pratchett - Looking for a mIRC help channel -> Check #mircscripting @ irc.swiftirc.net
kap #271788 13/06/23 06:00 PM
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
I recommend leveraging the current stable release v2.0.4 over the 1.x series.


I am SReject
My Stuff
kap #271789 13/06/23 08:16 PM
Joined: Apr 2022
Posts: 25
Z
Zenti Offline OP
Ameglian cow
OP Offline
Ameglian cow
Z
Joined: Apr 2022
Posts: 25
Originally Posted by kap
Would you consider using SReject's JSON-For-mIRC?

This works:

[code]
; First type /getJFM to load SReject's JSON-4-mIRC
; Afterwards you can type /getInfo as many times as you like

alias getInfo {
var %url = https://api.github.com/repos/svdermant/MinecraftBot/commits/master
var %jsonname = lcommit
JSONOPEN -u %jsonname %url
if ($json(%jsonname).httpStatus == 200) {
echo -ag Latest commit information:
echo -ag Author: $json(%jsonname,commit,author,name).value - Date: $json(%jsonname,commit,author,date).value
echo -ag Additions: $json(%jsonname,stats,additions).value - Deletions: $json(%jsonname,stats,deletions).value
echo -ag Commit message: $json(%jsonname,commit,message).value
}
}

I have found an issue with the code but i dont know why is this so...
i test this code with my github changes first time i type /getinfo irs correct...

Than i take changes on my github after it i type /getinfo - it shows always the old changes..

Zenti #271790 14/06/23 04:59 AM
Joined: Apr 2022
Posts: 25
Z
Zenti Offline OP
Ameglian cow
OP Offline
Ameglian cow
Z
Joined: Apr 2022
Posts: 25
the same effect when i using version 2.0.4 stable

i get always not the latest changes from my github after /getinfo...

only when i change the varname of the %jsonname = lcommit to another name

But why is this so?


Link Copied to Clipboard