vcf2fritzbox/vcf2fritzbox.go

181 lines
4.0 KiB
Go

package main
import (
"bufio"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"mime/quotedprintable"
"os"
"strings"
"time"
)
func optExit(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}
func unfold(s string) string {
replacer := strings.NewReplacer("\r\n ", "", "\r\n\t", "")
return replacer.Replace(s)
}
func vCardValue(line string) string {
start := strings.LastIndex(line, ":")
end := strings.LastIndex(line, ";")
if start == -1 {
return ""
}
if end < start {
end = len(line)
}
return line[start+1 : end]
}
type Contact struct {
Name string
Numbers NumberSet
}
type ContactSet []Contact
var uniqueID int = 1741
const xmlPreamble = `<?xml version="1.0" encoding="utf-8"?>
<phonebooks>
<phonebook>
<contact><category /><person><realName>AVM Ansage (HD)</realName></person><telephony
nid="1"><number prio="1" type="work" quickdial="99" id="0">500@hd-telefonie.avm.de</number></telephony><services /><setup /><uniqueid>0</uniqueid></contact><contact><category /><person><realName>HD-Musik</realName></person><telephony
nid="1"><number prio="1" type="home" quickdial="97" id="0">200@hd-telefonie.avm.de</number></telephony><services /><setup /><uniqueid>1</uniqueid></contact><contact><category /><person><realName>HD-Sprache</realName></person><telephony
nid="1"><number prio="1" type="home" quickdial="98" id="0">100@hd-telefonie.avm.de</number></telephony><services /><setup /><uniqueid>2</uniqueid></contact>
`
const xmlEnd = `</phonebook>
</phonebooks>
`
func (c ContactSet) ToFritzboxXML() string {
var s strings.Builder
s.WriteString(xmlPreamble)
for _, contact := range c {
contact.WriteFritzboxXML(&s)
}
s.WriteString(xmlEnd)
return s.String()
}
func (c *Contact) WriteFritzboxXML(s io.Writer) {
if c == nil {
return
}
uniqueID++
fmt.Fprint(s, "<contact><category>0</category><person><realName>")
xml.Escape(s, []byte(c.Name))
fmt.Fprintf(s, "</realName></person><telephony nid=\"%d\">\n", len(c.Numbers))
c.Numbers.WriteFritzboxXML(s)
fmt.Fprintf(s, "</telephony><services /><setup /><uniqueid>%d</uniqueid></contact>\n\n", uniqueID)
}
func (c *Contact) setName(line string) {
if c == nil {
c = &Contact{}
}
c.Name = vCardValue(line)
if strings.Count(c.Name, "=") > 1 {
tmp, err := ioutil.ReadAll(quotedprintable.NewReader(strings.NewReader(c.Name)))
if err == nil {
c.Name = string(tmp)
}
}
}
func (c *Contact) addNumber(line string) {
if c == nil {
c = &Contact{}
}
tolower := strings.ToLower(line)
val := vCardValue(line)
var n Number
if strings.Contains(tolower, "cell") {
n = Number{val, "mobile"}
} else {
n = Number{val, "home"}
}
c.Numbers = append(c.Numbers, n)
}
func (c *Contact) setValue(line string) {
if strings.HasPrefix(line, "FN") {
c.setName(line)
} else if strings.HasPrefix(line, "TEL") {
c.addNumber(line)
}
}
type Number struct {
Value string
Kind string
}
type NumberSet []Number
func (n NumberSet) WriteFritzboxXML(s io.Writer) {
for i, number := range n {
number.WriteFritzboxXML(s, i)
}
}
func (n *Number) WriteFritzboxXML(s io.Writer, i int) {
if n == nil {
return
}
var prio int
if i == 0 {
prio = 1
} else {
prio = 0
}
fmt.Fprintf(s, `<number type="%s" prio="%d" id="%d">%s</number>`, n.Kind, prio, i, n.Value)
fmt.Fprint(s, "\n")
}
func GetContacts(buf []byte) (c ContactSet) {
s := unfold(string(buf))
scanner := bufio.NewScanner(strings.NewReader(s))
var current *Contact
for scanner.Scan() {
l := strings.TrimSpace(scanner.Text())
if l == "BEGIN:VCARD" {
current = &Contact{}
} else if l == "END:VCARD" {
c = append(c, *current)
current = nil
} else {
current.setValue(l)
}
}
optExit(scanner.Err())
if current != nil {
c = append(c, *current)
}
return
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "VCF-FILE")
os.Exit(-1)
}
b, err := ioutil.ReadFile(os.Args[1])
optExit(err)
outfile := os.Args[1] + "." + time.Now().Format("20060102-150405") + ".xml"
err = ioutil.WriteFile(outfile, []byte(GetContacts(b).ToFritzboxXML()), 0644)
optExit(err)
fmt.Println(outfile)
}