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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
// Package vcard implements the vCard format, defined in RFC 6350.
package vcard
import (
"strconv"
"strings"
"time"
)
// MIME type and file extension for VCard, defined in RFC 6350 section 10.1.
const (
MIMEType = "text/vcard"
Extension = "vcf"
)
const timestampLayout = "20060102T150405Z"
// Card property parameters.
const (
ParamLanguage = "LANGUAGE"
ParamValue = "VALUE"
ParamPreferred = "PREF"
ParamAltID = "ALTID"
ParamPID = "PID"
ParamType = "TYPE"
ParamMediaType = "MEDIATYPE"
ParamCalendarScale = "CALSCALE"
ParamSortAs = "SORT-AS"
ParamGeolocation = "GEO"
ParamTimezone = "TZ"
)
// Card properties.
const (
// General Properties
FieldSource = "SOURCE"
FieldKind = "KIND"
FieldXML = "XML"
// Identification Properties
FieldFormattedName = "FN"
FieldName = "N"
FieldNickname = "NICKNAME"
FieldPhoto = "PHOTO"
FieldBirthday = "BDAY"
FieldAnniversary = "ANNIVERSARY"
FieldGender = "GENDER"
// Delivery Addressing Properties
FieldAddress = "ADR"
// Communications Properties
FieldTelephone = "TEL"
FieldEmail = "EMAIL"
FieldIMPP = "IMPP" // Instant Messaging and Presence Protocol
FieldLanguage = "LANG"
// Geographical Properties
FieldTimezone = "TZ"
FieldGeolocation = "GEO"
// Organizational Properties
FieldTitle = "TITLE"
FieldRole = "ROLE"
FieldLogo = "LOGO"
FieldOrganization = "ORG"
FieldMember = "MEMBER"
FieldRelated = "RELATED"
// Explanatory Properties
FieldCategories = "CATEGORIES"
FieldNote = "NOTE"
FieldProductID = "PRODID"
FieldRevision = "REV"
FieldSound = "SOUND"
FieldUID = "UID"
FieldClientPIDMap = "CLIENTPIDMAP"
FieldURL = "URL"
FieldVersion = "VERSION"
// Security Properties
FieldKey = "KEY"
// Calendar Properties
FieldFreeOrBusyURL = "FBURL"
FieldCalendarAddressURI = "CALADRURI"
FieldCalendarURI = "CALURI"
)
func maybeGet(l []string, i int) string {
if i < len(l) {
return l[i]
}
return ""
}
// A Card is an address book entry.
type Card map[string][]*Field
// Get returns the first field of the card for the given property. If there is
// no such field, it returns nil.
func (c Card) Get(k string) *Field {
fields := c[k]
if len(fields) == 0 {
return nil
}
return fields[0]
}
// Add adds the k, f pair to the list of fields. It appends to any existing
// fields.
func (c Card) Add(k string, f *Field) {
c[k] = append(c[k], f)
}
// Set sets the key k to the single field f. It replaces any existing field.
func (c Card) Set(k string, f *Field) {
c[k] = []*Field{f}
}
// Preferred returns the preferred field of the card for the given property.
func (c Card) Preferred(k string) *Field {
fields := c[k]
if len(fields) == 0 {
return nil
}
field := fields[0]
min := 100
for _, f := range fields {
n := 100
if pref := f.Params.Get(ParamPreferred); pref != "" {
n, _ = strconv.Atoi(pref)
} else if f.Params.HasType("pref") {
// Apple Contacts adds "pref" to the TYPE param
n = 1
}
if n < min {
min = n
field = f
}
}
return field
}
// Value returns the first field value of the card for the given property. If
// there is no such field, it returns an empty string.
func (c Card) Value(k string) string {
f := c.Get(k)
if f == nil {
return ""
}
return f.Value
}
// AddValue adds the k, v pair to the list of field values. It appends to any
// existing values.
func (c Card) AddValue(k, v string) {
c.Add(k, &Field{Value: v})
}
// SetValue sets the field k to the single value v. It replaces any existing
// value.
func (c Card) SetValue(k, v string) {
c.Set(k, &Field{Value: v})
}
// PreferredValue returns the preferred field value of the card.
func (c Card) PreferredValue(k string) string {
f := c.Preferred(k)
if f == nil {
return ""
}
return f.Value
}
// Values returns a list of values for a given property.
func (c Card) Values(k string) []string {
fields := c[k]
if fields == nil {
return nil
}
values := make([]string, len(fields))
for i, f := range fields {
values[i] = f.Value
}
return values
}
// Kind returns the kind of the object represented by this card. If it isn't
// specified, it returns the default: KindIndividual.
func (c Card) Kind() Kind {
kind := strings.ToLower(c.Value(FieldKind))
if kind == "" {
return KindIndividual
}
return Kind(kind)
}
// SetKind sets the kind of the object represented by this card.
func (c Card) SetKind(kind Kind) {
c.SetValue(FieldKind, string(kind))
}
// FormattedNames returns formatted names of the card. The length of the result
// is always greater or equal to 1.
func (c Card) FormattedNames() []*Field {
fns := c[FieldFormattedName]
if len(fns) == 0 {
return []*Field{{Value: ""}}
}
return fns
}
// Names returns names of the card.
func (c Card) Names() []*Name {
ns := c[FieldName]
if ns == nil {
return nil
}
names := make([]*Name, len(ns))
for i, n := range ns {
names[i] = newName(n)
}
return names
}
// Name returns the preferred name of the card. If it isn't specified, it
// returns nil.
func (c Card) Name() *Name {
n := c.Preferred(FieldName)
if n == nil {
return nil
}
return newName(n)
}
// AddName adds the specified name to the list of names.
func (c Card) AddName(name *Name) {
c.Add(FieldName, name.field())
}
// SetName replaces the list of names with the single specified name.
func (c Card) SetName(name *Name) {
c.Set(FieldName, name.field())
}
// Gender returns this card's gender.
func (c Card) Gender() (sex Sex, identity string) {
v := c.Value(FieldGender)
parts := strings.SplitN(v, ";", 2)
return Sex(strings.ToUpper(parts[0])), maybeGet(parts, 1)
}
// SetGender sets this card's gender.
func (c Card) SetGender(sex Sex, identity string) {
v := string(sex)
if identity != "" {
v += ";" + identity
}
c.SetValue(FieldGender, v)
}
// Addresses returns addresses of the card.
func (c Card) Addresses() []*Address {
adrs := c[FieldAddress]
if adrs == nil {
return nil
}
addresses := make([]*Address, len(adrs))
for i, adr := range adrs {
addresses[i] = newAddress(adr)
}
return addresses
}
// Address returns the preferred address of the card. If it isn't specified, it
// returns nil.
func (c Card) Address() *Address {
adr := c.Preferred(FieldAddress)
if adr == nil {
return nil
}
return newAddress(adr)
}
// AddAddress adds an address to the list of addresses.
func (c Card) AddAddress(address *Address) {
c.Add(FieldAddress, address.field())
}
// SetAddress replaces the list of addresses with the single specified address.
func (c Card) SetAddress(address *Address) {
c.Set(FieldAddress, address.field())
}
// Categories returns category information about the card, also known as "tags".
func (c Card) Categories() []string {
return strings.Split(c.PreferredValue(FieldCategories), ",")
}
// SetCategories sets category information about the card.
func (c Card) SetCategories(categories []string) {
c.SetValue(FieldCategories, strings.Join(categories, ","))
}
// Revision returns revision information about the current card.
func (c Card) Revision() (time.Time, error) {
rev := c.Value(FieldRevision)
if rev == "" {
return time.Time{}, nil
}
return time.Parse(timestampLayout, rev)
}
// SetRevision sets revision information about the current card.
func (c Card) SetRevision(t time.Time) {
c.SetValue(FieldRevision, t.Format(timestampLayout))
}
// A field contains a value and some parameters.
type Field struct {
Value string
Params Params
Group string
}
// Params is a set of field parameters.
type Params map[string][]string
// Get returns the first value with the key k. It returns an empty string if
// there is no such value.
func (p Params) Get(k string) string {
values := p[k]
if len(values) == 0 {
return ""
}
return values[0]
}
// Add adds the k, v pair to the list of parameters. It appends to any existing
// values.
func (p Params) Add(k, v string) {
p[k] = append(p[k], v)
}
// Set sets the parameter k to the single value v. It replaces any existing
// value.
func (p Params) Set(k, v string) {
p[k] = []string{v}
}
// Types returns the field types.
func (p Params) Types() []string {
types := p[ParamType]
list := make([]string, len(types))
for i, t := range types {
list[i] = strings.ToLower(t)
}
return list
}
// HasType returns true if and only if the field have the provided type.
func (p Params) HasType(t string) bool {
for _, tt := range p[ParamType] {
if strings.EqualFold(t, tt) {
return true
}
}
return false
}
// Kind is an object's kind.
type Kind string
// Values for FieldKind.
const (
KindIndividual Kind = "individual"
KindGroup Kind = "group"
KindOrganization Kind = "org"
KindLocation Kind = "location"
)
// Values for ParamType.
const (
// Generic
TypeHome = "home"
TypeWork = "work"
// For FieldTelephone
TypeText = "text"
TypeVoice = "voice" // Default
TypeFax = "fax"
TypeCell = "cell"
TypeVideo = "video"
TypePager = "pager"
TypeTextPhone = "textphone"
// For FieldRelated
TypeContact = "contact"
TypeAcquaintance = "acquaintance"
TypeFriend = "friend"
TypeMet = "met"
TypeCoWorker = "co-worker"
TypeColleague = "colleague"
TypeCoResident = "co-resident"
TypeNeighbor = "neighbor"
TypeChild = "child"
TypeParent = "parent"
TypeSibling = "sibling"
TypeSpouse = "spouse"
TypeKin = "kin"
TypeMuse = "muse"
TypeCrush = "crush"
TypeDate = "date"
TypeSweetheart = "sweetheart"
TypeMe = "me"
TypeAgent = "agent"
TypeEmergency = "emergency"
)
// Name contains an object's name components.
type Name struct {
*Field
FamilyName string
GivenName string
AdditionalName string
HonorificPrefix string
HonorificSuffix string
}
func newName(field *Field) *Name {
components := strings.Split(field.Value, ";")
return &Name{
field,
maybeGet(components, 0),
maybeGet(components, 1),
maybeGet(components, 2),
maybeGet(components, 3),
maybeGet(components, 4),
}
}
func (n *Name) field() *Field {
if n.Field == nil {
n.Field = new(Field)
}
n.Field.Value = strings.Join([]string{
n.FamilyName,
n.GivenName,
n.AdditionalName,
n.HonorificPrefix,
n.HonorificSuffix,
}, ";")
return n.Field
}
// Sex is an object's biological sex.
type Sex string
const (
SexUnspecified Sex = ""
SexFemale Sex = "F"
SexMale Sex = "M"
SexOther Sex = "O"
SexNone Sex = "N"
SexUnknown Sex = "U"
)
// An Address is a delivery address.
type Address struct {
*Field
PostOfficeBox string
ExtendedAddress string // e.g., apartment or suite number
StreetAddress string
Locality string // e.g., city
Region string // e.g., state or province
PostalCode string
Country string
}
func newAddress(field *Field) *Address {
components := strings.Split(field.Value, ";")
return &Address{
field,
maybeGet(components, 0),
maybeGet(components, 1),
maybeGet(components, 2),
maybeGet(components, 3),
maybeGet(components, 4),
maybeGet(components, 5),
maybeGet(components, 6),
}
}
func (a *Address) field() *Field {
if a.Field == nil {
a.Field = new(Field)
}
a.Field.Value = strings.Join([]string{
a.PostOfficeBox,
a.ExtendedAddress,
a.StreetAddress,
a.Locality,
a.Region,
a.PostalCode,
a.Country,
}, ";")
return a.Field
}
|