]> mj.ucw.cz Git - pciids.git/blob - PciIds/Notifications.pm
Removed references to SF.net
[pciids.git] / PciIds / Notifications.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
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, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::Notifications;
20 use strict;
21 use warnings;
22 use PciIds::Address;
23 use PciIds::Config;
24 use PciIds::Email;
25 use PciIds::Xmpp;
26 use base 'Exporter';
27
28 our @EXPORT = qw(&notify &sendNotifs &flushNotifs);
29
30 sub notify( $$$$$ ) {
31         my( $tables, $location, $comment, $priority, $reason ) = @_;
32         $tables->pushNotifications( $location, $comment, $priority, $reason );
33 }
34
35 sub sendNotif( $$$ ) {
36         my( $address, $message, $sendFun ) = @_;
37         return unless defined $address;
38         &{$sendFun}(
39                 $address,
40                 "Item change notifications for $config{hostname}",
41                 "$message\nThis is automatic notification message, do not respond to it.\nYou can change your notifications at http://$config{hostname}/mods/PC/?action=notifications\n" );
42 }
43
44 sub indent( $$ ) {
45         my( $text, $count ) = @_;
46         return '' unless defined $text;
47         my $ident = ' 'x$count;
48         $text =~ s/\n/$ident/s;
49         return $text;
50 }
51
52 sub sendOut( $$ ) {
53         my( $notifs, $sendFun ) = @_;
54         my( $last_address, $last_user );
55         my $message = '';
56         foreach( @{$notifs} ) {
57                 my( $user, $address, $reason, $text, $newname, $newdesc, $time, $author, $location, $name, $desc ) = @{$_};
58                 if( ( !defined $last_user ) || ( $last_user != $user ) ) {
59                         sendNotif( $last_address, $message, $sendFun );
60                         $last_address = $address;
61                         $last_user = $user;
62                         $message = '';
63                 }
64                 my $note;
65                 my $addr = PciIds::Address::new( $location );
66                 if( $reason == 0 ) {
67                         $note = "New item was created.\n  Id: ".$addr->pretty()."\n  Name: $newname\n";
68                         $note .= "  Description: $newdesc\n" if( defined $newdesc && ( $newdesc ne '' ) );
69                         $text = indent( $text, 4 );
70                         $note .= "  Comment text: $text\n" if $text ne '';
71                         $note .= "  Author: $author\n" if( defined $author && ( $author ne '' ) );
72                         $note .= "  Time: $time\n";
73                         $note .= "  Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
74                 } elsif( $reason == 1 ) {
75                         $note = "New comment created.\n  Item:\n";
76                         $note .= "    Id: ".$addr->pretty()."\n";
77                         $note .= "    Name: $name\n" if( defined $name && ( $name ne '' ) && ( ( !defined $newname ) || ( $name ne $newname ) ) );
78                         $note .= "    Description: $desc\n" if( defined $desc && ( $desc ne '' ) && ( ( !defined $newdesc ) || ( $desc ne $newdesc ) ) );
79                         $note .= "    Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
80                         $note .= "  Comment:\n";
81                         $note .= "    Proposed name: $newname\n" if( defined $newname && ( $newname ne '' ) );
82                         $note .= "    Deletion request\n" if defined $newname && $newname eq '';
83                         $note .= "    Proposed description: $newdesc\n" if( defined $newdesc && ( $newdesc ne '' ) );
84                         $text = indent( $text, 6 );
85                         $note .= "    Text: $text\n" if $text ne '';
86                         $note .= "    Author: $author\n" if( defined $author && ( $author ne '' ) );
87                         $note .= "    Time: $time\n";
88                 } elsif( $reason == 2 ) {
89                         if( $name ne '' ) {
90                                 $note = "Item name approved.\n  Id: ".$addr->pretty()."\n";
91                                 $note .= "  Name: $newname\n";
92                                 $note .= "  Description: $newdesc\n" if( defined $newdesc && ( $newdesc ne '' ) );
93                                 $text = indent( $text, 4 );
94                                 $note .= "  Comment text: $text\n" if $text ne '';
95                                 $note .= "  Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
96                         } else {
97                                 $note = "Item deletion approved.\n  Id: ".$addr->pretty()."\n";
98                                 $note .= "  Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
99                         }
100                 }
101                 $message .= "\n" unless $message eq '';
102                 $message .= $note;
103         }
104         sendNotif( $last_address, $message, $sendFun );
105 }
106
107 sub sendNotifs( $ ) {
108         my( $tables ) = @_;
109         my $time = $tables->time();
110         sendOut( $tables->mailNotifs( $time ), \&PciIds::Email::sendMail );
111         sendOut( $tables->xmppNotifs( $time ), \&PciIds::Xmpp::sendXmpp );
112         $tables->dropNotifs( $time );
113 }
114
115 checkConf( [ 'hostname' ] );
116
117 1;