<img src=http://www.blogbus.com/blogbus/blog/userf
<font size=4 color=#ffffff>Just my feelings && some Perl stuffs ^-^</font><br> <font color=#ffffff> 这个blog记录我的一些生活琐事,关于Perl的以及其他在学习计算机,网络技术时所得的经验和有趣的东西</font>


IRCRobot by Perl
时间: 2004-02-04

    今天想写个robot玩一下,有很多途径与robot沟通,但是我平常用及时通信软件比较多,所以找到了Net::Jabber,但是在我的win2000+activeperl上无法通过XML::Stream的安装测试,老是在第3个测试那里死掉。于是把希望投到Net::ICQ和Net::MSN上,无奈他们都出了最新的协议,CPAN上的模块不支持,sucks!只剩IRC了,IRC也不错嘛,正好Net::IRC能安装成功,最近weblog这么热,就写个抓新闻或者weblog站点rss文件,然后分析显示其中主题的IRCRobot吧,当然这个robot的功能非常少,只有:

自动登陆IRC并防掉线;
对该robot说的话包含他的名字,他回提示输入list查看可用rss站点名;
输入list,显示当前可用的rss站点名;
输入: grab 可用rss站点名,robot会自动抓取目的rss文件对其进行分析并返回最新的主题极其url。
输入:go home,robot关闭。

功能和robot的智能的确简单了点,不过这也是我第一次写IRC的robot,对IRC不熟,等了解了IRC的大部分命令后再写个好点的。
其实以后用IRC来监视远程系统运行情况,进行简单的系统维护还是不错的,甚至可以结合weblog的XML-RPC接口在IRC上实现对自己weblog的更新,呵呵,不错。

代码如下:

 
#!/usr/bin/perl
#perlchina.org
#cnhackTNT[at]4red.org

use strict;
use Net::IRC;
use XML::RSS;
use LWP::Simple;
use Encode;

$SIG{INT} = \&quit;
my %rss = (
    slashdot        => 'http://slashdot.org/index.rss',
    googleblog      => 'http://google.blogspace.com/index.xml',
    freshmeat       => 'http://freshmeat.net/backend/fm.rdf',
    blogcn          => 'http://www.blogcn.com/user_rss.asp',
    cnblog          => 'http://www.cnblog.org/blog/cache/cnblog.xml',
    AmericanInChina => 'http://www.blogcn.com/rss.asp?uid=aradosh',
    AnorwegiansLife => 'http://www.blogcn.com/rss.asp?uid=houshuang'
);
my %init = (
    Nick    => 'Grabber',
    Server  => shift || 'css.sunnet.org',
    Port    => 6667,
    Ircname => 'I am a rss grabber .~',
    Channel => shift || '#rssGrabber.pm'
);
my $irc  = new Net::IRC;
my $conn = $irc->newconn(%init);
$conn->add_global_handler( '376',        \&on_conn );
$conn->add_global_handler( 'disconnect', \&on_disconnect );
$conn->add_handler( 'public', \&on_line );
$conn->add_handler( 'msg',    \&on_line );
$irc->start;

sub on_conn {
    print "Joining $init{'Channel'}...\n";
    my $self = shift;
    $self->join("$init{'Channel'}");
    $self->privmsg( "$init{'Channel'}",
        "Hi!I am the rss grabber.~" );
}

sub on_line {
    my ( $self, $event ) = @_;
    my ( $nick, $mynick ) = ( $event->nick, $self->nick );
    my ($arg) = ( $event->args );

    print "<$nick> $arg\n";
    if ( $arg =~ /grab\s+(\w+)/i ) {
        my $key = $1;
        my $topic;

        if ( exists $rss{$key} ) {
            $self->privmsg( $nick,
"4 Plz wait for awhile,I am grabbing news from 12 $rss{$key}!!"
            );
            $topic = &grab($key);
            if ($topic) {
                $self->privmsg( $nick, "-" x 80 );
                for ( keys %$topic ) {
                    $self->privmsg( $nick,
                            encode( "euc-cn", $_ ) . ' 7| '
                          . encode( "euc-cn", $topic->{$_} ) );
                    sleep 2;
                }
                $self->privmsg( $nick, "-" x 80 );
                $self->privmsg( $nick, 'Done!' );
            }
            else {
                $self->privmsg( $nick, '4Empty?!mybe the server\'s problem' );
            }
        }
        else {
            $self->privmsg( $nick, '4I dont\'t have this server\'s rss url' );
        }
    }

    if ( $arg =~ m/$mynick/i ) {
        $self->privmsg( $nick,
'Do u want read news or blogs?type \'list\' for available rss seeds.'
        );
    }

    if ( $arg =~ m/list/i ) {
        $self->privmsg( $nick, '4the present rss seeds includes:' );
        $self->privmsg( $nick, '-' x 80 );
        $self->privmsg( $nick, '12' . $_ ) for keys %rss;
        $self->privmsg( $nick, '-' x 80 );
        $self->privmsg( $nick, 'Done!' );
    }

    if ( $arg =~ /go home/i ) {
        $self->privmsg( $init{'Channel'}, 'BYE BYE~~' );
        $self->quit;
        exit 0;
    }

}

sub on_disconnect {
    my ( $self, $event ) = @_;
    print "Disconnected from ", $event->from(), " (", ( $event->args() )[0],
      "). Attempting to reconnect...\n";
    $self->connect();
}

sub quit {
    print "Somebody force me exit:-(\n";
    exit 0;
}

sub grab {
    my $key     = shift;
    my $topic   = {};
    my $o       = new XML::RSS;
    my $content = get( $rss{$key} )
      or return ( { Error => "Can\'t grab news from $rss{$key} !!" } );
    $o->parse($content);
    foreach my $item ( @{ $o->{'items'} } ) {
        next unless defined( $item->{'title'} ) && defined( $item->{'link'} );
        $topic->{ '4' . $item->{'title'} } = '12' . $item->{'link'};
    }
    return $topic;
}


syntax highlighted by Code2HTML, v. 0.9.1

方便自己用的,没什么特殊功能。

(要支持中文的话,需要下载gb2312.enc文件,拷贝到perl目录下lib/xml/parser/encoding下,这个文件可以在http://atzone.fatb.org/source/gb2312.enc下载)





atzone   发表于   2004-02-04 00:51    引用(Trackback0)

评论

Please, if you have free time, visit one website. I’d be grateful as once I applied at a website and got bad experience. The card didn’t come, maybe even someone stole it from postbox. But my friend recommended another site he recently found. Please, tell me if I can apply for a good credit card at

<a href= http://forbad-crdeitcards.cn/refinancing-credit-bad.html >bad credit refinancing</a>
<a href= http://forbad-credity.cn/approval-capital-credit-one.html >capital one bad credit approval</a>
Good Credit (http://forbaddredit.cn)   发表于   2008-01-21 08:42:05

You need to make sure your credit history is ok if you want to apply for a rewards credit card. If you credit history is not that great you will lose more money than you’ll make with those rebates. Of course, there are offers for all types of credit history on the net, especially at

<a href= http://hardcreditcsrd.cn/usa-new-bad-credit.html >usa new bad credit</a>
<a href= http://hardcredit-credidt.cn/airways-premier-world-card.html >us airways premier world card</a>
Good Credit (http://forbadcsrds.cn)   发表于   2008-01-21 08:17:22

Hi 2 all! I have financial problems and need a credit card as soon as possible. I hope that it will help me to solve some of them. I have heard about instant approval credit cards and found the site where such kinds of credit cards are presented. Do you think this site is real and instant approval credit cards can help me in some case?

<a href= http://fixing-creedit.cn/rebuild-credit-cards-help.html >cards to help u rebuild your credit</a>
<a href= http://hardcreditcrediot.cn/rewards-limited-history-credit.html >best rewards card limited credit history</a>
Good Credit (http://fixing-gard.cn)   发表于   2008-01-21 08:11:07

Using credit cards wisely is not an easy thing. One should choose a card for his spending preferences and know how to make payments so that his credit score will increase and debt reduce. Among web sources for credit card applications and guide to personal finance there is one I like most

<a href= http://bestdealscrdut.cn/transfer-balance-bank-u.html >u s bank balance transfer</a>
<a href= http://fixing-crediot.cn/credit-long-does-take.html >how long does it take to fix your credit</a>
BadGood Cards (http://besr-ceredit.cn)   发表于   2008-01-19 22:52:47

There is a reason why people get credit cards. You pay more but there is no other way to buy the things you want. The thing is that you need to carefully compare credit card deals before choosing the one that will cost you less. If you want to find one check out

<a href= http://amazing-credircards.cn/discover-transfer-balance-free.html >discover free balance transfer for life</a>
<a href= http://bestcreditcredicared.cn/transfer-balances-dicover-cards.html >dicover cards low apr on transfer balances</a>
Good CreditCard (http://bestdealsceditcards.cn)   发表于   2008-01-17 06:23:33

My credit is extremely bad. My family is in emergency situation. How can I get a gas credit card? Not a secured or prepaid. With death in our family I’ve looked thru many sites. What can you suggest? Does

<a href= http://cardsinstantreply.cn/discover-credit-apply-card.html >discover credit card apply</a>

Ne72Ma9rod
Best CreditCard (http://decisionscardfs.cn)   发表于   2008-01-07 18:45:22

Hi people! I applied for credit cards for so many times. This is perhaps my last chance. Yesterday I found credit card applications at a website. Is it good to improve credit? Please consult. It’s called

<a href= http://cardsinstantapprobal.cn/metarewards-discover-card-and.html >metarewards and discover card</a>
credit secure (http://fastdecisioncardds.cn)   发表于   2008-01-07 12:32:53

Hi people! I applied for credit cards for so many times. This is perhaps my last chance. Yesterday I found credit card applications at a website. Is it good to improve credit? Please consult. It’s called

<a href= http://cardsinstanst.cn/discover-scores-needed-card.html >scores needed for discover card</a>
unsecured cards (http://fastercards.cn)   发表于   2008-01-07 12:30:33

? I’m choked up!! Need fast credit improvement. Please advice where to apply for a credit card from Orchard? Here’s a guy named Vic – he says some website is worth applying. I’m unsure. Please tell what you think about

<a href= http://cardfs-insecond.cn/discover-offer-card-life.html >discover offer card 0% life</a>

R56ma87de
Good Credit (http://cardfs-inmediate.cn)   发表于   2008-01-06 04:35:09

It turned out I was a victim of identity theft. After spending a great deal of time fixing the problem I became paranoid about using credit cards anywhere. It took me about half a year to get over it. Now if I need a credit card I go to

<a href= http://carditinsecondes.cn/discover-credit-cards-apr.html >0% apr discover credit cards</a>

R56ma87de
Good Credit (http://cardfs-instantresults.cn)   发表于   2008-01-05 21:15:08

There is a reason why people get credit cards. You pay more but there is no other way to buy the things you want. The thing is that you need to carefully compare credit card deals before choosing the one that will cost you less. If you want to find one check out

<a href= http://deliquencies-crtedit.cn/credit-apply-visa-even.html >apply for a visa even with bad credit</a>
credit monitor (http://deliquencies-cred.cn)   发表于   2008-01-05 07:30:24

It is very important to have good or at least decent credit history if you want to be approved for any kind of credit. Your credit report is the document that shows your creditworthiness. I’ve been working hard on my credit history - paying my bills, keeping my income stable and I’m really hoping to be a proved for a good credit card deal at

<a href= http://deliquenciescredif.cn/premier-credit-first-score.html >first premier bank credit score</a>
first usa (http://deliquencies-creidtcard.cn)   发表于   2008-01-05 06:52:15

下载不了
TOM ()   发表于   2005-10-08 14:09:22

以前的atzone.fatb.org域名过期啦,现在这个文件在http://atzone.ipchina.org/source/gb2312.enc
atzone (http://atzone.ipchina.org)   发表于   2005-02-21 22:53:11

我在网上找了很就gb2312.enc的下在,总算找到了一个可用的下在。

mt.fqq0127.com/download/gb2312.enc
兔牛牛 (http://mt.fqq0127.com)   发表于   2005-01-04 11:19:39
发表评论

用户名:

Email:

主页:
http://

   
最后更新


Skyshui
Eliiott
Fatb@zzu
Blogbus银河影院
Plod
广告狂热者
TAOWEN的个人主页
Google