;encode the parameter passed to /_trans as required by http
alias -l urlencode return $replace($regsubex($1,/(\W)/g,% $+ $base($asc(\1),10,16,2)),$chr(32),+)
alias _trans {
;close the socket in case it's in use, open it, and mark it with the encoded version of the parameters
sockclose trans
sockopen trans translate.google.com 80
sockmark trans $urlencode($$1-)
}
on *:sockopen:trans:{
if ($sockerr) echo 4 -s * /_trans: sockopen error: $v1
else {
;the GET request, that use what we originally stored in the mark
sockwrite -n trans GET $+(/translate_a/t?client=t&text=,$sock(trans).mark,&sl=en&tl=fr) HTTP/1.0
sockwrite trans Host: translate.google.com $str($crlf,2)
;delete the mark (used later for another purpose)
sockmark trans
}
}
on *:sockread:trans:{
if ($sockerr) echo 4 -s * /_trans: sockread error: $v1
;read a line or what is inside the buffer
var %a
sockread -f %a
;we use the mark as a boolen value ($null/1) to know if we received the headers
if ($sock(trans).mark) {
;seperate the line according to comma
tokenize 44 $remove(%a,],[,")
echo -a Translation of $qt($2) is $qt($1)
}
;if the mark is empty and if %a is $null, we received the headers
elseif (!%a) sockmark trans 1
}