Commit 97d929b9 authored by Philipp's avatar Philipp
Browse files

Initial commit

parents
#!/usr/bin/env lua5.3
-- Lua 5.3 is the minimum required version because of utf8.char()
-- By murks, 2021-04-09
-- SPDX-License-Identifier: GPL-3.0-or-later
-- Convert vcf to a simple format usable by SXMO
-- Written for vcf file generated by a 2008 Sony Ericsson phone, adjust as needed.
--[[
BEGIN:VCARD
VERSION:2.1
N:Sprachbox;
TEL;CELL:+431234567890
X-IRMC-LUID:00020000015D
END:VCARD
]]--
-- SXMO stores contacts in a file called contacts.tsv as described here:
-- https://git.sr.ht/~mil/sxmo-docs/tree/master/USERGUIDE.md#strongcalls-and-textingstrong
--[[
+122345628 John Smith
+128371642 Jeff Foo
+31612345678 Jan Janssen
]]--
-- prefix for Austria used as default, change this accordingly
PREFIX = "+43"
local infn = arg[1] -- input vcf file
local outfn = arg[2] -- you probably want to call it contacts.tsv
if not (infn and outfn) then
print "Read the bloody code, this is a hacky script after all."
os.exit()
end
-- open files
io.input(infn)
io.output(outfn)
-- this seems unnecessarily complicated
local entries = {}
local n = 0
local current = n
for line in io.lines() do
-- recognise VCARD entry
if line:match("^BEGIN:VCARD") then
n = n+1
current = n
entries[current] = {}
elseif line:match("^END:VCARD")
or line:match("^X%-IRMC%-LUID:")
or line:match("^VERSION:")
-- add to the list if you have more unwanted stuff
then
-- drop
else
-- remove '^M' (\r) from end of each line
line = line:sub(1, #line-1)
-- assign useful vcard lines to fields
table.insert(entries[current], line)
end
end
local function fix_name(v)
-- remove single word entries
v[1] = v[1]:gsub("^N:;", "")
-- remove crap
v[1] = v[1]:gsub("ENCODING=QUOTED%-PRINTABLE:", "")
-- reorder as my VCF contains "smart" lastname;firstname
-- please note that this may not solve every silly situation
v[1] = v[1]:gsub("N[:;]([%g%s]*);(%g*)", function (c1, c2)
return string.format("%s %s", c2, c1)
end)
-- trim spaces (from http://lua-users.org/wiki/CommonFunctions)
v[1] = v[1]:gsub("^%s*(.-)%s*$", "%1")
-- fix encoding only after reordering as there are issues with UTF-8
v[1] = v[1]:gsub("(=%x%x)", function (c)
-- strip prefix '='
local st = string.sub(c,2,#c);
-- convert to hex and UTF-8 character
return utf8.char(string.format("0x%s", st))
end)
end
local function fix_number(v)
-- get rid of crap
v[2] = v[2]:gsub("TEL;CELL:", "")
-- convert to '+43' prefix format
v[2] = v[2]:gsub("^00", "+")
-- assume a single 0 stands for the default PREFIX
v[2] = v[2]:gsub("^0", PREFIX)
-- assume no prefix also needs a PREFIX
v[2] = v[2]:gsub("^%d", function (c)
return string.format("%s%s", PREFIX, c)
end)
end
for _,v in ipairs(entries) do
fix_name(v)
fix_number(v)
-- write lines to output file in sxmo format
io.write(string.format("%s\t%s\n", v[2], v[1]))
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment