2 # Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
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.
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
13 # GNU General Public License for more details.
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
19 package PciIds::Notifications;
28 our @EXPORT = qw(¬ify &sendNotifs &flushNotifs);
31 my( $tables, $location, $comment, $priority, $reason ) = @_;
32 $tables->pushNotifications( $location, $comment, $priority, $reason );
35 sub sendNotif( $$$ ) {
36 my( $address, $message, $sendFun ) = @_;
37 return unless defined $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" );
45 my( $text, $count ) = @_;
46 return '' unless defined $text;
47 my $ident = ' 'x$count;
48 $text =~ s/\n/$ident/s;
53 my( $notifs, $sendFun ) = @_;
54 my( $last_address, $last_user );
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;
65 my $addr = PciIds::Address::new( $location );
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 ) {
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";
97 $note = "Item deletion approved.\n Id: ".$addr->pretty()."\n";
98 $note .= " Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
101 $message .= "\n" unless $message eq '';
104 sendNotif( $last_address, $message, $sendFun );
107 sub sendNotifs( $ ) {
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 );
115 checkConf( [ 'hostname' ] );