You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yacy_search_server/addon/yacy_chatscript.pl

177 lines
4.6 KiB

#!/usr/bin/perl
$VERSION = "0.05";
%IRSSI = (
authors => "Alexander Schier & Robert Weidlich",
contact => "",
name => "YaCy Script",
description => "A script to monitor and control YaCy",
license => "GPL",
url => "http://www.yacy-websuche.de",
changed => "Wed Apr 05 2006"
);
#use Irssi;
use strict;
use XML::Simple;
use LWP::Simple;
use Data::Dumper;
use vars qw($VERSION %IRSSI);
my $help = "/yacy show ppm|peer|version|network\n".
" set host|port|user|pass value\n".
" get host|port|user|pass\n".
" help";
our %cmds =
(
show => ["ppm","peer","version","network"],
set => ["user","pass","host","port"],
help => []
);
my $prog;
if ( defined(&Xchat::print) ) {
$prog = "xchat";
Xchat::register($IRSSI{'name'},$VERSION,$IRSSI{'description'});
} elsif ( defined(&Irssi::print) ) {
$prog = "irssi";
}
sub setting_init() {
if ($prog eq "irssi") {
Irssi::settings_add_str("yacy_script.pl", "yacy_host", "localhost");
Irssi::settings_add_int("yacy_script.pl", "yacy_port", 8080);
Irssi::settings_add_str("yacy_script.pl", "yacy_user", "admin");
Irssi::settings_add_str("yacy_script.pl", "yacy_pass", "");
} elsif ($prog eq "xchat") {
if ( ! -e Xchat::get_info('xchatdir')."/yacy.xml" ) {
my $data = {
host => "localhost",
port => "8080",
user => "admin",
pass => ""
};
XMLout($data, NoAttr => 1, OutputFile => Xchat::get_info('xchatdir')."/yacy.xml");
}
}
}
sub setting_set($$) {
if ($prog eq "xchat") {
my $data = XMLin(Xchat::get_info('xchatdir')."/yacy.xml");
$data->{$_[0]} = $_[1];
open my $fh, '>', Xchat::get_info('xchatdir')."/yacy.xml";
XMLout($data, NoAttr => 1, OutputFile => $fh);
close $fh;
} elsif ($prog eq "irssi") {
Irssi::settings_set_str("yacy_".$_[0],$_[1]);
}
}
sub setting_get($) {
if ($prog eq "xchat") {
my $data = XMLin(Xchat::get_info('xchatdir')."/yacy.xml");
return $data->{$_[0]};
} elsif ($prog eq "irssi") {
return Irssi::settings_get_str("yacy_".$_[0]);
}
}
sub yacy($$$) {
my ($host, $port, $user, $pass, $cmd, $arg, $arg2, $prnt, $output);
if ($prog eq "irssi") {
($cmd,$arg,$arg2)=split / /, shift;
} elsif ($prog eq "xchat") {
$cmd = $_[0][1];
$arg = $_[0][2];
$arg2 = $_[0][3];
}
$host = setting_get("host");
$port = setting_get("port");
$user = setting_get("user");
$pass = setting_get("pass");
if ($cmd eq "show") {
my $doc=get('http://'.$user.':'.$pass.'@'.$host.':'.$port.'/Network.xml');
if ( ! $doc ) {
$prnt = "Peer is not running.";
} else {
my $data = XMLin($doc);
if ($arg eq "ppm") {
$output = "is now crawling with YaCy at $data->{'your'}->{'ppm'} pages per minute.";
} elsif ($arg eq "peer") {
$output = "operates the $data->{'your'}->{'type'} YaCy peer $data->{'your'}->{'name'}, which is running $data->{'your'}->{'uptime'}";
} elsif ($arg eq "version") {
$output = "uses YaCy version $data->{'your'}->{'version'}";
} elsif ($arg eq "network") {
$output = "'s peer currently knows $data->{'active'}->{'count'} senior and $data->{'potential'}->{'count'} junior peers";
} else {
$prnt="Unknown argument: \"$arg\"\n$help";
}
}
} elsif ($cmd eq "set") {
if ($arg) {
if ($arg2) {
setting_set($arg,$arg2);
} else {
$prnt = "$arg is currently set to \"".setting_get($arg)."\"";
}
} else {
$prnt = "Argument required\n$help";
}
} elsif ($cmd eq "help") {
$prnt=$help;
} else {
$prnt="Unknown command: \"$cmd\"\n$help";
}
if ( $prog eq "irssi" ) {
Irssi::active_win->command("/me $output") if ($output);
Irssi::active_win->print($prnt) if ($prnt);
} elsif ( $prog eq "xchat" ) {
Xchat::print($prnt) if ($prnt);
Xchat::command("me $output") if ($output);
return 3;
}
}
sub cmd_help() {
my ($arg) = @_;
if ( $arg =~ /(yacy)/ ) {
Irssi::print($help);
Irssi::signal_stop();
}
}
sub signal_complete_word {
my ($list, $window, $word, $linestart, $want_space) = @_;
if($linestart =~ /\/yacy/){
Irssi::signal_stop();
my @words=split(/ /, $linestart);
if(@words == 1){
my @cmds2=keys(%cmds);
foreach (@cmds2){
if($_ =~/^$word/i){
push(@$list, $_);
}
}
}elsif(@words == 2){
my @cmds2=$cmds{@words[1]};
for my $i (0 .. $#{$cmds2[0]} ) {
if($cmds2[0][$i] =~/^$word/i){
push(@$list, $cmds2[0][$i]);
}
}
}
}
}
setting_init();
if ( $prog eq "irssi" ) {
Irssi::command_bind("help","cmd_help", "Irssi commands");
Irssi::command_bind('yacy', \&yacy);
Irssi::signal_add('complete word', 'signal_complete_word');
} elsif ( $prog eq "xchat") {
Xchat::hook_command("yacy","yacy",{help_text => $help});
}