-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridgebot.pl
122 lines (108 loc) · 2.84 KB
/
bridgebot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl
# BridgeBot - a simple Perl bot for bridging the gap between an IRC
# channel and an XMPP MUC groupchat.
#
# <standard legalese disclaimers>
#
# Not that special, not very useful, but there seemed to be no bots having
# this specific functionality advertised, and that's what I needed.
#
# (K) Petter Ericson - [email protected]
use utf8;
use AnyEvent;
use AnyEvent::XMPP::IM::Connection;
use AnyEvent::XMPP::Util qw/split_jid/;
use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::MUC;
use AnyEvent::XMPP::Ext::MUC::Message;
use AnyEvent::IRC::Client;
use Data::Dumper;
use warnings;
use strict;
#Some configuration
my $xmppconf = {
nick => '',
jid => '',
password => '',
room => ''
};
my $ircconf = {
nick => '',
server => '',
port => 6667,
channel => ''
};
# The event loop conditional
my $j = AnyEvent->condvar;
#IRC connection object
my $irc = new AnyEvent::IRC::Client;
#XMPP objects (xmpp, disco and muc)
my $xmpp = AnyEvent::XMPP::IM::Connection->new (
jid => $xmppconf->{'jid'},
password => $xmppconf->{'password'},
);
$xmpp->add_extension( my $disco = AnyEvent::XMPP::Ext::Disco->new );
$xmpp->add_extension( my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco));
#IRC callbacks, simple, stupid
$irc->reg_cb (
# Once registered, join the channel
registered => sub {
print "Joining IRC channel " . $ircconf->{ 'channel' } . "\n";
$irc->send_srv(JOIN => $ircconf->{ 'channel' });
},
#On receit of a public message
publicmsg => sub {
my ($con, $channel, $ircmsg) = @_;
# extract nick
my $nick = AnyEvent::IRC::Util::prefix_nick($ircmsg->{prefix});
# and message
my $message = $ircmsg->{params}->[1];
# if we didn't send it
if(not $nick eq $ircconf->{'nick'}) {
# pass it on
my $msg = AnyEvent::XMPP::Ext::MUC::Message->new(
to => $xmppconf->{'room'},
connection => $xmpp,
body => $nick . ": ". $message,
type => 'groupchat');
$msg->send($muc->get_room($xmpp, $xmppconf->{'room'}));
}
}
);
#MUC callbacks, simple, stupid
$muc->reg_cb (
#On receit of a MUC message
message => sub {
my ($muc, $room, $msg, $is_echo) = @_;
# if we didn't send it
if(not $msg->from_nick eq $xmppconf->{'nick'}) {
# pass it on
$irc->send_srv(PRIVMSG => $ircconf->{'channel'},
$msg->from_nick. ": " . $msg->any_body) ;
}
},
# Diagnostics
join => sub {
print "joined\n";
}
);
$xmpp->reg_cb (
# Join chat when ready
session_ready => sub {
my ($con, $acc) = @_;
print "XMPP session ready, joining chat\n";
$muc->join_room($con, $xmppconf->{'room'}, $xmppconf->{'nick'})
},
# Report errors
error => sub {
my ($con, $err) = @_;
print "ERROR: " . $err->string . "\n";
},
message_error => sub {
print "error";
}
);
$xmpp->connect();
$irc->connect($ircconf->{'server'}, $ircconf->{'port'}, { nick => $ircconf->{'nick'} });
$j->wait;
$irc->disconnect();