| 1 | #!/usr/bin/perl |
|---|
| 2 | # IRC<->LJ bot |
|---|
| 3 | |
|---|
| 4 | use warnings; |
|---|
| 5 | use strict; |
|---|
| 6 | use POE; |
|---|
| 7 | use POE::Component::IRC; |
|---|
| 8 | use POE::Component::IRC::Common qw(parse_user); |
|---|
| 9 | use LJ; |
|---|
| 10 | sub CHANNEL () { ("#lj", "#gnaa") } |
|---|
| 11 | |
|---|
| 12 | my $blogger; |
|---|
| 13 | #$blogger->post("Test1", "This is a test. feels good\n\ntest."); |
|---|
| 14 | #print $blogger->comment("http://lj.rossia.org/users/mao/80045.html?mode=reply", "nice blog!"); |
|---|
| 15 | |
|---|
| 16 | # Create the component that will represent an IRC network. |
|---|
| 17 | my $irc = POE::Component::IRC->spawn(); |
|---|
| 18 | |
|---|
| 19 | # Create the bot session. The new() call specifies the events the bot |
|---|
| 20 | # knows about and the functions that will handle those events. |
|---|
| 21 | POE::Session->create( |
|---|
| 22 | inline_states => { |
|---|
| 23 | _start => \&bot_start, |
|---|
| 24 | irc_001 => \&on_connect, |
|---|
| 25 | irc_public => \&on_public, |
|---|
| 26 | irc_invite => \&on_invite, |
|---|
| 27 | }, |
|---|
| 28 | ); |
|---|
| 29 | |
|---|
| 30 | sub on_invite { |
|---|
| 31 | my ($kernel, $who, $chan) = @_[KERNEL, ARG0, ARG1]; |
|---|
| 32 | print parse_user($who); |
|---|
| 33 | print "yes hello"; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | sub bot_start { |
|---|
| 37 | $irc->yield(register => "all"); |
|---|
| 38 | my $nick = 'KikeJournal'; |
|---|
| 39 | $irc->yield( |
|---|
| 40 | connect => { |
|---|
| 41 | Nick => $nick, |
|---|
| 42 | Username => 'lj', |
|---|
| 43 | Ircname => 'LJ<->IRC bot :: http://hardchats.lj.ru', |
|---|
| 44 | Server => 'irc.gnar.int.ru', |
|---|
| 45 | Port => '6667', |
|---|
| 46 | } |
|---|
| 47 | ); |
|---|
| 48 | $blogger = new LJ(user=>"hardchats", server=>"livejournal.com"); |
|---|
| 49 | $blogger->password("nigger88"); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | # The bot has successfully connected to a server. Join a channel. |
|---|
| 53 | sub on_connect { |
|---|
| 54 | foreach my $chan(CHANNEL) { |
|---|
| 55 | $irc->yield(join => $chan); |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | sub on_public { |
|---|
| 62 | my ($kernel, $who, $where, $msg) = @_[KERNEL, ARG0, ARG1, ARG2]; |
|---|
| 63 | my $nick = (split /!/, $who)[0]; |
|---|
| 64 | my $channel = $where->[0]; |
|---|
| 65 | my $ts = scalar localtime; |
|---|
| 66 | print " [$ts] <$nick:$channel> $msg\n"; |
|---|
| 67 | |
|---|
| 68 | if (my ($blogpost) = $msg =~ /^!lj (.+)/) { |
|---|
| 69 | if(my @ret = $blogger->post("$nick says", $blogpost)) { |
|---|
| 70 | $irc->yield(privmsg => $where, "Posted @ ".$ret[-1]); |
|---|
| 71 | } else { |
|---|
| 72 | $irc->yield(privmsg => $where, "something went gay"); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | $poe_kernel->run(); |
|---|
| 79 | exit 0; |
|---|
| 80 | |
|---|