summaryrefslogtreecommitdiff
path: root/lib/kolab_sync_data_email.php
blob: 9920276b826b0d6c3cc8fdba077da911292a27f1 (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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
<?php

/**
 +--------------------------------------------------------------------------+
 | Kolab Sync (ActiveSync for Kolab)                                        |
 |                                                                          |
 | Copyright (C) 2011-2012, 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/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

/**
 * Email data class for Syncroton
 */
class kolab_sync_data_email extends kolab_sync_data implements Syncroton_Data_IDataSearch
{
    const MAX_SEARCH_RESULT = 200;

    /**
     * Mapping from ActiveSync Email namespace fields
     */
    protected $mapping = array(
        'cc'                    => 'cc',
        //'contentClass'          => 'contentclass',
        'dateReceived'          => 'internaldate',
        //'displayTo'             => 'displayto', //?
        //'flag'                  => 'flag',
        'from'                  => 'from',
        //'importance'            => 'importance',
        'internetCPID'          => 'charset',
        //'messageClass'          => 'messageclass',
        'replyTo'               => 'replyto',
        //'read'                  => 'read',
        'subject'               => 'subject',
        //'threadTopic'           => 'threadtopic',
        'to'                    => 'to',
    );

    /**
     * Special folder type/name map
     *
     * @var array
     */
    protected $folder_types = array(
        2  => 'Inbox',
        3  => 'Drafts',
        4  => 'Deleted Items',
        5  => 'Sent Items',
        6  => 'Outbox',
    );

    /**
     * Kolab object type
     *
     * @var string
     */
    protected $modelName = 'mail';

    /**
     * Type of the default folder
     *
     * @var int
     */
    protected $defaultFolderType = Syncroton_Command_FolderSync::FOLDERTYPE_INBOX;

    /**
     * Default container for new entries
     *
     * @var string
     */
    protected $defaultFolder = 'INBOX';

    /**
     * Type of user created folders
     *
     * @var int
     */
    protected $folderType = Syncroton_Command_FolderSync::FOLDERTYPE_MAIL_USER_CREATED;


    /**
     * the constructor
     *
     * @param Syncroton_Model_IDevice $device
     * @param DateTime                $syncTimeStamp
     */
    public function __construct(Syncroton_Model_IDevice $device, DateTime $syncTimeStamp)
    {
        parent::__construct($device, $syncTimeStamp);

        $this->storage = rcube::get_instance()->get_storage();

        // Outlook 2013 support multi-folder
        $this->ext_devices[] = 'windowsoutlook15';

        if ($this->asversion >= 14) {
            $this->tag_categories = true;
        }
    }

    /**
     * Creates model object
     *
     * @param Syncroton_Model_SyncCollection $collection Collection data
     * @param string                         $serverId   Local entry identifier
     *
     * @return Syncroton_Model_Email Email object
     */
    public function getEntry(Syncroton_Model_SyncCollection $collection, $serverId)
    {
        $message = $this->getObject($serverId);

        // error (message doesn't exist?)
        if (empty($message) || empty($message->headers)) {
            throw new Syncroton_Exception_NotFound("Message $serverId not found");
        }

        $msg     = $this->parseMessageId($serverId);
        $headers = $message->headers; // rcube_message_header

        // Calendar namespace fields
        foreach ($this->mapping as $key => $name) {
            $value = null;

            switch ($name) {
            case 'internaldate':
                $value = self::date_from_kolab(rcube_imap_generic::strToTime($headers->internaldate));
                break;

            case 'cc':
            case 'to':
            case 'replyto':
            case 'from':
                $addresses = rcube_mime::decode_address_list($headers->$name, null, true, $headers->charset);

                foreach ($addresses as $idx => $part) {
                    // @FIXME: set name + address or address only?
                    $addresses[$idx] = format_email_recipient($part['mailto'], $part['name']);
                    $addresses[$idx] = rcube_charset::clean($addresses[$idx]);
                }

                $value = implode(',', $addresses);
                break;

            case 'subject':
                $value = $headers->get('subject');
                break;

            case 'charset':
                $value = self::charset_to_cp($headers->charset);
                break;
            }

            if (empty($value) || is_array($value)) {
                continue;
            }

            $result[$key] = $value;
        }

//        $result['ConversationId'] = 'FF68022058BD485996BE15F6F6D99320';
//        $result['ConversationIndex'] = 'CA2CFA8A23';

        // Read flag
        $result['read'] = intval(!empty($headers->flags['SEEN']));

        // Flagged message
        if (!empty($headers->flags['FLAGGED'])) {
            // Use FollowUp flag which is used in Android when message is marked with a star
            $result['flag'] = new Syncroton_Model_EmailFlag(array(
                'flagType' => 'FollowUp',
                'status' => Syncroton_Model_EmailFlag::STATUS_ACTIVE,
            ));
        }

        // Importance/Priority
        if ($headers->priority) {
            if ($headers->priority < 3) {
                $result['importance'] = 2; // High
            }
            else if ($headers->priority > 3) {
                 $result['importance'] = 0; // Low
            }
        }

        // get truncation and body type
        $airSyncBaseType = Syncroton_Command_Sync::BODY_TYPE_PLAIN_TEXT;
        $truncateAt = null;
        $opts       = $collection->options;
        $prefs      = $opts['bodyPreferences'];

        if ($opts['mimeSupport'] == Syncroton_Command_Sync::MIMESUPPORT_SEND_MIME) {
            $airSyncBaseType = Syncroton_Command_Sync::BODY_TYPE_MIME;

            if (isset($prefs[Syncroton_Command_Sync::BODY_TYPE_MIME]['truncationSize'])) {
                $truncateAt = $prefs[Syncroton_Command_Sync::BODY_TYPE_MIME]['truncationSize'];
            }
            else if (isset($opts['mimeTruncation']) && $opts['mimeTruncation'] < Syncroton_Command_Sync::TRUNCATE_NOTHING) {
                switch ($opts['mimeTruncation']) {
                case Syncroton_Command_Sync::TRUNCATE_ALL:
                    $truncateAt = 0;
                    break;
                case Syncroton_Command_Sync::TRUNCATE_4096:
                    $truncateAt = 4096;
                    break;
                case Syncroton_Command_Sync::TRUNCATE_5120:
                    $truncateAt = 5120;
                    break;
                case Syncroton_Command_Sync::TRUNCATE_7168:
                    $truncateAt = 7168;
                    break;
                case Syncroton_Command_Sync::TRUNCATE_10240:
                    $truncateAt = 10240;
                    break;
                case Syncroton_Command_Sync::TRUNCATE_20480:
                    $truncateAt = 20480;
                    break;
                case Syncroton_Command_Sync::TRUNCATE_51200:
                    $truncateAt = 51200;
                    break;
                case Syncroton_Command_Sync::TRUNCATE_102400:
                    $truncateAt = 102400;
                    break;
                }
            }
        }
        else {
            // The spec is not very clear, but it looks that if MimeSupport is not set
            // we can't add Syncroton_Command_Sync::BODY_TYPE_MIME to the supported types
            // list below (Bug #1688)
            $types = array(
                Syncroton_Command_Sync::BODY_TYPE_HTML,
                Syncroton_Command_Sync::BODY_TYPE_PLAIN_TEXT,
            );

            // @TODO: if client can support both HTML and TEXT use one of
            // them which is better according to the real message body type

            foreach ($types as $type) {
                if (!empty($prefs[$type])) {
                    if (!empty($prefs[$type]['truncationSize'])) {
                        $truncateAt = $prefs[$type]['truncationSize'];
                    }

                    $preview         = (int) $prefs[$type]['preview'];
                    $airSyncBaseType = $type;

                    break;
                }
            }
        }

        $body_params = array('type' => $airSyncBaseType);

        // Message body
        // In Sync examples there's one in which bodyPreferences is not defined
        // in such case Truncated=1 and there's no body sent to the client
        // only it's estimated size
        if (empty($prefs)) {
            $messageBody = '';
            $real_length = $message->size;
            $truncateAt  = 0;
            $body_length = 0;
            $isTruncated = 1;
        }
        else if ($airSyncBaseType == Syncroton_Command_Sync::BODY_TYPE_MIME) {
            $messageBody = $this->storage->get_raw_body($message->uid);
            // make the source safe (Bug #2715, #2757)
            $messageBody = kolab_sync_message::recode_message($messageBody);
            // strip out any non utf-8 characters
            $messageBody = rcube_charset::clean($messageBody);
            $real_length = $body_length = strlen($messageBody);
        }
        else {
            $messageBody = $this->getMessageBody($message, $airSyncBaseType == Syncroton_Command_Sync::BODY_TYPE_HTML);
            // strip out any non utf-8 characters
            $messageBody = rcube_charset::clean($messageBody);
            $real_length = $body_length = strlen($messageBody);
        }

        // add Preview element to the Body result
        if (!empty($preview) && $body_length) {
            $body_params['preview'] = $this->getPreview($messageBody, $airSyncBaseType, $preview);
        }

        // truncate the body if needed
        if ($truncateAt && $body_length > $truncateAt) {
            $messageBody = mb_strcut($messageBody, 0, $truncateAt);
            $body_length = strlen($messageBody);
            $isTruncated = 1;
        }

        if ($isTruncated) {
            $body_params['truncated']         = 1;
            $body_params['estimatedDataSize'] = $real_length;
        }

        // add Body element to the result
        $result['body'] = $this->setBody($messageBody, $body_params);

        // original body type
        // @TODO: get this value from getMessageBody()
        $result['nativeBodyType'] = $message->has_html_part() ? 2 : 1;

        // Message class
        // @TODO: add messageClass suffix for encrypted messages
        $result['messageClass'] = 'IPM.Note';
        $result['contentClass'] = 'urn:content-classes:message';

        // Categories (Tags)
        if ($this->tag_categories) {
            // convert kolab tags into categories
            $result['categories'] = $this->getKolabTags($message);
        }

        // attachments
        $attachments = array_merge($message->attachments, $message->inline_parts);
        if (!empty($attachments)) {
            $result['attachments'] = array();

            foreach ($attachments as $attachment) {
                $att = array();

                $filename = $attachment->filename;
                if (empty($filename) && $attachment->mimetype == 'text/html') {
                    $filename = 'HTML Part';
                }

                $att['displayName']   = $filename;
                $att['fileReference'] = $serverId . '::' . $attachment->mime_id;
                $att['method']        = 1;
                $att['estimatedDataSize'] = $attachment->size;

                if (!empty($attachment->content_id)) {
                    $att['contentId'] = $attachment->content_id;
                }
                if (!empty($attachment->content_location)) {
                    $att['contentLocation'] = $attachment->content_location;
                }

                if (in_array($attachment, $message->inline_parts)) {
                    $att['isInline'] = 1;
                }

                $result['attachments'][] = new Syncroton_Model_EmailAttachment($att);
            }
        }

        return new Syncroton_Model_Email($result);
    }

    /**
     * Returns properties of a message for Search response
     *
     * @param string $longId  Message identifier
     * @param array  $options Search options
     *
     * @return Syncroton_Model_Email Email object
     */
    public function getSearchEntry($longId, $options)
    {
        $collection = new Syncroton_Model_SyncCollection(array(
            'options' => $options,
        ));

        return $this->getEntry($collection, $longId);
    }

    /**
     * convert contact from xml to libkolab array
     *
     * @param Syncroton_Model_IEntry $data Contact to convert
     * @param string           $folderid   Folder identifier
     * @param array            $entry      Existing entry
     *
     * @return array
     */
    public function toKolab(Syncroton_Model_IEntry $data, $folderid, $entry = null)
    {
        // does nothing => you can't add emails via ActiveSync
    }

    /**
     * Returns filter query array according to specified ActiveSync FilterType
     *
     * @param int $filter_type  Filter type
     *
     * @param array  Filter query
     */
    protected function filter($filter_type = 0)
    {
        $filter = array();

        switch ($filter_type) {
        case Syncroton_Command_Sync::FILTER_1_DAY_BACK:
            $mod = '-1 day';
            break;
        case Syncroton_Command_Sync::FILTER_3_DAYS_BACK:
            $mod = '-3 days';
            break;
        case Syncroton_Command_Sync::FILTER_1_WEEK_BACK:
            $mod = '-1 week';
            break;
        case Syncroton_Command_Sync::FILTER_2_WEEKS_BACK:
            $mod = '-2 weeks';
            break;
        case Syncroton_Command_Sync::FILTER_1_MONTH_BACK:
            $mod = '-1 month';
            break;
        }

        if (!empty($mod)) {
            $dt = new DateTime('now', new DateTimeZone('UTC'));
            $dt->modify($mod);
            // RFC3501: IMAP SEARCH
            $filter[] = 'SINCE ' . $dt->format('d-M-Y');
        }

        return $filter;
    }

    /**
     * Return list of supported folders for this backend
     *
     * @return array
     */
    public function getAllFolders()
    {
        $list = $this->listFolders();

        if (!is_array($list)) {
            throw new Syncroton_Exception_Status_FolderSync(Syncroton_Exception_Status_FolderSync::FOLDER_SERVER_ERROR);
        }

        // device doesn't support multiple folders
        if (!in_array(strtolower($this->device->devicetype), $this->ext_devices)) {
            // We'll return max. one folder of supported type
            $result = array();
            $types  = $this->folder_types;

            foreach ($list as $idx => $folder) {
                $type = $folder['type'] == 12 ? 2 : $folder['type']; // unknown to Inbox
                if ($folder_id = $types[$type]) {
                    $result[$folder_id] = array(
                        'displayName' => $folder_id,
                        'serverId'    => $folder_id,
                        'parentId'    => 0,
                        'type'        => $type,
                    );
                }
            }

            $list = $result;
        }

        foreach ($list as $idx => $folder) {
            $list[$idx] = new Syncroton_Model_Folder($folder);
        }

        return $list;
    }

    /**
     * Return list of folders for specified folder ID
     *
     * @return array Folder identifiers list
     */
    protected function extractFolders($folder_id)
    {
        $list   = $this->listFolders();
        $result = array();

        if (!is_array($list)) {
            throw new Syncroton_Exception_NotFound('Folder not found');
        }

        // device supports multiple folders?
        if (in_array(strtolower($this->device->devicetype), $this->ext_devices)) {
            if ($list[$folder_id]) {
                $result[] = $folder_id;
            }
        }
        else if ($type = array_search($folder_id, $this->folder_types)) {
            foreach ($list as $id => $folder) {
                if ($folder['type'] == $type || ($folder_id == 'Inbox' && $folder['type'] == 12)) {
                    $result[] = $id;
                }
            }
        }

        if (empty($result)) {
            throw new Syncroton_Exception_NotFound('Folder not found');
        }

        return $result;
    }

    /**
     * Moves object into another location (folder)
     *
     * @param string $srcFolderId Source folder identifier
     * @param string $serverId    Object identifier
     * @param string $dstFolderId Destination folder identifier
     *
     * @throws Syncroton_Exception_Status
     * @return string New object identifier
     */
    public function moveItem($srcFolderId, $serverId, $dstFolderId)
    {
        $msg       = $this->parseMessageId($serverId);
        $dest      = $this->extractFolders($dstFolderId);
        $dest_id   = array_shift($dest);
        $dest_name = $this->backend->folder_id2name($dest_id, $this->device->deviceid);

        if (empty($msg)) {
            throw new Syncroton_Exception_Status_MoveItems(Syncroton_Exception_Status_MoveItems::INVALID_SOURCE);
        }

        if ($dest_name === null) {
            throw new Syncroton_Exception_Status_MoveItems(Syncroton_Exception_Status_MoveItems::INVALID_DESTINATION);
        }

        if (!$this->storage->move_message($msg['uid'], $dest_name, $msg['foldername'])) {
            throw new Syncroton_Exception_Status_MoveItems(Syncroton_Exception_Status_MoveItems::INVALID_SOURCE);
        }

        // Use COPYUID feature (RFC2359) to get the new UID of the copied message
        $copyuid = $this->storage->conn->data['COPYUID'];

        if (is_array($copyuid) && ($uid = $copyuid[1])) {
            return $this->createMessageId($dest_id, $uid);
        }
    }

    /**
     * add entry from xml data
     *
     * @param string                 $folderId Folder identifier
     * @param Syncroton_Model_IEntry $entry    Entry
     *
     * @return array
     */
    public function createEntry($folderId, Syncroton_Model_IEntry $entry)
    {
        // Throw exception here for better handling of unsupported
        // entry creation, it can be object of class Email or SMS here
        throw new Syncroton_Exception_Status_Sync(Syncroton_Exception_Status_Sync::SYNC_SERVER_ERROR);
    }

    /**
     * Update existing message
     *
     * @param string                 $folderId Folder identifier
     * @param string                 $serverId Entry identifier
     * @param Syncroton_Model_IEntry $entry    Entry
     */
    public function updateEntry($folderId, $serverId, Syncroton_Model_IEntry $entry)
    {
        $msg     = $this->parseMessageId($serverId);
        $message = $this->getObject($serverId);

        if (empty($message)) {
            throw new Syncroton_Exception_Status_Sync(Syncroton_Exception_Status_Sync::SYNC_SERVER_ERROR);
        }

        $is_flagged = !empty($message->headers->flags['FLAGGED']);

        // Read status change
        if (isset($entry->read)) {
            // here we update only Read flag
            $flag = (((int)$entry->read != 1) ? 'UN' : '') . 'SEEN';
            $this->storage->set_flag($msg['uid'], $flag, $msg['foldername']);
        }

        // Flag change
        if (isset($entry->flag) && (empty($entry->flag) || empty($entry->flag->flagType))) {
            if ($is_flagged) {
                $this->storage->set_flag($msg['uid'], 'UNFLAGGED', $msg['foldername']);
            }
        }
        else if (!$is_flagged && !empty($entry->flag)) {
            if ($entry->flag->flagType && preg_match('/follow\s*up/i', $entry->flag->flagType)) {
                $this->storage->set_flag($msg['uid'], 'FLAGGED', $msg['foldername']);
            }
        }

        // Categories (Tags) change
        if (isset($entry->categories)) {
            $this->setKolabTags($message, $entry->categories);
        }
    }

    /**
     * delete entry
     *
     * @param string                         $folderId
     * @param string                         $serverId
     * @param Syncroton_Model_SyncCollection $collection
     */
    public function deleteEntry($folderId, $serverId, $collection)
    {
        $trash = kolab_sync::get_instance()->config->get('trash_mbox');
        $msg   = $this->parseMessageId($serverId);

        if (empty($msg)) {
            throw new Syncroton_Exception_Status_Sync(Syncroton_Exception_Status_Sync::SYNC_SERVER_ERROR);
        }

        // move message to trash folder
        if ($collection->deletesAsMoves
            && strlen($trash)
            && $trash != $msg['foldername']
            && $this->storage->folder_exists($trash)
        ) {
            $this->storage->move_message($msg['uid'], $trash, $msg['foldername']);
        }
        // set delete flag
        else {
            $this->storage->set_flag($msg['uid'], 'DELETED', $msg['foldername']);
        }
    }

    /**
     * Send an email
     *
     * @param mixed   $message    MIME message
     * @param boolean $saveInSent Enables saving the sent message in Sent folder
     *
     * @param throws Syncroton_Exception_Status
     */
    public function sendEmail($message, $saveInSent)
    {
        if (!($message instanceof kolab_sync_message)) {
            $message = new kolab_sync_message($message);
        }

        $sent = $message->send($smtp_error);

        if (!$sent) {
            throw new Syncroton_Exception_Status(Syncroton_Exception_Status::MAIL_SUBMISSION_FAILED);
        }

        // Save sent message in Sent folder
        if ($saveInSent) {
            $sent_folder = kolab_sync::get_instance()->config->get('sent_mbox');

            if (strlen($sent_folder) && $this->storage->folder_exists($sent_folder)) {
                return $this->storage->save_message($sent_folder, $message->source(), '', false, array('SEEN'));
            }
        }
    }

    /**
     * Forward an email
     *
     * @param array|string    $itemId      A string LongId or an array with following properties:
     *                                     collectionId, itemId and instanceId
     * @param resource|string $body        MIME message
     * @param boolean         $saveInSent  Enables saving the sent message in Sent folder
     * @param boolean         $replaceMime If enabled, original message would be appended
     *
     * @param throws Syncroton_Exception_Status
     */
    public function forwardEmail($itemId, $body, $saveInSent, $replaceMime)
    {
        /*
        @TODO:
        The SmartForward command can be applied to a meeting. When SmartForward is applied to a recurring meeting,
        the InstanceId element (section 2.2.3.83.2) specifies the ID of a particular occurrence in the recurring meeting.
        If SmartForward is applied to a recurring meeting and the InstanceId element is absent, the server SHOULD
        forward the entire recurring meeting. If the value of the InstanceId element is invalid, the server responds
        with Status element (section 2.2.3.162.15) value 104, as specified in section 2.2.4.

        When the SmartForward command is used for an appointment, the original message is included by the server
        as an attachment to the outgoing message. When the SmartForward command is used for a normal message
        or a meeting, the behavior of the SmartForward command is the same as that of the SmartReply command (section 2.2.2.18).
        */

        $msg = $this->parseMessageId($itemId);

        if (empty($msg)) {
            throw new Syncroton_Exception_Status(Syncroton_Exception_Status::ITEM_NOT_FOUND);
        }

        // Parse message
        $sync_msg = new kolab_sync_message($body);

        // forward original message as attachment
        if (!$replaceMime) {
            $this->storage->set_folder($msg['foldername']);
            $attachment = $this->storage->get_raw_body($msg['uid']);

            if (empty($attachment)) {
                throw new Syncroton_Exception_Status(Syncroton_Exception_Status::ITEM_NOT_FOUND);
            }

            $sync_msg->add_attachment($attachment, array(
                'encoding'     => '8bit',
                'content_type' => 'message/rfc822',
                'disposition'  => 'inline',
                //'name'         => 'message.eml',
            ));
        }

        // Send message
        $sent = $this->sendEmail($sync_msg, $saveInSent);

        // Set FORWARDED flag on the replied message
        if (empty($message->headers->flags['FORWARDED'])) {
            $this->storage->set_flag($msg['uid'], 'FORWARDED', $msg['foldername']);
        }
    }

    /**
     * Reply to an email
     *
     * @param array|string    $itemId      A string LongId or an array with following properties:
     *                                     collectionId, itemId and instanceId
     * @param resource|string $body        MIME message
     * @param boolean         $saveInSent  Enables saving the sent message in Sent folder
     * @param boolean         $replaceMime If enabled, original message would be appended
     *
     * @param throws Syncroton_Exception_Status
     */
    public function replyEmail($itemId, $body, $saveInSent, $replaceMime)
    {
        $msg     = $this->parseMessageId($itemId);
        $message = $this->getObject($itemId);

        if (!$message) {
            throw new Syncroton_Exception_Status(Syncroton_Exception_Status::ITEM_NOT_FOUND);
        }

        $sync_msg = new kolab_sync_message($body);
        $headers = $sync_msg->headers();

        // Add References header
        if (empty($headers['References'])) {
            $sync_msg->set_header('References', trim($message->headers->references . ' ' . $message->headers->messageID));
        }

        // Get original message body
        if (!$replaceMime) {
            // @TODO: here we're assuming that reply message is in text/plain format
            // So, original message will be converted to plain text if needed
            $message_body = $this->getMessageBody($message, false);

            // Quote original message body
            $message_body = self::wrap_and_quote(trim($message_body), 72);

            // Join bodies
            $sync_msg->append("\n" . ltrim($message_body));
        }

        // Send message
        $sent = $this->sendEmail($sync_msg, $saveInSent);

        // Set ANSWERED flag on the replied message
        if (empty($message->headers->flags['ANSWERED'])) {
            $this->storage->set_flag($msg['uid'], 'ANSWERED', $msg['foldername']);
        }
    }

    /**
     * Search for existing entries
     *
     * @param string $folderid
     * @param array  $filter
     * @param int    $result_type  Type of the result (see RESULT_* constants)
     *
     * @return array|int  Search result as count or array of uids/objects
     */
    protected function searchEntries($folderid, $filter = array(), $result_type = self::RESULT_UID)
    {
        $folders    = $this->extractFolders($folderid);
        $filter_str = 'ALL UNDELETED';

        // convert filter into one IMAP search string
        foreach ($filter as $idx => $filter_item) {
            if (is_array($filter_item)) {
                // This is a request for changes since last time
                // we'll use HIGHESTMODSEQ value from the last Sync
                if ($filter_item[0] == 'changed' && $filter_item[1] == '>') {
                    $modseq_lasttime = $filter_item[2];
                    $modseq_data     = array();
                    $modseq = (array) $this->backend->modseq_get($this->device->id, $folderid, $modseq_lasttime);
                }
            }
            else {
                $filter_str .= ' ' . $filter_item;
            }
        }

        // get members of modified relations
        $changed_msgs = $this->getChangesByRelations($folderid, $filter);

        $result = $result_type == self::RESULT_COUNT ? 0 : array();
        // no sorting for best performance
        $sort_by = null;
        $found   = 0;
        $ts      = time();

        foreach ($folders as $folder_id) {
            $foldername = $this->backend->folder_id2name($folder_id, $this->device->deviceid);

            if ($foldername === null) {
                continue;
            }

            $found++;

            $this->storage->set_folder($foldername);

            // Synchronize folder (if it wasn't synced in this request already)
            if ($this->lastsync_folder != $folderid
                || $this->lastsync_time <= $ts - Syncroton_Registry::getPingTimeout()
            ) {
                $this->storage->folder_sync($foldername);
            }

            // We're in "get changes" mode
            $modified = false;
            if (isset($modseq_data)) {
                $folder_data = $this->storage->folder_data($foldername);
                $got_changes = true;

                if ($folder_data['HIGHESTMODSEQ']) {
                    $modseq_data[$foldername] = $folder_data['HIGHESTMODSEQ'];
                    if ($modseq_data[$foldername] != $modseq[$foldername]) {
                        $modseq_update = true;
                    }
                    else {
                        $got_changes = false;
                    }
                }

                // If previous HIGHESTMODSEQ doesn't exist we can't get changes
                // We can only get folder's HIGHESTMODSEQ value and store it for the next try
                // Skip search if HIGHESTMODSEQ didn't change
                if (!$got_changes || empty($modseq) || empty($modseq[$foldername])) {
                    $modified = true;
                }

                $filter_str .= " MODSEQ " . ($modseq[$foldername] + 1);
            }

            // We could use messages cache by replacing search() with index()
            // in some cases. This however is possible only if user has skip_deleted=true,
            // in his Roundcube preferences, otherwise we'd make often cache re-initialization,
            // because Roundcube message cache can work only with one skip_deleted
            // setting at a time. We'd also need to make sure folder_sync() was called
            // before (see above).
            //
            // if ($filter_str == 'ALL UNDELETED')
            //     $search = $this->storage->index($foldername, null, null, true, true);
            // else

            if ($modified) {
                $search = $this->storage->search_once($foldername, $filter_str);

                if (!($search instanceof rcube_result_index) || $search->is_error()) {
                    throw new Syncroton_Exception_Status(Syncroton_Exception_Status::SERVER_ERROR);
                }

                switch ($result_type) {
                case self::RESULT_COUNT:
                    $result += (int) $search->count();
                    break;

                case self::RESULT_UID:
                    if ($uids = $search->get()) {
                        foreach ($uids as $idx => $uid) {
                            $uids[$idx] = $this->createMessageId($folder_id, $uid);
                        }
                        $result = array_merge($result, $uids);
                    }
                    break;
                }
            }

            // handle relation changes
            if (!empty($changed_msgs)) {
                $uids = $this->findRelationMembersInFolder($foldername, $changed_msgs, $filter);

                switch ($result_type) {
                case self::RESULT_COUNT:
                    $result += (int) count($uids);
                    break;

                case self::RESULT_UID:
                    foreach ($uids as $idx => $uid) {
                        $uids[$idx] = $this->createMessageId($folder_id, $uid);
                    }
                    $result = array_unique(array_merge($result, $uids));
                    break;
                }
            }
        }

        if (!$found) {
            throw new Syncroton_Exception_Status(Syncroton_Exception_Status::SERVER_ERROR);
        }

        $this->lastsync_folder = $folderid;
        $this->lastsync_time   = $ts;

        if (!empty($modseq_update)) {
            $this->backend->modseq_set($this->device->id, $folderid,
                $this->syncTimeStamp, $modseq_data);

            // if previous modseq information does not exist save current set as it,
            // we would at least be able to detect changes since now
            if (empty($result) && empty($modseq)) {
                $this->backend->modseq_set($this->device->id, $folderid,
                    $modseq_lasttime, $modseq_data);
            }
        }

        return $result;
    }

    /**
     * Find members (messages) in specified folder
     */
    protected function findRelationMembersInFolder($foldername, $members, $filter)
    {
        foreach ($members as $member) {
            // IMAP URI members
            if ($url = kolab_storage_config::parse_member_url($member)) {
                $result[$url['folder']][$url['uid']] = $url['params'];
            }
        }

        // convert filter into one IMAP search string
        $filter_str = 'ALL UNDELETED';
        foreach ($filter as $idx => $filter_item) {
            if (is_string($filter_item)) {
                $filter_str .= ' ' . $filter_item;
            }
        }

        $rcube   = rcube::get_instance();
        $storage = $rcube->get_storage();
        $found   = array();

        // first find messages by UID
        if (!empty($result[$foldername])) {
            $index = $storage->search_once($foldername, 'UID '
                . rcube_imap_generic::compressMessageSet(array_keys($result[$foldername])));
            $found = $index->get();

            // remove found messages from the $result
            if (!empty($found)) {
                $result[$foldername] = array_diff_key($result[$foldername], array_flip($found));

                if (empty($result[$foldername])) {
                    unset($result[$foldername]);
                }

                // now apply the current filter to the found messages
                $index = $storage->search_once($foldername, $filter_str . ' UID '
                    . rcube_imap_generic::compressMessageSet($found));
                $found = $index->get();
            }
        }

        // search by message parameters
        if (!empty($result)) {
            // @TODO: do this search in chunks (for e.g. 25 messages)?
            $search       = '';
            $search_count = 0;

            foreach ($result as $folder => $data) {
                foreach ($data as $p) {
                    $search_params = array();
                    $search_count++;

                    foreach ($p as $key => $val) {
                        $key = strtoupper($key);
                        // don't search by subject, we don't want false-positives
                        if ($key != 'SUBJECT') {
                            $search_params[] = 'HEADER ' . $key . ' ' . rcube_imap_generic::escape($val);
                        }
                    }

                    $search .= ' (' . implode(' ', $search_params) . ')';
                }
            }

            $search_str .= ' ' . str_repeat(' OR', $search_count-1) . $search;

            // search messages in current folder
            $search = $storage->search_once($foldername, $search_str);
            $uids   = $search->get();

            if (!empty($uids)) {
                // add UIDs into the result
                $found = array_unique(array_merge($found, $uids));
            }
        }

        return $found;
    }

    /**
     * ActiveSync Search handler
     *
     * @param Syncroton_Model_StoreRequest $store Search query
     *
     * @return Syncroton_Model_StoreResponse Complete Search response
     */
    public function search(Syncroton_Model_StoreRequest $store)
    {
        list($folders, $search_str) = $this->parse_search_query($store);

        if (empty($search_str)) {
            throw new Exception('Empty/invalid search request');
        }

        if (!is_array($folders)) {
            throw new Syncroton_Exception_Status(Syncroton_Exception_Status::SERVER_ERROR);
        }

        $result = array();
        // no sorting for best performance
        $sort_by = null;

        // @TODO: caching with Options->RebuildResults support

        foreach ($folders as $folderid) {
            $foldername = $this->backend->folder_id2name($folderid, $this->device->deviceid);

            if ($foldername === null) {
                continue;
            }

//            $this->storage->set_folder($foldername);
//            $this->storage->folder_sync($foldername);

            $search = $this->storage->search_once($foldername, $search_str);

            if (!($search instanceof rcube_result_index)) {
                continue;
            }

            $uids = $search->get();
            foreach ($uids as $idx => $uid) {
                $uids[$idx] = new Syncroton_Model_StoreResponseResult(array(
                    'longId'       => $this->createMessageId($folderid, $uid),
                    'collectionId' => $folderid,
                    'class'        => 'Email',
                ));
            }
            $result = array_merge($result, $uids);

            // We don't want to search all folders if we've got already a lot messages
            if (count($result) >= self::MAX_SEARCH_RESULT) {
                break;
            }
        }

        $result   = array_values($result);
        $response = new Syncroton_Model_StoreResponse();

        // Calculate requested range
        $start = (int) $store->options['range'][0];
        $limit = (int) $store->options['range'][1] + 1;
        $total = count($result);
        $response->total = $total;

        // Get requested chunk of data set
        if ($total) {
            if ($start > $total) {
                $start = $total;
            }
            if ($limit > $total) {
                $limit = max($start+1, $total);
            }
            if ($start > 0 || $limit < $total) {
                $result = array_slice($result, $start, $limit-$start);
            }

            $response->range = array($start, $start + count($result) - 1);
        }

        // Build result array, convert to ActiveSync format
        foreach ($result as $idx => $rec) {
            $rec->properties    = $this->getSearchEntry($rec->longId, $store->options);
            $response->result[] = $rec;
            unset($result[$idx]);
        }

        return $response;
    }

    /**
     * Converts ActiveSync search parameters into IMAP search string
     */
    protected function parse_search_query($store)
    {
        $options    = $store->options;
        $query      = $store->query;
        $search_str = '';
        $folders    = array();

        if (empty($query) || !is_array($query)) {
            return array();
        }

        if (isset($query['and']['freeText']) && strlen($query['and']['freeText'])) {
            $search = $query['and']['freeText'];
        }

        if (!empty($query['and']['collections'])) {
            foreach ($query['and']['collections'] as $collection) {
                $folders = array_merge($folders, $this->extractFolders($collection));
            }
        }

        if (!empty($query['and']['greaterThan'])
            && !empty($query['and']['greaterThan']['dateReceived'])
            && !empty($query['and']['greaterThan']['value'])
        ) {
            $search_str .= ' SINCE ' . $query['and']['greaterThan']['value']->format('d-M-Y');
        }

        if (!empty($query['and']['lessThan'])
            && !empty($query['and']['lessThan']['dateReceived'])
            && !empty($query['and']['lessThan']['value'])
        ) {
            $search_str .= ' BEFORE ' . $query['and']['lessThan']['value']->format('d-M-Y');
        }

        if ($search !== null) {
            // @FIXME: should we use TEXT/BODY search?
            // ActiveSync protocol specification says "indexed fields"
            $search_keys = array('SUBJECT', 'TO', 'FROM', 'CC');
            $search_str .= str_repeat(' OR', count($search_keys)-1);
            foreach ($search_keys as $key) {
                $search_str .= sprintf(" %s {%d}\r\n%s", $key, strlen($search), $search);
            }
        }

        if (empty($search_str)) {
            return array();
        }

        $search_str = 'ALL UNDELETED ' . trim($search_str);

        // @TODO: DeepTraversal
        if (empty($folders)) {
            $folders = $this->listFolders();

            if (is_array($folders)) {
                $folders = array_keys($folders);
            }
        }

        return array($folders, $search_str);
    }

    /**
     * Fetches the entry from the backend
     */
    protected function getObject($entryid, &$folder = null)
    {
        $message = $this->parseMessageId($entryid);

        if (empty($message)) {
            // @TODO: exception?
            return null;
        }

        // set current folder
        $this->storage->set_folder($message['foldername']);

        // get message
        $message = new rcube_message($message['uid']);

        return $message;
    }

    /**
     * @return Syncroton_Model_FileReference
     */
    public function getFileReference($fileReference)
    {
        list($folderid, $uid, $part_id) = explode('::', $fileReference);

        $message = $this->getObject($fileReference);

        if (!$message) {
            throw new Syncroton_Exception_NotFound('Message not found');
        }

        $part         = $message->mime_parts[$part_id];
        $body         = $message->get_part_content($part_id);
        $content_type = $part->mimetype;

        return new Syncroton_Model_FileReference(array(
            'contentType' => $content_type,
            'data'        => $body,
        ));
    }

    /**
     * Parses entry ID to get folder name and UID of the message
     */
    protected function parseMessageId($entryid)
    {
        // replyEmail/forwardEmail
        if (is_array($entryid)) {
            $entryid = $entryid['itemId'];
        }

        list($folderid, $uid) = explode('::', $entryid);
        $foldername = $this->backend->folder_id2name($folderid, $this->device->deviceid);

        if ($foldername === null || $foldername === false) {
            // @TODO exception?
            return null;
        }

        return array(
            'uid'        => $uid,
            'folderid'   => $folderid,
            'foldername' => $foldername,
        );
    }

    /**
     * Creates entry ID of the message
     */
    public function createMessageId($folderid, $uid)
    {
        return $folderid . '::' . $uid;
    }

    /**
     * Returns body of the message in specified format
     */
    protected function getMessageBody($message, $html = false)
    {
        if (!is_array($message->parts) && empty($message->body)) {
            return '';
        }

        if (!empty($message->parts)) {
            foreach ($message->parts as $part) {
                // skip no-content and attachment parts (#1488557)
                if ($part->type != 'content' || !$part->size || $message->is_attachment($part)) {
                    continue;
                }

                return $this->getMessagePartBody($message, $part, $html);
            }
        }

        return $this->getMessagePartBody($message, $message, $html);
    }

    /**
     * Returns body of the message part in specified format
     */
    protected function getMessagePartBody($message, $part, $html = false)
    {
        // Check if we have enough memory to handle the message in it
        // @FIXME: we need up to 5x more memory than the body
        if (!rcube_utils::mem_check($part->size * 5)) {
            return '';
        }

        $body = $message->get_part_body($part->mime_id, true);

        // message is cached but not exists, or other error
        if ($body === false) {
            return '';
        }

        if ($html) {
            if ($part->ctype_secondary == 'html') {
                // charset was converted to UTF-8 in rcube_storage::get_message_part(),
                // change/add charset specification in HTML accordingly
                $meta = '<meta http-equiv="Content-Type" content="text/html; charset='.RCUBE_CHARSET.'" />';

                // remove old meta tag and add the new one, making sure
                // that it is placed in the head
                $body = preg_replace('/<meta[^>]+charset=[a-z0-9-_]+[^>]*>/Ui', '', $body);
                $body = preg_replace('/(<head[^>]*>)/Ui', '\\1'.$meta, $body, -1, $rcount);
                if (!$rcount) {
                    $body = '<head>' . $meta . '</head>' . $body;
                }
            }
            else if ($part->ctype_secondary == 'enriched') {
                $body = rcube_enriched::to_html($body);
            }
            else {
                $body = '<pre>' . $body . '</pre>';
            }
        }
        else {
            if ($part->ctype_secondary == 'enriched') {
                $body = rcube_enriched::to_html($body);
                $part->ctype_secondary = 'html';
            }

            if ($part->ctype_secondary == 'html') {
                $txt = new rcube_html2text($body, false, true);
                $body = $txt->get_text();
            }
            else {
                if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed') {
                    $body = rcube_mime::unfold_flowed($body);
                }
            }
        }

        return $body;
    }

    /**
     * Converts and truncates message body for use in <Preview>
     *
     * @return string Truncated plain text message
     */
    protected function getPreview($body, $type, $size)
    {
        if ($type == Syncroton_Command_Sync::BODY_TYPE_HTML) {
            $txt  = new rcube_html2text($body, false, true);
            $body = $txt->get_text();
        }

        // size limit defined in ActiveSync protocol
        if ($size > 255) {
            $size = 255;
        }

        return mb_strcut(trim($body), 0, $size);
    }

    /**
     * Returns list of tag names assigned to an email message
     */
    protected function getKolabTags($message)
    {
        // support only messages with message-id
        if (!($msg_id = $message->headers->get('message-id', false))) {
            return null;
        }

        $config = kolab_storage_config::get_instance();
        $delta  = Syncroton_Registry::getPingTimeout();
        $folder = $message->folder;
        $uid    = $message->uid;

        // get tag objects raleted to specified message-id
        $tags = $config->get_tags($msg_id);

        foreach ($tags as $idx => $tag) {
            // resolve members if it wasn't done recently
            $force   = empty($this->tag_rts[$tag['uid']]) || $this->tag_rts[$tag['uid']] <= time() - $delta;
            $members = $config->resolve_members($tag, $force);

            if (empty($members[$folder]) || !in_array($uid, $members[$folder])) {
                unset($tags[$idx]);
            }

            if ($force) {
                $this->tag_rts[$tag['uid']] = time();
            }
        }

        $tags = array_filter(array_map(function($v) { return $v['name']; }, $tags));

        // make sure current folder is set correctly again
        $this->storage->set_folder($folder);

        return !empty($tags) ? $tags : null;
    }

    /**
     * Set tags to an email message
     */
    protected function setKolabTags($message, $tags)
    {
        $config = kolab_storage_config::get_instance();
        $delta  = Syncroton_Registry::getPingTimeout();
        $folder = $message->folder;
        $uri    = kolab_storage_config::get_message_uri($message->headers, $folder);

        // for all tag objects...
        foreach ($config->get_tags() as $idx => $relation) {
            // resolve members if it wasn't done recently
            $uid     = $relation['uid'];
            $force   = empty($this->tag_rts[$uid]) || $this->tag_rts[$uid] <= time() - $delta;

            if ($force) {
                $config->resolve_members($relation, $force);
                $this->tag_rts[$tag['uid']] = time();
            }

            $selected = !empty($tags) && in_array($relation['name'], $tags);
            $found    = !empty($relation['members']) && in_array($uri, $relation['members']);
            $update   = false;

            // remove member from the relation
            if ($found && !$selected) {
                $relation['members'] = array_diff($relation['members'], (array) $uri);
                $update = true;
            }
            // add member to the relation
            else if (!$found && $selected) {
                $relation['members'][] = $uri;
                $update = true;
            }

            if ($update) {
                $config->save($relation, 'relation');
            }

            $tags = array_diff($tags, (array) $relation['name']);
        }

        // create new relations
        if (!empty($tags)) {
            foreach ($tags as $tag) {
                $relation = array(
                    'name'     => $tag,
                    'members'  => (array) $uri,
                    'category' => 'tag',
                );

                $config->save($relation, 'relation');
            }
        }

        // make sure current folder is set correctly again
        $this->storage->set_folder($folder);
    }

    public static function charset_to_cp($charset)
    {
        // @TODO: ?????
        // The body is converted to utf-8 in get_part_content(), what about headers?
        return 65001; // UTF-8

        $aliases = array(
            'asmo708' => 708,
            'shiftjis' => 932,
            'gb2312' => 936,
            'ksc56011987' => 949,
            'big5' => 950,
            'utf16' => 1200,
            'utf16le' => 1200,
            'unicodefffe' => 1201,
            'utf16be' => 1201,
            'johab' => 1361,
            'macintosh' => 10000,
            'macjapanese' => 10001,
            'macchinesetrad' => 10002,
            'mackorean' => 10003,
            'macarabic' => 10004,
            'machebrew' => 10005,
            'macgreek' => 10006,
            'maccyrillic' => 10007,
            'macchinesesimp' => 10008,
            'macromanian' => 10010,
            'macukrainian' => 10017,
            'macthai' => 10021,
            'macce' => 10029,
            'macicelandic' => 10079,
            'macturkish' => 10081,
            'maccroatian' => 10082,
            'utf32' => 12000,
            'utf32be' => 12001,
            'chinesecns' => 20000,
            'chineseeten' => 20002,
            'ia5' => 20105,
            'ia5german' => 20106,
            'ia5swedish' => 20107,
            'ia5norwegian' => 20108,
            'usascii' => 20127,
            'ibm273' => 20273,
            'ibm277' => 20277,
            'ibm278' => 20278,
            'ibm280' => 20280,
            'ibm284' => 20284,
            'ibm285' => 20285,
            'ibm290' => 20290,
            'ibm297' => 20297,
            'ibm420' => 20420,
            'ibm423' => 20423,
            'ibm424' => 20424,
            'ebcdickoreanextended' => 20833,
            'ibmthai' => 20838,
            'koi8r' => 20866,
            'ibm871' => 20871,
            'ibm880' => 20880,
            'ibm905' => 20905,
            'ibm00924' => 20924,
            'cp1025' => 21025,
            'koi8u' => 21866,
            'iso88591' => 28591,
            'iso88592' => 28592,
            'iso88593' => 28593,
            'iso88594' => 28594,
            'iso88595' => 28595,
            'iso88596' => 28596,
            'iso88597' => 28597,
            'iso88598' => 28598,
            'iso88599' => 28599,
            'iso885913' => 28603,
            'iso885915' => 28605,
            'xeuropa' => 29001,
            'iso88598i' => 38598,
            'iso2022jp' => 50220,
            'csiso2022jp' => 50221,
            'iso2022jp' => 50222,
            'iso2022kr' => 50225,
            'eucjp' => 51932,
            'euccn' => 51936,
            'euckr' => 51949,
            'hzgb2312' => 52936,
            'gb18030' => 54936,
            'isciide' => 57002,
            'isciibe' => 57003,
            'isciita' => 57004,
            'isciite' => 57005,
            'isciias' => 57006,
            'isciior' => 57007,
            'isciika' => 57008,
            'isciima' => 57009,
            'isciigu' => 57010,
            'isciipa' => 57011,
            'utf7' => 65000,
            'utf8' => 65001,
        );

        $charset = strtolower($charset);
        $charset = preg_replace(array('/^x-/', '/[^a-z0-9]/'), '', $charset);

        if (isset($aliases[$charset])) {
            return $aliases[$charset];
        }

        if (preg_match('/^(ibm|dos|cp|windows|win)[0-9]+/', $charset, $m)) {
            return substr($charset, strlen($m[1]) + 1);
        }
    }

    /**
     * Wrap text to a given number of characters per line
     * but respect the mail quotation of replies messages (>).
     * Finally add another quotation level by prepending the lines
     * with >
     *
     * @param string $text   Text to wrap
     * @param int    $length The line width
     *
     * @return string The wrapped text
     */
    protected static function wrap_and_quote($text, $length = 72)
    {
        // Function stolen from Roundcube ;)
        // Rebuild the message body with a maximum of $max chars, while keeping quoted message.
        $max = min(77, $length + 8);
        $lines = preg_split('/\r?\n/', trim($text));
        $out = '';

        foreach ($lines as $line) {
            // don't wrap already quoted lines
            if ($line[0] == '>') {
                $line = '>' . rtrim($line);
            }
            else if (mb_strlen($line) > $max) {
                $newline = '';
                foreach (explode("\n", rcube_mime::wordwrap($line, $length - 2)) as $l) {
                    if (strlen($l)) {
                        $newline .= '> ' . $l . "\n";
                    }
                    else {
                        $newline .= ">\n";
                    }
                }
                $line = rtrim($newline);
            }
            else {
                $line = '> ' . $line;
            }

            // Append the line
            $out .= $line . "\n";
        }

        return $out;
    }
}