File Coverage

blib/lib/Kwiki/Notify/Mail.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # $Header: /home/staff/peregrin/cvs/Kwiki-Notify-Mail/lib/Kwiki/Notify/Mail.pm,v 1.8 2005/11/11 23:04:17 peregrin Exp $
2             #
3             package Kwiki::Notify::Mail;
4 1     1   23541 use warnings;
  1         2  
  1         29  
5 1     1   6 use strict;
  1         1  
  1         37  
6 1     1   435 use Kwiki::Plugin '-Base';
  0            
  0            
7             use mixin 'Kwiki::Installer';
8             use MIME::Lite;
9              
10             our $VERSION = '0.04';
11              
12             const class_id => 'notify_mail';
13             const class_title => 'Kwiki page edit notification via email';
14             const config_file => 'notify_mail.yaml';
15              
16             sub debug {
17             my $debug = $self->hub->config->notify_mail_debug || 0;
18             return $debug;
19             }
20              
21             sub register {
22             my $registry = shift;
23             $registry->add(
24             hook => 'page:store',
25             post => 'notify',
26             );
27             }
28              
29             sub recipient_list {
30             my $notify_mail_obj = $self->hub->load_class('notify_mail');
31             my $mail_to = $notify_mail_obj->config->notify_mail_to;
32             my $topic = $notify_mail_obj->config->notify_mail_topic;
33             my $meta_data = $self->hub->edit->pages->current->metadata;
34             my $who = $meta_data->{edit_by};
35             my $page_name = $meta_data->{id};
36             my ( $cfg, $page, $email );
37              
38             return undef
39             unless defined $mail_to && defined $who && defined $page_name;
40              
41             # Support for a notify_mail_topic configuration entry giving a page from
42             # which notification info can be read.
43             $cfg = $self->hub->pages->new_page($topic);
44             if ( defined $cfg ) {
45             foreach ( split( /\n/, $cfg->content ) ) {
46             s/#.*//;
47             next if /^\s*$/;
48             unless ( ( $page, $email ) = /^([^:]+):\s*(.+)/ ) {
49             print STDERR "Kwiki::Notify::Mail: Unrecognised line in ",
50             $topic, ": ", $_, "\n";
51             next;
52             }
53             next unless $page_name =~ /^$page$/;
54             $mail_to .= " " . $email;
55             }
56             }
57              
58             return $mail_to;
59             }
60              
61             sub notify {
62             my $hook = pop;
63             my $page = shift;
64             my $notify_mail_obj = $self->hub->load_class('notify_mail');
65              
66             my $meta_data = $self->hub->edit->pages->current->metadata;
67             my $site_title = $self->hub->config->site_title;
68              
69             my $edited_by = $meta_data->{edit_by} || 'unknown name';
70             my $page_name = $meta_data->{id} || 'unknown page';
71             my $to = $notify_mail_obj->recipient_list();
72             my $from = $notify_mail_obj->config->notify_mail_from || 'unknown';
73             my $subject = sprintf( $notify_mail_obj->config->notify_mail_subject,
74             $site_title, $page_name, $edited_by )
75             || 'unknown';
76             $subject =~ s/\$1/$site_title/g;
77             $subject =~ s/\$2/$page_name/g;
78             $subject =~ s/\$3/$edited_by/g;
79              
80             my $body = "$site_title page $page_name edited by $edited_by\n";
81              
82             $notify_mail_obj->mail_it( $to, $from, $subject, $body ) if $to;
83             return $self;
84             }
85              
86             sub mail_it {
87             my ( $to, $from, $subject, $body ) = @_;
88              
89             my $msg = MIME::Lite->new(
90             To => $to,
91             From => $from,
92             Subject => $subject,
93             Data => $body,
94             );
95              
96             if ( debug($self) ) {
97             open( TEMPFILE, '>', '/tmp/kwiki_notify_mail.txt' )
98             || die "can't open tmp file $!";
99             $msg->print( \*TEMPFILE );
100             close TEMPFILE;
101             }
102             else {
103             $msg->send;
104             }
105             }
106              
107             1; # End of Kwiki::Notify::Mail
108              
109             __DATA__