« blib/lib/Lemonldap/NG/Common/Notifications.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Logger/Loki.pm »
| 1 | ## @file | ||
| 2 | # File storage methods for notifications | ||
| 3 | |||
| 4 | ## @class | ||
| 5 | # File storage methods for notifications | ||
| 6 | package Lemonldap::NG::Common::Notifications::File; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Mouse; | ||
| 10 | use Time::Local; | ||
| 11 | use MIME::Base64; | ||
| 12 | |||
| 13 | our $VERSION = '2.21.0'; | ||
| 14 | |||
| 15 | extends 'Lemonldap::NG::Common::Notifications'; | ||
| 16 | |||
| 17 | sub import { | ||
| 18 | shift; | ||
| 19 | return Lemonldap::NG::Common::Notifications->import(@_); | ||
| 20 | } | ||
| 21 | |||
| 22 | has dirName => ( is => 'ro', required => 1 ); | ||
| 23 | |||
| 24 | has table => ( | ||
| 25 | is => 'rw', | ||
| 26 | trigger => sub { | ||
| 27 | $_[0]->{dirName} =~ s/\/conf\/?$//; | ||
| 28 | $_[0]->{dirName} .= "/$_[0]->{table}"; | ||
| 29 | } | ||
| 30 | ); | ||
| 31 | |||
| 32 | has fileNameSeparator => ( is => 'rw', default => '_' ); | ||
| 33 | |||
| 34 | # Returns notifications corresponding to the user $uid. | ||
| 35 | # If $ref is set, returns only notification corresponding to this reference. | ||
| 36 | sub get { | ||
| 37 | my ( $self, $uid, $ref ) = @_; | ||
| 38 | return () unless ($uid); | ||
| 39 | my $ext = $self->extension; | ||
| 40 | my $fns = $self->{fileNameSeparator}; | ||
| 41 | my $identifier = &getIdentifier( $self, $uid, $ref ); | ||
| 42 | |||
| 43 | opendir D, $self->{dirName}; | ||
| 44 | my @notif = grep /^\d{8}${fns}${identifier}\S*\.$ext$/, readdir(D); | ||
| 45 | closedir D; | ||
| 46 | |||
| 47 | my $files; | ||
| 48 | foreach my $file (@notif) { | ||
| 49 | unless ( open F, '<', $self->{dirName} . "/$file" ) { | ||
| 50 | $self->logger->error( | ||
| 51 | "Unable to read notification $self->{dirName}/$file"); | ||
| 52 | next; | ||
| 53 | } | ||
| 54 | $files->{$file} = join( '', <F> ); | ||
| 55 | } | ||
| 56 | return $files; | ||
| 57 | } | ||
| 58 | |||
| 59 | # Returns accepted notification corresponding to the user $uid. | ||
| 60 | # If $ref is set, returns only notification corresponding to this reference. | ||
| 61 | sub getAccepted { | ||
| 62 | my ( $self, $uid, $ref ) = @_; | ||
| 63 | return () unless ($uid); | ||
| 64 | my $ext = $self->extension; | ||
| 65 | my $fns = $self->{fileNameSeparator}; | ||
| 66 | my $identifier = &getIdentifier( $self, $uid, $ref ); | ||
| 67 | |||
| 68 | opendir D, $self->{dirName}; | ||
| 69 | my @notif = grep /^\d{8}${fns}${identifier}\S*\.(?:done|$ext)$/, readdir(D); | ||
| 70 | closedir D; | ||
| 71 | |||
| 72 | my $files; | ||
| 73 | foreach my $file (@notif) { | ||
| 74 | unless ( open F, '<', $self->{dirName} . "/$file" ) { | ||
| 75 | $self->logger->error( | ||
| 76 | "Unable to read notification $self->{dirName}/$file"); | ||
| 77 | next; | ||
| 78 | } | ||
| 79 | $files->{$file} = join( '', <F> ); | ||
| 80 | } | ||
| 81 | return $files; | ||
| 82 | } | ||
| 83 | |||
| 84 | ## @method hashref getAll() | ||
| 85 | # Return all pending notifications. | ||
| 86 | # @return hashref where keys are internal reference and values are hashref with | ||
| 87 | # keys date, uid, ref and condition. | ||
| 88 | sub getAll { | ||
| 89 | my $self = shift; | ||
| 90 | my $ext = $self->extension; | ||
| 91 | opendir D, $self->{dirName}; | ||
| 92 | my @notif; | ||
| 93 | my $fns = $self->{fileNameSeparator}; | ||
| 94 | @notif = grep /^\S*\.$ext$/, readdir(D); | ||
| 95 | my %h = map { | ||
| 96 | /^(\d{8})${fns}([^\s${fns}]+)${fns}([^\s${fns}]+)(?:${fns}([^\s${fns}]+))?\.$ext$/ | ||
| 97 | ? ( | ||
| 98 | $_ => { | ||
| 99 | date => $1, | ||
| 100 | uid => $2, | ||
| 101 | ref => decode_base64($3), | ||
| 102 | condition => decode_base64( $4 // '' ) | ||
| 103 | } | ||
| 104 | ) | ||
| 105 | : () | ||
| 106 | } @notif; | ||
| 107 | return \%h; | ||
| 108 | } | ||
| 109 | |||
| 110 | ## @method hashref getExisting() | ||
| 111 | # Return all notifications. | ||
| 112 | # @return hashref where keys are internal reference and values are hashref with | ||
| 113 | # keys date, uid, ref and condition. | ||
| 114 | sub getExisting { | ||
| 115 | my $self = shift; | ||
| 116 | my $ext = $self->extension; | ||
| 117 | opendir D, $self->{dirName}; | ||
| 118 | my @notif; | ||
| 119 | my $fns = $self->{fileNameSeparator}; | ||
| 120 | @notif = grep /^\S*\.$ext$/, readdir(D); | ||
| 121 | my %h = map { | ||
| 122 | /^(\d{8})${fns}([^\s${fns}]+)${fns}([^\s${fns}]+)(?:${fns}([^\s${fns}]+))?\.(?:$ext|done)$/ | ||
| 123 | ? ( | ||
| 124 | $_ => { | ||
| 125 | date => $1, | ||
| 126 | uid => $2, | ||
| 127 | ref => decode_base64($3), | ||
| 128 | condition => decode_base64( $4 // '' ) | ||
| 129 | } | ||
| 130 | ) | ||
| 131 | : () | ||
| 132 | } @notif; | ||
| 133 | return \%h; | ||
| 134 | } | ||
| 135 | |||
| 136 | ## @method boolean delete(string myref) | ||
| 137 | # Mark a notification as done. | ||
| 138 | # @param $myref identifier returned by get() or getAll() | ||
| 139 | sub delete { | ||
| 140 | my ( $self, $myref ) = @_; | ||
| 141 | my $ext = $self->extension; | ||
| 142 | my $new = ( $myref =~ /(.*?)(?:\.$ext)$/ )[0] . '.done'; | ||
| 143 | return rename( $self->{dirName} . "/$myref", $self->{dirName} . "/$new" ); | ||
| 144 | } | ||
| 145 | |||
| 146 | ## @method boolean purge(string myref) | ||
| 147 | # Purge notification (really delete record) | ||
| 148 | # @param $myref identifier returned by get() or getAll() | ||
| 149 | # @return true if something was deleted | ||
| 150 | sub purge { | ||
| 151 | my ( $self, $myref ) = @_; | ||
| 152 | return unlink( $self->{dirName} . "/$myref" ); | ||
| 153 | } | ||
| 154 | |||
| 155 | # Insert a new notification | ||
| 156 | sub newNotif { | ||
| 157 | my ( $self, $date, $uid, $ref, $condition, $content ) = @_; | ||
| 158 | my $ext = $self->extension; | ||
| 159 | my $fns = $self->{fileNameSeparator}; | ||
| 160 | $fns ||= '_'; | ||
| 161 | my @t = split( /\D+/, $date ); | ||
| 162 | $t[1]--; | ||
| 163 | eval { | ||
| 164 | timelocal( $t[5] || 0, $t[4] || 0, $t[3] || 0, $t[2], $t[1], $t[0] ); | ||
| 165 | }; | ||
| 166 | return ( 0, "Bad date" ) if ($@); | ||
| 167 | $date =~ s/-//g; | ||
| 168 | return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ ); | ||
| 169 | my $filename = | ||
| 170 | $self->{dirName} | ||
| 171 | . "/${date}${fns}${uid}${fns}" | ||
| 172 | . encode_base64( $ref, '' ); | ||
| 173 | $filename .= "${fns}" . encode_base64( $condition, '' ) if $condition; | ||
| 174 | $filename .= ".$ext"; | ||
| 175 | |||
| 176 | return ( 0, 'This notification still exists' ) if ( -e $filename ); | ||
| 177 | my $old = ( $filename =~ /(.*?)(?:\.$ext)$/ )[0] . '.done'; | ||
| 178 | return ( 0, 'This notification has been done' ) if ( -e $old ); | ||
| 179 | open my $F, '>', $filename | ||
| 180 | or return ( 0, "Unable to create $filename ($!)" ); | ||
| 181 | binmode($F); | ||
| 182 | print $F $content; | ||
| 183 | return ( 0, "Unable to close $filename ($!)" ) unless ( close $F ); | ||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | |||
| 187 | ## @method hashref getDone() | ||
| 188 | # Returns a list of notification that have been done | ||
| 189 | # @return hashref where keys are internal reference and values are hashref with | ||
| 190 | # keys notified, uid and ref. | ||
| 191 | sub getDone { | ||
| 192 | my ($self) = @_; | ||
| 193 | opendir D, $self->{dirName}; | ||
| 194 | my @notif; | ||
| 195 | my $fns = $self->{fileNameSeparator}; | ||
| 196 | @notif = grep /^\d{8}${fns}\S*\.done$/, readdir(D); | ||
| 197 | my $res; | ||
| 198 | foreach my $file (@notif) { | ||
| 199 | my ( $u, $r, $c ) = | ||
| 200 | ( $file =~ | ||
| 201 | /^\d+${fns}([^${fns}]+)${fns}([^${fns}]+)(?:${fns}([^${fns}]+))?\.done$/ | ||
| 202 | ); | ||
| 203 | die unless ( -f "$self->{dirName}/$file" ); | ||
| 204 | my $time = ( stat("$self->{dirName}/$file") )[10]; | ||
| 205 | $res->{$file} = { | ||
| 206 | 'uid' => $u, | ||
| 207 | 'ref' => decode_base64($r), | ||
| 208 | 'notified' => $time, | ||
| 209 | 'condition' => decode_base64( $c // '' ), | ||
| 210 | }; | ||
| 211 | } | ||
| 212 | return $res; | ||
| 213 | } | ||
| 214 | |||
| 215 | ## @method string getIdentifier(string uid, string ref, string date) | ||
| 216 | # Get notification identifier | ||
| 217 | # @param $uid uid | ||
| 218 | # @param $ref ref | ||
| 219 | # @param $date date | ||
| 220 | # @return the notification identifier | ||
| 221 | sub getIdentifier { | ||
| 222 | my ( $self, $uid, $ref, $date ) = @_; | ||
| 223 | my $result; | ||
| 224 | |||
| 225 | # Special fix to manage purge from notification explorer | ||
| 226 | return $date if $date; | ||
| 227 | |||
| 228 | my $fns = $self->{fileNameSeparator}; | ||
| 229 | if ($date) { | ||
| 230 | $result .= $date . $fns; | ||
| 231 | } | ||
| 232 | $result .= $uid; | ||
| 233 | if ($ref) { | ||
| 234 | my $tmp = encode_base64( $ref, '' ); | ||
| 235 | $result .= $fns . $tmp; | ||
| 236 | } | ||
| 237 | return $result; | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 |