Giter Club home page Giter Club logo

go-vcard's Introduction

go-vcard

Go Reference builds.sr.ht status

A Go library to parse and format vCard.

Usage

f, err := os.Open("cards.vcf")
if err != nil {
	log.Fatal(err)
}
defer f.Close()

dec := vcard.NewDecoder(f)
for {
	card, err := dec.Decode()
	if err == io.EOF {
		break
	} else if err != nil {
		log.Fatal(err)
	}

	log.Println(card.PreferredValue(vcard.FieldFormattedName))
}

License

MIT

go-vcard's People

Contributors

barisere avatar emersion avatar geish avatar googlom avatar gorthmohogany avatar ngocanh1909 avatar nilsocket avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-vcard's Issues

Decode and Encode to string

I've this string

Hasan Yousef
BEGIN:VCARD
VERSION:3.0
N:Yousef;Hasan;;;
FN:Hasan Yousef
item1.TEL;waid=966xxxxxxxxx:+966 xx xxx xxxx
item1.X-ABLabel:Mobile
END:VCARD
  1. How can I convert the above string to to vcard.Card, i.e. Encode string into vCard
  2. If I have the vcard.Card how can I convert it into string, i.e. Decode a vCard into string

`ENCODING=QUOTED-PRINTABLE` unsupported

Hi, first of all thanks for you lib.

I have exported my contacts from the default contact app on an android phone (lineagos 21 enchiladas) and each time a field have an accent or a non-ascii character the file with have its content formatted as follow:

TITLE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=4B=69=6E=C3=A9=73=69=74=68=C3=A9=72=61=70=65=75=74=65

For example this field TITLE field contains Kinésithérapeute as value. As you have probably guessed, when I parse it with you lib it will print directly the content without decoding.

The RFC6350 section 3.1 make a reference to the RFC3536 which have a reference to the QUOTED-PRINTABLE notation.

example error

example error:


func main() {
	destFile, err := os.Create(".\Contacts2.vcf")
	if err != nil {
		log.Fatal(err)
	}
	defer destFile.Close()

	// data in order: first name, middle name, last name, telephone number
	contacts := [][4]string{
		{"John", "Webber", "Maxwell", "(+1) 199 8714"},
		{"Donald", "", "Ron", "(+44) 421 8913"},
		{"Eric", "E.", "Peter", "(+37) 221 9903"},
		{"Nelson", "D.", "Patrick", "(+1) 122 8810"},
	}

	var (
		// card is a map of strings to []*vcard.Field objects
		card vcard.Card

		// destination where the vcard will be encoded to
		enc = vcard.NewEncoder(destFile)
	)

	for _, entry := range contacts {
		// set only the value of a field by using card.SetValue.
		// This does not set parameters
		card.SetValue(vcard.FieldFormattedName, strings.Join(entry[:3], " "))
		card.SetValue(vcard.FieldTelephone, entry[3])

		// set the value of a field and other parameters by using card.Set
		card.Set(vcard.FieldName, &vcard.Field{
			Value: strings.Join(entry[:3], ";"),
			Params: map[string][]string{
				vcard.ParamSortAs: []string{
					entry[0] + " " + entry[2],
				},
			},
		})

		// make the vCard version 4 compliant
		vcard.ToV4(card)
		err := enc.Encode(card)
		if err != nil {
			log.Fatal(err)
		}
	}
}

panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/emersion/go-vcard.Card.Set(...)
D:/gopath/src/github.com/emersion/go-vcard/card.go:118
github.com/emersion/go-vcard.Card.SetValue(...)
D:/gopath/src/github.com/emersion/go-vcard/card.go:166
main.main()
/vcard_cretor2.go:110 +0x3cd

How can I convert enc to string

I wrote the below that created vcard, but once I tried to read the vcard back to its string I failed:

package main

import (
	"io"
	"log"
	"strings"

	"github.com/emersion/go-vcard"
)

func main() {

	var content strings.Builder
	content.WriteString("BEGIN:VCARD\n")
	content.WriteString("VERSION:3.0\n")
	content.WriteString("FN:Hasan Yousef\n")
	content.WriteString("TEL:999\n")
	content.WriteString("Fitem1.TEL;waid=966000000000\n")
	content.WriteString("item1.X-ABLabel:Mobile\n")
	content.WriteString("END:VCARD")

	r := strings.NewReader(content.String())
	dec := vcard.NewDecoder(r)
	var card vcard.Card
	var err error
	for {
		card, err = dec.Decode()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}

		log.Println(card.PreferredValue(vcard.FieldFormattedName))
		log.Println(card.PreferredValue(vcard.FieldTelephone))
	}

	var enc vcard.Encoder

	// enc = vcard.NewEncoder(destFile)
	err = enc.Encode(card)
	if err != nil {
		log.Fatal(err)
	}

	// How can I convert enc to string!

	log.Println(&enc)

}

I got:

2022/05/07 12:44:47 Hasan Yousef
2022/05/07 12:44:47 999
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x10549d]

goroutine 1 [running]:
io.WriteString({0x0, 0x0}, {0x15c770, 0xd})
        D:/Development/go/src/io/io.go:314 +0x7d
github.com/emersion/go-vcard.(*Encoder).Encode(0xc00004a360, 0x3?)
        D:/Development/gopath/pkg/mod/github.com/emersion/[email protected]/encoder.go:23 +0x57
main.main()
        D:/Desktop/main.go:41 +0x77e
exit status 2

invalid BEGIN value

I tried:

package main

import (
	"io"
	"log"
	"strings"

	"github.com/emersion/go-vcard"
)

func main() {

	var content strings.Builder
	content.WriteString("BEGIN:VCARD")
	content.WriteString("VERSION:3.0")
	content.WriteString("FN:Hasan Yousef")
	content.WriteString("Fitem1.TEL;waid=966000000000")
	content.WriteString("item1.X-ABLabel:Mobile")
	content.WriteString("END:VCARD")

	//	byteData := bytes.Buffer{}
	//	byteData.Write([]byte(content.String()))
	//	dec := vcard.Decoder(buf)
	//	r := bytes.NewReader([]byte(content.String()))

	r := strings.NewReader(content.String())
	dec := vcard.NewDecoder(r)
	for {
		card, err := dec.Decode()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}

		log.Println(card.PreferredValue(vcard.FieldFormattedName))
	}
}

But got:

2022/05/07 10:18:24 vcard: invalid BEGIN value
exit status 1

Once I tried commenting it:

//	content.WriteString("BEGIN:VCARD")

I got:

2022/05/07 10:21:31 vcard: no BEGIN field found

Unable to read TEL:CEL values

Consider this VCF:

BEGIN:VCARD
VERSION:2.1
N:;Dev;;;
FN:Dev
TEL;CELL:+911234567890
TITLE:Developer
END:VCARD

On decoding, TEL params are empty.

Add cards and save cards example

Thanks for the library, I was able to see my contacts. Since, I am new to golang, and just wanted to see if it is possible that you provide a way to add a new contact and save all contacts in documentation.

Reading telephone number

I noticed telephone number is defined as TEL

FieldTelephone = "TEL"

And can be read as:

log.Println(card.PreferredValue(vcard.FieldTelephone))

But in my vcard, telephone number is defined as:

Fitem1.TEL;waid=966000000000

How can I read it?

RFC 6350

Hi! I was wondering if this lib is "RFC6350 friendly"? I mean does lib fully meets the specification?

Unable to use Field Geolocation

Hello,
I cannot get a same result as RFC describes for the Geolocation field!

RFC GEO Example:
GEO;TYPE=work:geo:46.772673,-71.282945

My result:
GEO;TYPE=work:geo:51.446570\,35.662524

go-vcard escapes the value and there is no way to use the actual chars in value.

The code:

card.Set(
		vcard.FieldGeolocation, &vcard.Field{
			Value: fmt.Sprintf("%f,%f", info.Location.X, info.Location.Y),
			Params: map[string][]string{
				vcard.ParamType: {vcard.TypeWork + ":geo"},
			},
			Group: "",
		},
	)

I was also getting GEO;TYPE=work;51.446570\,35.662524 which is why i used vcard.TypeWork + ":geo" as a value for ParamType

GetAllFields

I would love to access all Fields, so far you only can Get one(the preferred) value by calling Get.
You have access to all the Values, but that might not be sufficient: In my case, I want to have the details on all existing phone Numbers, so whether that phoneNumber is for Work, Home, Cell, etc.

So my simple suggestion:

// GetAllFields returns all fields of the card for the given property. If there is    
// no such field, it returns nil.  
func (c Card) GetAllFields(k string) []*Field {  
	fields := c[k]  
	if len(fields) == 0 {  
		return nil  
	}  
	return fields  
}

and on that you can extract all necessary information.
Or is there a better way to extract all phone Numbers with the details on them?

Parse Field.Value not possible with vcard 2.1 format parameters

When trying to test processing a vcf file exported using thunderbird, we encounter empty value for some fields, like email.

The vcard format is 2.1 and it appear that parameters are not encoded as key=value tuples as in format 3.0 and 4.0 they are. This make parseParam function of decoder.go parsing the Field.Value as a parameter value.

Example of format not parsed correctly :

begin:vcard
fn:someone
email;internet:[email protected]
version:2.1
end:vcard

Using this temporary solution I get back Field.Value filled correctly , but bypass completly any parameters:

https://gist.github.com/gdchamal/43c58f9d581cee05c40706692f852373

Yes I know vcard2.1 format should not exist anymore ;)

What do you think ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.