-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgp-migrate.sh
executable file
·1098 lines (1004 loc) · 46.5 KB
/
cgp-migrate.sh
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
#!/bin/bash
# Copyright (C) 2014
# Stefan Jakobs <projects AT localside.net>
# Michael Kromer <m.kromer AT zarafa.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
### CommunigatePro Import scripts
### Version 1.11sj
# Description:
# TODO
# ...
# I tried to call the do_curl_* functions in background with &, but that
# causes too many connections to the webserver which in turn causes the
# curl commands to fail. So I decided to make serialize all function calls.
CONFIG_FILE='/etc/zarafa/cgp-migrate.conf'
FAILED_ITEMS_FOLDER='cgp-migrate.failed.items'
STATUS='0'
CHECK_ITEMS='0'
CHECK_COUNT='1'
SEND_INFO_MAIL='1'
FAIL_ON_RULES='0'
DEFAULT_CALENDAR_FOLDERS='Agenda|Kalender|Calendario|Calendar'
DEFAULT_TASK_FOLDERS='Tâches|Aufgaben|Tareas|Tasks'
DEFAULT_CONTACT_FOLDERS='Attività|Contacts|Kontakte|Contactos|Contatti'
MY_LANG='de_DE.UTF-8'
# keep track of mail folders
declare -A used_folders
declare -A created_folders
declare -A zarafa_folders
# have ipf types in
declare -A ipf_types;
ipf_types['CONTACT']='IPF.Contact'
ipf_types['TASK']='IPF.Task'
ipf_types['CALENDAR']='IPF.Appointment'
ipf_types['NOTE']='IPF.StickyNote'
# store failed calendar items in this file:
failed_cal_items='failed_calendar_items'
##### Functions start ######
function usage {
echo "Usage: ${0##*/} -u <primary source user> -U <destination user>"
echo " [-s <secondary source user> [-s ...]]"
echo " [-p <source user pw>] [-P <destination user pw>]"
echo " [-h <source imap host>] [-H <destination imap host>]"
echo " [-m <info mail rcpt>]"
echo " [-C] [-T] [-M] [-R]"
echo
echo " Use -C to check a successfull upload after each item"
echo " Use -T do disable checking the folder count"
echo " (compare amount of to import and imported items)"
echo " Use -M to prevent script from sending mails"
echo " Use -R to fail on rule import errors"
#"echo " [-D <destination domain>]"
exit 1
}
function log_msg {
local LOG_TYPE=$1
local MSG=$2
if [[ ${#LOG_TYPE} -lt 12 ]] ; then
for f in $(seq ${#LOG_TYPE} 10); do
LOG_TYPE=" $LOG_TYPE"
done
fi
date "+[${LOG_TYPE}] %H:%M:%S === ${MSG} ==="
}
function get_status {
local RETVAL="$?"
if [[ "$RETVAL" != '0' ]]; then
STATUS=1
echo "error" >> ${RAMDISK_LOCATION}/${DEST_USER}.errors
log_msg 'MIGRATION' "ERROR: $1 faild with $RETVAL"
fi
return $RETVAL
}
function clean_ramdisk {
if mount | grep -q "${RAMDISK_LOCATION}" ; then
umount ${RAMDISK_LOCATION} 2>&1 >/dev/null
fi
if rm -rf ${RAMDISK_LOCATION} ; then
log_msg 'PREPARE' "remove working directory: ${RAMDISK_LOCATION}"
else
log_msg 'PREPARE' "ERROR: failed to remove working directory: ${RAMDISK_LOCATION}"
fi
}
function rebuild_calendar {
# attribute1: required: calendar file
if [ -z "$1" ]; then
log_msg 'FUNCTION' "ERROR: function $FUNCNAME needs argument"
fi
local ics_file="$1"
sed -i '/\(^\(BEGIN\|END\):VCALENDAR\|^VERSION:2.0\|^METHOD:PUBLISH\)/d' "$ics_file"
sed -i '1s/^/BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\n/' "$ics_file"
echo -e "END:VCALENDAR" >> "$ics_file"
}
function do_curl_CALENDAR {
## use MBOXNAME_TASK instead of MBOXNAME
# attribute1: required: source ics file (e.g. import-${MBOXNAME}.cal.ics)
local source_ics="$1"
# attribute2: optional: true or no_priv
local import_private=${2:-'true'}
local upload_url="http://${ICAL_IP}:${ICAL_PORT}/ical/${DEST_USER}/${MBOXNAME_CALENDAR}/"
local item_count="$(grep -c BEGIN:VEVENT $source_ics)"
local pre_item_count=0
local empty_calendar='BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nEND:VCALENDAR'
if [[ "$CHECK_ITEMS" -eq 1 ]]; then
# empty folder before uploading
[[ $DEBUG -eq 1 ]] && echo curl --retry 3 -sS -u "$ICAL_USER" -T - $upload_url
echo -e "$empty_calendar" | curl --retry 3 -sS -u "$ICAL_USER" -T - $upload_url
get_status curl
pre_item_count="$(curl --retry 3 -sS -u "$ICAL_USER" $upload_url | grep -c BEGIN:VEVENT)"
fi
# import once with no private flags for comparison and make the summary uniq
# and keep a copy of the original for comparision
cat "$source_ics" | perl -ne 's/^SUMMARY:/"SUMMARY:" . ++$cnt ." "/ge; print $_;' > "${source_ics}.uniq"
cp "${source_ics}.uniq" "${source_ics}.noprivate.uniq"
sed -i '/^CLASS:/d' "${source_ics}.noprivate.uniq"
log_msg 'CALENDAR' "Importing calendar with non-private settings"
[[ $DEBUG -eq 1 ]] && echo curl --retry 3 -sS -u "$ICAL_USER" -T "${source_ics}.noprivate.uniq" $upload_url
curl --retry 3 -sS -u "$ICAL_USER" -T "${source_ics}.noprivate.uniq" $upload_url
get_status curl
rm -f "${source_ics}.noprivate.uniq"
# do comparison check here
grep -i ^SUMMARY "${source_ics}.uniq" | sed -e 's#\\"##g' -e 's#"##g' -e 's# $##g' | \
sed -e 's#^\(SUMMARY:[0-9]*\).*#\1#g' | sort > "${source_ics}.src"
curl --retry 3 -sS -u "$ICAL_USER" $upload_url | grep -i ^SUMMARY | \
sed -e 's#\\"##g' -e 's#"##g' -e 's# $##g' | sed -e 's#^\(SUMMARY:[0-9]*\).*#\1#g' | sort > "${source_ics}.dst"
dos2unix -q "${source_ics}.dst"
[[ $DEBUG -eq 1 ]] && diff -p "${source_ics}.src" "${source_ics}.dst"
diff -p "${source_ics}.src" "${source_ics}.dst" | grep '^- SUMMARY' | \
sed -e '/^- SUMMARY:$/d' -e 's#- ##g' | uniq > $failed_cal_items
while read line; do
if [ -n "$line" ]; then
grep "$line" "${source_ics}.uniq" -B40 -A40 | \
sed '/^BEGIN:VEVENT/,/^END:VEVENT/!d' >> \
"${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}"/calendar
fi
done < $failed_cal_items
cp $failed_cal_items "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}/"
if [ -f "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}"/calendar ]; then
pushd "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}" >/dev/null
#csplit -q calendar "/^BEGIN:\(VCALENDAR\|VCARD\|VGROUP\)/" {*}
csplit -q calendar "/^BEGIN:VEVENT/" {*}
rm -f xx00
for i in xx*; do
if [ -f $i ]; then
if grep -q -f $failed_cal_items $i ; then
mv $i "${source_ics%.cal.ics}-${i}.ics"
else
rm -f $i
fi
else
log_msg 'MIGRATION' "WARN: failed to extract VCALENDAR from ${source_ics}"
fi
done
rm -f calendar
popd >/dev/null
fi
rm -f "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}/${failed_cal_items}"
## check status if running in CHECK_ITEMS mode and import private is not requested
if [[ "$CHECK_ITEMS" -eq 1 ]] && [[ "$import_private" != 'true' ]]; then
local failed_counter="$(cat $failed_cal_items | wc -l)"
if [ "$failed_counter" -gt 0 ]; then
log_msg 'MIGRATION' "CHECKING: failed items: ${MBOXNAME_CALENDAR}: $failed_counter"
echo FAIL
else
echo OK
fi
fi
if [[ "$import_private" == 'true' ]]; then
# now import all with appropriate private flags
echo -e "$empty_calendar" | curl --retry 3 -sS -u "$ICAL_USER" -T - $upload_url
log_msg 'CALENDAR' "Importing calendar with all private settings"
[[ $DEBUG -eq 1 ]] && echo curl --retry 3 -sS -u "$ICAL_USER" -T "$source_ics" $upload_url
curl --retry 3 -sS -u "$ICAL_USER" -T "$source_ics" $upload_url
get_status curl
if [[ "$CHECK_ITEMS" -eq 1 ]]; then
local item="$(curl --retry 3 -sS -u "$ICAL_USER" ${upload_url})"
get_status curl
local post_item_count="$(echo "$item" | grep -c BEGIN:VEVENT)"
log_msg 'CALENDAR' "Before upload: ${pre_item_count}; Items uploaded: ${item_count}; After upload: ${post_item_count}"
if [[ $(( $item_count + $pre_item_count )) -ne "$post_item_count" ]]; then
# this works only with single events
# cat "$source_ics" >> "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}/${source_ics##*/}"
log_msg 'MIGRATION' "ERROR: failed to upload ics. ${DEST_USER}: ${source_ics##*/}"
fi
fi
fi
}
function do_curl_CONTACT {
## use MBOXNAME_CONTACT instead of MBOXNAME
# attribute1: required: source vcf file (e.g. import-${MBOXNAME_CONTACT}-${VCFNAME}.${SUFFIX})
local source_vcf="$1"
local upload_url="http://${VCARD_IP}/zarafa-sabre/addressbooks/${DEST_USER}/${MBOXNAME_CONTACT}/"
local upload_user="${DEST_USER}:notused"
[[ $DEBUG -eq 1 ]] && echo curl --retry 3 -sS -u "$upload_user" -T "$source_vcf" $upload_url
curl --retry 3 -sS -u "$upload_user" -T "$source_vcf" $upload_url
get_status curl
}
function do_curl_TASK {
## use MBOXNAME_TASK instead of MBOXNAME
# attribute1: required: source task file (e.g. import-${MBOXNAME}-${TASKNAME}.${SUFFIX}"
local source_task="$1"
local upload_url="http://${ICAL_IP}:${ICAL_PORT}/caldav/${DEST_USER}/${MBOXNAME_TASK}/"
[[ $DEBUG -eq 1 ]] && echo curl --retry 3 -sS -u "$ICAL_USER" -T "$source_task" $upload_url
curl --retry 3 -sS -u "$ICAL_USER" -T "$source_task" $upload_url
get_status curl
}
function do_imapsync {
local account="$1"
local prefix=''
# if primary account is not processed then set an other prefix
if [ "$account" != "${SOURCE_USER}" ]; then
local prefix="${account%@*}/"
fi
# set imapsync command
# defaults: --nosyncacls --syncinternaldates --skipsize
# provided by NAMESPACE: --sep2 '/' --prefix2 ""
imapsync --noreleasecheck --addheader --buffersize 8192000 --nofoldersizes --fast --noauthmd5 \
--useheader ALL --reconnectretry1 10 --reconnectretry2 10 \
--host1 "$SOURCE_IMAP_IP" --user1 "$account" --password1 "$SOURCE_IMAP_PASSWORD" --ssl1 \
--host2 "$DEST_IMAP_IP" --user2 "$DEST_USER" --password2 "$DEST_IMAP_PASSWORD" \
--authmech2 plain --prefix2 "$prefix" --delete2 \
--regextrans2 's/^Sent$/Gesendete Objekte/' --regextrans2 's/^Drafts$/Entw&APw-rfe/' \
--regextrans2 's/^Trash$/Gel&APY-schte Objekte/' --regextrans2 's/^Junk-E-Mail$/Junk E-Mail/'
# 2> /dev/null
get_status imapsync
}
function do_import_rules {
# attribute1: required: path to account.settings file
local account_settings="$1"
if ! [ -r "$account_settings" ]; then
log_msg 'RULES' "error: can not read $account_settings"
return 1
else
python ${TOOL_LOCATION}/convert_rules.py ${DEBUG:+-v} \
-m -u ${DEST_USER} $account_settings ;
fi
}
function update_zarafa_folder_list {
## Assuming:
## * each calendar, tasks and contacts folder name is unique
## * each account is empty before import starts
## (calendar will be overwriten anyway)
##
## get folder list with element count from zarafa.
## list-folder-size.py gives the following output:
## --IPM_SUBTREE|0
## ----Aufgaben|0
## ------Subtasks|1
## ----Mailbox
## ----Kalender|2
## ------Subkalender2|1
## ...
## Try to find Aufgaben, Kalender and Kontakte and from there on process
## each folder if its name starts with '------'
local next=0
local count_pos=0
local folder_pos=0
local folder_count=0
local folder_name=''
while read line ; do
if [[ "$line" =~ ^----$CALENDAR_FOLDER ]] ||
[[ "$line" =~ ^----$CONTACTS_FOLDER ]] ||
[[ "$line" =~ ^----$TASKS_FOLDER ]] ||
( [ "$next" -eq 1 ] && [[ "$line" =~ ^------ ]] ); then
next=1
# number of bars == count position
count_pos="$(echo $line | sed -e 's/[^|]//g' | wc -c)"
folder_pos=$(( $count_pos - 1 ))
folder_count="$(echo $line | cut -d'|' -f $count_pos)"
folder_name="$(echo $line | cut -d'|' -f 1-${folder_pos} | sed -e 's/^-----*//')"
if [ "$folder_count" -ne 0 ]; then
if [ -n "${zarafa_folders["$folder_name"]}" ]; then
zarafa_folders["$folder_name"]=$(($folder_count - ${zarafa_folders["$folder_name"]}))
else
zarafa_folders["$folder_name"]=$folder_count
fi
fi
elif [ "$next" -eq 1 ] && [[ "$line" =~ '^----' ]] ; then
next=0
fi
done <<< "$(LANG="$MY_LANG" ${TOOL_LOCATION}/list-folder-size.py ${DEST_USER} | head -n -1)"
}
##### Functions end ######
##### MAIN START ######
# check for needed perl modules
if ! perl -MEncode -MEncode::IMAPUTF7 -e 'print $Encode::IMAPUTF7::VERSION' &> /dev/null ; then
log_msg 'STARTUP' 'ERROR: CAN NOT LOAD Perl Library Encode::IMAPUTF7'
exit 1
fi
# check for needed binaries
for exe in '/usr/bin/uuidgen' '/usr/bin/imapsync' '/usr/bin/dos2unix' ; do
if ! [ -x "$exe" ]; then
log_msg 'STARTUP' "ERROR: can not find/execute ${exe}"
exit 1
fi
done
# source config file
if [ -r "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
else
log_msg 'STARTUP' "ERROR: can not source $CONFIG_FILE !"
exit 1
fi
# try to find an other config file in cwd
if [ -r './cgp-migrate.conf' ]; then
source "./cgp-migrate.conf"
fi
# check config file options:
if [ -z "$INFO_MAIL_SENDER" ] || [ -z "$INFO_MAIL_SUBJECT" ] || \
[ -z "$INFO_MAIL_BODY" ];then
log_msg 'STARTUP' 'ERROR one or more INFO_MAIL_* variables are empty'
exit 1
fi
# overwrite config file options by command line options
while getopts ":u:U:m:p:P:s:h:H:CMT" opt; do
case $opt in
u) SOURCE_USER="$OPTARG"
;;
s) SOURCE_SEC_USER="$SOURCE_SEC_USER $OPTARG"
;;
U) DEST_USER="$OPTARG"
;;
p) SOURCE_IMAP_PASSWORD="$OPTARG"
;;
P) DEST_IMAP_PASSWORD="$OPTARG"
;;
h) SOURCE_IMAP_IP="$OPTARG"
;;
H) DEST_IMAP_IP="$OPTARG"
;;
m) INFO_MAIL_RCPT="$OPTARG"
;;
M) SEND_INFO_MAIL='0'
;;
C) CHECK_ITEMS=1
;;
T) CHECK_COUNT=0
;;
R) FAIL_ON_RULES=1
;;
\?) usage
;;
:) usage
;;
esac
done
# check options for validity:
if [ -z "$SOURCE_USER" ] || [ -z "$DEST_USER" ]; then
log_msg 'STARTUP' 'ERROR: No source or destination user defined'
usage
fi
if [ -z "$SOURCE_IMAP_IP" ] || [ -z "$DEST_IMAP_IP" ]; then
log_msg 'STARTUP' 'ERROR: No source or destination IMAP Host/IP defined'
usage
fi
if [ -z "$SOURCE_IMAP_PASSWORD" ]; then
log_msg 'STARTUP' 'ERROR: No source IMAP password defined'
usage
fi
# set variables which depend on the config file
ICAL_USER="${ZARAFA_ADMIN_USER}:${ZARAFA_ADMIN_PASSWORD}"
RAMDISK_LOCATION="${RAMDISK_ROOT}/${DEST_USER}"
# check if source domain folder exists:
if ! [ -d "${DATA_LOCATION}/${SOURCE_USER##*@}" ]; then
log_msg 'STARTUP' "ERROR: directory ${DATA_LOCATION}/${SOURCE_USER##*@} does not exists"
exit 1
fi
if [[ $DEBUG -eq 1 ]]; then
exec 2> "/tmp/trace_${DEST_USER}"
set -x
fi
if [ "$CLEAN_RAMDISK_BEFORE_RUN" = 1 ] ; then
clean_ramdisk
fi
mkdir -p "${RAMDISK_LOCATION}" 2>&1 >/dev/null
log_msg 'PREPARE' "created working dir: ${RAMDISK_LOCATION}"
if [ "$CREATE_RAMDISK" = 1 ]; then
mount -t tmpfs -o size=${RAMDISK_SIZE} none "${RAMDISK_LOCATION}"
fi
## create a file which will store async. error msgs
touch "${RAMDISK_LOCATION}/${DEST_USER}.errors"
# create a folder where failed items will be stored
mkdir -p "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}"
get_status mkdir
pushd "${DATA_LOCATION}/${SOURCE_USER##*@}" 2>&1 >/dev/null
if [ -d "${SOURCE_USER%%@*}.macnt" ]; then
# get CGP's default calendar, task and contacts folder
tmp_cal_path=$(grep '#Viewer' ${SOURCE_USER%%@*}.macnt/account.info | \
cut -d{ -f2 | tr ';' '\n' | grep CalendarBox | cut -d= -f2 | tr -d '"')
CALENDARBOX="$(echo ${tmp_cal_path##*/} | sed -e 's# #_#g')"
CALENDARBOX_PATH="$(echo ${tmp_cal_path} | \
sed -e 's#[^/]*$##' -e 's# #_#g')"
tmp_task_path=$(grep '#Viewer' ${SOURCE_USER%%@*}.macnt/account.info | \
cut -d{ -f2 | tr ';' '\n' | grep TasksBox | cut -d= -f2 | tr -d '"')
TASKSBOX="$(echo ${tmp_task_path##*/} | sed -e 's# #_#g')"
TASKSBOX_PATH="$(echo ${tmp_task_path} | \
sed -e 's#[^/]*$##' -e 's# #_#g')"
tmp_contact_path=$(grep '#Viewer' ${SOURCE_USER%%@*}.macnt/account.info | \
cut -d{ -f2 | tr ';' '\n' | grep ContactsBox | cut -d= -f2 | tr -d '"')
CONTACTSBOX="$(echo ${tmp_contact_path##*/} | sed -e 's# #_#g')"
CONTACTSBOX_PATH="$(echo ${tmp_contact_path} | \
sed -e 's#[^/]*$##' -e 's# #_#g')"
else
log_msg 'MIGRATION' "ERROR: source user directory doesn't exist: ${SOURCE_USER%%@*}.macnt"
STATUS=1
fi
[ -z "$CALENDARBOX" ] && CALENDARBOX='Calendar'
[ -z "$TASKSBOX" ] && TASKSBOX='Tasks'
[ -z "$CONTACTSBOX" ] && CONTACTSBOX='Contacts'
# get Zarafa user's default calendar, tasks and contacts folder
${TOOL_LOCATION}/get-folder-name.py -u ${DEST_USER} > ${RAMDISK_LOCATION}/${DEST_USER}.destfolders
if [[ `cat ${RAMDISK_LOCATION}/${DEST_USER}.destfolders | wc -l` -gt "0" ]]; then
CALENDAR_FOLDER="`grep CALENDAR ${RAMDISK_LOCATION}/${DEST_USER}.destfolders | awk '{ print $2 }'`"
TASKS_FOLDER="`grep TASKS ${RAMDISK_LOCATION}/${DEST_USER}.destfolders | awk '{ print $2 }'`"
CONTACTS_FOLDER="`grep CONTACTS ${RAMDISK_LOCATION}/${DEST_USER}.destfolders | awk '{ print $2 }'`"
else
log_msg 'MIGRATION' "ERROR: DEFAULT FOLDERS COULD NOT BE FOUND IN DESTINATION - EXIT ($DEST_USER)"
exit 1
fi
# mark these folders as created
created_folders["CALENDAR:${CALENDAR_FOLDER}"]='1'
created_folders["TASK:${TASKS_FOLDER}"]='1'
created_folders["CONTACT:${CONTACTS_FOLDER}"]='1'
log_msg MIGRATION "PROCESSING user ${SOURCE_USER%%@*} ${SOURCE_SEC_USER%%@*} -> ${DEST_USER}"
# log the standard folder
log_msg 'DEFAULTS' "CALENDARBOX: ${CALENDARBOX_PATH}${CALENDARBOX} -> ${CALENDAR_FOLDER}"
log_msg 'DEFAULTS' "TASKSBOX: ${TASKBOX_PATH}${TASKSBOX} -> ${TASKS_FOLDER}"
log_msg 'DEFAULTS' "CONTACTSBOX: ${CONTACTSBOX_PATH}${CONTACTSBOX} -> ${CONTACTS_FOLDER}"
## iterate over all source user accounts
for source_account in ${SOURCE_USER} ${SOURCE_SEC_USER} ; do
pushd "${DATA_LOCATION}/${source_account##*@}" 2>&1 >/dev/null
# Get a list of all mbox files
MBOXFILE_LIST="$(find -L ${source_account%%@*}.macnt -iname "*.mbox" | sed -e '/Trash/d')"
log_msg 'MIGRATION' "START PROCESSING user ${source_account%%@*} -> ${DEST_USER}"
while read -r MBOXFILE; do
if [ -z "$MBOXFILE" ]; then
continue
fi
# keep track of UIDs in calendars
unset uid_list
declare -A uid_list
MBOXNAME="$(echo ${MBOXFILE} | sed -e 's#.mbox##' -e 's#.*\/##g' -e 's# #_#g' -e 's#\[##g' -e 's#\]##g' \
| perl -MEncode -MEncode::IMAPUTF7 -e "print encode('UTF8', decode('IMAP-UTF-7', <>));" )"
log_msg 'MBOXFILE' "START ${MBOXFILE}"
log_msg 'MBOXFILE' "MBOXNAME: ${MBOXNAME}"
# get folder level
LEVEL=$(echo ${MBOXFILE} | tr '/' '\n' | wc -l)
FPATH=$(echo ${MBOXFILE//.folder} | \
sed -e 's#/\+#/#' -e 's#^[^/]*/##' -e 's#[^/]*$##' -e 's# #_#g')
# generate a random foldername extension
URAND="_$(cat /dev/urandom | tr -cd [:alnum:] | head -c 4)"
# remove CGP standard folder from folder path, because zarafa folders are
# per default under the default folder
for f in ${DEFAULT_CALENDAR_FOLDERS//|/ } ${DEFAULT_TASK_FOLDERS//|/ } \
${DEFAULT_CONTACT_FOLDERS//|/ } ; do
FPATH=${FPATH#${f}/}
done
# if primary account is not processed then add folder (== account name) to path
if [ "$source_account" != "${SOURCE_USER}" ]; then
FPATH="${source_account%%@*}/${FPATH}"
MBOXNAME="${source_account%%@*}_${MBOXNAME}"
fi
# Is the actual mailbox our default calendar folder?
if [[ "$FPATH" == "${CALENDARBOX_PATH}" ]] && [[ "${MBOXNAME}" == "${CALENDARBOX}" ]] ; then
MBOXNAME="$CALENDAR_FOLDER"
FPATH=''
LEVEL=2
## import as mbox
#cp "${MBOXFILE}" "${RAMDISK_LOCATION}/${MBOXFILE##*/}_with_attachment"
#${TOOL_LOCATION}/import-mbox.py "${DEST_USER}" 'IPF.Appointment' "${RAMDISK_LOCATION}/${MBOXFILE##*/}_with_attachment"
#rm -f "${RAMDISK_LOCATION}/${MBOXFILE##*/}_with_attachment"
# zarafa thinks 'calendar' == 'kalender', so rewrite it to an other name
# this includes $CALENDAR_FOLDER
elif [[ "${MBOXNAME}" =~ ^${DEFAULT_CALENDAR_FOLDERS//|/$|^}$ ]] ; then
MBOXNAME="${MBOXNAME}${URAND}"
fi
# Is the actual mailbox our default tasks folder?
if [[ "$FPATH" == "${TASKSBOX_PATH}" ]] && [[ "${MBOXNAME}" == "${TASKSBOX}" ]] ; then
MBOXNAME="$TASKS_FOLDER"
FPATH=''
LEVEL=2
## import as mbox
#cp "${MBOXFILE}" "${RAMDISK_LOCATION}/${MBOXFILE##*/}_with_attachment"
#${TOOL_LOCATION}/import-mbox.py "${DEST_USER}" 'IPF.Task' "${RAMDISK_LOCATION}/${MBOXFILE##*/}_with_attachment"
#rm -f "${RAMDISK_LOCATION}/${MBOXFILE##*/}_with_attachment"
# zarafa thinks 'Tasks' == 'Aufgaben', so rewrite it to an other name
# this includes $TASKS_FOLDER
elif [[ "${MBOXNAME}" =~ ^${DEFAULT_TASK_FOLDERS//|/$|^}$ ]] ; then
MBOXNAME="${MBOXNAME}${URAND}"
fi
# Is the actual mailbox our default contacts folder?
if [[ "$FPATH" == "${CONTACTSBOX_PATH}" ]] && [[ "${MBOXNAME}" == "${CONTACTSBOX}" ]] ; then
MBOXNAME=$CONTACTS_FOLDER
FPATH=''
LEVEL=2
# zarafa thinks 'Contacts' == 'Kontakte', so rewrite it to an other name
# this includes $CONTACTS_FOLDER
elif [[ "${MBOXNAME}" =~ ^${DEFAULT_CONTACT_FOLDERS//|/$|^}$ ]] ; then
MBOXNAME="${MBOXNAME}${URAND}"
fi
# if mbox exists, rename it; don't use $URAND
if [ -n "${used_folders["${MBOXNAME}"]}" ]; then
MBOXNAME="${MBOXNAME}_$(cat /dev/urandom | tr -cd [:alnum:] | head -c 4)"
fi
# mark mbox as used
if [ -n "${used_folders["${MBOXNAME}"]}" ]; then
log_msg MIGRATION "FAILED: FOLDER ${MBOXNAME} EXISTS (${DEST_USER})"
exit 1
else
used_folders["${MBOXNAME}"]='0'
fi
# log the infos we obtained so far
log_msg 'MBOXFILE' "MBOXNAME: ${MBOXNAME} LEVEL: ${LEVEL} FPATH: ${FPATH}"
mkdir -p "${RAMDISK_LOCATION}/${MBOXFILE}"
cp "${MBOXFILE}" "${RAMDISK_LOCATION}/${MBOXFILE}/"
pushd "${RAMDISK_LOCATION}/${MBOXFILE}" 2>&1 >/dev/null
csplit -q -- *.mbox "/^BEGIN:\(VCALENDAR\|VCARD\|VGROUP\)/" {*} 2>&1 >/dev/null
get_status csplit
MBOXNAME_DEFAULT="$MBOXNAME"
FPATH_DEFAULT="$FPATH"
FIRSTTYPE=''
for STRIPFILE in xx*; do
MBOXNAME="$MBOXNAME_DEFAULT"
FPATH="$FPATH_DEFAULT"
if [ "$(grep -a ^BEGIN:VCALENDAR ${STRIPFILE} | wc -l)" -gt '0' ] && \
[ "$(grep -a ^METHOD:PUBLISH ${STRIPFILE} | wc -l)" -gt '0' ] && \
[ "$(grep -a ^BEGIN:VTODO ${STRIPFILE} | wc -l)" -lt '1' ] && \
[ "${MBOXNAME}" != "INBOX" ]; then
TYPE='CALENDAR'
TAG='VCALENDAR'
SUFFIX="cal.ics"
[[ ${DO_CALENDAR} -eq 0 ]] && DO_SKIP=1
elif [ "$(grep -a ^BEGIN:VCALENDAR ${STRIPFILE} | wc -l)" -gt '0' ] && \
[ "$(grep -a ^METHOD:PUBLISH ${STRIPFILE} | wc -l)" -gt '0' ] && \
[ "$(grep -a ^BEGIN:VTODO ${STRIPFILE} | wc -l)" -gt '0' ]; then
TYPE='TASK'
TAG='VCALENDAR'
SUFFIX='task.ics'
[[ ${DO_TASKS} -eq 0 ]] && DO_SKIP=1
elif [ "$(grep -a '^BEGIN:VCARD' ${STRIPFILE} | wc -l)" -gt '0' ] ; then
TYPE='CONTACT'
TAG='VCARD'
SUFFIX='vcf'
[[ ${DO_CONTACTS} -eq 0 ]] && DO_SKIP=1
elif [ "$(grep -a '^BEGIN:VGROUP' ${STRIPFILE} | wc -l)" -gt '0' ] ; then
TYPE='GROUP'
TAG='VGROUP'
SUFFIX='vgf'
[[ ${DO_CONTACTS} -eq 0 ]] && DO_SKIP=1
else
TYPE="FOLDER"
fi
if [[ ( "${TYPE}" = 'TASK' || \
"${TYPE}" = 'CALENDAR' || \
"${TYPE}" = 'CONTACT' || \
"${TYPE}" = 'GROUP' ) && \
${DO_SKIP} -eq 0 && "${MBOXNAME}" != 'INBOX' ]]; then
grep -a -B 10000000 "^END:${TAG}" ${STRIPFILE} | sed -e '/^PRODID/d' \
>> ${STRIPFILE}.${SUFFIX}
rm -f ${STRIPFILE}
log_msg "${TYPE}" "START ${MBOXFILE} ${STRIPFILE}"
if [ -z "$FIRSTTYPE" ]; then
FIRSTTYPE="$TYPE"
fi
if [[ -s ${STRIPFILE}.${SUFFIX} ]]; then
if [[ ${TYPE} = "CALENDAR" ]]; then
import_status_cal='OK' # set default
# change mboxname if mailbox has changed its type
if [[ "$FIRSTTYPE" != "$TYPE" ]] ; then
log_msg "${TYPE}" "RENAME FOLDER ${MBOXNAME} -> ${MBOXNAME}_C"
MBOXNAME="${MBOXNAME}_C"
FPATH="${FPATH//\//_C/}"
fi
## create folder
if [ -z "${created_folders["${TYPE}:${FPATH}${MBOXNAME}"]}" ]; then
${TOOL_LOCATION}/create-folder.py -u ${DEST_USER} -f "${FPATH}${MBOXNAME}" -t ${ipf_types[$TYPE]}
created_folders["${TYPE}:${FPATH}${MBOXNAME}"]='1'
fi
# remember mboxname for counting items
MBOXNAME_CALENDAR="$MBOXNAME"
# use tempfile for preprocessing and rewriting of uids
UID_TEST_FILE="import-uid-test.${SUFFIX}"
# preprocess calendar
#perl -MMIME::QuotedPrint -ne 'if ($_ =~ /=$/) {print decode_qp($_); } else { print $_ ; }' ${STRIPFILE}.${SUFFIX} | \
perl -p00e 's/\r?\n //g' ${STRIPFILE}.${SUFFIX} | \
sed -e 's#DT\(.*\);TZID=.*:\(.*\)#DT\1:\2Z#g' | \
grep -vf ${TOOL_LOCATION}/cgp-attribute-strip.txt > "${UID_TEST_FILE}"
# keep track of UIDs
cal_uid="$(grep '^UID:' "${UID_TEST_FILE}" | cut -d':' -f 2 )"
if [ "$(echo "$cal_uid" | wc -l)" -gt 1 ]; then
log_msg "$TYPE" "WARN: found more that one UID in VEVENT"
fi
for u in $cal_uid ; do
if [ -n "${uid_list[$u]}" ]; then
new_uid="$(uuidgen)"
log_msg "$TYPE" "INFO: found double uid; creating new one"
if [[ $DEBUG -eq 1 ]]; then
log_msg "$TYPE" "DEBUG: old uid: $u"
log_msg "$TYPE" "DEBUG: new uid: $new_uid"
fi
sed -i "0,/^UID:$u/{s/$u/$new_uid/}" "${UID_TEST_FILE}"
uid_list["$new_uid"]='1'
else
uid_list["$u"]='1'
fi
done
cat "${UID_TEST_FILE}" >> import-${MBOXNAME}.${SUFFIX}
rm "${UID_TEST_FILE}"
# test upload if strict checking is requested
if [ "$CHECK_ITEMS" -eq 1 ]; then
# backup calendar
if [ -e "import-${MBOXNAME}.${SUFFIX}" ]; then
cp "import-${MBOXNAME}.${SUFFIX}" "import-${MBOXNAME}.${SUFFIX}.bak"
fi
rebuild_calendar "import-${MBOXNAME}.${SUFFIX}"
import_status_cal="$(do_curl_CALENDAR "import-${MBOXNAME}.${SUFFIX}" no_priv)"
log_msg "$TYPE" "Import Status: $(echo "$import_status_cal" | tail -1)"
# restore calendar
if [ -e "import-${MBOXNAME}.${SUFFIX}.bak" ]; then
mv "import-${MBOXNAME}.${SUFFIX}.bak" "import-${MBOXNAME}.${SUFFIX}"
else
rm "import-${MBOXNAME}.${SUFFIX}"
fi
if [[ "$(echo "$import_status_cal" | tail -1)" != 'OK' ]]; then
cat "${STRIPFILE}.${SUFFIX}" >> \
"${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}/orig-${MBOXNAME}.${SUFFIX}"
rm -f "${STRIPFILE}.${SUFFIX}"
log_msg "$TYPE" 'WARN: skipping import of calendar item'
fi
fi
elif [[ "${TYPE}" = "CONTACT" ]]; then
import_status_contact='OK' # set default
# change mboxname if mailbox has changed its type
if [[ "$FIRSTTYPE" != "$TYPE" ]] ; then
log_msg "${TYPE}" "RENAME FOLDER ${MBOXNAME} -> ${MBOXNAME}_B"
MBOXNAME="${MBOXNAME}_B"
FPATH="${FPATH//\//_B/}"
fi
## create folder
if [ -z "${created_folders["${TYPE}:${FPATH}${MBOXNAME}"]}" ]; then
${TOOL_LOCATION}/create-folder.py -u ${DEST_USER} -f "${FPATH}${MBOXNAME}" -t ${ipf_types[$TYPE]}
created_folders["${TYPE}:${FPATH}${MBOXNAME}"]='1'
fi
# remember mboxname for counting items
MBOXNAME_CONTACT="$MBOXNAME"
VCFNAME="`grep '^UID:' ${STRIPFILE}.${SUFFIX} | sed -e 's#^UID:##' -e 's#[[:space:]]##'`"
if [ -z "$VCFNAME" ] ; then
VCFNAME="$(/usr/bin/uuidgen)"
fi
# added: perl -p00e 's/=\r?\n//g' | perl -p00e 's/=3D\r?\n//g' to fix errors in rus
# rewrite unknown TYPE POSTAL to TYPE OTHER
sed ':a;N;$!ba;s/=\n//g' ${STRIPFILE}.${SUFFIX} | \
perl -MMIME::QuotedPrint -ne 'if ($_ =~ /QUOTED-PRINTABLE/) { print $_ ; } else { print decode_qp($_); }' | \
perl -p00e 's/\r?\n //g' | \
perl -p00e 's/=\r?\n//g' | perl -p00e 's/=3D\r?\n//g' | \
sed -e 's#DT\(.*\);TZID=.*:\(.*\)#DT\1:\2Z#g' | \
sed -e 's#;TYPE=POSTAL#;TYPE=OTHER#gi' | \
grep -vf ${TOOL_LOCATION}/cgp-attribute-strip.txt \
> "import-${MBOXNAME}-${VCFNAME}.${SUFFIX}"
# Add FN if no X-CN or FN exists (which breaks zarafa-sabre)
if ! egrep -q '^FN:|^EMAIL.*X-CN=' "import-${MBOXNAME}-${VCFNAME}.${SUFFIX}" ; then
sed -i 's/^\(VERSION:2.1\)$/\1\nFN:Not Named/' "import-${MBOXNAME}-${VCFNAME}.${SUFFIX}"
fi
# upload contact - compare and count results later
do_curl_CONTACT "import-${MBOXNAME}-${VCFNAME}.${SUFFIX}"
elif [[ "${TYPE}" = "GROUP" ]]; then
VGFNAME="`grep '^UID:' ${STRIPFILE}.${SUFFIX} | sed -e 's#^UID:##' -e 's#[[:space:]]##'`"
if [ -z "$VGFNAME" ] ; then
VGFNAME="$(/usr/bin/uuidgen)"
fi
# move vgroup away, so that we can send it per mail
mv "${STRIPFILE}.${SUFFIX}" "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}/import-${MBOXNAME}-${VGFNAME}.${SUFFIX}"
elif [[ "${TYPE}" = "TASK" ]]; then
import_status_task='OK' # set default
# change mboxname if mailbox has changed its type
if [[ "$FIRSTTYPE" != "$TYPE" ]] ; then
log_msg "${TYPE}" "RENAME FOLDER ${MBOXNAME} -> ${MBOXNAME}_A"
MBOXNAME="${MBOXNAME}_A"
FPATH="${FPATH//\//_A/}"
fi
## create folder
if [ -z "${created_folders["${TYPE}:${FPATH}${MBOXNAME}"]}" ]; then
${TOOL_LOCATION}/create-folder.py -u ${DEST_USER} -f "${FPATH}${MBOXNAME}" -t ${ipf_types[$TYPE]}
created_folders["${TYPE}:${FPATH}${MBOXNAME}"]='1'
fi
MBOXNAME_TASK="$MBOXNAME"
TASKNAME="`grep '^UID:' ${STRIPFILE}.${SUFFIX} | sed -e 's#^UID:##'`"
sed ':a;N;$!ba;s/=\n//g' ${STRIPFILE}.${SUFFIX} | \
perl -MMIME::QuotedPrint -ne 'if ($_ =~ /QUOTED-PRINTABLE/) { print $_ ; } else { print decode_qp($_); }' | \
perl -p00e 's/\r?\n //g' | sed -e 's#DT\(.*\);TZID=.*:\(.*\)#DT\1:\2Z#g' | \
grep -vf ${TOOL_LOCATION}/cgp-attribute-strip.txt > \
"import-${MBOXNAME}-${TASKNAME}.${SUFFIX}"
rebuild_calendar "import-${MBOXNAME}-${TASKNAME}.${SUFFIX}"
# private items need special treatment:
# import twice: first public, then private (after checking and counting results)
if grep -q -i '^CLASS:PRIVATE' "import-${MBOXNAME}-${TASKNAME}.${SUFFIX}" ; then
cp "import-${MBOXNAME}-${TASKNAME}.${SUFFIX}" "import-${MBOXNAME}-${TASKNAME}.${SUFFIX}.private"
fi
sed -i '/^CLASS:/d' "import-${MBOXNAME}-${TASKNAME}.${SUFFIX}"
# upload task - compare and count results later
do_curl_TASK "import-${MBOXNAME}-${TASKNAME}.${SUFFIX}"
fi
# register folder if necessary and increase element count
if [ -z "${used_folders["${MBOXNAME}"]}" ]; then
used_folders["${MBOXNAME}"]='0'
fi
#let used_folders["${MBOXNAME}"]=${used_folders["${MBOXNAME}"]}+1
else
rm -f ${STRIPFILE}.${SUFFIX}
fi
fi
done
# upload final calendar file
if [ -e "import-${MBOXNAME_CALENDAR}.cal.ics" ]; then
rebuild_calendar "import-${MBOXNAME_CALENDAR}.cal.ics"
# count calendar items
calendar_count="$(grep -c BEGIN:VEVENT "import-${MBOXNAME_CALENDAR}.cal.ics")"
let used_folders["${MBOXNAME_CALENDAR}"]=${used_folders["${MBOXNAME_CALENDAR}"]}+${calendar_count}
do_curl_CALENDAR "import-${MBOXNAME_CALENDAR}.cal.ics"
fi
wait
# count failed calendar item
if [ -f $failed_cal_items ]; then
failed_counter="$(cat $failed_cal_items | wc -l)"
if [ "$failed_counter" -gt 0 ]; then
let used_folders["${MBOXNAME_CALENDAR}"]=${used_folders["${MBOXNAME_CALENDAR}"]}-"$failed_counter"
log_msg 'MIGRATION' "WARN: failed items: ${MBOXNAME_CALENDAR}: $failed_counter"
fi
fi
# check contacts list
contact_count="$(ls -1 import-*.vcf 2>/dev/null | wc -l)"
if [[ "$contact_count" -gt 0 ]]; then
used_folders["${MBOXNAME_CONTACT}"]="$contact_count"
ls -1 import-*.vcf | sort > contact_files
# <td><a href="/zarafa-sabre/addressbooks/st000003/Kontakte_in_der_Inbox/import-Kontakte_in_der_Inbox-555188919.6.herbie%40po2.uni-stuttgart.de.vcf">import-Kontakte_in_der_Inbox-555188919.6.herbie@po2.uni-stuttgart.de.vcf</a></td>
curl --retry 3 -sS -u ${DEST_USER}:notused "http://${VCARD_IP}/zarafa-sabre/addressbooks/${DEST_USER}/${MBOXNAME_CONTACT}/" | \
grep -v 'img src=' | grep '.vcf' | sed -r 's#^.*<a href="([^"]+)">([^<]+)</a>.*$#\2#' | sort > upload_contact_files
get_status curl
while read file ; do
if [ -n "$file" ]; then
cp "$file" ${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}/
let used_folders["${MBOXNAME_CONTACT}"]=${used_folders["${MBOXNAME_CONTACT}"]}-1
log_msg 'MIGRATION' "ERROR: failed to upload vcf. ${DEST_USER}: ${file}"
fi
done <<< "$(join -v1 contact_files upload_contact_files)"
fi
# check tasks list
task_count="$(ls -1 import-*.task.ics 2>/dev/null | wc -l)"
if [[ "$task_count" -gt 0 ]]; then
used_folders["${MBOXNAME_TASK}"]="$task_count"
# do comparison check here
grep -h -i ^SUMMARY import-*.task.ics | \
sed -e 's#\\"##g' -e 's#"##g' -e 's# $##g' | sort > tasklist.src
curl --retry 3 -sS -u "$ICAL_USER" "http://${ICAL_IP}:${ICAL_PORT}/ical/${DEST_USER}/${MBOXNAME_TASK}/" | \
grep -i ^SUMMARY | sed -e 's#\\"##g' -e 's#"##g' -e 's# $##g' | sort > tasklist.dst
dos2unix -q tasklist.dst
diff -p tasklist.src tasklist.dst | grep '^- SUMMARY' | \
sed -e '/^- SUMMARY:$/d' -e 's#- ##g' | uniq | while read line; do
log_msg 'MIGRATION' "ERROR: failed to upload task. ${DEST_USER}: ${line}"
let used_folders["${MBOXNAME_TASK}"]=${used_folders["${MBOXNAME_TASK}"]}-1
grep -h "$line" import-*.task.ics -B40 -A40 | \
sed '/^BEGIN:VCALENDAR/,/^END:VCALENDAR/!d' >> \
"${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}"/tasks
done
if [ -f "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}"/tasks ]; then
pushd "${RAMDISK_LOCATION}/${FAILED_ITEMS_FOLDER}" >/dev/null
csplit -q tasks "/^BEGIN:VCALENDAR/" {*}
rm -f xx00
for i in xx*; do
if [ -f $i ]; then
mv $i "${MBOXNAME_TASK}-${i}.task.ics"
else
log_msg 'MIGRATION' "WARN: failed to extract VCALENDAR from ${MBOXNAME_TASK}"
fi
done
rm -f tasks
popd >/dev/null
fi
# import private items
for file in import-*.task.ics.private ; do
if [ -e $file ]; then
do_curl_TASK "$file"
get_status curl
fi
done
fi
popd 2>&1 >/dev/null
rm -f ${RAMDISK_LOCATION}/${MBOXFILE}/*.mbox
log_msg "${TYPE}" "END ${MBOXFILE}"
if [[ "${DO_NOTES}" = "1" ]]; then
NOTE_COUNT=$(grep -c '^X-MAPI-Message-Class: IPM.StickyNote' "${MBOXFILE}")
if [ "$NOTE_COUNT" -gt 0 ] ; then
log_msg 'NOTES' "START ${MBOXFILE}"
[[ $DEBUG -eq 1 ]] && echo ${TOOL_LOCATION}/import-mbox.py "${DEST_USER}" 'IPF.StickyNote' "${MBOXFILE}"
# notes mbox files may contain mails without X-MAPI-Message-Class. Count every mail.
NOTE_COUNT=$(grep -c '^From ' "${MBOXFILE}")
${TOOL_LOCATION}/import-mbox.py "${DEST_USER}" 'IPF.StickyNote' "${MBOXFILE}"
get_status 'import-mbox.py'
log_msg 'NOTES' "END ${MBOXFILE}"
# register folder if necessary and increase element count
NOTESNAME="$(basename ${MBOXFILE%.mbox})"
if [ -z "${used_folders["${NOTESNAME}"]}" ]; then
used_folders["${NOTESNAME}"]='0'
fi
let used_folders["${NOTESNAME}"]=${used_folders["${NOTESNAME}"]}+$NOTE_COUNT
fi
fi
TYPE="NONE"
DO_SKIP=0
# deregister mailbox if it wasn't used
if [ "${used_folders["${MBOXNAME}"]}" -eq 0 ]; then
unset used_folders["${MBOXNAME}"]
fi
done <<< "$MBOXFILE_LIST"
if [[ "${DO_FOLDER}" = "1" ]]; then
log_msg 'IMAPSYNC' "START ${source_account}"
#[[ $DEBUG -eq 1 ]] && echo ${TOOL_LOCATION}/import-mbox.py "${DEST_USER}" "${MBOXFILE}"
#${TOOL_LOCATION}/import-mbox.py "${DEST_USER}" "${MBOXFILE}"
#get_status 'import-mbox.py'
[[ $DEBUG -eq 1 ]] && type do_imapsync
do_imapsync $source_account
log_msg 'IMAPSYNC' "END ${source_account}"
else
# give zarafa-server some time to import items
sleep 30
fi
if [[ "${DO_RULES}" = "1" ]]; then
log_msg 'RULES' "START ${DEST_USER}"
# remove Password line from settings as it may contain funny characters
grep -v '^ Password = ' "${source_account%%@*}.macnt/account.settings" > "${RAMDISK_LOCATION}/accout.settings"
if ! do_import_rules "${RAMDISK_LOCATION}/accout.settings" ; then
log_msg 'RULES' "converting rules failed"
if [[ "$FAIL_ON_RULES" -eq 1 ]]; then
STATUS=1
fi
fi
log_msg 'RULES' "END ${DEST_USER}"
fi
# Now, as we assume everything else has been imported, we import the permissions
if [[ "${DO_PERMISSIONS}" = "1" ]]; then
log_msg 'PERMISSIONS' "START PERMISSIONS OF ${DEST_USER}"
grep Access ${source_account%%@*}.macnt/account.info | sed -e 's#^ \(.*\) = .*Access={\(.*\)};Box.*#\1\t\2#' -e 's#"##g' -e 's#^#[PERMISSIONS]\t#g'
grep Access ${source_account%%@*}.macnt/account.info | sed -e 's#^ \(.*\) = .*Access={\(.*\)};Box.*#\1\t;\2#' -e 's#"##g' -e 's#;$##g' > ${RAMDISK_LOCATION}/${DEST_USER}.perm
sed -e 's#.*\t;##g' -e 's#=l[rswipcda]*##g' -e 's#;#\n#' ${RAMDISK_LOCATION}/${DEST_USER}.perm | sort | uniq | while read PERMUSER; do
PERMSTRING=""
grep -i ${PERMUSER} ${RAMDISK_LOCATION}/${DEST_USER}.perm | awk '{ print $1 }' | while read FOLDER; do
PERMS="`grep -i ${PERMUSER} ${RAMDISK_LOCATION}/${DEST_USER}.perm | grep $FOLDER | sed \"s#.*${PERMUSER}=\(l[rswipcda]*\).*#\1#g\"`"
if [[ "$PERMS" = "lrswipcda" ]]; then
ZPERMS="fullcontrol"
elif [[ "$PERMS" = "lrswipcd" ]]; then
ZPERMS="owner"
elif [[ "$PERMS" = "lrsip" || "$PERMS" = "lrswip" || "$PERMS" = "lrps" ]]; then
ZPERMS="secretary"
else
ZPERMS="readonly"
fi
if [[ "`echo $FOLDER | grep -i cal | wc -l`" -gt "0" ]]; then
PERMSTRING="${PERMSTRING} --calendar ${ZPERMS}"
elif [[ "`echo $FOLDER | grep -i contact | wc -l`" -gt "0" ]]; then
PERMSTRING="${PERMSTRING} --contacts ${ZPERMS}"
elif [[ "`echo $FOLDER | grep -i task | wc -l`" -gt "0" ]]; then
PERMSTRING="${PERMSTRING} --tasks ${ZPERMS}"
elif [[ "`echo $FOLDER | grep -i inbox | wc -l`" -gt "0" ]]; then
PERMSTRING="${PERMSTRING} --inbox ${ZPERMS}"
fi
echo $PERMSTRING > ${RAMDISK_LOCATION}/${DEST_USER}.$PERMUSER.perm
done
echo /usr/bin/zarafa-mailbox-permissions --update-delegate "${PERMUSER}" "`cat ${RAMDISK_LOCATION}/${DEST_USER}.${PERMUSER}.perm`" "${DEST_USER}" >> ${RAMDISK_LOCATION}/all_permissions.sh
rm -f ${RAMDISK_LOCATION}/${DEST_USER}.${PERMUSER}.perm
done
chmod u+x ${RAMDISK_LOCATION}/all_permissions.sh
sh ${RAMDISK_LOCATION}/all_permissions.sh
get_status all_permissions.sh
rm -f ${RAMDISK_LOCATION}/${DEST_USER}.perm
log_msg 'PERMISSIONS' "END PERMISSIONS OF ${DEST_USER}"
fi
## wait for background processes
wait
log_msg 'MIGRATION' "FINISHED PROCESSING user ${source_account%%@*} -> ${DEST_USER}"
popd 2>&1 >/dev/null
done # for loop
# check and fix contacts, calendar and tasks items
log_msg 'MIGRATION' "Starting zarafa-fsck"
zarafa-fsck -u st000003 --autofix yes --autodel no --acceptdisclaimer | grep -A 19 ^Statistics
log_msg 'MIGRATION' "Finished zarafa-fsck"
log_msg 'MIGRATION' "DONE user $DEST_USER"
log_msg 'REVIEW' "START REVIEW PROCESS for $DEST_USER"
# calculate the new elements in the zarafa folders:
update_zarafa_folder_list
## check if all elements were accepted by zarafa
for name in "${!used_folders[@]}"; do
if [ -z "${zarafa_folders[$name]}" ] ; then
if [ "${used_folders[$name]}" -gt 0 ]; then
log_msg 'REVIEW' "ERROR: $name doesn't exist in zarafa_folders (should be: ${used_folders[$name]})!"
if [ "$CHECK_COUNT" -eq 1 ]; then
STATUS=1
fi
fi
elif [ "${used_folders[$name]}" -gt "${zarafa_folders[$name]}" ]; then
log_msg 'REVIEW' "ERROR: $name ${used_folders[$name]} > ${zarafa_folders[$name]} (uploaded : stored)"
if [ "$CHECK_COUNT" -eq 1 ]; then
STATUS=1
fi
elif [ "${used_folders[$name]}" -lt "${zarafa_folders[$name]}" ]; then
log_msg 'REVIEW' "WARN: $name ${used_folders[$name]} < ${zarafa_folders[$name]} (uploaded : stored)"
fi
done