# # Copyright (c) 2006-2007 by FlashCode # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # WeeChat perl script sample # This script uses whole WeeChat API functions, it may be used for # testing purposes. use strict; weechat::register("wplsample", "0.3", "wplend", "WeeChat perl script sample"); # some command handlers weechat::add_command_handler("wplnothing", "wplnothing"); weechat::add_command_handler("wpltest", "wpltest", "test command", "channel", "a channel name", "%C"); weechat::add_command_handler("wplinfo", "wplinfo", "display info with get_info()", "version | nick | channel | server | away | weechat_dir | weechat_libdir | weechat_sharedir", "info to read", "version|nick|channel|server|away|weechat_dir|weechat_libdir|weechat_sharedir"); weechat::add_command_handler("wplcommand", "wplcommand", "execute a WeeChat comand", "weechat_cmd | text"); weechat::add_command_handler("wpldcc", "wpldcc", "display DCC", "", "", ""); weechat::add_command_handler("wplserver", "wplserver", "display server list", "", "", ""); weechat::add_command_handler("wplchannel", "wplchannel", "display channel list", "", "", ""); weechat::add_command_handler("wplnick", "wplnick", "display nick list", "", "", ""); weechat::add_command_handler("wplwindow", "wplwindow", "display window list", "", "", ""); weechat::add_command_handler("wplbuffer", "wplbuffer", "display buffer list", "", "", ""); weechat::add_command_handler("wplbufferdata", "wplbufferdata", "display current buffer content", "", "", ""); weechat::add_command_handler("wplgetconfig", "wplgetconfig", "get a config option value", "option", "", ""); weechat::add_command_handler("wplsetconfig", "wplsetconfig", "set a config option value", "option value", "", ""); weechat::add_command_handler("wplgetplugin", "wplgetplugin", "get a plugin config option value", "option", "", ""); weechat::add_command_handler("wplsetplugin", "wplsetplugin", "set a plugin config option value", "option value", "", ""); # time handler weechat::add_timer_handler(60*5, "wpltimer"); # keyboard handler weechat::add_keyboard_handler("wplkeyboard"); # message handler weechat::add_message_handler("privmsg", "wplprivmsg"); # modifiers weechat::add_modifier("irc_in", "privmsg", "wplmodin"); weechat::add_modifier("irc_user", "", "wplmoduser"); weechat::add_modifier("irc_out", "privmsg", "wplmodout"); # command handlers for removing some handlers weechat::add_command_handler("wpldelmsg", "wpldelmsg", "delete privmsg message handler", "", "", ""); weechat::add_command_handler("wpldelcmd", "wpldelcmd", "delete wplnothing command handler", "", "", ""); weechat::add_command_handler("wpldeltimer", "wpldeltimer", "delete wpltimer timer handler", "", "", ""); weechat::add_command_handler("wpldelkeyboard", "wpldelkeyboard", "delete wplkeyboard keyboard handler", "", "", ""); weechat::add_command_handler("wpldelmod", "wpldelmod", "delete all modifiers", "", "", ""); # command handler for clearing infobar weechat::add_command_handler("wpldelinfo", "wpldelinfo", "remove infobar message(s)", "", "", ""); sub wplnothing { weechat::print("wplnothing: nothing done there!"); return weechat::PLUGIN_RC_OK; } sub wpltest { my $server = shift; my $args = shift; weechat::print("wpltest: server = '$server', args = '$args'"); weechat::print_infobar(10, "wpltest: test infobar"); weechat::log("wpltest: test log on photon/#abc", "#abc", "photon"); weechat::log("wpltest: test log on photon", "", "photon"); return weechat::PLUGIN_RC_OK; } sub wplinfo { my $server = shift; my $args = shift; if ($args) { weechat::print("get_info($args) = ".weechat::get_info($args)); } else { weechat::print("wplinfo: missing info argument"); } return weechat::PLUGIN_RC_OK; } sub wplcommand { my $server = shift; my $args = shift; if ($args) { weechat::command($args); } else { weechat::print("wplcommand: missing command argument"); } return weechat::PLUGIN_RC_OK; } sub wpldcc { my @dccs = weechat::get_dcc_info(); if (@dccs) { foreach my $dcc (@dccs) { while ( my ($key, $value) = each %$dcc) { weechat::print("$key = '$value'"); } } } else { weechat::print("wpldcc: no DCC"); } return weechat::PLUGIN_RC_OK; } sub wplserver { my $servers = weechat::get_server_info(); if ($servers) { while (my ($srvname, $srvinfos) = each %$servers) { while (my ($key, $value) = each %$srvinfos) { weechat::print("$srvname -> $key = '$value'"); } } } else { weechat::print("wplserver: no server"); } return weechat::PLUGIN_RC_OK; } sub wplchannel { my $channels = weechat::get_channel_info(weechat::get_info("server")); if ($channels) { while (my ($channame, $chaninfos) = each %$channels) { while (my ($key, $value) = each %$chaninfos) { weechat::print("$channame -> $key = '$value'"); } } } else { weechat::print("wplchannel: no channel"); } return weechat::PLUGIN_RC_OK; } sub wplnick { my $channels = weechat::get_channel_info(weechat::get_info("server")); if ($channels) { while (my ($channame, $chaninfos) = each %$channels) { my $nicks = weechat::get_nick_info(weechat::get_info("server"), $channame); if ($nicks) { while (my ($nickname, $nickinfos) = each %$nicks) { while (my ($key, $value) = each %$nickinfos) { weechat::print("$channame -> $nickname -> $key = '$value'"); } } } else { weechat::print("wplnick: no nick"); } } } else { weechat::print("wplnick: no channel"); } return weechat::PLUGIN_RC_OK; } sub wplwindow { my @wf = weechat::get_window_info(); if (@wf) { weechat::print("**** windows infos ****"); foreach my $w (@wf) { while ( my ($key, $value) = each %$w) { weechat::print(" > $key => $value"); } weechat::print("----------------------"); } } else { weechat::print("wplwindow: no window info"); } return weechat::PLUGIN_RC_OK; } sub wplbuffer { my $bf = weechat::get_buffer_info(); if ($bf) { weechat::print("**** buffers infos ****"); while ( my ($nobuf, $binfos) = each %$bf) { while ( my ($key, $value) = each %$binfos) { weechat::print(" > $key => $value"); } weechat::print("----------------------"); } } else { weechat::print("wplbuffer: no buffer info"); } return weechat::PLUGIN_RC_OK; } sub wplbufferdata { my $server = weechat::get_info("server"); my $channel = weechat::get_info("channel"); my @bc = weechat::get_buffer_data($server, $channel); if (@bc) { weechat::print("**** buffer data for '$channel'\@'$server' ****"); foreach my $l (@bc) { while ( my ($key, $value) = each %$l) { weechat::print(" > $key => $value"); } weechat::print("----------------------"); } } else { weechat::print("wplbufferdata: no buffer data"); } return weechat::PLUGIN_RC_OK; } sub wplgetconfig { my $server = shift; my $option = shift; if ($option) { weechat::print("$option = ".weechat::get_config($option)); } else { weechat::print("wplgetconfig: missing option"); } return weechat::PLUGIN_RC_OK; } sub wplsetconfig { my $server = shift; my $args = shift; if ($args) { if ($args =~ /(\w+) (.*)/) { weechat::print("wplsetconfig: set_config: $1 = $2"); weechat::set_config($1, $2); } else { weechat::print("wplsetconfig: missing value for option"); } } else { weechat::print("wplsetconfig: missing option/value"); } return weechat::PLUGIN_RC_OK; } sub wplgetplugin { my $server = shift; my $option = shift; if ($option) { weechat::print("$option = ".weechat::get_plugin_config($option)); } else { weechat::print("wplgetplugin: missing option"); } return weechat::PLUGIN_RC_OK; } sub wplsetplugin { my $server = shift; my $args = shift; if ($args) { if ($args =~ /(\w+) (.*)/) { weechat::print("wplsetplugin: set_config: $1 = $2"); weechat::set_plugin_config($1, $2); } else { weechat::print("wplsetplugin: missing value for option"); } } else { weechat::print("wplsetplugin: missing option/value"); } return weechat::PLUGIN_RC_OK; } sub wpltimer { weechat::print("wpltimer: this is the timer handler"); return weechat::PLUGIN_RC_OK; } sub wplkeyboard { my $key = shift; my $input_before = shift; my $input_after = shift; # nothing here return weechat::PLUGIN_RC_OK; } sub wplprivmsg { my $server = shift; my $args = shift; weechat::print("wplprivmsg: privmsg received: server = '$server', args = '$args'"); return weechat::PLUGIN_RC_OK; } sub wplmodin { return "$_[1] [modifier IN]"; } sub wplmoduser { # return message as-is return $_[1]; } sub wplmodout { return "$_[1] [modifier OUT]"; } sub wpldelmsg { weechat::remove_handler("privmsg", "wplprivmsg"); weechat::print("wpldelmsg: privmsg handler removed"); return weechat::PLUGIN_RC_OK; } sub wpldelcmd { weechat::remove_handler("wplnothing", "wplnothing"); weechat::print("wpldelcmd: wplnothing command handler removed"); return weechat::PLUGIN_RC_OK; } sub wpldeltimer { weechat::remove_timer_handler("wpltimer"); weechat::print("wpldeltimer: wpltimer timer handler removed"); return weechat::PLUGIN_RC_OK; } sub wpldelkeyboard { weechat::remove_keyboard_handler("wplkeyboard"); weechat::print("wpldelkeyboard: wplkeyboard keyboard handler removed"); return weechat::PLUGIN_RC_OK; } sub wpldelmod { weechat::remove_modifier("irc_in", "privmsg", "wplmodin"); weechat::remove_modifier("irc_user", "", "wplmoduser"); weechat::remove_modifier("irc_out", "privmsg", "wplmodout"); weechat::print("wpldelmod: modifiers removed"); return weechat::PLUGIN_RC_OK; } sub wpldelinfo { my $server = shift; my $args = shift; if ($args) { weechat::remove_infobar($args); } else { weechat::remove_infobar(); } weechat::print("wpldelinfo: infobar message(s) removed"); return weechat::PLUGIN_RC_OK; } sub wplend { weechat::print("wplsample: ending..."); return weechat::PLUGIN_RC_OK; }