<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: internet code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 19:51:41 GMT</pubDate>
    <description>DZone Snippets: internet code</description>
    <item>
      <title>Internet Explorer automation using win32::OLE</title>
      <link>http://snippets.dzone.com/posts/show/4586</link>
      <description>// Sample code used for one of my client&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl -w&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;use Data::Dumper;&lt;br /&gt;use Win32::OLE qw( EVENTS );&lt;br /&gt;&lt;br /&gt;my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];&lt;br /&gt;$mon++; # 0-based index&lt;br /&gt;$year = $year + 1900;&lt;br /&gt;my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);&lt;br /&gt;&lt;br /&gt;my $Disconnect;&lt;br /&gt;my $Menu;&lt;br /&gt;my $TreeView;&lt;br /&gt;my $WatchDog;&lt;br /&gt;my $MenuClicked=0;&lt;br /&gt;&lt;br /&gt;my $ScenarioCompleted=0;&lt;br /&gt;&lt;br /&gt;my @TreeViewLinks=("Appareillage du B","Branchement Comptage","Branchement individuel");&lt;br /&gt;my $Previouslink=$TreeViewLinks[0];&lt;br /&gt;&lt;br /&gt;my $ie = Win32::OLE-&gt;new( 'InternetExplorer.Application' ) or die "error starting IE";&lt;br /&gt;$ie-&gt;{visible} = 1;&lt;br /&gt;&lt;br /&gt;Win32::OLE-&gt;Option( Warn =&gt; 3 );&lt;br /&gt;&lt;br /&gt;$WatchDog=time();&lt;br /&gt;Win32::OLE-&gt;WithEvents( $ie, \&amp;Event, 'DWebBrowserEvents2' );&lt;br /&gt;$ie-&gt;navigate( 'http://www.xxx.fr' );&lt;br /&gt;Win32::OLE-&gt;MessageLoop();&lt;br /&gt;unlink("noemis.err") if -f "noemis.err";&lt;br /&gt;if ( ! $ScenarioCompleted ) {&lt;br /&gt;	open( ERR , "&gt;noemis.err" ); &lt;br /&gt;	print ERR "Problem executing Noemis scenario, please check www.xxx.fr.\n" ;&lt;br /&gt;	close(ERR);&lt;br /&gt;}&lt;br /&gt;$Disconnect-&gt;Click();&lt;br /&gt;Win32::OLE-&gt;SpinMessageLoop;&lt;br /&gt;&lt;br /&gt;# Maintenance du fichier historique&lt;br /&gt;open ( STATS , "noemis.txt" );&lt;br /&gt;my @lines=&lt;STATS&gt;;&lt;br /&gt;close (STATS);&lt;br /&gt;open( STATS , "&gt;noemis.txt" ); &lt;br /&gt;for my $line (@lines) {&lt;br /&gt;	my ($datetime) = split ( /;/ , $line );&lt;br /&gt;	my ($h_year,$h_mon) = $datetime =~ /^([0-9]{4})-([0-9]{2})/;&lt;br /&gt;	print STATS $line if ($year*12+$mon) - ($h_year*12+$h_mon) &lt; 2;&lt;br /&gt;}&lt;br /&gt;print STATS join(";",$date,"Noemis Scenario",( time() - $WatchDog ))."\n";&lt;br /&gt;close( STATS );&lt;br /&gt;&lt;br /&gt;sleep 2;&lt;br /&gt;Win32::OLE-&gt;SpinMessageLoop;&lt;br /&gt;sleep 1;&lt;br /&gt;$ie-&gt;Quit();&lt;br /&gt;exit 0;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sub Event {&lt;br /&gt;	my ($Obj,$Event,@Args) = @_;&lt;br /&gt;	my $IEObject = shift @Args;&lt;br /&gt;	print " Event triggered: $Event\n";    &lt;br /&gt;&lt;br /&gt;	my ($i,$anchor);&lt;br /&gt;	my $anchors;&lt;br /&gt;    &lt;br /&gt;	# STEP 1 : Find the main menu, login to the web site, find the treeview&lt;br /&gt;	if ($Event eq "DocumentComplete") {    &lt;br /&gt;		print "URL: " . $IEObject-&gt;Document-&gt;URL . "\n";&lt;br /&gt;		if ( $IEObject-&gt;Document-&gt;URL eq "http://www.xxx.fr/ident.aspx" ) {&lt;br /&gt;			my $forms = $IEObject-&gt;Document-&gt;forms;&lt;br /&gt;			my $form = $forms-&gt;item(0);&lt;br /&gt;			if ( defined($form-&gt;elements("fldNumCli")) ) {&lt;br /&gt;				print "--------------------------------------------\n";&lt;br /&gt;				print "Found the login box, authenticating ...\n";&lt;br /&gt;				print "--------------------------------------------\n";&lt;br /&gt;			    $form-&gt;elements("fldNumCli")-&gt;{value} = "xxxx";&lt;br /&gt;			    $form-&gt;elements("fldUtil")-&gt;{value} = "xxx";&lt;br /&gt;			    $form-&gt;elements("fldPwd")-&gt;{value} = "xxx";&lt;br /&gt;		    	$form-&gt;elements("btIdent")-&gt;Click();&lt;br /&gt;	    		}&lt;br /&gt;		}&lt;br /&gt;		if ( $IEObject-&gt;Document-&gt;URL eq "http://www.xxx.fr/menu.aspx" ) {&lt;br /&gt;			print "Found the menu.\n";&lt;br /&gt;			$Menu = $IEObject-&gt;Document;&lt;br /&gt;			$anchors = $IEObject-&gt;Document-&gt;links;&lt;br /&gt;			for (my $i=0; $i &lt; $anchors-&gt;length; $i++) {&lt;br /&gt;				$anchor = $anchors-&gt;item($i);&lt;br /&gt;				print $anchor-&gt;href."\n";&lt;br /&gt;				$Disconnect = $anchor if $anchor-&gt;href eq "http://www.xxx.fr/ident.aspx?qs=deconnecter";&lt;br /&gt;			}&lt;br /&gt;	      	}	    &lt;br /&gt;		if ( $IEObject-&gt;Document-&gt;URL eq "http://www.xxx.fr/client/frameTreeview.aspx" ) {&lt;br /&gt;			print "Found the TreeView.\n";&lt;br /&gt;			$TreeView = $IEObject-&gt;Document;&lt;br /&gt;      		}		&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	# STEP 2 : Click on the Menu and TreeView links   &lt;br /&gt;	if ($Event eq "DocumentComplete") {    		&lt;br /&gt;	if ( ! $MenuClicked and defined($Menu) ) {&lt;br /&gt;		my $MenuItem = $Menu-&gt;getElementById("SM_CLIE_RECH");&lt;br /&gt;		if ( defined($MenuItem) ) { &lt;br /&gt;			print $MenuItem-&gt;ID."\n";&lt;br /&gt;			$MenuItem-&gt;Click;&lt;br /&gt;			$MenuClicked = 1;&lt;br /&gt;		}&lt;br /&gt;	}}&lt;br /&gt;&lt;br /&gt;	if ( $Event eq "CommandStateChange" or $Event eq "StatusTextChange" ) {&lt;br /&gt;		print Dumper($IEObject);&lt;br /&gt;	}&lt;br /&gt;	if ( @TreeViewLinks != 0 and &lt;br /&gt;	     defined($TreeView) and &lt;br /&gt;	     $Event eq "DocumentComplete" &lt;br /&gt;	) {&lt;br /&gt;		my $link = shift(@TreeViewLinks);&lt;br /&gt;		$anchors = $TreeView-&gt;links;&lt;br /&gt;		my $found=0;&lt;br /&gt;		print "Looking for '$link' in the TreeView ... \n";&lt;br /&gt;	        for (my $i=0; $i &lt; $anchors-&gt;length; $i++) {&lt;br /&gt;		       	$anchor = $anchors-&gt;item($i);&lt;br /&gt;	        	#print $anchor-&gt;innerHTML."\n";&lt;br /&gt;		       	if ( $anchor-&gt;innerHTML =~ /$link/ ) {&lt;br /&gt;				print "Clicking on '$link' ... \n";&lt;br /&gt;	                	$anchor-&gt;Click;&lt;br /&gt;				$found=1;&lt;br /&gt;				$Previouslink=$link;&lt;br /&gt;				last;&lt;br /&gt;			}&lt;br /&gt;	        }&lt;br /&gt;		if ( ! $found ) { &lt;br /&gt;			# Le TreeView a bugge, on reclique&lt;br /&gt;			sleep 1;&lt;br /&gt;			print "Looking for '$Previouslink' in the TreeView ... \n";&lt;br /&gt;		        for (my $i=0; $i &lt; $anchors-&gt;length; $i++) {&lt;br /&gt;			       	$anchor = $anchors-&gt;item($i);&lt;br /&gt;	        		#print $anchor-&gt;innerHTML."\n";&lt;br /&gt;		       		if ( $anchor-&gt;innerHTML =~ /$Previouslink/ ) {&lt;br /&gt;					print "Clicking on '$Previouslink' ... \n";&lt;br /&gt;	              		  	$anchor-&gt;Click;&lt;br /&gt;					last;&lt;br /&gt;				}&lt;br /&gt;		        }&lt;br /&gt;			unshift @TreeViewLinks,$link;&lt;br /&gt;		}&lt;br /&gt;	} &lt;br /&gt;   &lt;br /&gt;	# STEP 3 : Verify the list displayed &lt;br /&gt;		&lt;br /&gt;	if ($Event eq "DocumentComplete") {    &lt;br /&gt;   		if ( @TreeViewLinks == 0 and $IEObject-&gt;Document-&gt;URL =~ /listeRefPlof.aspx/ ) {&lt;br /&gt;			print "Scenario completed, exiting ...\n";&lt;br /&gt;			$ScenarioCompleted=1;&lt;br /&gt;	   		Win32::OLE-&gt;QuitMessageLoop;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;	# Exit on errors&lt;br /&gt;	    &lt;br /&gt;	Win32::OLE-&gt;QuitMessageLoop() if $Event eq "OnQuit" or time() &gt; $WatchDog + 60;&lt;br /&gt;    &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Sep 2007 11:45:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4586</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
    <item>
      <title>Punycoded URLs in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4575</link>
      <description>This is just a proof-of-concept snippet for how to internationalize domain names using &lt;a href="http://raa.ruby-lang.org/project/punycode4r/"&gt;punycode4r&lt;/a&gt; (sudo gem install punycode4r).&lt;br /&gt;&lt;br /&gt;For more information please see:&lt;br /&gt;- &lt;a href="http://en.wikipedia.org/wiki/Punycode"&gt;Punycode&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://en.wikipedia.org/wiki/Internationalizing_Domain_Names_in_Applications"&gt;Internationalized domain name&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;#!/usr/local/bin/ruby -Ku&lt;br /&gt;&lt;br /&gt;# NOTE: The following is not the complete source code by Kazuhiro NISHIYAMA.&lt;br /&gt;#       For the full source code with more features, comments &amp; test cases please see: &lt;br /&gt;#       open -e `gem environment gemdir`/gems/punycode4r-0.2.0/lib/punycode.rb&lt;br /&gt;#&lt;br /&gt;# This is pure Ruby implementing Punycode (RFC 3492).&lt;br /&gt;# (original ANSI C code (C89) implementing Punycode is in RFC 3492)&lt;br /&gt;#&lt;br /&gt;# copyright (c) 2005 Kazuhiro NISHIYAMA&lt;br /&gt;# You can redistribute it and/or modify it under the same terms as Ruby.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;require "unicode"     # sudo gem install unicode&lt;br /&gt;&lt;br /&gt;module Punycode&lt;br /&gt;&lt;br /&gt;  module Status&lt;br /&gt;    class Error &lt; StandardError; end&lt;br /&gt;    class PunycodeSuccess; end&lt;br /&gt;    # Input is invalid.&lt;br /&gt;    class PunycodeBadInput &lt; Error; end&lt;br /&gt;    # Output would exceed the space provided.&lt;br /&gt;    class PunycodeBigOutput&lt; Error; end&lt;br /&gt;    # Input needs wider integers to process.&lt;br /&gt;    class PunycodeOverflow &lt; Error; end&lt;br /&gt;  end&lt;br /&gt;  include Status&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  BASE = 36; TMIN = 1; TMAX = 26; SKEW = 38; DAMP = 700&lt;br /&gt;  INITIAL_BIAS = 72; INITIAL_N = 0x80; DELIMITER = 0x2D&lt;br /&gt;&lt;br /&gt;  module_function&lt;br /&gt;&lt;br /&gt;  def basic(cp)&lt;br /&gt;    cp &lt; 0x80&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def delim(cp)&lt;br /&gt;    cp == DELIMITER&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def decode_digit(cp)&lt;br /&gt;    cp - 48 &lt; 10 ? cp - 22 :  cp - 65 &lt; 26 ? cp - 65 :&lt;br /&gt;      cp - 97 &lt; 26 ? cp - 97 : BASE&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def encode_digit(d, flag)&lt;br /&gt;    return d + 22 + 75 * ((d &lt; 26) ? 1 : 0) - ((flag ? 1 : 0) &lt;&lt; 5)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def flagged(bcp)&lt;br /&gt;    (0...26) === (bcp - 65)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def encode_basic(bcp, flag)&lt;br /&gt;    # bcp -= (bcp - 97 &lt; 26) &lt;&lt; 5;&lt;br /&gt;    if (0...26) === (bcp - 97)&lt;br /&gt;      bcp -= 1 &lt;&lt; 5&lt;br /&gt;    end&lt;br /&gt;    # return bcp + ((!flag &amp;&amp; (bcp - 65 &lt; 26)) &lt;&lt; 5);&lt;br /&gt;    if !flag and (0...26) === (bcp - 65)&lt;br /&gt;      bcp += 1 &lt;&lt; 5&lt;br /&gt;    end&lt;br /&gt;    bcp&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  MAXINT = 1 &lt;&lt; 64&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  def adapt(delta, numpoints, firsttime)&lt;br /&gt;    delta = firsttime ? delta / DAMP : delta &gt;&gt; 1&lt;br /&gt;    delta += delta / numpoints&lt;br /&gt;&lt;br /&gt;    k = 0&lt;br /&gt;    while delta &gt; ((BASE - TMIN) * TMAX) / 2&lt;br /&gt;      delta /= BASE - TMIN&lt;br /&gt;      k += BASE&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    k + (BASE - TMIN + 1) * delta / (delta + SKEW)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def punycode_encode(input_length, input, case_flags, output_length, output)&lt;br /&gt;&lt;br /&gt;    n = INITIAL_N&lt;br /&gt;    delta = out = 0&lt;br /&gt;    max_out = output_length[0]&lt;br /&gt;    bias = INITIAL_BIAS&lt;br /&gt;&lt;br /&gt;    input_length.times do |j|&lt;br /&gt;      if basic(input[j])&lt;br /&gt;        raise PunycodeBigOutput if max_out - out &lt; 2&lt;br /&gt;        output[out] =&lt;br /&gt;          if case_flags&lt;br /&gt;            encode_basic(input[j], case_flags[j])&lt;br /&gt;          else&lt;br /&gt;            input[j]&lt;br /&gt;          end&lt;br /&gt;        out+=1&lt;br /&gt;      # elsif (input[j] &lt; n)&lt;br /&gt;      #   raise PunycodeBadInput&lt;br /&gt;      # (not needed for Punycode with unsigned code points)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    h = b = out&lt;br /&gt;&lt;br /&gt;    if b &gt; 0&lt;br /&gt;      output[out] = DELIMITER&lt;br /&gt;      out+=1&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;   while h &lt; input_length&lt;br /&gt;&lt;br /&gt;      m = MAXINT&lt;br /&gt;      input_length.times do |j|&lt;br /&gt;        # next if basic(input[j])&lt;br /&gt;        # (not needed for Punycode)&lt;br /&gt;        m = input[j] if (n...m) === input[j]&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      raise PunycodeOverflow if m - n &gt; (MAXINT - delta) / (h + 1)&lt;br /&gt;      delta += (m - n) * (h + 1)&lt;br /&gt;      n = m&lt;br /&gt;&lt;br /&gt;      input_length.times do |j|&lt;br /&gt;        # Punycode does not need to check whether input[j] is basic:&lt;br /&gt;        if input[j] &lt; n # || basic(input[j])&lt;br /&gt;          delta+=1&lt;br /&gt;          raise PunycodeOverflow if delta == 0&lt;br /&gt;        end&lt;br /&gt;&lt;br /&gt;        if input[j] == n&lt;br /&gt;&lt;br /&gt;          q = delta; k = BASE&lt;br /&gt;          while true&lt;br /&gt;            raise PunycodeBigOutput if out &gt;= max_out&lt;br /&gt;            t = if k &lt;= bias # + TMIN # +TMIN not needed&lt;br /&gt;                  TMIN&lt;br /&gt;                elsif k &gt;= bias + TMAX&lt;br /&gt;                  TMAX&lt;br /&gt;                else&lt;br /&gt;                  k - bias&lt;br /&gt;                end&lt;br /&gt;            break if q &lt; t&lt;br /&gt;            output[out] = encode_digit(t + (q - t) % (BASE - t), false)&lt;br /&gt;            out+=1&lt;br /&gt;            q = (q - t) / (BASE - t)&lt;br /&gt;            k += BASE&lt;br /&gt;          end&lt;br /&gt;&lt;br /&gt;          output[out] = encode_digit(q, case_flags &amp;&amp; case_flags[j])&lt;br /&gt;          out+=1&lt;br /&gt;          bias = adapt(delta, h + 1, h == b)&lt;br /&gt;          delta = 0&lt;br /&gt;          h+=1&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      delta+=1; n+=1&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    output_length[0] = out&lt;br /&gt;    return PunycodeSuccess&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def punycode_decode(input_length, input, output_length, output, case_flags)&lt;br /&gt;&lt;br /&gt;    n = INITIAL_N&lt;br /&gt;&lt;br /&gt;    out = i = 0&lt;br /&gt;    max_out = output_length[0]&lt;br /&gt;    bias = INITIAL_BIAS&lt;br /&gt;&lt;br /&gt;    b = 0&lt;br /&gt;    input_length.times do |j|&lt;br /&gt;      b = j if delim(input[j])&lt;br /&gt;    end&lt;br /&gt;    raise PunycodeBigOutput if b &gt; max_out&lt;br /&gt;&lt;br /&gt;    b.times do |j|&lt;br /&gt;      case_flags[out] = flagged(input[j]) if case_flags&lt;br /&gt;      raise PunycodeBadInput unless basic(input[j])&lt;br /&gt;      output[out] = input[j]&lt;br /&gt;      out+=1&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    in_ = b &gt; 0 ? b + 1 : 0&lt;br /&gt;    while in_ &lt; input_length&lt;br /&gt;&lt;br /&gt;      oldi = i; w = 1; k = BASE&lt;br /&gt;      while true&lt;br /&gt;        raise PunycodeBadInput if in_ &gt;= input_length&lt;br /&gt;        digit = decode_digit(input[in_])&lt;br /&gt;        in_+=1&lt;br /&gt;        raise PunycodeBadInput if digit &gt;= BASE&lt;br /&gt;        raise PunycodeOverflow if digit &gt; (MAXINT - i) / w&lt;br /&gt;        i += digit * w&lt;br /&gt;        t = if k &lt;= bias # + TMIN # +TMIN not needed&lt;br /&gt;              TMIN&lt;br /&gt;            elsif k &gt;= bias + TMAX&lt;br /&gt;              TMAX&lt;br /&gt;            else&lt;br /&gt;              k - bias&lt;br /&gt;            end&lt;br /&gt;        break if digit &lt; t&lt;br /&gt;        raise PunycodeOverflow if w &gt; MAXINT / (BASE - t)&lt;br /&gt;        w *= BASE - t&lt;br /&gt;        k += BASE&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      bias = adapt(i - oldi, out + 1, oldi == 0)&lt;br /&gt;&lt;br /&gt;      raise PunycodeOverflow if i / (out + 1) &gt; MAXINT - n&lt;br /&gt;      n += i / (out + 1)&lt;br /&gt;      i %= out + 1&lt;br /&gt;&lt;br /&gt;      # not needed for Punycode:&lt;br /&gt;      # raise PUNYCODE_INVALID_INPUT if decode_digit(n) &lt;= base&lt;br /&gt;      raise PunycodeBigOutput if out &gt;= max_out&lt;br /&gt;&lt;br /&gt;      if case_flags&lt;br /&gt;        #memmove(case_flags + i + 1, case_flags + i, out - i)&lt;br /&gt;        case_flags[i + 1, out - i] = case_flags[i, out - i]&lt;br /&gt;&lt;br /&gt;        # Case of last character determines uppercase flag:&lt;br /&gt;        case_flags[i] = flagged(input[in_ - 1])&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      #memmove(output + i + 1, output + i, (out - i) * sizeof *output)&lt;br /&gt;      output[i + 1, out - i] = output[i, out - i]&lt;br /&gt;      output[i] = n&lt;br /&gt;      i+=1&lt;br /&gt;&lt;br /&gt;      out+=1&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    output_length[0] = out&lt;br /&gt;    return PunycodeSuccess&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def encode(unicode_string, case_flags=nil, print_ascii_only=false)&lt;br /&gt;    input = unicode_string.unpack('U*')&lt;br /&gt;    output = [0] * (ACE_MAX_LENGTH+1)&lt;br /&gt;    output_length = [ACE_MAX_LENGTH]&lt;br /&gt;&lt;br /&gt;    punycode_encode(input.size, input, case_flags, output_length, output)&lt;br /&gt;&lt;br /&gt;    outlen = output_length[0]&lt;br /&gt;    outlen.times do |j|&lt;br /&gt;      c = output[j]&lt;br /&gt;      unless c &gt;= 0 &amp;&amp; c &lt;= 127&lt;br /&gt;        raise Error, "assertion error: invalid output char"&lt;br /&gt;      end&lt;br /&gt;      unless PRINT_ASCII[c]&lt;br /&gt;        raise PunycodeBadInput&lt;br /&gt;      end&lt;br /&gt;      output[j] = PRINT_ASCII[c] if print_ascii_only&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    output[0..outlen].map{|x|x.chr}.join('').sub(/\0+\z/, '')&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def decode(punycode, case_flags=[])&lt;br /&gt;    input = []&lt;br /&gt;    output = []&lt;br /&gt;&lt;br /&gt;    if ACE_MAX_LENGTH*2 &lt; punycode.size&lt;br /&gt;      raise PunycodeBigOutput&lt;br /&gt;    end&lt;br /&gt;    punycode.each_byte do |c|&lt;br /&gt;      unless c &gt;= 0 &amp;&amp; c &lt;= 127&lt;br /&gt;        raise PunycodeBadInput&lt;br /&gt;      end&lt;br /&gt;      input.push(c)&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    output_length = [UNICODE_MAX_LENGTH]&lt;br /&gt;    Punycode.punycode_decode(input.length, input, output_length,&lt;br /&gt;                             output, case_flags)&lt;br /&gt;    output.pack('U*')&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  UNICODE_MAX_LENGTH = 256&lt;br /&gt;  ACE_MAX_LENGTH = 256&lt;br /&gt;&lt;br /&gt;  # The following string is used to convert printable&lt;br /&gt;  # characters between ASCII and the native charset:&lt;br /&gt;&lt;br /&gt;  PRINT_ASCII =&lt;br /&gt;    "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" \&lt;br /&gt;    "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" \&lt;br /&gt;    " !\"\#$%&amp;'()*+,-./" \&lt;br /&gt;    "0123456789:;&lt;=&gt;?" \&lt;br /&gt;    "@ABCDEFGHIJKLMNO" \&lt;br /&gt;    "PQRSTUVWXYZ[\\]^_" \&lt;br /&gt;    "`abcdefghijklmno" \&lt;br /&gt;    "pqrstuvwxyz{|}~\n"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# cf. http://snippets.dzone.com/posts/show/4527&lt;br /&gt;&lt;br /&gt;UTF8REGEX = /\A(?:                                                            &lt;br /&gt;              [\x09\x0A\x0D\x20-\x7E]            # ASCII&lt;br /&gt;            | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte&lt;br /&gt;            |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs&lt;br /&gt;            | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte&lt;br /&gt;            |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates&lt;br /&gt;            |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3&lt;br /&gt;            | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15&lt;br /&gt;            |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16&lt;br /&gt;            )*\z/mnx&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;UTF8_REGEX_MBYTE = /(?:                                 &lt;br /&gt;                 [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte&lt;br /&gt;               |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs&lt;br /&gt;               | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte&lt;br /&gt;               |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates&lt;br /&gt;               |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3&lt;br /&gt;               | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15&lt;br /&gt;               |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16&lt;br /&gt;               )/mnx&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# cf. http://demo.icu-project.org/icu-bin/idnbrowser (samples)&lt;br /&gt;# on Mac OS X you can check the Ruby conversions with the GUI app PunyCode, http://software.dibomedia.de/products/show/2&lt;br /&gt;&lt;br /&gt;str = "http://www.&#65201;&#65202;&#65207;.com/"&lt;br /&gt;str = "www.&#1089;&#1076;&#1077;&#1083;&#1072;&#1090; &#1082;&#1072;&#1088;&#1090;&#1080;&#1085;&#1082;&#1080;.com"&lt;br /&gt;str = "http://www.&#1089;&#1076;&#1077;&#1083;&#1072;&#1090;&#1082;&#1072;&#1088;&#1090;&#1080;&#1085;&#1082;&#1080;.com/"&lt;br /&gt;str = "http://t&#363;dali&#326;.lv/"&lt;br /&gt;str = "http://www.z&#252;rich.com/"&lt;br /&gt;str = "http://www.h&#246;ren.at/"&lt;br /&gt;str = "http://www.&#382;lut&#253; k&#367;&#328;.com/"&lt;br /&gt;str = "www.f&#228;rgbolaget.nu"&lt;br /&gt;str = "www.br&#230;ndendek&#230;rlighed.com"&lt;br /&gt;str = "www.m&#228;kitorppa.com"&lt;br /&gt;str = "www.f&#228;rjestadsbk.net"&lt;br /&gt;str = "&#12354;&#12540;&#12427;&#12356;&#12435;.com"&lt;br /&gt;str = "www.&#50696;&#48708;&#44368;&#49324;.com"&lt;br /&gt;str = "www.&#12495;&#12531;&#12489;&#12508;&#12540;&#12523;&#12469;&#12512;&#12474;.com"&lt;br /&gt;str = "www.&#26085;&#26412;&#24179;.jp"&lt;br /&gt;str = "www.r&#228;ksm&#246;rg&#229;s.se"&lt;br /&gt;str = "www.r&#243;&#380;yczka.pl/"&lt;br /&gt;str = "&#29702;&#23481;&#12490;&#12459;&#12512;&#12521;.com"&lt;br /&gt;str = "http://B&#252;cher.ch/"&lt;br /&gt;str = "t&#363;dali&#326;.lv"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if str =~ UTF8REGEX &amp;&amp; str =~ UTF8_REGEX_MBYTE&lt;br /&gt;&lt;br /&gt;   s1 = str.gsub(/^(http:\/\/www\.|http:\/\/|).*?\.[^\.\/]+\/?$/n, '\1')&lt;br /&gt;   s2 = str.gsub(/^(?:http:\/\/www\.|http:\/\/|)(www\.|).*?\.[^\.\/]+\/?$/n, '\1')&lt;br /&gt;   s3 = str.gsub(/^(?:http:\/\/www\.|http:\/\/|www\.|)(.*?)\.[^\.\/]+\/?$/n, '\1')&lt;br /&gt;   s4 = str.gsub(/^(?:http:\/\/www\.|http:\/\/|www\.|).*?(\.[^\.\/]+\/?)$/n, '\1')&lt;br /&gt;&lt;br /&gt;   if s1.empty? then s1 = 'http://' end&lt;br /&gt;&lt;br /&gt;   s3 = Punycode.encode(Unicode::normalize_KC(Unicode::downcase(s3)))&lt;br /&gt;&lt;br /&gt;   punycoded_url = s1 &lt;&lt; s2 &lt;&lt; "xn--" &lt;&lt; s3 &lt;&lt; s4&lt;br /&gt;&lt;br /&gt;   puts punycoded_url&lt;br /&gt;&lt;br /&gt;   %x{ /usr/bin/open "#{punycoded_url}" }&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 26 Sep 2007 21:00:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4575</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Babelfish translate</title>
      <link>http://snippets.dzone.com/posts/show/4265</link>
      <description>Ok, so maybe some of you might think that Babelfish's the best translator out there. I don't know about that , but I do know where my towel is ;)&lt;br /&gt;So, boys and girls, here's the bookmarklet that allows to translate your current web-page using the babelfish translation engine.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;javascript:location.href='http://babelfish.altavista.com/babelfish/tr?trurl='+encodeURIComponent(location.href)+'&amp;lp=%s&amp;btnTrUrl=Translate'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To make it useful, save it as a bookmark in FF (or as a new search engine in Opera) and give it a keyword/shortcut by editing the bookmark's properties in FF (or the search properties in Opera). Let's call him, say 'bf', that should do it.&lt;br /&gt;&lt;br /&gt;Here are some examples:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;bf en_fr&lt;br /&gt;bf fr_en&lt;br /&gt;bf en_ja&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Enjoy!&lt;br /&gt;G.R.</description>
      <pubDate>Thu, 05 Jul 2007 20:48:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4265</guid>
      <author>griflet (guillaume riflet)</author>
    </item>
    <item>
      <title>Server name/IP converter</title>
      <link>http://snippets.dzone.com/posts/show/4246</link>
      <description>When passed either an IPv4 address or the name of a domain or server, this script will return either a name or an IP, respectively.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl -w&lt;br /&gt;use strict;&lt;br /&gt;use Socket;&lt;br /&gt;my $arg = shift;&lt;br /&gt;if ($arg =~ /^(\d+\.){3}\d+$/) {&lt;br /&gt; print scalar gethostbyaddr(inet_aton($arg), AF_INET), "\n"&lt;br /&gt;} else { printf "%vd\n", scalar gethostbyname $arg }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 02:24:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4246</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Ruby port of range2cidr. Convert IP ranges to set of CIDR.</title>
      <link>http://snippets.dzone.com/posts/show/3798</link>
      <description>See rfc1878 for more detail about CIDR&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;# $Hg: range_cidr.rb,v af9566d89389 2007-04-12 20:28 +0400 $&lt;br /&gt;# (C) 2007 under terms of LGPL v2.1&lt;br /&gt;# by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt;&lt;br /&gt;#&lt;br /&gt;# backported from perl code&lt;br /&gt;# http://www.irbs.net/internet/postfix/0401/att-3032/cidr_range.pl.gz&lt;br /&gt;&lt;br /&gt;def range_cidr(first, last, &amp;block)&lt;br /&gt;  if first &lt; last&lt;br /&gt;    idx1 = 32&lt;br /&gt;    idx1 -= 1 while first[idx1] == last[idx1]&lt;br /&gt;    prefix = first &gt;&gt; idx1+1 &lt;&lt; idx1+1&lt;br /&gt;&lt;br /&gt;    idx2 = 0&lt;br /&gt;    idx2 += 1 while idx2 &lt;= idx1 and first[idx2] == 0 and last[idx2] == 1&lt;br /&gt;&lt;br /&gt;    if idx2 &lt;= idx1&lt;br /&gt;      range_cidr(first, prefix | 2**idx1-1, &amp;block)&lt;br /&gt;      range_cidr(prefix | 1 &lt;&lt; idx1, last, &amp;block)&lt;br /&gt;    else&lt;br /&gt;      yield prefix, 32-idx2&lt;br /&gt;    end&lt;br /&gt;  else&lt;br /&gt;    yield first, 32&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example Usage&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;# $Hg: range2cidr.rb,v 2142c33ada8b 2007-04-11 23:14 +0400 $&lt;br /&gt;# (C) 2007 under terms of GPL v2&lt;br /&gt;# by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt;&lt;br /&gt;#&lt;br /&gt;# example usage of range_cidr.rb&lt;br /&gt;&lt;br /&gt;require 'lib/range_cidr'&lt;br /&gt;require 'ipaddr'&lt;br /&gt;require 'socket'&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  if ARGV.size == 2&lt;br /&gt;    range_cidr(IPAddr.new(ARGV[0]).to_i, IPAddr.new(ARGV[1]).to_i) { |subnet, mask|&lt;br /&gt;      puts "#{IPAddr.new(subnet, Socket::AF_INET).to_s}/#{mask}"  }&lt;br /&gt;  else&lt;br /&gt;    puts "usage: range2cidr &lt;first_ip&gt; &lt;last_ip&gt;"&lt;br /&gt;    puts "example: range2cidr 192.168.1.0 192.168.2.255"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Type in terminal and look result&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ ruby range2cidr.rb 192.168.1.0 192.168.2.255&lt;br /&gt;192.168.1.0/24&lt;br /&gt;192.168.2.0/24&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I hate perl.</description>
      <pubDate>Wed, 11 Apr 2007 19:32:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3798</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>Download - download file from Internet and store it locally</title>
      <link>http://snippets.dzone.com/posts/show/1586</link>
      <description>// download file from Internet and store it locally&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;download: func [&lt;br /&gt;    "download remote-file from Internet and store it in loc-file"&lt;br /&gt;    loc-file [file!] remote-file [url!]&lt;br /&gt;][write/binary loc-file read/binary remote-file]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 27 Feb 2006 17:54:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1586</guid>
      <author>rebolek ()</author>
    </item>
    <item>
      <title>Python - get Interface ethernet</title>
      <link>http://snippets.dzone.com/posts/show/1531</link>
      <description>// Ritorna l'indirizzo di un interfaccia di rete....&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from socket import *&lt;br /&gt;import fcntl&lt;br /&gt;import struct&lt;br /&gt;&lt;br /&gt;def get_ip_address(ifname):&lt;br /&gt;&lt;br /&gt;	s = socket(AF_INET, SOCK_STREAM)&lt;br /&gt;	return inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// get_ip_address('lo')&lt;br /&gt;//&lt;br /&gt;// '127.0.0.1'</description>
      <pubDate>Mon, 20 Feb 2006 00:07:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1531</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>Turn off image buttons / icons on your pages in IE</title>
      <link>http://snippets.dzone.com/posts/show/327</link>
      <description>Turn off the image buttons (when you hover over an image) and Smart Tags in IE:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;meta http-equiv="imagetoolbar" content="false" /&gt;&lt;br /&gt;&lt;meta name="MSSmartTagsPreventParsing" content="true" /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 26 May 2005 09:32:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/327</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
