1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
/*
Copyright (C) 2004-2005 SKYRIX Software AG
Copyright (C) 2005-2013 Inverse inc.
This file is part of SOGo.
SOGo is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
SOGo 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with OGo; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/WORequest.h>
#import <NGObjWeb/WOResponse.h>
#import <NGExtensions/NSException+misc.h>
#import <NGExtensions/NGHashMap.h>
#import <NGExtensions/NSString+misc.h>
#import <NGMime/NGMimeBodyPart.h>
#import <NGMime/NGMimeMultipartBody.h>
#import <NGMail/NGMailAddress.h>
#import <NGMail/NGMailAddressParser.h>
#import <NGMail/NGMimeMessage.h>
#import <NGMail/NGMimeMessageGenerator.h>
#import <NGImap4/NGImap4Client.h>
#import <NGImap4/NGImap4Connection.h>
#import <NGImap4/NGImap4Envelope.h>
#import <NGImap4/NGImap4EnvelopeAddress.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoBuild.h>
#import <SOGo/SOGoMailer.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserDefaults.h>
#import <SOGo/SOGoUserManager.h>
#import <SOGoUI/UIxComponent.h>
#import <Mailer/SOGoMailObject.h>
#import <Mailer/SOGoMailAccount.h>
#import <Mailer/SOGoMailFolder.h>
#import <MailPartViewers/UIxMailRenderingContext.h> // cyclic
#import <MailPartViewers/UIxMailSizeFormatter.h>
#import "WOContext+UIxMailer.h"
@interface UIxMailView : UIxComponent
{
id currentAddress;
NSString *shouldAskReceipt;
NSString *matchingIdentityEMail;
NSDictionary *attachment;
NSArray *attachmentAttrs;
}
@end
@implementation UIxMailView
static NSString *mailETag = nil;
+ (void) initialize
{
mailETag = [[NSString alloc] initWithFormat:@"\"imap4url_%@_%@_%@\"",
SOGO_MAJOR_VERSION,
SOGO_MINOR_VERSION,
SOGO_SUBMINOR_VERSION];
NSLog (@"Note: using constant etag for mail viewer: '%@'", mailETag);
}
- (void) dealloc
{
[matchingIdentityEMail release];
[attachment release];
[attachmentAttrs release];
[super dealloc];
}
/* accessors */
- (void) setCurrentAddress: (id) _addr
{
currentAddress = _addr;
}
- (id) currentAddress
{
return currentAddress;
}
- (NSString *) messageSubject
{
NSString *subject;
subject = [[self clientObject] decodedSubject];
return subject;
}
- (NSString *) panelTitle
{
return [NSString stringWithFormat: @"%@: %@",
[self labelForKey: @"View Mail"],
[self messageSubject]];
}
- (void) setAttachment: (NSDictionary *) newAttachment
{
ASSIGN (attachment, newAttachment);
}
- (NSDictionary *) attachment
{
return attachment;
}
/* links (DUP to UIxMailPartViewer!) */
- (NSString *) linkToEnvelopeAddress: (NGImap4EnvelopeAddress *) _address
{
// TODO: make some web-link, eg open a new compose panel?
return [NSString stringWithFormat: @"mailto: %@", [_address baseEMail]];
}
- (NSString *) currentAddressLink
{
return [self linkToEnvelopeAddress:[self currentAddress]];
}
/* fetching */
- (id) message
{
return [[self clientObject] fetchCoreInfos];
}
- (BOOL) hasCC
{
return [[[self clientObject] ccEnvelopeAddresses] count] > 0 ? YES : NO;
}
- (BOOL) hasBCC
{
return [[[self clientObject] bccEnvelopeAddresses] count] > 0 ? YES : NO;
}
- (BOOL) hasReplyTo
{
return [[[self clientObject] replyToEnvelopeAddresses] count] > 0 ? YES : NO;
}
/* attachment helper */
- (NSArray *) attachmentAttrs
{
if (!attachmentAttrs)
{
ASSIGN (attachmentAttrs, [[self clientObject] fetchFileAttachmentKeys]);
}
return attachmentAttrs;
}
- (BOOL) hasAttachments
{
return [[self attachmentAttrs] count] > 0 ? YES : NO;
}
- (NSFormatter *) sizeFormatter
{
return [UIxMailSizeFormatter sharedMailSizeFormatter];
}
- (NSString *) attachmentsText
{
if ([[self attachmentAttrs] count] > 1)
return [self labelForKey: @"files"];
else
return [self labelForKey: @"file"];
}
/* viewers */
- (id) contentViewerComponent
{
// TODO: I would prefer to flatten the body structure prior rendering,
// using some delegate to decide which parts to select for alternative.
id info;
info = [[self clientObject] bodyStructure];
return [[context mailRenderingContext] viewerForBodyInfo:info];
}
/* actions */
- (id) defaultAction
{
WOResponse *response;
NSString *s;
/* check etag to see whether we really must rerender */
/*
Note: There is one thing which *can* change for an existing message,
those are the IMAP4 flags (and annotations, which we do not use).
Since we don't render the flags, it should be OK, if this changes
we must embed the flagging into the etag.
*/
s = [[context request] headerForKey: @"if-none-match"];
if (s)
{
if ([s rangeOfString:mailETag].length > 0) /* not perfectly correct */
{
/* client already has the proper entity */
// [self logWithFormat:@"MATCH: %@ (tag %@)", s, mailETag];
if (![[self clientObject] doesMailExist]) {
return [NSException exceptionWithHTTPStatus:404 /* Not Found */
reason:@"message got deleted"];
}
response = [context response];
[response setStatus: 304 /* Not Modified */];
return response;
}
}
if (![self message]) // TODO: redirect to proper error
return [NSException exceptionWithHTTPStatus:404 /* Not Found */
reason:@"did not find specified message!"];
return self;
}
/* MDN */
- (BOOL) _userHasEMail: (NSString *) email
{
NSArray *identities;
NSString *identityEmail;
SOGoMailAccount *account;
int count, max;
BOOL rc;
rc = NO;
account = [[self clientObject] mailAccountFolder];
identities = [account identities];
max = [identities count];
for (count = 0; !rc && count < max; count++)
{
identityEmail = [[identities objectAtIndex: count]
objectForKey: @"email"];
rc = ([identityEmail caseInsensitiveCompare: email] == NSOrderedSame);
}
return rc;
}
- (BOOL) _messageHasDraftOrMDNSentFlag
{
NSArray *flags;
NSDictionary *coreInfos;
coreInfos = [[self clientObject] fetchCoreInfos];
flags = [coreInfos objectForKey: @"flags"];
return ([flags containsObject: @"draft"]
|| [flags containsObject: @"$mdnsent"]);
}
- (NSString *) _matchingIdentityEMail
{
NSMutableArray *recipients;
NSArray *headerRecipients;
NSString *currentEMail;
NGImap4EnvelopeAddress *address;
NSInteger count, max;
SOGoMailObject *co;
if (!matchingIdentityEMail)
{
recipients = [NSMutableArray array];
co = [self clientObject];
headerRecipients = [co toEnvelopeAddresses];
if ([headerRecipients count])
[recipients addObjectsFromArray: headerRecipients];
headerRecipients = [co ccEnvelopeAddresses];
if ([headerRecipients count])
[recipients addObjectsFromArray: headerRecipients];
max = [recipients count];
for (count = 0; !matchingIdentityEMail && count < max; count++)
{
address = [recipients objectAtIndex: count];
currentEMail = [NSString stringWithFormat: @"%@@%@",
[address mailbox],
[address host]];
if ([self _userHasEMail: currentEMail])
{
matchingIdentityEMail = currentEMail;
[matchingIdentityEMail retain];
}
}
}
return matchingIdentityEMail;
}
- (NSString *) _domainFromEMail: (NSString *) email
{
NSString *domain;
NSRange separator;
separator = [email rangeOfString: @"@"];
if (separator.location != NSNotFound)
domain = [email substringFromIndex: NSMaxRange (separator)];
else
domain = nil;
return domain;
}
- (NSArray *) _userEMailDomains
{
NSMutableArray *domains;
NSArray *identities;
NSString *email, *domain;
SOGoMailAccount *account;
NSInteger count, max;
account = [[self clientObject] mailAccountFolder];
identities = [account identities];
max = [identities count];
domains = [NSMutableArray arrayWithCapacity: max];
for (count = 0; count < max; count++)
{
email = [[identities objectAtIndex: count]
objectForKey: @"email"];
domain = [self _domainFromEMail: email];
if (domain)
[domains addObject: domain];
}
return domains;
}
- (BOOL) _senderIsInUserDomain: (NSDictionary *) headers
{
NSString *sender, *senderDomain;
BOOL rc;
sender = [[headers objectForKey: @"from"] pureEMailAddress];
senderDomain = [self _domainFromEMail: sender];
if (senderDomain)
rc = [[self _userEMailDomains] containsObject: senderDomain];
else
rc = NO;
return rc;
}
- (NSString *) _receiptAction
{
SOGoUserDefaults *ud;
NSString *action;
NSDictionary *headers;
headers = [[self clientObject] mailHeaders];
ud = [[context activeUser] userDefaults];
if ([ud allowUserReceipt])
{
if ([self _matchingIdentityEMail])
{
if ([self _senderIsInUserDomain: headers])
action = [ud userReceiptAnyAction];
else
action = [ud userReceiptOutsideDomainAction];
}
else
action = [ud userReceiptNonRecipientAction];
}
else
action = @"ignore";
return action;
}
- (void) _flagMessageWithMDNSent
{
[[self clientObject] addFlags: @"$MDNSent"];
}
- (void) _appendReceiptTextToBody: (NGMimeMultipartBody *) body
{
NGMutableHashMap *map;
NGMimeBodyPart *bodyPart;
NSString *textPartFormat, *textPartMessage;
map = [[NGMutableHashMap alloc] initWithCapacity: 1];
[map setObject: @"text/plain; charset=utf-8; format=flowed"
forKey: @"content-type"];
bodyPart = [[NGMimeBodyPart alloc] initWithHeader: map];
[map release];
textPartFormat = [self labelForKey: @"This is a Return Receipt for the mail"
@" that you sent to %@.\n\nNote: This Return Receipt"
@" only acknowledges that the message was displayed"
@" on the recipient's computer. There is no"
@" guarantee that the recipient has read or"
@" understood the message contents."];
textPartMessage = [NSString stringWithFormat: textPartFormat,
[self _matchingIdentityEMail]];
[bodyPart setBody: [textPartMessage
dataUsingEncoding: NSUTF8StringEncoding]];
[body addBodyPart: bodyPart];
[bodyPart release];
}
- (void) _appendMDNToBody: (NGMimeMultipartBody *) body
{
NGMutableHashMap *map;
NGMimeBodyPart *bodyPart;
NSString *messageId;
NSMutableString *mdnPartMessage;
map = [[NGMutableHashMap alloc] initWithCapacity: 3];
[map addObject: @"message/disposition-notification; name=\"MDNPart2.txt\""
forKey: @"content-type"];
[map addObject: @"inline" forKey: @"content-disposition"];
[map addObject: @"7bit" forKey: @"content-transfer-encoding"];
bodyPart = [[NGMimeBodyPart alloc] initWithHeader: map];
[map release];
mdnPartMessage = [[NSMutableString alloc] initWithCapacity: 100];
[mdnPartMessage appendFormat: @"Reporting-UA: SOGoMail %@\n", SOGoVersion];
[mdnPartMessage appendFormat: @"Final-Recipient: rfc822;%@\n",
[self _matchingIdentityEMail]];
messageId = [[self clientObject] messageId];
[mdnPartMessage appendFormat: @"Original-Message-ID: %@\n",
messageId];
[mdnPartMessage appendString: @"Disposition:"
@" manual-action/MDN-sent-manually; displayed"];
[bodyPart setBody: [mdnPartMessage
dataUsingEncoding: NSASCIIStringEncoding]];
[mdnPartMessage release];
[body addBodyPart: bodyPart];
[bodyPart release];
}
- (void) _appendHeadersToBody: (NGMimeMultipartBody *) body
{
NGMutableHashMap *map;
NGMimeBodyPart *bodyPart;
NSDictionary *coreInfos;
map = [[NGMutableHashMap alloc] initWithCapacity: 3];
[map addObject: @"text/rfc822-headers; name=\"MDNPart3.txt\""
forKey: @"content-type"];
[map addObject: @"inline" forKey: @"content-disposition"];
[map addObject: @"7bit" forKey: @"content-transfer-encoding"];
bodyPart = [[NGMimeBodyPart alloc] initWithHeader: map];
[map release];
coreInfos = [[self clientObject] fetchCoreInfos];
[bodyPart setBody: [coreInfos objectForKey: @"header"]];
[body addBodyPart: bodyPart];
[bodyPart release];
}
- (NGHashMap *) _receiptMessageHeaderTo: (NSString *) email
{
NGMutableHashMap *map;
NSString *subject;
map = [[NGMutableHashMap alloc] initWithCapacity: 1];
[map autorelease];
[map setObject: email forKey: @"to"];
[map setObject: [self _matchingIdentityEMail] forKey: @"from"];
[map setObject: @"multipart/report; report-type=disposition-notification"
forKey: @"content-type"];
subject = [NSString stringWithFormat:
[self labelForKey: @"Return Receipt (displayed) - %@"],
[self messageSubject]];
[map setObject: [subject asQPSubjectString: @"utf-8"]
forKey: @"subject"];
return map;
}
- (void) _sendEMailReceiptTo: (NSString *) email
{
NGMimeMultipartBody *body;
NGMimeMessage *message;
NGMimeMessageGenerator *generator;
SOGoDomainDefaults *dd;
message = [NGMimeMessage
messageWithHeader: [self _receiptMessageHeaderTo: email]];
body = [[NGMimeMultipartBody alloc] initWithPart: message];
[self _appendReceiptTextToBody: body];
[self _appendMDNToBody: body];
[self _appendHeadersToBody: body];
[message setBody: body];
[body release];
dd = [[context activeUser] domainDefaults];
generator = [NGMimeMessageGenerator new];
[generator autorelease];
if (![[SOGoMailer mailerWithDomainDefaults: dd]
sendMailData: [generator generateMimeFromPart: message]
toRecipients: [NSArray arrayWithObject: email]
sender: [self _matchingIdentityEMail]
withAuthenticator: [self authenticatorInContext: context]
inContext: context])
[self _flagMessageWithMDNSent];
}
- (NSString *) shouldAskReceipt
{
NGMailAddress *mailAddress;
NSDictionary *mailHeaders;
NSString *email, *action;
if (!shouldAskReceipt)
{
shouldAskReceipt = @"false";
mailHeaders = [[self clientObject] mailHeaders];
email = [mailHeaders objectForKey: @"disposition-notification-to"];
if (!email)
{
email = [mailHeaders objectForKey: @"x-confirm-reading-to"];
if (!email)
email = [mailHeaders objectForKey: @"return-receipt-to"];
}
// email here can be "foo@bar.com" or "Foo Bar <foo@bar.com>"
// we must extract the actual email address
mailAddress = [[NGMailAddressParser mailAddressParserWithString: email] parse];
if ([mailAddress isKindOfClass: [NGMailAddress class]])
email = [mailAddress address];
else
email = nil;
if (email)
{
if (![self _userHasEMail: email]
&& ![self _messageHasDraftOrMDNSentFlag])
{
action = [self _receiptAction];
if ([action isEqualToString: @"ask"])
{
shouldAskReceipt = @"true";
[self _flagMessageWithMDNSent];
}
else if ([action isEqualToString: @"send"])
[self _sendEMailReceiptTo: email];
}
}
}
return shouldAskReceipt;
}
- (WOResponse *) sendMDNAction
{
WOResponse *response;
NSDictionary *mailHeaders;
NSString *email, *action;
mailHeaders = [[self clientObject] mailHeaders];
email = [mailHeaders objectForKey: @"disposition-notification-to"];
if (!email)
{
email = [mailHeaders objectForKey: @"x-confirm-reading-to"];
if (!email)
email = [mailHeaders objectForKey: @"return-receipt-to"];
}
/* We perform most of the validation steps that were done in
-shouldAskReceipt in order to enforce consistency. */
if (email)
{
if ([self _userHasEMail: email])
response = [self responseWithStatus: 403
andString: (@"One cannot send an MDN to"
@" oneself.")];
else
{
action = [self _receiptAction];
if ([action isEqualToString: @"ask"])
{
[self _sendEMailReceiptTo: email];
response = [self responseWithStatus: 204];
}
else
response = [self responseWithStatus: 403
andString: (@"No notification header found in"
@" original message.")];
}
}
else
response = [self responseWithStatus: 403
andString: (@"No notification header found in"
@" original message.")];
return response;
}
/* /MDN */
- (BOOL) mailIsDraft
{
return [[self clientObject] isInDraftsFolder];
}
- (id) redirectToParentFolder
{
id url;
url = [[[self clientObject] container] baseURLInContext: context];
return [self redirectToLocation: url];
}
/* generating response */
- (void) appendToResponse: (WOResponse *) _response
inContext: (WOContext *) _ctx
{
UIxMailRenderingContext *mctx;
[[_ctx response] setHeader:mailETag forKey:@"etag"];
mctx = [[UIxMailRenderingContext alloc] initWithViewer: self
context: _ctx];
[_ctx pushMailRenderingContext: mctx];
[mctx release];
[super appendToResponse: _response inContext: _ctx];
[[_ctx popMailRenderingContext] reset];
}
@end /* UIxMailView */
|