]> mj.ucw.cz Git - pciids.git/blob - PciIds/Notifications.pm
Output better path
[pciids.git] / PciIds / Notifications.pm
1 package PciIds::Notifications;
2 use strict;
3 use warnings;
4 use PciIds::Address;
5 use PciIds::Config;
6 use PciIds::Email;
7 use PciIds::Xmpp;
8 use base 'Exporter';
9
10 our @EXPORT = qw(&notify &sendNotifs &flushNotifs);
11
12 sub notify( $$$$$ ) {
13         my( $tables, $location, $comment, $priority, $reason ) = @_;
14         $tables->pushNotifications( $location, $comment, $priority, $reason );
15 }
16
17 sub sendNotif( $$$ ) {
18         my( $address, $message, $sendFun ) = @_;
19         return unless defined $address;
20         &{$sendFun}(
21                 $address,
22                 "Item change notifications for $config{hostname}",
23                 "$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" );
24 }
25
26 sub sendOut( $$ ) {
27         my( $notifs, $sendFun ) = @_;
28         my( $last_address, $last_user );
29         my $message = '';
30         foreach( @{$notifs} ) {
31                 my( $user, $address, $reason, $text, $newname, $newdesc, $time, $author, $location, $name, $desc ) = @{$_};
32                 if( ( !defined $last_user ) || ( $last_user != $user ) ) {
33                         sendNotif( $last_address, $message, $sendFun );
34                         $last_address = $address;
35                         $last_user = $user;
36                         $message = '';
37                 }
38                 my $note;
39                 my $addr = PciIds::Address::new( $location );
40                 if( $reason == 0 ) {
41                         $note = "New item was created.\n  Id: ".$addr->pretty()."\n  Name: $newname\n";
42                         $note .= "  Description: $newdesc\n" if( defined $newdesc && ( $newdesc ne '' ) );
43                         $note .= "  Comment text: $text\n" if( defined $text && ( $text ne '' ) );
44                         $note .= "  Author: $author\n" if( defined $author && ( $author ne '' ) );
45                         $note .= "  Time: $time\n";
46                         $note .= "  Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
47                 } elsif( $reason == 1 ) {
48                         $note = "New comment created.\n  Item:\n";
49                         $note .= "    Id: ".$addr->pretty()."\n";
50                         $note .= "    Name: $name\n" if( defined $name && ( $name ne '' ) && ( $name ne $newname ) );
51                         $note .= "    Description: $desc\n" if( defined $desc && ( $desc ne '' ) && ( $desc ne $newdesc ) );
52                         $note .= "    Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
53                         $note .= "  Comment:\n";
54                         $note .= "    Proposed name: $newname\n" if( defined $newname && ( $newname ne '' ) );
55                         $note .= "    Deletion request\n" if defined $newname && $newname eq '';
56                         $note .= "    Proposed description: $newdesc\n" if( defined $newdesc && ( $newdesc ne '' ) );
57                         $note .= "    Text: $text\n" if( defined $text && ( $text ne '' ) );
58                         $note .= "    Author: $author\n" if( defined $author && ( $author ne '' ) );
59                         $note .= "    Time: $time\n";
60                 } elsif( $reason == 2 ) {
61                         if( $name ne '' ) {
62                                 $note = "Item name validated.\n  Id:".$addr->pretty()."\n";
63                                 $note .= "  Name: $newname\n";
64                                 $note .= "  Description: $newdesc\n" if( defined $newdesc && ( $newdesc ne '' ) );
65                                 $note .= "  Comment text: $text\n" if( defined $text && ( $text ne '' ) );
66                                 $note .= "  Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
67                         } else {
68                                 $note = "Item deletion validated.\n  Id:".$addr->pretty()."\n";
69                                 $note .= "  Address: http://".$config{'hostname'}."/read/".$addr->get()."\n";
70                         }
71                 }
72                 $message .= "\n" unless $message eq '';
73                 $message .= $note;
74         }
75         sendNotif( $last_address, $message, $sendFun );
76 }
77
78 sub sendNotifs( $ ) {
79         my( $tables ) = @_;
80         my $time = $tables->time();
81         sendOut( $tables->mailNotifs( $time ), \&PciIds::Email::sendMail );
82         sendOut( $tables->xmppNotifs( $time ), \&PciIds::Xmpp::sendXmpp );
83         $tables->dropNotifs( $time );
84 }
85
86 checkConf( [ 'hostname' ] );
87
88 1;