root/trunk/irssi/irssi2jabber.pl

Revision 61, 5.5 kB (checked in by alex, 2 years ago)
  • created new project phase5inc
  • updated svn properties of other perl stuff
  • Property svn:mime-type set to text/x-perl
  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 #
3 # Sends a message to jabber if a hilight occurs in irssi.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #
19 # Add config to your irssi configuration in the following style:
20 #
21 #   settings = {
22 #       "perl/core/scripts" = {
23 #           irssi2jabber_server = "yourserver.com";
24 #           irssi2jabber_port = "5222";
25 #           irssi2jabber_user = "youruser";
26 #           irssi2jabber_password = "yourpassword";
27 #           irssi2jabber_resource = "yourresource";
28 #           irssi2jabber_dest = "alias@yourdestination.com";
29 #           irssi2jabber_awayonly = 1;
30 #       };
31 #   };
32 #
33 # "port" and "resource" may be left out. The ports defaults to 5222 and
34 # the resource default is "irssi".
35 #
36 # See also http://blog.antiblau.de/2008/07/10/irssi-hilight-mit-jabber/
37
38 use strict;
39 use warnings;
40
41 use Net::Jabber;
42 use Irssi;
43 use Encode;
44
45 use vars qw($VERSION %IRSSI);
46
47 $VERSION='0.4';
48 %IRSSI = (
49     authors => 'Stefan Haun',
50     contact => 'mail\@tuxathome.de',
51     name => 'irssi2jabber.pl',
52     description => 'Send irssi hilights and privmsgs to jabber.',
53     license => 'GPLv3');
54
55 # Register settings
56 Irssi::settings_add_str('misc', 'irssi2jabber_server', '');
57 Irssi::settings_add_str('misc', 'irssi2jabber_port', '5222');
58 Irssi::settings_add_str('misc', 'irssi2jabber_user', '');
59 Irssi::settings_add_str('misc', 'irssi2jabber_password', '');
60 Irssi::settings_add_str('misc', 'irssi2jabber_resource', 'irssi');
61 Irssi::settings_add_str('misc', 'irssi2jabber_destination', '');
62 Irssi::settings_add_bool('misc', 'irssi2jabber_awayonly', 1);
63
64 # Trap signals which are emitted on interesting messages
65 Irssi::signal_add('message private', 'trap_privmsg');
66 Irssi::signal_add('print text', 'trap_hilight');
67
68 ## Called when a hilighted message is print out
69 #
70 # Adheres to the irssi declaration for this signal
71
72 sub trap_hilight {
73     my ($dest, $text, $stripped) = @_;
74     my $server = $dest->{server};
75     my ($hilight) = Irssi::parse_special('$;');
76
77     # only highlighted messages/actions 
78     return if (!$server || !($dest->{level} & MSGLEVEL_HILIGHT) ||
79         ($dest->{level} & (
80                             MSGLEVEL_MSGS|MSGLEVEL_NOTICES|MSGLEVEL_SNOTES|
81                             MSGLEVEL_CTCPS|MSGLEVEL_JOINS|
82                             MSGLEVEL_PARTS|MSGLEVEL_QUITS|MSGLEVEL_KICKS|
83                             MSGLEVEL_MODES|MSGLEVEL_TOPICS|MSGLEVEL_WALLOPS|
84                             MSGLEVEL_INVITES|MSGLEVEL_NICKS|MSGLEVEL_DCC|
85                             MSGLEVEL_DCCMSGS|MSGLEVEL_CLIENTNOTICE|
86                             MSGLEVEL_CLIENTERROR   
87                           )
88         )
89     );
90
91     # only if in away mode
92     if (!Irssi::settings_get_bool('irssi2jabber_awayonly')
93         || $server->{usermode_away}) {
94         &send_message("Hilight from $hilight in $dest->{target} " .
95             "on $server->{chatnet}", $stripped);
96     }
97 }
98
99 ## Called when a private message has been received
100 #
101 # Adheres to the irssi declaration for this signal
102
103 sub trap_privmsg {
104     my ($server, $msg, $nick, $address) = @_;
105
106     # only if in away mode
107     if (!Irssi::settings_get_bool('irssi2jabber_awayonly')
108         || $server->{usermode_away}) {
109         &send_message("Private message from $nick on $server->{chatnet}", $msg);
110     }
111 }
112
113 ## Send a message to the designated jabber account
114 #
115 # Since connecting to the jabber server for simple message dispatching
116 # is quite fast, the connection is established for every message. This
117 # is simpler than managing persistent connection, which may be broken
118 # or outdated due to configuration changes.
119 #
120 # Parameters:
121 #   1. $subject     The message subject
122 #   2. $body        The message body
123 #
124 # Prints an error message to the irssi console if something goes wrong.
125
126 sub send_message {
127     my ($subject, $body) = @_;
128
129     my $conn = Net::Jabber::Client->new();
130     $conn->Connect(
131         "hostname" => Irssi::settings_get_str('irssi2jabber_server'),
132         "port" => Irssi::settings_get_str('irssi2jabber_port')
133     ) or do {
134         Irssi::print "Cannot connect: $!\n";
135         return;
136     };
137
138     my @result = $conn->AuthSend(
139         "username" => Irssi::settings_get_str('irssi2jabber_user'),
140         "password" => Irssi::settings_get_str('irssi2jabber_password'),
141         "resource" => Irssi::settings_get_str('irssi2jabber_resource')
142     );
143
144     if ($result[0] ne 'ok') {
145         Irssi::print "Auth failed: $result[0] - $result[1]\n";
146         return;
147     }
148
149     # assuming irssi has no perl strings but binary strings
150     # and Net::Jabber requires decoded perl strings
151     my $irssitermenc = Irssi::settings_get_str('term_charset');
152     if (Encode::resolve_alias($irssitermenc) eq 'ascii')
153     {
154         $irssitermenc = Irssi::settings_get_str('recode_fallback');
155     }
156     $conn->MessageSend(
157         'to' => Irssi::settings_get_str('irssi2jabber_destination'),
158         'subject' => decode($irssitermenc, $subject),
159         'body' => decode($irssitermenc, $body)
160     );
161
162     $conn->Disconnect();
163 }
164
165 # End of Script
166
Note: See TracBrowser for help on using the browser.