weechat-opal/scripts/yiff.pl

103 lines
3.0 KiB
Perl
Raw Permalink Normal View History

2016-09-30 22:40:39 +00:00
# original yiff.pl by jakk, berry, and mike
2016-09-30 22:51:36 +00:00
# this version is for weechat and supports any db file
2016-09-30 22:40:39 +00:00
use utf8;
use strict;
use warnings;
no strict 'subs';
my $SCRIPT_NAME = 'yiff';
my $SCRIPT_AUTHOR = 'The Krusty Krab <wowaname@volatile.ch>';
my $SCRIPT_VERSION = '1.0';
my $SCRIPT_LICENCE = 'WTFPL';
2016-09-30 22:51:36 +00:00
my $SCRIPT_DESC = 'Rewrite of irssi/X-Chat yiff.pl for WeeChat';
2016-09-30 22:40:39 +00:00
if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION,
$SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) {
weechat::hook_command('yiff', 'yiffs a selected person', '[-dbfile] [nick]',
"dbfile: use a different database file than the default ".
"(include hyphen before name)\n".
"nick: person to yiff", '', 'cmd_yiff', '');
weechat::hook_command('ryiff', 'yiffs a random person', '[-dbfile]',
"dbfile: use a different database file than the default ".
"(include hyphen before name)", '', 'cmd_ryiff', '');
my %OPTIONS = (
dir => ['Database directory',
weechat::info_get('weechat_dir', '').'/yiffs'],
db => ['Default database', 'yiffs'],
solodb => ['Default database when nick is omitted', 'solos'],
);
for my $option (keys %OPTIONS) {
weechat::config_set_plugin($option, $OPTIONS{$option}[1])
unless weechat::config_is_set_plugin($option);
weechat::config_set_desc_plugin($option, $OPTIONS{$option}[0]);
}
}
sub nicklist {
my $buffer = $_[0];
my @nicks;
my $iptr = weechat::infolist_get('irc_nick', '',
weechat::buffer_get_string($buffer, 'localvar_server').','.
weechat::buffer_get_string($buffer, 'localvar_channel'));
2016-10-01 00:02:24 +00:00
return () unless $iptr;
2016-09-30 22:40:39 +00:00
while (weechat::infolist_next($iptr)) {
push @nicks, weechat::infolist_string($iptr, 'name');
}
2016-10-01 00:02:24 +00:00
weechat::infolist_free($iptr);
2016-09-30 22:40:39 +00:00
return @nicks;
}
sub yiff {
my ($buffer, $dbfile, $target, @users) = @_;
my $user = $users[rand @users];
my $nick = weechat::buffer_get_string($buffer, 'localvar_nick');
my $channel = weechat::buffer_get_string($buffer, 'localvar_channel');
if ($dbfile) { $dbfile =~ s/^-// }
elsif ($target) { $dbfile = weechat::config_get_plugin('db') }
else { $dbfile = weechat::config_get_plugin('solodb') }
if (open YIFFFILE, "<", weechat::config_get_plugin('dir')."/$dbfile") {
my @yiffs = <YIFFFILE>;
close YIFFFILE;
my $yiff = $yiffs[rand @yiffs];
if ($target) { $yiff =~ s/\$target/$target/g }
$yiff =~ s/\$user/$user/g;
$yiff =~ s/\$nick/$nick/g;
$yiff =~ s/\$channel/$channel/g;
weechat::command($buffer, "/me $yiff");
} else {
weechat::print($buffer, weechat::prefix('error').
"Cannot open '$dbfile' ".
2016-09-30 22:40:39 +00:00
"(make sure the db directory and file exists)");
return weechat::WEECHAT_RC_ERROR;
}
return weechat::WEECHAT_RC_OK;
}
sub cmd_yiff {
my $buffer = $_[1];
2016-09-30 23:06:01 +00:00
my ($dbfile, $target) = $_[2] =~ /^(-[^ ]+|) *([^ ]*)/;
2016-09-30 22:40:39 +00:00
my @users = nicklist($buffer);
return yiff($buffer, $dbfile, $target, @users);
}
sub cmd_ryiff {
my $buffer = $_[1];
2016-09-30 23:06:01 +00:00
my ($dbfile) = $_[2] =~ /^(-[^ ]+|)/;
2016-09-30 22:40:39 +00:00
my @users = nicklist($buffer);
my $target = $users[rand @users];
return yiff($buffer, $dbfile, $target, @users);
}