diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a8e60f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.secrets.pm +*.secrets.pm +.env diff --git a/bridge.pl b/bridge.pl index 6d03592..9fa22c5 100644 --- a/bridge.pl +++ b/bridge.pl @@ -1,43 +1,30 @@ #!/usr/bin/env perl use strict; use warnings; - use POE; use POE::Component::IRC; use WWW::Telegram::BotAPI; - -# ------------------------------------------------- -# Import secrets -# ------------------------------------------------- require './.secrets.pm'; my $IRC_SERVER = '127.0.0.1'; my $IRC_PORT = 6667; my $IRC_NICK = 'Jerry'; my $IRC_CHANNEL = $Secrets::IRC_CHANNEL; - my $NICKSERV_PASSWORD = $Secrets::NICKSERV_PASSWORD; -# ------------------------------------------------- -# Telegram Setup -# ------------------------------------------------- my $telegram = WWW::Telegram::BotAPI->new( token => $Secrets::TELEGRAM_TOKEN ); -my $allowed_user = $Secrets::TELEGRAM_USER_ID; +my $myTele = $Secrets::TELEGRAM_USER_ID; -# Keep track of last update to avoid duplicates +# We don't wanna double post the last thing we saw my $last_update_id = 0; - -# ------------------------------------------------- -# IRC Setup -# ------------------------------------------------- my $irc = POE::Component::IRC->spawn( Nick => $IRC_NICK, Server => $IRC_SERVER, Port => $IRC_PORT, - Ircname => 'Telegram Bridge Bot', + Ircname => 'Jerry', ) or die "Cannot spawn IRC component\n"; POE::Session->create( @@ -57,16 +44,10 @@ POE::Session->create( $poe_kernel->run(); exit 0; - -# ------------------------------------------------- -# POE Handlers -# ------------------------------------------------- sub _start { my ($kernel) = $_[KERNEL]; - $irc->yield(register => 'all'); $irc->yield(connect => {}); - # Poll Telegram every 2 seconds $kernel->delay(poll_telegram => 2); } @@ -81,16 +62,13 @@ sub irc_public { my ($who, $where, $what) = @_[ARG0, ARG1, ARG2]; my $nick = (split /!/, $who)[0]; my $channel = $where->[0]; - return unless $channel eq $IRC_CHANNEL; - send_telegram("[$nick] $what"); } sub irc_msg { my ($who, $what) = @_[ARG0, ARG2]; my $nick = (split /!/, $who)[0]; - send_telegram("[PM from $nick] $what"); } @@ -98,43 +76,31 @@ sub irc_disconnected { $irc->yield(connect => {}); } -# ------------------------------------------------- -# Telegram Polling -# ------------------------------------------------- sub poll_telegram { my ($kernel) = $_[KERNEL]; - my $updates = eval { $telegram->getUpdates({ offset => $last_update_id }) }; if ($@) { warn "Telegram poll failed: $@"; } elsif ($updates && $updates->{ok}) { for my $update (@{ $updates->{result} }) { $last_update_id = $update->{update_id} + 1; - next unless $update->{message}; my $msg = $update->{message}; - # Only allow the specific user - next unless $msg->{from}{id} == $allowed_user; - + next unless $msg->{from}{id} == $myTele; my $text = $msg->{text} // ''; next unless $text; - $irc->yield(privmsg => $IRC_CHANNEL => $text); } } - $kernel->delay(poll_telegram => 2); } -# ------------------------------------------------- -# Send message to Telegram -# ------------------------------------------------- sub send_telegram { my ($text) = @_; eval { $telegram->sendMessage({ - chat_id => $allowed_user, + chat_id => $myTele, text => $text, }); };