Skip to content

Commit

Permalink
(fix) email alarms are sent too many times (fixes #4100)
Browse files Browse the repository at this point in the history
  • Loading branch information
extrafu committed Jun 9, 2017
1 parent 5fd8c9d commit a86f8a8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 38 deletions.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -21,6 +21,7 @@ Bug fixes
- [web] fixed responsive condition of login page (960px to 1023px)
- [core] newly subscribed calendars are excluded from freebusy (#3354)
- [core] strip cr during LDIF import process (#4172)
- [core] email alarms are sent too many times (#4100)

3.2.9 (2017-05-09)
------------------
Expand Down
5 changes: 4 additions & 1 deletion SoObjects/Appointments/iCalAlarm+SOGo.h
@@ -1,6 +1,6 @@
/* iCalAlarm+SOGo.h - this file is part of SOGo
*
* Copyright (C) 2015 Inverse inc.
* Copyright (C) 2015-2017 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +21,7 @@
#import <NGCards/iCalAlarm.h>

@class iCalEntityObject;
@class SOGoUser;

@interface iCalAlarm (SOGoExtensions)

Expand All @@ -34,4 +35,6 @@
emailAttendees: (BOOL) reminderEmailAttendees
emailOrganizer: (BOOL) reminderEmailOrganizer;

- (BOOL) userIsAttendee: (SOGoUser *) user;

@end
22 changes: 21 additions & 1 deletion SoObjects/Appointments/iCalAlarm+SOGo.m
@@ -1,6 +1,6 @@
/* iCalAlarm+SOGo.m - this file is part of SOGo
*
* Copyright (C) 2015 Inverse inc.
* Copyright (C) 2015-2017 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -125,4 +125,24 @@ + (id) alarmForEvent: (iCalEntityObject *) theEntity
return AUTORELEASE(anAlarm);
}

- (BOOL) userIsAttendee: (SOGoUser *) user
{
NSEnumerator *attendees;
iCalPerson *currentAttendee;
BOOL isAttendee;

isAttendee = NO;

attendees = [[self attendees] objectEnumerator];
currentAttendee = [attendees nextObject];
while (!isAttendee
&& currentAttendee)
if ([user hasEmail: [currentAttendee rfc822Email]])
isAttendee = YES;
else
currentAttendee = [attendees nextObject];

return isAttendee;
}

@end
24 changes: 16 additions & 8 deletions SoObjects/Appointments/iCalEntityObject+SOGo.m
Expand Up @@ -690,6 +690,7 @@ - (void) updateNextAlarmDateInRow: (NSMutableDictionary *) row
{
NSCalendarDate *nextAlarmDate;
GCSAlarmsFolder *af;
SOGoUser *alarmOwner;
NSString *path;

int email_alarm_number;
Expand All @@ -698,11 +699,13 @@ - (void) updateNextAlarmDateInRow: (NSMutableDictionary *) row
{
af = [[GCSFolderManager defaultFolderManager] alarmsFolder];
path = [theContainer ocsPath];
alarmOwner = [SOGoUser userWithLogin: [[path componentsSeparatedByString: @"/"] objectAtIndex: 2]];
}
else
{
af = nil;
path = nil;
alarmOwner = nil;
}

nextAlarmDate = nil;
Expand Down Expand Up @@ -736,7 +739,8 @@ - (void) updateNextAlarmDateInRow: (NSMutableDictionary *) row
email_alarm_number = [[self alarms] indexOfObject: anAlarm];

// The email alarm is too old, let's just remove it
if ([nextAlarmDate earlierDate: [NSDate date]] == nextAlarmDate)
if ([nextAlarmDate earlierDate: [NSDate date]] == nextAlarmDate ||
![anAlarm userIsAttendee: alarmOwner])
nextAlarmDate = nil;
else
{
Expand Down Expand Up @@ -831,17 +835,21 @@ - (void) updateNextAlarmDateInRow: (NSMutableDictionary *) row
nextAlarmDate = [NSDate dateWithTimeIntervalSince1970: [[[alarms objectAtIndex: i] objectForKey: @"c_nextalarm"] intValue]];
}
else if ((anAlarm = [self firstEmailAlarm]) && af)

{
nextAlarmDate = [NSDate dateWithTimeIntervalSince1970: [[[alarms objectAtIndex: i] objectForKey: @"c_nextalarm"] intValue]];
email_alarm_number = [[self alarms] indexOfObject: anAlarm];

[af writeRecordForEntryWithCName: nameInContainer
inCalendarAtPath: path
forUID: [self uid]
recurrenceId: [self recurrenceId]
alarmNumber: [NSNumber numberWithInt: email_alarm_number]
andAlarmDate: nextAlarmDate];
}
if ([anAlarm userIsAttendee: alarmOwner])
[af writeRecordForEntryWithCName: nameInContainer
inCalendarAtPath: path
forUID: [self uid]
recurrenceId: [self recurrenceId]
alarmNumber: [NSNumber numberWithInt: email_alarm_number]
andAlarmDate: nextAlarmDate];
else
nextAlarmDate = nil;
}
}
}
} // for ( ... )
Expand Down
42 changes: 14 additions & 28 deletions Tools/SOGoEAlarmsNotifier.m
Expand Up @@ -124,28 +124,22 @@ - (NGMutableHashMap *) _headersForAlarm: (iCalAlarm *) alarm

- (void) _sendMessageWithHeaders: (NGMutableHashMap *) headers
content: (NSData *) content
toAttendee: (iCalPerson *) attendee
from: (NSString *) from
to: (NSDictionary *) to
withMailer: (SOGoMailer *) mailer
{
NGMimeMessage *message;
NSString *to, *headerTo, *attendeeName;
NSString *headerTo;

attendeeName = [attendee cnWithoutQuotes];
if ([attendeeName length])
headerTo = [NSString stringWithFormat: @"%@ <%@>", attendeeName,
[attendee rfc822Email]];
else
headerTo = [attendee rfc822Email];
headerTo = [NSString stringWithFormat: @"%@ <%@>", [to objectForKey: @"fullName"],
[to objectForKey: @"email"]];
[headers setObject: headerTo forKey: @"To"];
[headers setObject: [self _messageID] forKey: @"Message-Id"];
message = [NGMimeMessage messageWithHeader: headers];
[message setBody: content];
to = [attendee rfc822Email];

[mailer sendMimePart: message
toRecipients: [NSArray arrayWithObject: to]
sender: from
toRecipients: [NSArray arrayWithObject: [to objectForKey: @"email"]]
sender: [to objectForKey: @"email"]
withAuthenticator: staticAuthenticator inContext: nil];
}

Expand All @@ -154,11 +148,10 @@ - (void) _processAlarm: (iCalAlarm *) alarm
andContainerPath: (NSString *) containerPath
{
NGMutableHashMap *headers;
NSArray *attendees, *parts;
NSArray *parts;
NSData *content, *qpContent;
int count, max;
SOGoMailer *mailer;
NSString *from, *subject;
NSString *subject;
SOGoUser *owner;

WOContext *localContext;
Expand All @@ -171,7 +164,6 @@ - (void) _processAlarm: (iCalAlarm *) alarm
SOGoAppointmentFolder *folder;
SOGoUserFolder *userFolder;


owner = [SOGoUser userWithLogin: ownerId];
mailer = [SOGoMailer mailerWithDomainDefaults: [owner domainDefaults]];

Expand All @@ -180,13 +172,13 @@ - (void) _processAlarm: (iCalAlarm *) alarm
app = [[WOApplication alloc] initWithName: @"SOGo"];

rm = [[WEResourceManager alloc] init];
[app setResourceManager:rm];
[app setResourceManager: rm];
[rm release];
[app _setCurrentContext:localContext];

userFolder = [[localContext activeUser] homeFolderInContext: localContext ];
folders = [userFolder privateCalendars: @"Calendar"
inContext: localContext];
inContext: localContext];

pageName = [NSString stringWithFormat: @"SOGoAptMail%@", @"Reminder"];

Expand All @@ -209,16 +201,10 @@ - (void) _processAlarm: (iCalAlarm *) alarm

headers = [self _headersForAlarm: alarm withOwner: owner withSubject: subject];
qpContent = [content dataByEncodingQuotedPrintable];
from = [[owner primaryIdentity] objectForKey: @"email"];

attendees = [alarm attendees];
max = [attendees count];
for (count = 0; count < max; count++)
[self _sendMessageWithHeaders: headers
content: qpContent
toAttendee: [attendees objectAtIndex: count]
from: from
withMailer: mailer];
[self _sendMessageWithHeaders: headers
content: qpContent
to: [owner primaryIdentity]
withMailer: mailer];
}

- (void) usage
Expand Down

0 comments on commit a86f8a8

Please sign in to comment.