#!/usr/bin/perl ########################################NEED libwww-perl libxml-parser-perl INSTALLED################## #* Copyright (c) 2007, Ed Holland #* All rights reserved. #* #* Redistribution and use in source and binary forms, with or without #* modification, are permitted provided that the following conditions are met: #* * Redistributions of source code must retain the above copyright #* notice, this list of conditions and the following disclaimer. #* * Redistributions in binary form must reproduce the above copyright #* notice, this list of conditions and the following disclaimer in the #* documentation and/or other materials provided with the distribution. #* THIS SOFTWARE IS PROVIDED BY ED HOLLAND ``AS IS'' AND ANY #* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #* DISCLAIMED. IN NO EVENT SHALL ED HOLLAND BE LIABLE FOR ANY #* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; #* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND #* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT #* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS #* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ######################################## #Example XML data returned from server (with added newlines) # # #This Th's Thus Th HS #test tat ST St st # ######################################### use LWP::UserAgent; use XML::Parser; use Irssi qw(signal_add_last); use vars qw($VERSION %IRSSI); $VERSION = '0.10'; %IRSSI = ( authors => 'Ed Holland', contact => 'edholland@ntlworld.com', name => 'Google Spell Check', description => 'This script preforms ' . 'a google spell check' . 'operation on your text.', license => 'New BSD', ); sub sig_complete { my ($complist, $window, $word, $linestart, $want_space) = @_; $line = $word; process(); push @$complist, $line; } sub process { $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(POST => 'https://www.google.com/tbproxy/spell?lang=en&hl=en'); $req->content_type('text/xml'); $req->content(''.$line.''); my $res = $ua->request($req); $string = $res->as_string; @data = split('\n\n', $string); # initialize parser $xp = new XML::Parser(); # set callback functions $xp->setHandlers(Start => \&start, Char => \&cdata); # parse XML $xp->parse($data[1]); } sub start { # extract variables my ($parser, $name, @attr) = @_; $currentTag = lc($name); $skip = 0; if (($currentTag eq 'c') && ($attr[5] == 0)) { $skip = 1; } } sub cdata { my ($parser, $cdata) = @_; @cdata = split('\t',$cdata); if ($skip != 1) { $line = $cdata[0] } } signal_add_last 'complete word' => \&sig_complete;