html/email_privmsgs.pl
1 # Copyright (c) 2010 Adam James <atj@pulsewidth.org.uk>
2
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # THE SOFTWARE.
20
21 use strict;
22 use vars qw($VERSION %IRSSI);
23
24 use Irssi;
25 use POSIX qw(strftime);
26
27 use Email::Sender::Simple qw(try_to_sendmail);
28 use Email::Simple;
29 use Email::Simple::Creator;
30
31 $VERSION = '0.5';
32 %IRSSI = (
33 authors => 'Adam James',
34 contact => 'atj@pulsewidth.org.uk',
35 url => 'http://git.pulsewidth.org.uk/?p=irssi-scripts.git;a=summary'
36 name => 'email_privmsgs',
37 description =>
38 "Emails you private messages sent while you're away. " .
39 "Useful in combination with screen_away. " .
40 "Requires Email::Sender.",
41 license => 'MIT',
42 );
43
44 my $FORMAT = $IRSSI{'name'} . '_crap';
45 my $msgs = {};
46
47 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_from_address',
48 'irssi@' . (%ENV->{'HOST'} || 'localhost'));
49 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_to_address',
50 %ENV->{'USER'});
51 Irssi::settings_add_str('misc', $IRSSI{'name'} . '_subject',
52 'IRC Private Messages from Irssi');
53
54 Irssi::theme_register([
55 $FORMAT,
56 '{line_start}{hilight ' . $IRSSI{'name'} . ':} $0'
57 ]);
58
59 # check the message log every 30 minutes
60 Irssi::timeout_add(30*60*1000, 'check_messages', '');
61
62 sub handle_privmsg() {
63 my ($server, $message, $user, $address, $target) = @_;
64
65 # only log messages if the user is away
66 unless ($server->{usermode_away}) {
67 return;
68 }
69
70 add_privmsg($server, $message, $user, $address);
71 }
72
73 sub check_messages() {
74 if (scalar(keys(%{$msgs})) > 0) {
75 send_email();
76 $msgs = {};
77 }
78
79 return 0;
80 }
81
82 sub add_privmsg() {
83 my ($server, $message, $user, $addr) = @_;
84
85 unless (defined $msgs->{$server->{chatnet}}) {
86 $msgs->{$server->{chatnet}} = {};
87 };
88
89 unless (defined $msgs->{$server->{chatnet}}{$user}) {
90 $msgs->{$server->{chatnet}}->{$user} = [];
91 };
92
93 push(@{$msgs->{$server->{chatnet}}->{$user}},
94 [time, $message]
95 );
96 }
97
98 sub generate_email() {
99 my @lines = ();
100
101 if (scalar(keys(%{$msgs})) == 0) {
102 return undef;
103 }
104
105 for my $network (keys %{$msgs}) {
106 push(@lines, $network);
107 push(@lines, '=' x length($network));
108 push(@lines, '');
109
110 for my $user (keys %{$msgs->{$network}}) {
111 for my $ele (@{$msgs->{$network}->{$user}}) {
112 push(@lines, sprintf("[%s] <%s> %s",
113 strftime("%T", localtime($ele->[0])),
114 $user, $ele->[1])
115 );
116 }
117 push(@lines, '');
118 }
119 }
120
121 return \@lines;
122 }
123
124 sub send_email() {
125 my $body = generate_email();
126
127 unless (defined($body)) {
128 return;
129 }
130
131 my $email = Email::Simple->create(
132 header => [
133 To => Irssi::settings_get_str($IRSSI{'name'} . '_to_address'),
134 From => Irssi::settings_get_str($IRSSI{'name'} . '_from_address'),
135 Subject => Irssi::settings_get_str($IRSSI{'name'} . '_subject'),
136 ],
137 body => join("\n", @{$body}),
138 );
139
140 if (! try_to_sendmail($email)) {
141 Irssi::printformat(MSGLEVEL_CLIENTCRAP, $FORMAT,
142 "an error occurred when sending an email to " .
143 Irssi::settings_get_str($IRSSI{'name'} . '_to_address')
144 );
145 }
146 }
147
148 Irssi::signal_add_last("message private", "handle_privmsg");