home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.madoka.org
/
2014.12.ftp.madoka.org.tar
/
ftp.madoka.org
/
pub
/
plum
/
plum2_33_1.lzh
/
module
/
auto
/
random.plm
< prev
next >
Wrap
Text File
|
1999-03-24
|
10KB
|
352 lines
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><!-- $_ if 0; # -*- perl -*-
# $Id: random.plm,v 2.38 1999/01/28 14:55:38 hasegawa Exp $
# copyright (c)1997-1999 Yoshinori Hasegawa <hasegawa@madoka.org>
package auto_random;
$RATE = 100;
$FORMAT = '#(name|nick|nick.now): #(message)';
$_ = 'auto_random';
sub ss_privmsg {
local($serverno, $prefix, $cmd, @params) = @_;
local($userno, $mtime, $name, $code, $req, $reply, $regex, $rate, $to, @list, %alias, @format);
$userno = $'userno[$serverno];
($name, $code) = &filename(&'property($userno, 'file'));
$name = &'expand($name);
if ($name) {
$mtime = (stat($name))[9];
if (defined($mtime) && ($modify[$userno] || 0) != $mtime) {
$modify[$userno] = $mtime;
&read_data($userno);
}
}
($req, $reply) = split(/\s+/, $params[1], 2);
if ($reply) {
foreach $add (&'property($userno, 'add')) {
next unless $req eq $add;
&add_file($userno, $reply);
return ($prefix, $cmd, $params[0], $params[1]);
}
foreach $remove (&'property($userno, 'remove')) {
next unless $req eq $remove;
&remove_file($userno, $reply);
return ($prefix, $cmd, @params);
}
}
$rate = &'property($userno, 'rate') || $RATE;
if (rand(100) < $rate) {
foreach $req (&'property($userno, 'request')) {
$regex = &'regex(&'jis_euc($req));
next unless &'jis_euc($params[1]) =~ /$regex/;
@list = &'array($message[$userno]);
$str = $list[rand(@list)];
last unless $str;
%alias = &get_alias_user($userno, $prefix);
$alias{'nick.now'} = (&'prefix($prefix))[0];
$alias{'message'} = &'format($str, %alias);
if (&'channel($params[0])) {
$alias{'channel'} = $params[0];
$to = $params[0];
} else {
$to = &'prefix($prefix);
}
@format = &'property($userno, 'format');
if (@format) {
foreach $reply (@format) {
&privmsg($serverno, $to, &'format($reply, %alias));
}
} else {
&privmsg($serverno, $to, &'format($FORMAT, %alias));
}
last;
}
}
return ($prefix, $cmd, @params);
}
sub privmsg {
local($serverno, $to, $msg) = @_;
&'s_print($serverno, '', 'PRIVMSG', $to, $msg);
foreach $cno (&'array($'clientlist)) {
next unless $'avail[$cno];
next unless $'server[$cno] == $serverno;
&'c_print($cno, &'user($cno), 'PRIVMSG', $to, $msg);
}
}
sub add_file {
local($userno, $reply) = @_;
$message[$userno] = &'add($message[$userno], $reply);
&write_file($userno);
}
sub remove_file {
local($userno, $reply) = @_;
$message[$userno] = &'remove($message[$userno], $reply);
&write_file($userno);
}
sub read_data {
local($userno) = @_;
local($name, $code, $line);
($name, $code) = &filename(&'property($userno, 'file'));
if ($name && open(FILE, $name)) {
undef($message[$userno]);
while (defined($line = <FILE>)) {
$line =~ tr/\r\n//d;
next unless $line;
$line = &code_jis($line, $code) if $code;
$message[$userno] = &'add($message[$userno], $line);
}
close(FILE);
}
}
sub write_file {
local($userno) = @_;
local($name, $code);
($name, $code) = &filename(&'property($userno, 'file'));
if ($name && open(FILE, ">$name")) {
foreach $line (&'array($message[$userno])) {
$line = &jis_code($line, $code) if $code;
print FILE $line, "\n";
}
close(FILE);
}
}
sub get_alias_user {
local($userno, $from, $prefix) = @_;
local($file, @list);
$file = &'property($userno, 'alias');
foreach $list (&split_list('nick', &read_file($file))) {
@list = &'array($list);
if (&match_alias_user($from, @list)) {
return &parse_alias($prefix, @list);
}
}
return ();
}
sub match_alias_user {
local($from, @list) = @_;
local($var, $arg, $regex);
foreach $line (@list) {
($var, $arg) = split(/\s+/, $line, 2);
next unless "\L$var\E" eq 'user';
$regex = &'regex($arg);
next unless $from =~ /$regex/i;
return 1;
}
return 0;
}
sub parse_alias {
local($prefix, @list) = @_;
local(%alias, $var, $arg, $key);
%alias = ();
foreach $line (@list) {
($var, $arg) = split(/\s+/, $line, 2);
$var = "\L$var\E";
if ($prefix) {
$key = $prefix . '.' . $var;
} else {
$key = $var;
}
next if defined($alias{$key});
if ($var eq 'nick') {
$alias{$key} = (split(/\,/, $arg))[0];
} elsif ($var eq 'name') {
$alias{$key} = (split(/\s+/, $arg))[0];
} else {
$alias{$key} = $arg;
}
}
return %alias;
}
sub split_list {
local($field, @list) = @_;
local($var, $arg, @array, @entry);
$field = "\L$field\E";
@array = ();
@entry = ();
foreach $line (@list) {
($var, $arg) = split(/\s*\:\s*/, $line, 2);
if ($field eq "\L$var\E") {
push(@array, &'list(@entry)) if @entry;
@entry = ();
}
push(@entry, $var . ' ' . $arg);
}
push(@array, &'list(@entry)) if @entry;
return @array;
}
sub read_file {
local($file) = @_;
local($name, $code, $mtime, @data, $line);
($name, $code) = &filename($file);
$mtime = (stat($name))[9];
if (defined($mtime)) {
$modify{$name} = -1 unless defined($modify{$name});
if ($modify{$name} != $mtime) {
if (open(TMP, $name)) {
@data = ();
while (defined($line = <TMP>)) {
$line =~ s/^\s+//;
next if $line =~ /^[\#\;]/;
$line =~ tr/\r\n//d;
next unless $line;
$line =~ s/\s+$//;
$line = &code_jis($line, $code) if $code;
push(@data, $line);
}
close(TMP);
$modify{$name} = $mtime;
$cache{$name} = &'list(@data);
return @data;
}
} else {
return &'array($cache{$name});
}
}
return ();
}
sub filename {
local($file) = @_;
local($idx, $name, $code);
return ('', '') unless $file;
if (($idx = rindex($file, ';')) != -1) {
$name = substr($file, 0, $idx);
$code = substr($file, $idx + 1);
} else {
$name = $file;
$code = '';
}
return (&'expand($name), $code);
}
sub code_jis {
local($line, $list) = @_;
foreach $code (split(/\,/, "\L$list\E")) {
if ($code eq 'euc') {
$line = &'euc_jis($line);
} elsif ($code eq 'jis') {
$line = &'jis_jis($line);
} elsif ($code eq 'sjis') {
$line = &'sjis_jis($line);
}
}
return $line;
}
sub jis_code {
local($line, $list) = @_;
local($code);
$code = (split(/\,/, "\L$list\E"))[0];
if ($code eq 'euc') {
$line = &'jis_euc($line);
} elsif ($code eq 'jis') {
$line = &'jis_jis($line);
} elsif ($code eq 'sjis') {
$line = &'jis_sjis($line);
}
return $line;
}
__END__
--><HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-2022-JP">
<LINK REV="made" HREF="mailto:hasegawa@madoka.org">
<TITLE>auto/random.plm</TITLE></HEAD><BODY>
$B%*%s%i%$%s%I%-%e%a%s%H(B
<HR><H3>$BL>A0(B</H3>
auto/random.plm - $BFCDj$NH/8@$KH?1~$7$F%i%s%@%`$JH/8@$r$9$k(B
<HR><H3>$B@bL@(B</H3>
$BFCDj$N%a%C%;!<%8$,H/8@$5$l$k$H!";XDj$5$l$?%a%C%;!<%8$NCf$+$i(B
$B$I$l$+(B1$B$D$r%i%s%@%`$KA*Br$7$FH/8@$7$^$9!#(B
<HR><H3>$B%W%m%Q%F%#(B</H3>
<DL>
<DT> auto.random.file $B%U%!%$%kL>(B[;({jis|euc|sjis})]
</DT>
<DD> $B%i%s%@%`$KH/8@$9$k%a%C%;!<%8$N=q$+$l$?%U%!%$%k$r;XDj$7$^$9!#(B
$B%U%!%$%k$NCf$G$O(B1$B9T$K(B1$B$D$N%a%C%;!<%8$r=q$$$F2<$5$$!#(B
$B%U%!%$%k$O;XDj$7$?4A;z%3!<%I$GJ]B8$5$l$^$9!#(B
$B4A;z%3!<%I$rJ#?t;XDj$7$?>l9g$O!":G=i$K;XDj$7$?4A;z%3!<%I$G(B
$BJ]B8$5$l!">JN,$7$?>l9g$O!V(Bjis$B!W$r;XDj$7$?$3$H$HF1$8$G$9!#(B
$B$^$?!"!V(Beuc$B!W$H!V(Bsjis$B!W$rF1;~$K;XDj$9$k$3$H$O$G$-$^$;$s!#(B
</DD>
<DT> auto.random.alias $B%U%!%$%kL>(B[;({jis|euc|sjis})]
</DT>
<DD> $B%(%$%j%"%9$r;2>H$9$k%U%!%$%k$r;XDj$7$^$9!#(B
$B$3$N%U%!%$%k$,;XDj$5$l$F$$$k$H!"C/$KBP$9$kH/8@$+$r(B
$B%K%C%/%M!<%`$G$O$J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D>
</DL>
<HR><H3>$B@_DjNc(B</H3>
<PRE>
+ auto/random.plm
auto.random.request: $B:#F|$N1?@*$O(B?
auto.random.request: $B$&$i$J$$(B
auto.random.file: random.txt;jis
auto.random.format: #(name|nick|nick.now)$B$5$s$N1?@*$O!V(B#(message)$B!W$@$h$C(B
auto.random.rate: 100
auto.random.add: $B$&$i$J$$DI2C(B
auto.random.remove: $B$&$i$J$$:o=|(B
</PRE>
$B!V:#F|$N1?@*$O(B?$B!W$+!V$&$i$J$$!W$N$I$A$i$+$NH/8@$,$"$C$?$H$-$K!"(B
$B!V(Brandom.txt$B!W$NCf$K=q$+$l$?%a%C%;!<%8$NCf$N$I$l$+(B1$B$D$rH/8@$7$^$9!#(B
random.txt$B$O(Bjis$B$G=q$+$l$F$$$J$$$H$$$1$^$;$s!#(B
$B$^$?!"!V$&$i$J$$DI2C(B $BBg5H$G$9!y!W$J$I$N$h$&$KH/8@$9$k$3$H$G!"(B
$B?7$7$$%a%C%;!<%8$rDI2C$9$k$3$H$,$G$-$^$9!#(B
</BODY></HTML>