Monotone-Parent: ce0a852faf4ba22891a76c3c85731b7a32e2d14a

Monotone-Revision: 863c571be4782f1a0a91daa6a3747c901ac69aaa

Monotone-Author: wsourdeau@inverse.ca
Monotone-Date: 2010-08-20T01:40:07
Monotone-Branch: ca.inverse.sogo
maint-2.0.2
Wolfgang Sourdeau 2010-08-20 01:40:07 +00:00
parent 2f66264657
commit 3c426c52cf
4 changed files with 291 additions and 0 deletions

View File

@ -8,6 +8,9 @@
2010-08-19 Wolfgang Sourdeau <wsourdeau@inverse.ca>
* Tools/sogo-ealarms-notify.m: new tool meant to be executed every
minute from cron for handling email alarms notifications.
* SoObjects/Appointments/SOGoEMailAlarmsManager.[hm]: new GCS
aware singleton class for main email alarms operations on the db.

View File

@ -0,0 +1,36 @@
/* SOGoEAlarmsNotifier.h - this file is part of SOGo
*
* Copyright (C) 2010 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* 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
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef SOGOEALARMSNOTIFIER_H
#define SOGOEALARMSNOTIFIER_H
#import <Foundation/NSObject.h>
@interface SOGoEAlarmsNotifier : NSObject
{
}
- (BOOL) run;
@end
#endif /* SOGOEALARMSNOTIFIER_H */

View File

@ -0,0 +1,186 @@
/* SOGoEAlarmsNotifier.m - this file is part of SOGo
*
* Copyright (C) 2010 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* 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
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <unistd.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSString.h>
#import <NGExtensions/NGHashMap.h>
#import <NGExtensions/NGQuotedPrintableCoding.h>
#import <NGExtensions/NSObject+Logs.h>
#import <NGMail/NGMimeMessage.h>
#import <NGMime/NGMimeBodyPart.h>
#import <NGCards/iCalAlarm.h>
#import <NGCards/iCalEntityObject.h>
#import <NGCards/iCalPerson.h>
#import <SOGo/NSCalendarDate+SOGo.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoDomainDefaults.h>
#import <SOGo/SOGoSystemDefaults.h>
#import <SOGo/SOGoMailer.h>
#import <SOGo/SOGoProductLoader.h>
#import <SOGo/SOGoUser.h>
#import <Appointments/iCalPerson+SOGo.h>
#import <Appointments/SOGoEMailAlarmsManager.h>
#import "SOGoEAlarmsNotifier.h"
@implementation SOGoEAlarmsNotifier
- (NSString *) _messageID
{
static int pid = 0;
static int sequence = 0;
NSString *messageID, *pGUID;
int timestamp;
if (pid == 0)
pid = getpid();
sequence++;
timestamp = (int) [[NSDate date] timeIntervalSince1970];
pGUID = [[NSProcessInfo processInfo] globallyUniqueString];
messageID = [NSString stringWithFormat: @"<%0X-%0X-%0X-%0X@%u>",
pid, timestamp, sequence, random(), [pGUID hash]];
return [messageID lowercaseString];
}
- (NGMutableHashMap *) _headersForAlarm: (iCalAlarm *) alarm
withOwner: (SOGoUser *) owner
{
NGMutableHashMap *headers;
NSString *dateString, *subject, *fullName, *email;
NSDictionary *identity;
headers = [NGMutableHashMap hashMap];
subject = [[alarm summary] asQPSubjectString: @"utf-8"];
[headers addObject: subject forKey: @"Subject"];
dateString = [[NSCalendarDate date] rfc822DateString];
[headers addObject: dateString forKey: @"Date"];
[headers addObject: @"1.0" forKey: @"MIME-Version"];
[headers addObject: @"SOGo Alarms Notifier/1.0" forKey: @"User-Agent"];
[headers addObject: @"high" forKey: @"Importance"];
[headers addObject: @"1" forKey: @"X-Priority"];
[headers setObject: @"text/plain; charset=\"utf-8\"" forKey: @"Content-Type"];
[headers setObject: @"quoted-printable" forKey: @"Content-Transfer-Encoding"];
identity = [owner primaryIdentity];
fullName = [identity objectForKey: @"fullName"];
if ([fullName length])
email = [NSString stringWithFormat: @"%@ <%@>", fullName,
[identity objectForKey: @"email"]];
else
email = [identity objectForKey: @"email"];
[headers addObject: email forKey: @"From"];
return headers;
}
- (void) _sendMessageWithHeaders: (NGMutableHashMap *) headers
content: (NSData *) content
toAttendee: (iCalPerson *) attendee
from: (NSString *) from
withMailer: (SOGoMailer *) mailer
{
NGMimeMessage *message;
NSString *to;
[headers setObject: [attendee mailAddress] 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];
}
- (void) _processAlarm: (iCalAlarm *) alarm
withOwner: (NSString *) ownerId
{
NGMutableHashMap *headers;
NSArray *attendees;
NSData *content, *qpContent;
int count, max;
SOGoMailer *mailer;
NSString *from;
SOGoUser *owner;
owner = [SOGoUser userWithLogin: ownerId];
mailer = [SOGoMailer mailerWithDomainDefaults: [owner domainDefaults]];
headers = [self _headersForAlarm: alarm withOwner: owner];
content = [[alarm comment] dataUsingEncoding: NSUTF8StringEncoding];
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];
}
- (BOOL) run
{
SOGoEMailAlarmsManager *eaMgr;
NSCalendarDate *startDate, *toDate;
NSArray *alarms;
NSMutableArray *owners;
int count, max;
[[SOGoProductLoader productLoader]
loadProducts: [NSArray arrayWithObject: @"Appointments.SOGo"]];
eaMgr = [NSClassFromString (@"SOGoEMailAlarmsManager")
sharedEMailAlarmsManager];
startDate = [NSCalendarDate calendarDate];
toDate = [startDate addYear: 0 month: 0 day: 0
hour: 0 minute: 0
second: -[startDate secondOfMinute]];
alarms = [eaMgr scheduledAlarmsFromDate: [toDate addYear: 0 month: 0 day: 0
hour: 0 minute: -5
second: 0]
toDate: toDate
withOwners: &owners];
max = [alarms count];
for (count = 0; count < max; count++)
[self _processAlarm: [alarms objectAtIndex: count]
withOwner: [owners objectAtIndex: count]];
[eaMgr deleteAlarmsUntilDate: toDate];
return YES;
}
@end

View File

@ -0,0 +1,66 @@
/* sogo-ealarms-notify.m - this file is part of SOGo
*
* Copyright (C) 2010 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* 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
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSUserDefaults.h>
#import "SOGoEAlarmsNotifier.h"
int
main (int argc, char **argv, char **env)
{
NSAutoreleasePool *pool;
NSUserDefaults *ud;
SOGoEAlarmsNotifier *notifier;
int rc;
rc = 0;
pool = [NSAutoreleasePool new];
ud = [NSUserDefaults standardUserDefaults];
[ud addSuiteNamed: @"sogod"];
if ([ud objectForKey: @"SOGoUserSources"])
{
if ([ud objectForKey: @"SOGoEnableEMailAlarms"])
{
notifier = [SOGoEAlarmsNotifier new];
if (![notifier run])
rc = -1;
[notifier release];
}
else
NSLog (@"EMail alarms are disabled in the SOGo configuration.");
}
else
{
NSLog (@"No LDAP source is configured in the SOGo configuration of this"
@" account. Please make sure to use this tool under the same"
@" username as SOGo.");
rc = -1;
}
[pool release];
return rc;
}