summaryrefslogtreecommitdiff
path: root/plugins/libcalendaring/lib/libcalendaring_itip.php
blob: f56463c3fa74cfbf593e3ac6912ddb72224b97d2 (plain)
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
<?php

/**
 * iTIP functions for the calendar-based Roudncube plugins
 *
 * Class providing functionality to manage iTIP invitations
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2011-2014, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
class libcalendaring_itip
{
    protected $rc;
    protected $lib;
    protected $plugin;
    protected $sender;
    protected $domain;
    protected $itip_send = false;
    protected $rsvp_actions = array('accepted','tentative','declined','delegated');
    protected $rsvp_status  = array('accepted','tentative','declined','delegated');

    function __construct($plugin, $domain = 'libcalendaring')
    {
        $this->plugin = $plugin;
        $this->rc = rcube::get_instance();
        $this->lib = libcalendaring::get_instance();
        $this->domain = $domain;

        $hook = $this->rc->plugins->exec_hook('calendar_load_itip',
            array('identity' => $this->rc->user->list_emails(true)));
        $this->sender = $hook['identity'];

        $this->plugin->add_hook('message_before_send', array($this, 'before_send_hook'));
        $this->plugin->add_hook('smtp_connect', array($this, 'smtp_connect_hook'));
    }

    public function set_sender_email($email)
    {
        if (!empty($email))
            $this->sender['email'] = $email;
    }

    public function set_rsvp_actions($actions)
    {
        $this->rsvp_actions = (array)$actions;
        $this->rsvp_status = array_merge($this->rsvp_actions, array('delegated'));
    }

    public function set_rsvp_status($status)
    {
        $this->rsvp_status = $status;
    }

    /**
     * Wrapper for rcube_plugin::gettext()
     * Checking for a label in different domains
     *
     * @see rcube::gettext()
     */
    public function gettext($p)
    {
        $label = is_array($p) ? $p['name'] : $p;
        $domain = $this->domain;
        if (!$this->rc->text_exists($label, $domain)) {
            $domain = 'libcalendaring';
        }
        return $this->rc->gettext($p, $domain);
    }

    /**
     * Send an iTip mail message
     *
     * @param array   Event object to send
     * @param string  iTip method (REQUEST|REPLY|CANCEL)
     * @param array   Hash array with recipient data (name, email)
     * @param string  Mail subject
     * @param string  Mail body text label
     * @param object  Mail_mime object with message data
     * @param boolean Request RSVP
     * @return boolean True on success, false on failure
     */
    public function send_itip_message($event, $method, $recipient, $subject, $bodytext, $message = null, $rsvp = true)
    {
        if (!$this->sender['name'])
            $this->sender['name'] = $this->sender['email'];

        if (!$message) {
            libcalendaring::identify_recurrence_instance($event);
            $message = $this->compose_itip_message($event, $method, $rsvp);
        }

        $mailto = rcube_idn_to_ascii($recipient['email']);

        $headers = $message->headers();
        $headers['To'] = format_email_recipient($mailto, $recipient['name']);
        $headers['Subject'] = $this->gettext(array(
            'name' => $subject,
            'vars' => array(
                'title' => $event['title'],
                'name' => $this->sender['name']
            )
        ));

        // compose a list of all event attendees
        $attendees_list = array();
        foreach ((array)$event['attendees'] as $attendee) {
            $attendees_list[] = ($attendee['name'] && $attendee['email']) ?
                $attendee['name'] . ' <' . $attendee['email'] . '>' :
                ($attendee['name'] ? $attendee['name'] : $attendee['email']);
        }

        $recurrence_info = '';
        if (!empty($event['recurrence_id'])) {
            $recurrence_info = "\n\n** " . $this->gettext($event['thisandfuture'] ? 'itipmessagefutureoccurrence' : 'itipmessagesingleoccurrence') . ' **';
        }
        else if (!empty($event['recurrence'])) {
            $recurrence_info = sprintf("\n%s: %s", $this->gettext('recurring'), $this->lib->recurrence_text($event['recurrence']));
        }

        $mailbody = $this->gettext(array(
            'name' => $bodytext,
            'vars' => array(
                'title' => $event['title'],
                'date' => $this->lib->event_date_text($event, true) . $recurrence_info,
                'attendees' => join(",\n ", $attendees_list),
                'sender' => $this->sender['name'],
                'organizer' => $this->sender['name'],
            )
        ));

        // if (!empty($event['comment'])) {
        //     $mailbody .= "\n\n" . $this->gettext('itipsendercomment') . $event['comment'];
        // }

        // append links for direct invitation replies
        if ($method == 'REQUEST' && $rsvp && ($token = $this->store_invitation($event, $recipient['email']))) {
            $mailbody .= "\n\n" . $this->gettext(array(
                'name' => 'invitationattendlinks',
                'vars' => array('url' => $this->plugin->get_url(array('action' => 'attend', 't' => $token))),
            ));
        }
        else if ($method == 'CANCEL' && $event['cancelled']) {
            $this->cancel_itip_invitation($event);
        }

        $message->headers($headers, true);
        $message->setTXTBody(rcube_mime::format_flowed($mailbody, 79));

        if ($this->rc->config->get('libcalendaring_itip_debug', false)) {
            console('iTip ' . $method, $message->txtHeaders() . "\n\r" . $message->get());
        }

        // finally send the message
        $this->itip_send = true;
        $sent = $this->rc->deliver_message($message, $headers['X-Sender'], $mailto, $smtp_error);
        $this->itip_send = false;

        return $sent;
    }

    /**
     * Plugin hook triggered by rcube::deliver_message() before delivering a message.
     * Here we can set the 'smtp_server' config option to '' in order to use
     * PHP's mail() function for unauthenticated email sending.
     */
    public function before_send_hook($p)
    {
        if ($this->itip_send && !$this->rc->user->ID && $this->rc->config->get('calendar_itip_smtp_server', null) === '') {
            $this->rc->config->set('smtp_server', '');
        }

        return $p;
    }

    /**
     * Plugin hook to alter SMTP authentication.
     * This is used if iTip messages are to be sent from an unauthenticated session
     */
    public function smtp_connect_hook($p)
    {
        // replace smtp auth settings if we're not in an authenticated session
        if ($this->itip_send && !$this->rc->user->ID) {
            foreach (array('smtp_server', 'smtp_user', 'smtp_pass') as $prop) {
                $p[$prop] = $this->rc->config->get("calendar_itip_$prop", $p[$prop]);
            }
        }

      return $p;
    }

    /**
     * Helper function to build a Mail_mime object to send an iTip message
     *
     * @param array   Event object to send
     * @param string  iTip method (REQUEST|REPLY|CANCEL)
     * @param boolean Request RSVP
     * @return object Mail_mime object with message data
     */
    public function compose_itip_message($event, $method, $rsvp = true)
    {
        $from = rcube_idn_to_ascii($this->sender['email']);
        $from_utf = rcube_utils::idn_to_utf8($from);
        $sender = format_email_recipient($from, $this->sender['name']);

        // truncate list attendees down to the recipient of the iTip Reply.
        // constraints for a METHOD:REPLY according to RFC 5546
        if ($method == 'REPLY') {
            $replying_attendee = null;
            $reply_attendees = array();
            foreach ($event['attendees'] as $attendee) {
                if ($attendee['role'] == 'ORGANIZER') {
                    $reply_attendees[] = $attendee;
                }
                else if (strcasecmp($attendee['email'], $from) == 0 || strcasecmp($attendee['email'], $from_utf) == 0) {
                    $replying_attendee = $attendee;
                    if ($attendee['status'] != 'DELEGATED') {
                        unset($replying_attendee['rsvp']);  // unset the RSVP attribute
                    }
                }
                // include attendees relevant for delegation (RFC 5546, Section 4.2.5)
                else if ((!empty($attendee['delegated-to']) &&
                            (strcasecmp($attendee['delegated-to'], $from) == 0 || strcasecmp($attendee['delegated-to'], $from_utf) == 0)) ||
                         (!empty($attendee['delegated-from']) &&
                            (strcasecmp($attendee['delegated-from'], $from) == 0 || strcasecmp($attendee['delegated-from'], $from_utf) == 0))) {
                    $reply_attendees[] = $attendee;
                }
            }
            if ($replying_attendee) {
                array_unshift($reply_attendees, $replying_attendee);
                $event['attendees'] = $reply_attendees;
            }
            if ($event['recurrence']) {
                unset($event['recurrence']['EXCEPTIONS']);
            }
        }
        // set RSVP for every attendee
        else if ($method == 'REQUEST') {
            foreach ($event['attendees'] as $i => $attendee) {
                if ($attendee['status'] != 'DELEGATED') {
                    $event['attendees'][$i]['rsvp']= $rsvp ? true : null;
                }
            }
        }
        else if ($method == 'CANCEL') {
            if ($event['recurrence']) {
                unset($event['recurrence']['EXCEPTIONS']);
            }
        }

        // compose multipart message using PEAR:Mail_Mime
        $message = new Mail_mime("\r\n");
        $message->setParam('text_encoding', 'quoted-printable');
        $message->setParam('head_encoding', 'quoted-printable');
        $message->setParam('head_charset', RCMAIL_CHARSET);
        $message->setParam('text_charset', RCMAIL_CHARSET . ";\r\n format=flowed");
        $message->setContentType('multipart/alternative');

        // compose common headers array
        $headers = array(
            'From' => $sender,
            'Date' => $this->rc->user_date(),
            'Message-ID' => $this->rc->gen_message_id(),
            'X-Sender' => $from,
        );
        if ($agent = $this->rc->config->get('useragent')) {
            $headers['User-Agent'] = $agent;
        }

        $message->headers($headers);

        // attach ics file for this event
        $ical = libcalendaring::get_ical();
        $ics = $ical->export(array($event), $method, false, $method == 'REQUEST' && $this->plugin->driver ? array($this->plugin->driver, 'get_attachment_body') : false);
        $filename = $event['_type'] == 'task' ? 'todo.ics' : 'event.ics';
        $message->addAttachment($ics, 'text/calendar', $filename, false, '8bit', '', RCMAIL_CHARSET . "; method=" . $method);

        return $message;
    }

    /**
     * Forward the given iTip event as delegation to another person
     *
     * @param array Event object to delegate
     * @param mixed Delegatee as string or hash array with keys 'name' and 'mailto'
     * @param boolean The delegator's RSVP flag
     * @return boolean True on success, False on failure
     */
    public function delegate_to(&$event, $delegate, $rsvp = false)
    {
        if (is_string($delegate)) {
            $delegates = rcube_mime::decode_address_list($delegate, 1, false);
            if (count($delegates) > 0) {
                $delegate = reset($delegates);
            }
        }

        $emails = $this->lib->get_user_emails();
        $me     = $this->rc->user->list_emails(true);

        // find/create the delegate attendee
        $delegate_attendee = array(
            'email' => $delegate['mailto'],
            'name'  => $delegate['name'],
            'role'  => 'REQ-PARTICIPANT',
        );
        $delegate_index = count($event['attendees']);

        foreach ($event['attendees'] as $i => $attendee) {
          // set myself the DELEGATED-TO parameter
          if ($attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
              $event['attendees'][$i]['delegated-to'] = $delegate['mailto'];
              $event['attendees'][$i]['status'] = 'DELEGATED';
              $event['attendees'][$i]['role'] = 'NON-PARTICIPANT';
              $event['attendees'][$i]['rsvp'] = $rsvp;

              $me['email'] = $attendee['email'];
              $delegate_attendee['role'] = $attendee['role'];
          }
          // the disired delegatee is already listed as an attendee
          else if (stripos($delegate['mailto'], $attendee['email']) !== false && $attendee['role'] != 'ORGANIZER') {
              $delegate_attendee = $attendee;
              $delegate_index = $i;
              break;
          }
          // TODO: remove previous delegatee (i.e. attendee that has DELEGATED-FROM == $me)
        }

        // set/add delegate attendee with RSVP=TRUE and DELEGATED-FROM parameter
        $delegate_attendee['rsvp'] = true;
        $delegate_attendee['status'] = 'NEEDS-ACTION';
        $delegate_attendee['delegated-from'] = $me['email'];
        $event['attendees'][$delegate_index] = $delegate_attendee;

        $this->set_sender_email($me['email']);
        return $this->send_itip_message($event, 'REQUEST', $delegate_attendee, 'itipsubjectdelegatedto', 'itipmailbodydelegatedto');
    }

    /**
     * Handler for calendar/itip-status requests
     */
    public function get_itip_status($event, $existing = null)
    {
      $action = $event['rsvp'] ? 'rsvp' : '';
      $status = $event['fallback'];
      $latest = false;
      $html = '';

      if (is_numeric($event['changed']))
        $event['changed'] = new DateTime('@'.$event['changed']);

      // check if the given itip object matches the last state
      if ($existing) {
        $latest = (isset($event['sequence']) && $existing['sequence'] == $event['sequence']) ||
                  (!isset($event['sequence']) && $existing['changed'] && $existing['changed'] >= $event['changed']);
      }

      // determine action for REQUEST
      if ($event['method'] == 'REQUEST') {
        $html = html::div('rsvp-status', $this->gettext('acceptinvitation'));

        if ($existing) {
          $rsvp = $event['rsvp'];
          $emails = $this->lib->get_user_emails();
          foreach ($existing['attendees'] as $attendee) {
            if ($attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
              $status = strtoupper($attendee['status']);
              break;
            }
          }
        }
        else {
          $rsvp = $event['rsvp'] && $this->rc->config->get('calendar_allow_itip_uninvited', true);
        }

        $status_lc = strtolower($status);

        if ($status_lc == 'unknown' && !$this->rc->config->get('calendar_allow_itip_uninvited', true)) {
          $html = html::div('rsvp-status', $this->gettext('notanattendee'));
          $action = 'import';
        }
        else if (in_array($status_lc, $this->rsvp_status)) {
          $status_text = $this->gettext(($latest ? 'youhave' : 'youhavepreviously') . $status_lc);

          if ($existing && ($existing['sequence'] > $event['sequence'] || (!isset($event['sequence']) && $existing['changed'] && $existing['changed'] > $event['changed']))) {
            $action = '';  // nothing to do here, outdated invitation
            if ($status_lc == 'needs-action')
              $status_text = $this->gettext('outdatedinvitation');
          }
          else if (!$existing && !$rsvp) {
            $action = 'import';
          }
          else if ($latest && $status_lc != 'needs-action') {
            $action = 'update';
          }

          $html = html::div('rsvp-status ' . $status_lc, $status_text);
        }
      }
      // determine action for REPLY
      else if ($event['method'] == 'REPLY') {
        // check whether the sender already is an attendee
        if ($existing) {
          $action = $this->rc->config->get('calendar_allow_itip_uninvited', true) ? 'accept' : '';
          $listed = false;
          foreach ($existing['attendees'] as $attendee) {
            if ($attendee['role'] != 'ORGANIZER' && strcasecmp($attendee['email'], $event['attendee']) == 0) {
              $status_lc = strtolower($status);
              if (in_array($status_lc, $this->rsvp_status)) {
                $html = html::div('rsvp-status ' . $status_lc, $this->gettext(array(
                    'name' => 'attendee' . $status_lc,
                    'vars' => array(
                        'delegatedto' => Q($event['delegated-to'] ?: ($attendee['delegated-to'] ?: '?')),
                    )
                )));
              }
              $action = $attendee['status'] == $status || !$latest ? '' : 'update';
              $listed = true;
              break;
            }
          }

          if (!$listed) {
            $html = html::div('rsvp-status', $this->gettext('itipnewattendee'));
          }
        }
        else {
          $html = html::div('rsvp-status hint', $this->gettext('itipobjectnotfound'));
          $action = '';
        }
      }
      else if ($event['method'] == 'CANCEL') {
        if (!$existing) {
          $html = html::div('rsvp-status hint', $this->gettext('itipobjectnotfound'));
          $action = '';
        }
      }

      return array(
          'uid' => $event['uid'],
          'id' => asciiwords($event['uid'], true),
          'existing' => $existing ? true : false,
          'saved' => $existing ? true : false,
          'latest' => $latest,
          'status' => $status,
          'action' => $action,
          'html' => $html,
      );
    }

    /**
     * Build inline UI elements for iTip messages
     */
    public function mail_itip_inline_ui($event, $method, $mime_id, $task, $message_date = null, $preview_url = null)
    {
        $buttons = array();
        $dom_id = asciiwords($event['uid'], true);
        $rsvp_status = 'unknown';

        // pass some metadata about the event and trigger the asynchronous status check
        $changed = is_object($event['changed']) ? $event['changed'] : $message_date;
        $metadata = array(
            'uid'      => $event['uid'],
            '_instance' => $event['_instance'],
            'changed'  => $changed ? $changed->format('U') : 0,
            'sequence' => intval($event['sequence']),
            'method'   => $method,
            'task'     => $task,
        );

        // create buttons to be activated from async request checking existence of this event in local calendars
        $buttons[] = html::div(array('id' => 'loading-'.$dom_id, 'class' => 'rsvp-status loading'), $this->gettext('loading'));

        // on iTip REPLY we have two options:
        if ($method == 'REPLY') {
            $title = $this->gettext('itipreply');

            foreach ($event['attendees'] as $attendee) {
                if (!empty($attendee['email']) && $attendee['role'] != 'ORGANIZER' &&
                      (empty($event['_sender']) || ($attendee['email'] == $event['_sender'] || $attendee['email'] == $event['_sender_utf']))) {
                    $metadata['attendee'] = $attendee['email'];
                    $rsvp_status = strtoupper($attendee['status']);
                    if ($attendee['delegated-to'])
                        $metadata['delegated-to'] = $attendee['delegated-to'];
                    break;
                }
            }

            // 1. update the attendee status on our copy
            $update_button = html::tag('input', array(
                'type' => 'button',
                'class' => 'button',
                'onclick' => "rcube_libcalendaring.add_from_itip_mail('" . JQ($mime_id) . "', '$task')",
                'value' => $this->gettext('updateattendeestatus'),
            ));

            // 2. accept or decline a new or delegate attendee
            $accept_buttons = html::tag('input', array(
                'type' => 'button',
                'class' => "button accept",
                'onclick' => "rcube_libcalendaring.add_from_itip_mail('" . JQ($mime_id) . "', '$task')",
                'value' => $this->gettext('acceptattendee'),
            ));
            $accept_buttons .= html::tag('input', array(
                'type' => 'button',
                'class' => "button decline",
                'onclick' => "rcube_libcalendaring.decline_attendee_reply('" . JQ($mime_id) . "', '$task')",
                'value' => $this->gettext('declineattendee'),
            ));

            $buttons[] = html::div(array('id' => 'update-'.$dom_id, 'style' => 'display:none'), $update_button);
            $buttons[] = html::div(array('id' => 'accept-'.$dom_id, 'style' => 'display:none'), $accept_buttons);
        }
        // when receiving iTip REQUEST messages:
        else if ($method == 'REQUEST') {
            $emails = $this->lib->get_user_emails();
            $title = $event['sequence'] > 0 ? $this->gettext('itipupdate') : $this->gettext('itipinvitation');
            $metadata['rsvp'] = true;
            $metadata['sensitivity'] = $event['sensitivity'];

            if (is_object($event['start'])) {
                $metadata['date'] = $event['start']->format('U');
            }

            // check for X-KOLAB-INVITATIONTYPE property and only show accept/decline buttons
            if (self::get_custom_property($event, 'X-KOLAB-INVITATIONTYPE') == 'CONFIRMATION') {
                $this->rsvp_actions = array('accepted','declined');
                $metadata['nosave'] = true;
            }

            // 1. display RSVP buttons (if the user was invited)
            foreach ($this->rsvp_actions as $method) {
                $rsvp_buttons .= html::tag('input', array(
                    'type' => 'button',
                    'class' => "button $method",
                    'onclick' => "rcube_libcalendaring.add_from_itip_mail('" . JQ($mime_id) . "', '$task', '$method', '$dom_id')",
                    'value' => $this->gettext('itip' . $method),
                ));
            }

            // add button to open calendar/preview
            if (!empty($preview_url)) {
              $msgref = $this->lib->ical_message->folder . '/' . $this->lib->ical_message->uid . '#' . $mime_id;
              $rsvp_buttons .= html::tag('input', array(
                  'type' => 'button',
                  'class' => "button preview",
                  'onclick' => "rcube_libcalendaring.open_itip_preview('" . JQ($preview_url) . "', '" . JQ($msgref) . "')",
                  'value' => $this->gettext('openpreview'),
              ));
            }

            // 2. update the local copy with minor changes
            $update_button = html::tag('input', array(
                'type' => 'button',
                'class' => 'button',
                'onclick' => "rcube_libcalendaring.add_from_itip_mail('" . JQ($mime_id) . "', '$task')",
                'value' => $this->gettext('updatemycopy'),
            ));

            // 3. Simply import the event without replying
            $import_button = html::tag('input', array(
                'type' => 'button',
                'class' => 'button',
                'onclick' => "rcube_libcalendaring.add_from_itip_mail('" . JQ($mime_id) . "', '$task')",
                'value' => $this->gettext('importtocalendar'),
            ));

            // check my status
            foreach ($event['attendees'] as $attendee) {
                if ($attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
                    $metadata['attendee'] = $attendee['email'];
                    $metadata['rsvp'] = $attendee['rsvp'] || $attendee['role'] != 'NON-PARTICIPANT';
                    $rsvp_status = !empty($attendee['status']) ? strtoupper($attendee['status']) : 'NEEDS-ACTION';
                    break;
                }
            }

            // add itip reply message controls
            $rsvp_buttons .= html::div('itip-reply-controls', $this->itip_rsvp_options_ui($dom_id, $metadata['nosave']));

            $buttons[] = html::div(array('id' => 'rsvp-'.$dom_id, 'class' => 'rsvp-buttons', 'style' => 'display:none'), $rsvp_buttons);
            $buttons[] = html::div(array('id' => 'update-'.$dom_id, 'style' => 'display:none'), $update_button);

            // prepare autocompletion for delegation dialog
            if (in_array('delegated', $this->rsvp_actions)) {
                $this->rc->autocomplete_init();
            }
        }
        // for CANCEL messages, we can:
        else if ($method == 'CANCEL') {
            $title = $this->gettext('itipcancellation');
            $event_prop = array_filter(array('uid' => $event['uid'], '_instance' => $event['_instance']));

            // 1. remove the event from our calendar
            $button_remove = html::tag('input', array(
                'type' => 'button',
                'class' => 'button',
                'onclick' => "rcube_libcalendaring.remove_from_itip(" . rcube_output::json_serialize($event_prop) . ", '$task', '" . JQ($event['title']) . "')",
                'value' => $this->gettext('removefromcalendar'),
            ));

            // 2. update our copy with status=cancelled
            $button_update = html::tag('input', array(
              'type' => 'button',
              'class' => 'button',
              'onclick' => "rcube_libcalendaring.add_from_itip_mail('" . JQ($mime_id) . "', '$task')",
              'value' => $this->gettext('updatemycopy'),
            ));

            $buttons[] = html::div(array('id' => 'rsvp-'.$dom_id, 'style' => 'display:none'), $button_remove . $button_update);

            $rsvp_status = 'CANCELLED';
            $metadata['rsvp'] = true;
        }

        // append generic import button
        if ($import_button) {
            $buttons[] = html::div(array('id' => 'import-'.$dom_id, 'style' => 'display:none'), $import_button);
        }

        // pass some metadata about the event and trigger the asynchronous status check
        $metadata['fallback'] = $rsvp_status;
        $metadata['rsvp'] = intval($metadata['rsvp']);

        $this->rc->output->add_script("rcube_libcalendaring.fetch_itip_object_status(" . json_serialize($metadata) . ")", 'docready');

        // get localized texts from the right domain
        foreach (array('savingdata','deleteobjectconfirm','declinedeleteconfirm','declineattendee',
            'cancel','itipdelegated','declineattendeeconfirm','itipcomment','delegateinvitation',
            'delegateto','delegatersvpme','delegateinvalidaddress') as $label) {
          $this->rc->output->command('add_label', "itip.$label", $this->gettext($label));
        }

        // show event details with buttons
        return $this->itip_object_details_table($event, $title) .
            html::div(array('class' => 'itip-buttons', 'id' => 'itip-buttons-' . asciiwords($metadata['uid'], true)), join('', $buttons));
    }

    /**
     * Render an RSVP UI widget with buttons to respond on iTip invitations
     */
    function itip_rsvp_buttons($attrib = array(), $actions = null)
    {
        $attrib += array('type' => 'button');

        if (!$actions)
            $actions = $this->rsvp_actions;

        foreach ($actions as $method) {
            $buttons .= html::tag('input', array(
                'type'  => $attrib['type'],
                'name'  => $attrib['iname'],
                'class' => 'button',
                'rel'   => $method,
                'value' => $this->gettext('itip' . $method),
            ));
        }

        // add localized texts for the delegation dialog
        if (in_array('delegated', $actions)) {
            foreach (array('itipdelegated','itipcomment','delegateinvitation',
                  'delegateto','delegatersvpme','delegateinvalidaddress','cancel') as $label) {
                $this->rc->output->command('add_label', "itip.$label", $this->gettext($label));
            }
        }

        $savemode_radio = new html_radiobutton(array('name' => '_rsvpmode', 'class' => 'rsvp-replymode'));

        return html::div($attrib,
            html::div('label', $this->gettext('acceptinvitation')) .
            html::div('rsvp-buttons',
                $buttons .
                html::div(array('class' => 'rsvp-replymode-message', 'style' => 'display:none'),
                    html::div('message', html::span('ui-icon ui-icon-alert', '') . $this->gettext('rsvprecurringevent')) .
                    html::div('replymode-select', 
                        html::label(null, $savemode_radio->show('all', array('value' => 'all')) . $this->gettext('allevents')) .
                        html::label(null, $savemode_radio->show(null, array('value' => 'current')) . $this->gettext('currentevent')) .
                        html::label(null, $savemode_radio->show(null, array('value' => 'future')) . $this->gettext('futurevents'))
                    )
                ) .
                html::div('itip-reply-controls', $this->itip_rsvp_options_ui($attrib['id']))
            )
        );
    }

    /**
     * Render UI elements to control iTip reply message sending
     */
    public function itip_rsvp_options_ui($dom_id, $disable = false)
    {
        $itip_sending = $this->rc->config->get('calendar_itip_send_option', 3);

        // itip sending is entirely disabled
        if ($itip_sending === 0) {
            return '';
        }
        // add checkbox to suppress itip reply message
        else if ($itip_sending >= 2) {
            $rsvp_additions = html::label(array('class' => 'noreply-toggle'),
                html::tag('input', array('type' => 'checkbox', 'id' => 'noreply-'.$dom_id, 'value' => 1, 'disabled' => $disable, 'checked' => ($itip_sending & 1) == 0))
                . ' ' . $this->gettext('itipsuppressreply')
            );
        }

        // add input field for reply comment
        $rsvp_additions .= html::a(array('href' => '#toggle', 'class' => 'reply-comment-toggle'), $this->gettext('itipeditresponse'));
        $rsvp_additions .= html::div('itip-reply-comment',
            html::tag('textarea', array('id' => 'reply-comment-'.$dom_id, 'name' => '_comment', 'cols' => 40, 'rows' => 6, 'style' => 'display:none', 'placeholder' => $this->gettext('itipcomment')), '')
        );

        return $rsvp_additions;
    }

    /**
     * Render event/task details in a table
     */
    function itip_object_details_table($event, $title)
    {
        $table = new html_table(array('cols' => 2, 'border' => 0, 'class' => 'calendar-eventdetails'));
        $table->add('ititle', $title);
        $table->add('title', Q($event['title']));
        if ($event['start'] && $event['end']) {
            $table->add('label', $this->gettext('date'));
            $table->add('date', Q($this->lib->event_date_text($event)));
        }
        else if ($event['due'] && $event['_type'] == 'task') {
            $table->add('label', $this->gettext('date'));
            $table->add('date', Q($this->lib->event_date_text($event)));
        }
        if (!empty($event['recurrence_date'])) {
            $table->add('label', '');
            $table->add('recurrence-id', $this->gettext($event['thisandfuture'] ? 'itipfutureoccurrence' : 'itipsingleoccurrence'));
        }
        else if (!empty($event['recurrence'])) {
            $table->add('label', $this->gettext('recurring'));
            $table->add('recurrence', $this->lib->recurrence_text($event['recurrence']));
        }
        if ($event['location']) {
            $table->add('label', $this->gettext('location'));
            $table->add('location', Q($event['location']));
        }
        if ($event['sensitivity'] && $event['sensitivity'] != 'public') {
            $table->add('label', $this->gettext('sensitivity'));
            $table->add('sensitivity', ucfirst($this->gettext($event['sensitivity'])) . '!');
        }
        if ($event['status'] == 'COMPLETED' || $event['status'] == 'CANCELLED') {
            $table->add('label', $this->gettext('status'));
            $table->add('status', $this->gettext('status-' . strtolower($event['status'])));
        }
        if ($event['comment']) {
            $table->add('label', $this->gettext('comment'));
            $table->add('location', Q($event['comment']));
        }

        return $table->show();
    }


    /**
     * Create iTIP invitation token for later replies via URL
     *
     * @param array Hash array with event properties
     * @param string Attendee email address
     * @return string Invitation token
     */
    public function store_invitation($event, $attendee)
    {
        // empty stub
        return false;
    }

    /**
     * Mark invitations for the given event as cancelled
     *
     * @param array Hash array with event properties
     */
    public function cancel_itip_invitation($event)
    {
        // empty stub
        return false;
    }

    /**
     * Utility function to get the value of a custom property
     */
    public static function get_custom_property($event, $name)
    {
      $ret = false;

      if (is_array($event['x-custom'])) {
          array_walk($event['x-custom'], function($prop, $i) use ($name, &$ret) {
              if (strcasecmp($prop[0], $name) === 0) {
                  $ret = $prop[1];
              }
          });
      }

      return $ret;
    }

}