无聊的时候,改了一个bot

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

无聊的时候,改了一个bot

#1

帖子 eexpress » 2009-03-17 14:37

代码: 全选

sudo cpan NET::IRC 
还有NET::jabber。如果喜欢上jabber的聊天室。

在里面找test.pl
or
http://www.perlmonks.org/?node_id=266428

在 on_public 函数里面加上:

代码: 全选

#========================================	
	my @Anick=(
	["roylez","金日成:π"],
	["GundamZZ","包包:π"],
	["bones7456","排骨:π"],
	["AutumnCat","球猫:π"],
	["iNutshell","栗子壳:π"],
	["manphiz","糖糖:π"],
	["freeflying","狒狒:π"],
	["iPeipei","佩佩朶:π"],
	["Arthrun","老雕:π"],
	["ShelyII","猞猁:π"],
	["lerosua","广陵散:π"],
	["^k^","这猪猪bot"],
	["sunmoon1997","月月"],
	);
	for my $i ( 0 .. $#Anick ){
	if($Anick[$i][0] eq $nick){
	$self->privmsg("${cfg_room}", "$Anick[$i][1]\n");
	}
	}
	my ($c,$w)=split(/\s/,$arg);
	my @Afunc=(
	["t", "sdcv.bash"],
	["a", "ip-ip纯真库.pl"],
	["f", "t", 2],
        );
	for my $i ( 0 .. $#Afunc ){
	if($Afunc[$i][0] eq $c){
	my $r=`$Afunc[$i][1] $w`;
#        chomp($r);
#        print "$r\n";
	for my $i ($r=~/(.*)\n/g){
	$self->privmsg("${cfg_room}", $i);
	}
#        $self->privmsg("${cfg_room}", `$Afunc[$i][1] $w`);
	}
	}
#========================================	
在 gir.cfg 里面写上

代码: 全选

nick=xxx
altnick=xxx
ident=xxx-bot
port=8000
server=irc.freenode.net
room=#ubuntu-cn
就完成了。
Afunc 哪里是加功能的。
凡是碰到第一个参数开头的,就调用第二个命令(命令的参数是文本信息的第2个字段),然后输出到聊天室。
● 鸣学
头像
lerosua
论坛版主
帖子: 8455
注册时间: 2007-11-29 9:41
联系:

Re: 无聊的时候,改了一个bot

#2

帖子 lerosua » 2009-03-17 14:43

有空看看 jabber的bot,吸取下经验做自己的bot
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 无聊的时候,改了一个bot

#3

帖子 eexpress » 2009-04-10 10:02

代码: 全选

2009-04-10 10:01:52 五 ~/应用/脚本  
☎ cat gir.pl 
#!/usr/bin/perl
#
# girbot.pl
# Gir IRC Bot
#
# Author: Wraithnix ([email protected])
# Version: 0.1
# Web: http://www.riotmod.com
#
# This script is released under the
# GNU General Public License.
#
use strict;
use Net::IRC;
use Switch;

# ============
# SUPPORT CODE
# ============

# GetSetting
# Arguments: setting to retrieve,configuration file
# Returns: The setting value, or "" if it doesn't exist
# Description: Use this sub to load values
#              from a configuration file.
#              Settings are stored in this format:
#              <setting>=<value>
sub GetSetting
{
  my ($setting,$config_file)=@_;
  open(CFGFILE,"<$config_file")
    or die "Can't open configuration file ($config_file)";
  my @slist=<CFGFILE>;
  foreach my $selem (@slist)
  {
    if (index($selem,"#")==0) { next; }
    my @ln=split("=",$selem);
    if ($ln[0] =~ /$setting/i)
    {
        chomp $ln[1];
        return $ln[1];
    }
  }
  close CFGFILE;
}

# =============
# MAIN BOT CODE
# =============

# Set our configuration file
my $configuration_file = "/home/exp/.irc-bot.cfg";
# You can start the bot with a config file as a
# commandline argument.  Without the argument,
# the bot loads its settings from "gir.cfg", in
# the same directory as the bot.
if($#ARGV==0) { $configuration_file=$ARGV[0]; }
# Now, we can load in our script's settings
my $cfg_nick=GetSetting("nick",$configuration_file);
my $cfg_altnick=GetSetting("altnick",$configuration_file);
my $cfg_ident=GetSetting("ident",$configuration_file);
my $cfg_port=GetSetting("port",$configuration_file);
my $cfg_server=GetSetting("server",$configuration_file);
my $cfg_room=GetSetting("room",$configuration_file);
my $cfg_master=GetSetting("master",$configuration_file);
#my $welcome=GetSetting("welcome",$configuration_file);

#----------------------------------------------
my $ppp='\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
my $ipv6='[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}';
my @Amynick=("iPhone","iFvwm","iGoogle","iGnome","iOpera");
push(@Amynick,$cfg_master);
my @ACmd=("welcome","voice","http");
my $voice=1;
my $welcome=0;
my $http=0;
#----------------------------------------------

# Just about all of the settings are "strings", except
# for the "port".  Let's make sure that that setting
# is numerical, and if not, set it to the most common
# port, 6667:
if($cfg_port=~/\D/) { $cfg_port=6667; }
#if($cfg_room=~/\D/) { $cfg_room="#ubuntu-cn"; }

# Now that all of our settings are loaded in,
# let's create the IRC object
my $irc = new Net::IRC;
print "Creating connection to IRC server...";
my $conn = $irc->newconn(Server   => "$cfg_server",
             Port     => $cfg_port,
             Nick     => "$cfg_nick",
             Ircname  => "$cfg_ident",
             Username => "$cfg_ident")
    or die "Can't connect to IRC server.";
print "done!\n";

# With that out of the way, let's create
# some subs for our object handlers

# What our bot will do when it finishes
# connecting to the IRC server
sub on_connect {
    my $self = shift;
  print "*** Connected to IRC.\n";
        print "Joining $cfg_room ...\n";
        $self->join("${cfg_room}");
        $self->privmsg("${cfg_room}", "Ξ");

}
# This sub will print various
# incoming date while we're still
# connecting to IRC
sub on_init {
    my ($self, $event) = @_;
    my (@args) = ($event->args);
    shift (@args);

    print "*** @args\n";
}
# This sub will handle what happens when the
# bot receives public (channel) text.
sub on_public {
    my ($self, $event) = @_;
    my @to = $event->to;
    my ($nick, $mynick) = ($event->nick, $self->nick); # Sender text, 
#+Bot nick
    my $host=$event->host; # Sender's hostname
    my ($arg) = ($event->args); # The message
    
    # Here's where we want to "parse" channel text
#            print "<$nick> $arg\n";
#----------------------------------------------
        if($nick=~$self->nick) {return 1;}      #自己的话
        if($voice==1){
        foreach (@Amynick){
        if($arg=~/\b$_\b/i){
`aplay 2>/dev/null '/home/exp/应用/Game-Grid-Wars/sounds/bonus1.wav'`;
        last;}}}
#----------------------------------------------
        my ($c,$w)=split(/\s/,$arg);
        my @Afunc=(
        ["h", "echo t 翻译 p 查IP库 ap 精确ip r 写倒字 f 写点字 c 计算 y 荧光字 w 城市天气 u url字串和utf8互转 py 汉字查拼音 bd 百度 deb 包信息 en 中文翻译英文 cn 英文翻译中文"],
        ["t", "sdcv.pl"],
        ["p", "ip-ip纯真库.pl"],
        ["f", "t.bash", 2],
        ["c", "c"],
        ["r", "r"],
        ["w", "w.pl"],
        ["y", "f-荧光字贴图.bash"],
        ["u","url字串和utf8互转.pl"],
        ["py","han-pinyin.pl"],
        ["bd","baidu.pl", 1],
        ["deb","deb.pl"],
        ["ap","apnic.pl"],
        ["en","cn-en.bash"],
        ["cn","en-cn.bash", 1],
        );
        for my $i ( 0 .. $#Afunc ){
        if($Afunc[$i][0] eq $c){
        if($Afunc[$i][2]==1){$w=$arg; $w=~s/^.*? //; $w=~s/ /+/g;}      #全部参数
        print "cmd:\t <$Afunc[$i][1] 2>/dev/null $w>\n";
        my $r=`$Afunc[$i][1] \'$w\'`;
        if($Afunc[$i][2]==2){   #多行输出
        for my $i ($r=~/(.*)\n/g){
        $self->privmsg("${cfg_room}", $i);
        }
        }
        else{
        $self->privmsg("${cfg_room}", $r);
        }
#        $self->privmsg("${cfg_room}", `$Afunc[$i][1] $w`);
        }
        }
#----------------------------------------------
        if($http==1){
        if($arg=~/http:\/\/\S*/){
        my $g=$&;
        if ($g!~/\.flv$|\.w..$|\.mp.{1,2}$|\.gz$|\.rar$|paste\.|\.zip$/){
        my $t=`web-title.pl \'$g\'`;
#        my $r=`wget --no-cookies --no-follow-ftp \'$&\' -O -|enconv`;
#        w3m -no-cookie -dump_source http://www.lwpl.cn/|g title
#        $r=~/<title>(.*?)<\/title>/is;my $t=$&;$t=~s/<[^>]*>//g;
        $self->privmsg("${cfg_room}", "$g 网页标题:$t\n");
        }
        else{$self->privmsg("${cfg_room}", "$g 流媒体地址。\n");}
        }
        }
#----------------------------------------------
        if($arg=~s/.*知道(.+)吗.*/\1/){
#        $arg=~s/.*知道(.+)[吗??]*/--\1--/;
#        print "解析0:=$&=\n";
        print "解析1:=$arg=\n";
        }
#----------------------------------------------
}
# This sub will handle what happens when the
# bot receives private message text
sub on_join {
    my ($self, $event) = @_;
    my ($nick) = $event->nick; # Message Sender
#    my ($arg) = ($event->args); # Message Text
    my $host=$event->host;
#----------------------------------------------
if(join(" ",@Amynick)=~$nick) {return 1;}       # 主人列表,忽略
#----------------------------------------------
        my @Anick=(
        ["roylez","金主席"],["GundamZZ","包包"],["bones7456","排骨"],
        ["AutumnCat","球猫"],["iNutshell","栗子壳"],["manphiz","糖糖"],
        ["freeflying","狒狒"],["iPeipei","佩佩朶"],["Arthrun","老雕"],
        ["ShelyII","猞猁"],["lerosua","广陵散"],["^k^","这猪猪bot"],
        ["sunmoon1997","月月神教"],["palomino|working","破马"],
        ["Epocher","洗脚"],["oneleaf","叶子"],["mOo","摸光"],
        ["zhan","鲇鱼"],["GNUdog","狗狗"],["eXopeth","蜗牛"],
        ["DawnFantasy","豆腐"],["Fong","赌棍"],
        );
        for my $i ( 0 .. $#Anick ){
        if($nick=~/^$Anick[$i][0]\d*\b/i){
        $nick=$Anick[$i][1]; last;
        }
        }
#----------------------------------------------
my $a="";
while($a eq ""){
        switch ($host) {
                case "59.36.101.19"     {$a="ubuntu-cn论坛的webirc"}
                case /^$ipv6.*/ {$a="太阳系v6"}
                case /^$ppp$/   {$a=`ip-ip纯真库.pl $host`}
                case /.*mibbit.*/       {$a="mibbit"}
                case m:/:       {$a="太阳系"}
                else{
                print "---------\n";
                while (my ($k,$v) = each %$event){print "- $k => $v\n";}
                $host=`host $host`;
                print "- new host----$host.\n";
                if($host=~/\b$ppp\b/){$host=$&;}
                else{$a="鬼搞鬼搞的地方";}
                }
        }
}
#----------------------------------------------
        if($a=~"IANA"){$a="外太空";}
        chomp($a);
        print "$host==$a==\n";
        if($welcome==1) {
        $self->privmsg("${cfg_room}", "欢迎来自 $a 的 $nick 加入 ubuntu 技术聊天室。《".$event->user."》\n");}
        else{
        print "欢迎来自 $a 的 $nick 加入 ubuntu 技术聊天室。《".$event->user."》\n";}
#----------------------------------------------
}
sub on_msg {
    my ($self, $event) = @_;
    my ($nick) = $event->nick; # Message Sender
    my ($arg) = ($event->args); # Message Text
    my $host=$event->host;
    
    # Here's where we want to "parse" message text
#    print " - $nick -  $arg\n";
#----------------------------------------------
if(join(" ",@Amynick)=~$nick){  # 主人列表,私聊命令
        my ($c,$w)=split(/\s/,$arg);
switch ($c){
        case "join" {$self->join("${cfg_room}");}
        case "nick" {$self->nick($w);}
        case "help" {print join(",",@ACmd);}
        case (\@ACmd) {
                my $cmd;
                if($w eq "on"){$cmd="$c=1";}else{$cmd="$c=0";}
                eval "\$$cmd";
                $self->privmsg("${cfg_room}", "Yes, Sir. $cmd\n");
                $cmd="print \"----$c=\$$c----\t\"";
                eval "$cmd";
#                while (my ($k,$v) = each %$self){print "- $k => $v\n";}
                last;
        }
        else {$self->privmsg("${cfg_room}", "$arg\n");}
}
}
#----------------------------------------------
else {
$self->privmsg("${cfg_room}", "$nick: 别私聊。不告诉你,气死你。:D\n");
}
#----------------------------------------------
}
# This sub will get triggered if our bot's nick
# is taken, setting it to our alternate nick.
sub on_nick_taken {
    my ($self) = shift;

    $self->nick($cfg_altnick);
}
# Now that all of our handler subs are created,
# let's install them
print "Installing local handlers...";
$conn->add_handler('public', \&on_public);
$conn->add_handler('msg',    \&on_msg);
$conn->add_handler('join',    \&on_join);
print "done!\nInstalling global handlers...";
$conn->add_global_handler([ 251,252,253,254,302,255 ], \&on_init);
$conn->add_global_handler(376, \&on_connect);
$conn->add_global_handler(433, \&on_nick_taken);
print "done!\n";
# Everything's installed, so there's nothing
# holding up back from starting up!
$irc->start;

● 鸣学
头像
shellex
帖子: 2180
注册时间: 2007-02-18 19:33
系统: OSX
来自: lyric.im
联系:

Re: 无聊的时候,改了一个bot

#4

帖子 shellex » 2009-04-10 10:06

。。。。
既然你诚心诚意地问了
我就大慈大悲地告诉你
为了防止世界被破坏
为了维护世界的和平
贯彻爱与真实的罪恶
可爱而又迷人的反派角色
武藏,小次郎
我们是穿越银河的火箭队,白洞白色的明天在等着我们。就是这样!!喵~~
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 无聊的时候,改了一个bot

#5

帖子 eexpress » 2013-01-09 16:39

add to rpi
● 鸣学
jtshs256
帖子: 22323
注册时间: 2010-07-19 21:41
系统: OS X

Re: 无聊的时候,改了一个bot

#6

帖子 jtshs256 » 2013-01-09 16:44

ee 的神坟啊…… :em70
躺平
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

Re: 无聊的时候,改了一个bot

#7

帖子 bones7456 » 2013-01-28 21:30

09年的帖子哦,看到我的名字了~
关注我的blog: ε==3
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: 无聊的时候,改了一个bot

#8

帖子 枫叶饭团 » 2013-01-28 21:35

...
回复