func parseVCards(vcardContacts []string) ([]textsecure.Contact, error) { country := defaultCountry() // for now allocate space for 3 phones for each contact. // FIXME: make it cleaner by using up only as much space as needed. contacts := make([]textsecure.Contact, len(vcardContacts)*3) i := 0 for _, c := range vcardContacts { di := vcard.NewDirectoryInfoReader(strings.NewReader(c)) vc := &vcard.VCard{} vc.ReadFrom(di) for t := 0; t < len(vc.Telephones); t++ { contacts[i].Name = vc.FormattedName contacts[i].Tel = formatE164(vc.Telephones[t].Number, country) if vc.Photo.Data != "" { b, err := base64.StdEncoding.DecodeString(vc.Photo.Data) if err == nil { contacts[i].Photo = string(b) } else { log.Printf("Parsing VCard %d %s\n", i, err.Error()) } } i++ } } return contacts[:i], nil }
func phoneFromVCardFile(file string) (string, error) { r, err := os.Open(file) if err != nil { return "", err } defer r.Close() di := vcard.NewDirectoryInfoReader(r) vc := &vcard.VCard{} vc.ReadFrom(di) if len(vc.Telephones) > 0 { return vc.Telephones[0].Number, nil } return "", errors.New("No phone number for contact.") }
func main() { var output io.Writer outputFilename := flag.String("o", "", "Output vcard file") flag.Parse() if *outputFilename == "" { output = os.Stdout } else { file, err := os.Create(*outputFilename) bufoutput := bufio.NewWriter(file) output = bufoutput defer file.Close() defer bufoutput.Flush() if err != nil { log.Printf("Can't create %s\n", *outputFilename) return } } var args []string if len(flag.Args()) > 0 { args = flag.Args() } else { args = []string{"contacts.vcf"} } var addressBook vcard.AddressBook for _, abpath := range args { f, err := os.Open(abpath) defer f.Close() if err != nil { log.Printf("Can't read file %s\n", abpath) return } reader := vcard.NewDirectoryInfoReader(f) addressBook.ReadFrom(reader) log.Printf("Read %s\n", abpath) } /*switchFamilyNamesGivenName(&addressBook) integrateAdditionalName(&addressBook) mobilePhone(&addressBook) removeAdditionalName(&addressBook) */ capitalize(&addressBook) writer := vcard.NewDirectoryInfoWriter(output) addressBook.WriteTo(writer) log.Printf("Write %s\n", *outputFilename) }