mIRC Homepage
Posted By: Mary_juana Regsub problem - 21/12/05 01:43 PM
I found a tinyurl snippet that works by just typing /tinyurl <url>

But i decided to add one for long urls that are posted in a channel.
Problem is, if the url has normal text before or after it IE:

<nick> Check this site out <url>
<nick> <url> This site is good

It picks up the text and the url that returns is wrong, this is what i have

Code:
 on ^1:TEXT:*:#root:{ 
  var %input = $1-
  if ( $regsub(%input,/((?:ftp:\/\/|https?:\/\/|www2?\.)[^&lt;&gt;\.\s]+(?:\.[^&lt;&gt;\.\s]+)+(?:\/[^&lt;&gt;\.\s]+)*)/g,\1\,%input) ) &amp;&amp; ( ($len(%input) &gt; 51) ) { /tinyurl %input }
  haltdef
}
 


any help would be really great, thanks
Posted By: Aenei Re: Regsub problem - 21/12/05 02:35 PM
What that does is calls your tinyurl alias if it detects a url but it passes it the whole line, ie the text before and after which will be confusing you tiny url generator.

You therefore would need to pass the actual url to the generator, which could be achieved by
Code:
on ^1:TEXT:*:#root: { 
  var %input = $1-
  if ( $regml($regex(%input,/((?:ftp:\/\/|https?:\/\/|www2?\.)[^&lt;&gt;\.\s]+(?:\.[^&lt;&gt;\.\s]+)+(?:\/[^&lt;&gt;\.\s]+)*)/g)) &amp;&amp; ( ($len(%input) &gt; 51) )  ) { /tinyurl $regml($regex(%input,/((?:ftp:\/\/|https?:\/\/|www2?\.)[^&lt;&gt;\.\s]+(?:\.[^&lt;&gt;\.\s]+)+(?:\/[^&lt;&gt;\.\s]+)*)/g))
  haltdef
}


I think that will work. Now someone else will come up with a much tidier way of doing it smile

Edit: Where did you find the tinyurl snippet incidentally?
Posted By: FiberOPtics Re: Regsub problem - 21/12/05 03:32 PM
What you should do is alter the tinyurl alias, so that it returns the tinyurl like an identifier: $tinyurl(<longurl>) and it returns the shorturl.

Then you can do something simple like this:

Code:
alias tinyurls {
  var %a, %b = $regsub($1,/(?=(?:ftp:\/\/|https?:\/\/|www2?\.)[^&lt;&gt;\.\s]+(?:\.[^&lt;&gt;\.\s]+)+(?:\/[^&lt;&gt;\.\s]+)*)(\S{51,})/Sg,$tinyurl(\1),%a)
  return %a
}


//echo -a $tinyurls(<string>)

This way, you can pass the entire sentence to $tinyurls, and it will return the same sentence with the longurls transformed to shorturls.

Since you've given no information whatsoever about how the tinyurl alias works, it might not even work, because we need the shorturls to be returned like an identifier. If this tinyurl thing works with sockets, then this won't work.
Posted By: Mary_juana Re: Regsub problem - 21/12/05 03:42 PM
This is all the tinyurl stuff for anyone that wants to use it, and for you FO
as you requested
Code:
alias tinyurl {
  if (!$0) {
    echo -ac info * /tinyurl insufficient parameters
    return
  }
  var %s = tinyurl_ $+ $ticks
  sockopen %s tinyurl.com 80
  sockmark %s $1-
}

ON *:SOCKOPEN:tinyurl_*:{
  if ($sockerr) {
    echo -ac ctcp * Error Connecting
    return
  }
  var %sTiny = sockwrite -n $sockname, $&amp;
    %cData = $+(url=, $urlencode($sock($sockname).mark), &amp;submit=Make+TinyURL%21)
  %sTiny POST /create.php HTTP/1.1
  %sTiny Content-Length: $len(%cData)
  %sTiny Host: tinyurl.com
  %sTiny Content-Type: application/x-www-form-urlencoded
  %sTiny Connection: Close
  %sTiny
  %sTiny %cData
}

ON *:SOCKREAD:tinyurl_*:{
  if ($sockerr) return
  var %tmp
  sockread %tmp
  while ($sockbr) {
    if ($regex(%tmp, /&lt;a href="(.+?)" target="_blank"&gt;/i)) {
      msg #root 00Tinyurl: $regml(1) 
    }
    sockread %tmp
  }
}

alias urlencode {
  var %cRet = $1-
  while ($regex(%cRet, /([^\w-.\x03])/)) {  
    !haltdef $regsub(%cRet, /([^\w-.\x03])/,  $+ $base($asc($regml(1)), 10, 16, 2), %cRet)
  }
  return $replace(%cRet, , %) 
}  
© mIRC Discussion Forums