Irssi – Wikipedia script

Simple script based on Google.pl to query wikipedia. Print to channel as public message or keep the results private (default). Specify max number of results or which result out of many you want. E.g If somebody needs info about the java programming language. You could first do a private search like this

/wiki -5 java

You will get 5 articles back from wikipedia, and you will see that the second article is named “Java (programming language)” and that is probably the one you want. Then you could do

/wiki -p #2 java

and the second result will be sent as a public message to the active channel.

wiki.pl (4.4 kB)

# - Wiki.pl

# - You have to modify this line to the path
# - of your LWP-dir

use lib '/System/Library/Perl/Extras/5.8.8/LWP';

use Irssi;
use LWP::UserAgent;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = '0.2.1';
%IRSSI = (
    authors     => 'Hersson',
    homepage    => 'www.hersson.net',
    name        => 'Wiki',
    description => 'This script queries wikipedia.org and returns a short version of the result with a link to the article',
    license     => 'Public Domain',
);

## Usage:
## /wiki [-p, prints to current window] [-<number>, number of searchresults returned | #<number> which search result to return] search-criteria1 search-criteria2 ...
##
## The Wiki.pl script is based on Google.pl
##
## History:
## - Sun Mar 1 2009
##      Version 0.1 - Initial release
##      Version 0.2 - Added link to articles
## - Mon Mar 2 2009
##      Version 0.2.1 - Fixed problem when article url
##                      contained %
## -------------------------------



sub cmd_wiki {

    my ($data, $server, $witem) = @_;

#   #-------------------------------------------------
    my $nr_results = 1; # Default number of results returned
    my $spesific_res = 0; # Default specific result is disabled
    my $prefix = "/wiki"; # Message printed before results
    my $url = "http://en.wikipedia.org";
    my $mode = "quiet"; # Default mode
#   #------------------------------------------------- 

#   # If user supplied nr_results, activate his setting
    if ( $data =~ /-(\d\s)/ ) { $nr_results = $1 };
    if ($data =~ /-10/) { $nr_results = 10 };
    $data =~ s/-\d+//g; # remove nr_results from $data

#   # If user supplied spesific result activate his setting
    if ( $data =~ /#(\d\s)/ ) { $spesific_res = $1-1 };
    if ( $data =~ /#10/) { $spesific_res = 9};
    $data =~ s/#\d+//g; # remove spesific_res from $data
   
#   # If user supplied spesific result make sure to get that many, but not more
    if ($spesific_res != 0) {$nr_results = $spesific_res + 1};

#   # Switch to public mode
#   # and return error msg if invalid window
    if ( $data =~ /-p/ ) {
        $mode = "public";
        if ( ! $witem ) {
          Irssi::active_win()->print("Must be run run in a valid window (CHANNEL|QUERY)");
          return;
        }
    }
    $data =~ s/-p//g; # remove -p from $data

#   # Get query as written by user
    my $written_query = $data;

#   # Format the query-string
    $data =~ s/\s/+/g;


    my $query = $data;


#   # Initialize LWP
    my $ua = new LWP::UserAgent;
    $ua->agent("AgentName/0.1 " . $ua->agent);

#   # Do the actual seach
        my $req = new HTTP::Request GET => $url . '/w/index.php?title=Special%3ASearch&ns0=1&search=' . $query . '&=MediaWiki+search&fulltext=Advanced+search';
        my $res = $ua->request($req);
        my $content = $res->content;

#   # Replace <br> with newlines
         $content =~ s/\<br\>/\n/g;

 
#   # Make an arry of lines
    my @lines = split("\n", $content);

#   # Array for the results
    my @results;
   
    my $nr_found = 0;
    foreach (@lines) {
#       # Get the interesting stuff
        if ($_ =~ m/\<div class\=\'searchresult\'\>.*?\<span class\=\'searchmatch\'\>/){
                       
#           # Get url to article
            my $article_url;
            if ($_ =~ m/"(.+?)"/) {
                $article_url = $url . $1;

#               #Check for % and change them to %%
                            $article_url =~ s/\%/%%/g;
            }

           
#           #Remove tags
            $_ =~ s/\<.+?\>//g;
           
#           #Remove wikipedia's info of a redirect link
            $_ =~ s/\(redirect.+?\){1,2}//;
           
#           #Substitue multiple spaces with ' - 'between title and main text
            $_ =~ s/\s{3,}/ - /;
           
#           #Cut the crap at the end
            $_ =~ s/\W{4,}$/... /;
           
            if ($nr_found < $nr_results) {
                if ($prefix ne ""){
                    $results[$nr_found] = $prefix . " " . $written_query . " >> " . $_ . $article_url;
                } else {
                     $results[$nr_found] = $_ . $article_url;
                }
                $nr_found++;
            }
           
        }
       
    }

#   # Prevent script from printing empty lines at the end
    if (@results < $nr_results) {$nr_results = @results;}

#   # Print result to current window if public-mode specified
#   # else display a private notice of returned results
    if ( $mode eq "public") {
        if ($spesific_res != 0 && $nr_results > $spesific_res) {
            $witem->command("/SAY $results[$spesific_res]");   
        } else {
            for (my $i=0; $i<$nr_results; $i++) {
                $witem->command("/SAY $results[$i]");
            }
        }
    }
    else {
        if ($spesific_res != 0 && $nr_results > $spesific_res) {
            Irssi::active_win()->print("$results[$spesific_res]");
        } else {
            for (my $i=0; $i<$nr_results; $i++) {
                Irssi::active_win()->print("$results[$i]");
            }
        }

    }

}

Irssi::command_bind('wiki', 'cmd_wiki');

Irssi – All public messages script

Sends all public messages from all active channels to the window named ‘allpubmsgs’, excluding the currently active window and any channel names in the exclude list. Displays channel names with different colors. I use this script together with the ‘hilightwin.pl’. I have split my irssi terminal into 3 windows, one for the active channel, one for allpubmsg and one for hilightwin. I think this gives a very nice view of whats going on in all channels without having to switch back and forth all the time.

allpubmsgs.pl (1.7 kB)

use strict;
use Irssi;
use Irssi::Irc;

use vars qw($VERSION %IRSSI);

$VERSION = "0.3";
%IRSSI = (
        "authors"       => 'Hersson',
        "homepage"      => 'www.hersson.net',
        "name"          => 'allpubmsgs',
        "description"   => 'Sends all public messsages from all channels to one window, exluding the currently active window and entries in the exclude list. Separates channels with different colors.',
        "license"       => 'Public Domain',
        "changed"       => '2011-28-03'
);


# Change the names and colors to your liking
my %chan_colors = (
    '#archlinux' => '%m',
    '##mac' => '%y',
    '#macosx' => '%r',
    '#freenode' => '%g',
    );
# Channels to exclude
my @exclude_list = qw{#mac1};

sub sig_printtext
{
  my ($dest, $text, $stripped) = @_;

  my $color="%N"; #Set color to default
 
  # If channel is a key in hash change color
  while(my ($key, $value) = each(%chan_colors)){
    if (lc $dest->{target} eq lc $key) {
        $color = $value;
    }
  }

  # Fix so any % char are printed
  $text =~ s/\%/%%/g;
   
    if (grep {m|^$dest->{target}$|} @exclude_list) {} else {
   
        if ($dest->{level} & MSGLEVEL_PUBLIC) {
            my $windowname = Irssi::window_find_name('allpubmsgs');

            my $witem = Irssi::active_win()->{active}; #Get active window
            if (lc $witem->{name} ne lc $dest->{target} ) {  #If msg is not from active window print it to allpubmsg
                $windowname->print($color . $dest->{target} . ":%n " . $text, MSGLEVEL_CLIENTCRAP) if ($windowname);
            }
        }
    }
}

my $windowname = Irssi::window_find_name('allpubmsgs');
if (!$windowname)
{
  Irssi::command("window new");
  Irssi::command("window name allpubmsgs");
}

Irssi::signal_add(
{
  'print text', 'sig_printtext'
}
);

The perfect instant messaging setup

To stay in touch with all your contacts you often install many different kinds of IM-applications. In a worst case scenario you could end up with one application for each of these services: MSN, Google Talk, AIM, Yahoo, ICQ, IRC, Facebook Chat, Twitter, Skype and maybe a few more, but the truth is probably that you have one multi protocol application and then maybe Skype and Twitter on the side. That was my situation, and I didn’t like it. In addition to the new and maybe more popular services I still use IRC, and IRC is now coming to the rescue…
Continue reading

RaopX – Installer package available

Since a lot of people don’t have the Developer Tools package installed, and doing so maybe just to try out one application is a bit tedious, I have put together an installer package for RaopX. The installer should work on any Intel based Mac running Os X 10.5 or newer, and it installs both the RaopX executable and the libsamplerate library. Unfortunately I am not able to test on anything but 10.6, so any confirmation that it works on 10.5 is highly appreciated.

Happy new year, and thanks for stopping by. :-)

Update January 8
I’ve just heard that the RaopX build in the installer package does not work on Os X 10.5. I’ll have a new package out as soon as I can. Tested on both versions this time.