forked from stachre/dump-contacts2db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-contacts2db.sh
418 lines (343 loc) · 14.2 KB
/
dump-contacts2db.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
#!/bin/bash
# Copyright (C) 2012-2013, Stachre
#
# 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 2 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/>,
# or write to the Free Software Foundation, Inc., 51 Franklin Street,
# Fifth Floor, Boston, MA 02110-1301, USA.
#
#
# dump-contacts2db.sh
# Version 0.4, 2013-02-11
# Dumps contacts from an Android contacts2.db to stdout in vCard format
# Usage: dump-contacts2db.sh path/to/contacts2.db > path/to/output-file.vcf
# Dependencies: perl; base64; sqlite3 / libsqlite3-dev
# expects single argument, path to contacts2.db
if [ "$#" -ne 1 ]
then echo -e "Dumps contacts from an Android contacts2.db to stdout in vCard format\n"
echo -e "Usage: dump-contacts2db.sh path/to/contacts2.db > path/to/output-file.vcf\n"
echo -e "Dependencies: perl; base64; sqlite3 / libsqlite3-dev"
exit 1
fi
# TODO: verify specified contacts2.db file exists
# inits
declare -i cur_contact_id=0
declare -i prev_contact_id=0
CONTACTS2_PATH=$1
# store Internal Field Separator
ORIG_IFS=$IFS
function replace_newlines()
{
local FIELD="$1"
echo "REPLACE(REPLACE(REPLACE(${FIELD},X'0D0A','\n'),X'0D','\n'),X'0A','\n')"
}
# fetch contact data
# TODO: order by account, with delimiters if possible
record_set=$(sqlite3 $CONTACTS2_PATH "SELECT raw_contacts._id, raw_contacts.display_name, raw_contacts.display_name_alt, mimetypes.mimetype, $(replace_newlines data.data1), data.data2, data.data3, $(replace_newlines data.data4), data.data5, data.data6, data.data7, data.data8, data.data9, data.data10, quote(data.data15) FROM raw_contacts, data, mimetypes WHERE raw_contacts.deleted = 0 AND raw_contacts._id = data.raw_contact_id AND data.mimetype_id = mimetypes._id ORDER BY raw_contacts._id, mimetypes._id, data.data2")
# modify Internal Field Separator for parsing rows from recordset
IFS=`echo -e "\n\r"`
# iterate through contacts data rows
# use "for" instead of piped "while" to preserve var values post-loop
for row in $record_set
do
# modify Internal Field Separator for parsing cols from row
IFS="|"
i=0
for col in $row
do
i=$[i+1]
# contact data fields stored in generic value columns
# schema determined by "mimetype", which varies by row
case $i in
1) # raw_contacts._id
cur_contact_id=$col
;;
2) # raw_contacts.display_name
cur_display_name=$col
;;
3) # raw_contacts.display_name_alt
# replace comma-space with semicolon
cur_display_name_alt=${col/, /\;}
;;
4) # mimetypes.mimetype
cur_mimetype=$col
;;
5) # data.data1
cur_data1=$col
;;
6) # data.data2
cur_data2=$col
;;
7) # data.data3
cur_data3=$col
;;
8) # data.data4
cur_data4=$col
;;
9) # data.data5
cur_data5=$col
;;
10) # data.data6
cur_data6=$col
;;
11) # data.data7
cur_data7=$col
;;
12) # data.data8
cur_data8=$col
;;
13) # data.data9
cur_data9=$col
;;
14) # data.data10
cur_data10=$col
;;
15) # data.data15
cur_data15=$col
;;
16) # contacts.contact_last_updated_timestamp
last_updated_timestamp=$col
secs_since_epoch=$(($last_updated_timestamp/1000))
cur_rev=`date "+%Y-%m-%dT%H:%M:%SZ" --utc --date="@$secs_since_epoch"`
;;
esac
done
# new contact
if [ $prev_contact_id -ne $cur_contact_id ]; then
if [ $prev_contact_id -ne 0 ]; then
# echo current vcard prior to reinitializing variables
# some contacts apps don't have IM fields; add to top of NOTE: field
if [ ${#cur_vcard_im_note} -ne 0 ]
then cur_vcard_note=$cur_vcard_im_note"\n"$cur_vcard_note
fi
# generate and echo vcard
if [ ${#cur_vcard_note} -ne 0 ]
then cur_vcard_note="NOTE:"$cur_vcard_note$'\n'
fi
cur_vcard=$cur_vcard$cur_vcard_fn$cur_vcard_n
cur_vcard=$cur_vcard$cur_vcard_nick$cur_vcard_org$cur_vcard_title$cur_vcard_tel
cur_vcard=$cur_vcard$cur_vcard_adr$cur_vcard_email$cur_vcard_url$cur_vcard_note
cur_vcard=$cur_vcard$cur_vcard_bday$cur_vcard_anniversary$cur_vcard_photo$cur_vcard_im
cur_vcard=$cur_vcard"END:VCARD"
echo $cur_vcard
fi
# init new vcard
cur_vcard="BEGIN:VCARD"$'\n'"VERSION:3.0"$'\n'
#cur_vcard=$cur_vcard"N:"$cur_display_name_alt$'\n'"FN:"$cur_display_name$'\n'
cur_vcard_n=""
cur_vcard_fn=""
cur_vcard=$cur_vcard"REV:"$cur_rev$'\n'
cur_vcard_nick=""
cur_vcard_org=""
cur_vcard_title=""
cur_vcard_tel=""
cur_vcard_adr=""
cur_vcard_email=""
cur_vcard_url=""
cur_vcard_note=""
cur_vcard_im=""
cur_vcard_im_note=""
cur_vcard_bday=""
cur_vcard_anniversary=""
cur_vcard_photo=""
fi
# add current row to current vcard
# again, "mimetype" determines schema on a row-by-row basis
# TODO: handle following types
# * (**) vnd.com.google.cursor.item/contact_misc (not exported by Android 4.1 Jelly Bean)
# * (14) vnd.android.cursor.item/website
# * (13) vnd.android.cursor.item/group_membership (not exported by Android 4.1 Jelly Bean)
# * (12) vnd.android.cursor.item/note
# * (11) vnd.android.cursor.item/contact_event
# * (10) vnd.android.cursor.item/photo
# * (9) vnd.android.cursor.item/identity (not exported by Android 4.1 Jelly Bean)
# * (8) vnd.android.cursor.item/postal-address_v2
# * (7) vnd.android.cursor.item/name
# * (6) vnd.android.cursor.item/sip_address
# * (5) vnd.android.cursor.item/phone_v2
# * (4) vnd.android.cursor.item/organization
# * (3) vnd.android.cursor.item/nickname
# * (2) vnd.android.cursor.item/im
# * (1) vnd.android.cursor.item/email_v2
case $cur_mimetype in
vnd.android.cursor.item/name)
cur_vcard_n="N:"$cur_data3";"$cur_data2";"$cur_data5";"$cur_data4";"$cur_data6$'\n'
cur_vcard_fn=$cur_vcard_fn"FN:"$cur_data1$'\n'
# TODO: data7,8,9 - X-PHONETIC-*
;;
vnd.android.cursor.item/nickname)
if [ ${#cur_data1} -ne 0 ]
then cur_vcard_nick="NICKNAME:"$cur_data1$'\n'
fi
;;
vnd.android.cursor.item/organization)
if [ ${#cur_data1} -ne 0 ]
then cur_vcard_org=$cur_vcard_org"ORG:"$cur_data1$'\n'
fi
if [ ${#cur_data4} -ne 0 ]
then cur_vcard_title="TITLE:"$cur_data4$'\n'
fi
;;
vnd.android.cursor.item/phone_v2)
case $cur_data2 in
1)
cur_vcard_tel_type="HOME,VOICE"
;;
2)
cur_vcard_tel_type="CELL,VOICE,PREF"
;;
3)
cur_vcard_tel_type="WORK,VOICE"
;;
4)
cur_vcard_tel_type="WORK,FAX"
;;
5)
cur_vcard_tel_type="HOME,FAX"
;;
6)
cur_vcard_tel_type="PAGER"
;;
7)
cur_vcard_tel_type="OTHER"
;;
8)
cur_vcard_tel_type="CUSTOM"
;;
9)
cur_vcard_tel_type="CAR,VOICE"
;;
esac
cur_vcard_tel=$cur_vcard_tel"TEL;TYPE="$cur_vcard_tel_type":"$cur_data1$'\n'
;;
vnd.android.cursor.item/postal-address_v2)
case $cur_data2 in
1)
cur_vcard_adr_type="HOME"
;;
2)
cur_vcard_adr_type="WORK"
;;
esac
# ignore addresses that contain only USA (MS Exchange)
# TODO: validate general address pattern instead
if [ $cur_data1 != "United States of America" ]
then cur_vcard_adr=$cur_vcard_adr"ADR;TYPE="$cur_vcard_adr_type":;;"$cur_data4";"$cur_data7";"$cur_data8";"$cur_data9";"$cur_data10$'\n'
cur_vcard_adr=$cur_vcard_adr"LABEL;TYPE="$cur_vcard_adr_type":"$cur_data1$'\n'
fi
;;
vnd.android.cursor.item/email_v2)
cur_vcard_email=$cur_vcard_email"EMAIL:"$cur_data1$'\n'
;;
vnd.android.cursor.item/website)
cur_vcard_url=$cur_vcard_url"URL:"$cur_data1$'\n'
;;
vnd.android.cursor.item/im)
# handle entire string within each case to avoid unhandled cases
case $cur_data5 in
-1)
cur_vcard_im_note=$cur_vcard_im_note"IM-Custom-"$cur_data6": "$cur_data1"\n"
;;
0)
cur_vcard_im=$cur_vcard_im"X-AIM:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-AIM: "$cur_data1"\n"
;;
1)
cur_vcard_im=$cur_vcard_im"X-MSN:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-MSN: "$cur_data1"\n"
;;
2)
cur_vcard_im=$cur_vcard_im"X-YAHOO:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-Yahoo: "$cur_data1"\n"
;;
3)
cur_vcard_im=$cur_vcard_im"X-SKYPE-USERNAME:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-Skype: "$cur_data1"\n"
;;
4)
cur_vcard_im=$cur_vcard_im"X-QQ:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-QQ: "$cur_data1"\n"
;;
5)
cur_vcard_im=$cur_vcard_im"X-GOOGLE-TALK:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-Google-Talk: "$cur_data1"\n"
;;
6)
cur_vcard_im=$cur_vcard_im"X-ICQ:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-ICQ: "$cur_data1"\n"
;;
7)
cur_vcard_im=$cur_vcard_im"X-JABBER:"$cur_data1$'\n'
cur_vcard_im_note=$cur_vcard_im_note"IM-Jabber: "$cur_data1"\n"
;;
*)
# Android 2.3 Gingerbread doesn't identify service; data5==""
cur_vcard_im_note=$cur_vcard_im_note"IM: "$cur_data1"\n"
;;
esac
;;
vnd.android.cursor.item/photo)
if [ $cur_data15 != "NULL" ]; then
# Remove the prefix "X'" and suffix "'" from the sqlite3 quote(BLOB) hex output
photo=`echo $cur_data15 | sed -e "s/^X'//" -e "s/'$//"`
# Convert the hex to base64
# TODO: optimize
photo=`echo $photo | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' | base64 --wrap=0`
cur_vcard_photo=$cur_vcard_photo"PHOTO;ENCODING=BASE64;JPEG:"$photo$'\n'
# TODO: line wrapping; Android import doesn't like base64's wrapping
# For testing
#echo $cur_data15 > "images/$cur_display_name.txt"
#echo $cur_data15 | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' > "images/$cur_display_name.jpg"
fi
;;
vnd.android.cursor.item/note)
# "NOTE:" and trailing \n appended when vCard is finished and echoed
if [ ${#cur_vcard_note} -ne 0 ]
then cur_vcard_note=$cur_vcard_note"\n\n"$cur_data1
else cur_vcard_note=$cur_data1
fi
;;
vnd.android.cursor.item/contact_event)
# Identify and record birthday event
case $cur_data2 in
3)
cur_vcard_bday="BDAY:"$cur_data1$'\n'
;;
1)
cur_vcard_anniversary="X-ANNIVERSARY:"$cur_data1$'\n'
;;
esac
;;
esac
prev_contact_id=$cur_contact_id
# reset Internal Field Separator for parent loop
IFS=`echo -e "\n\r"`
done
# set Internal Field Separator to other-than-newline prior to echoing final vcard
IFS="|"
# some contacts apps don't have IM fields; add to top of NOTE: field
if [ ${#cur_vcard_im_note} -ne 0 ]
then cur_vcard_note=$cur_vcard_im_note"\n"$cur_vcard_note
fi
# generate and echo vcard
if [ ${#cur_vcard_note} -ne 0 ]
then cur_vcard_note="NOTE:"$cur_vcard_note$'\n'
fi
cur_vcard=$cur_vcard$cur_vcard_fn$cur_vcard_n
cur_vcard=$cur_vcard$cur_vcard_nick$cur_vcard_org$cur_vcard_title$cur_vcard_tel
cur_vcard=$cur_vcard$cur_vcard_adr$cur_vcard_email$cur_vcard_url$cur_vcard_note
cur_vcard=$cur_vcard$cur_vcard_bday$cur_vcard_anniversary$cur_vcard_photo$cur_vcard_im
cur_vcard=$cur_vcard"END:VCARD"
echo $cur_vcard
# restore original Internal Field Separator
IFS=$ORIG_IFS