~psic4t/qcard

qcard/parse.go -rw-r--r-- 5.4 KiB
0fa8bcd0psic4t remove binary a month ago
                                                                                
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
package main

import (
	"fmt"
	"regexp"
	"strings"
)

func trimField(field, cutset string) string {
	re, _ := regexp.Compile(cutset)
	cutsetRem := re.ReplaceAllString(field, "")
	return strings.TrimRight(cutsetRem, "\r\n")
}

func parseContactFullName(contactData *string) string {
	re, _ := regexp.Compile(`\nFN:.*\n`)
	result := re.FindString(*contactData)
	return trimField(result, "\nFN:")
}

func parseContactName(contactData *string) string {
	re, _ := regexp.Compile(`\nN:.*?\n`)
	result := re.FindString(*contactData)
	result = strings.Replace(result, ";;;", "", -1) // remove triple semicola
	return trimField(result, "\nN:")
}

func parseContactPhoneCell(contactData *string) string {
	re, _ := regexp.Compile(`(?i)TEL;TYPE=CELL:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)TEL;TYPE=CELL:")
}

func parseContactPhoneHome(contactData *string) string {
	re, _ := regexp.Compile(`(?i)TEL;TYPE=HOME:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)TEL;TYPE=HOME:")
}

func parseContactPhoneWork(contactData *string) string {
	re, _ := regexp.Compile(`(?i)TEL;TYPE=WORK:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)TEL;TYPE=WORK:")
}

func parseContactEmail(contactData *string) string {
	var emailType string
	re, _ := regexp.Compile(`(?i)EMAIL(;TYPE=(.*))?:.*?\n`)
	parts := re.FindStringSubmatch(*contactData)

	if len(parts) > 1 {
		types := strings.Split(parts[2], ",")
		for _, i := range types {
			//fmt.Println(i)
			switch strings.ToLower(i) {
			case "internet":
				emailType = "home"
			case "home":
				emailType = "home"
			case "pref":
				emailType = "home"
			case "work":
				emailType = "work"
			}
		}
	}

	fmt.Println(emailType)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)EMAIL(;TYPE=(.*))?:")
}

func parseContactEmailHome(contactData *string) string {
	/*workre, _ := regexp.Compile(`(?i)EMAIL;TYPE=(.*WORK.*):.*?\n`) // check, if work email
	if workre.FindString(*contactData) != "" {
		fmt.Println("yes")
		return "" // if work email, get out
	}*/
	re, _ := regexp.Compile(`(?i)EMAIL(;TYPE=(HOME|INTERNET|PREF|INTERNET,HOME|HOME,INTERNET))?:.*?\n`)
	//re, _ := regexp.Compile(`(?i)EMAIL(;TYPE=(.*))?:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)EMAIL(;TYPE=(.*))?:")

}

func parseContactEmailWork(contactData *string) string {
	//re, _ := regexp.Compile(`(?i)EMAIL;TYPE=(WORK|INTERNET,WORK):.*?\n`)
	re, _ := regexp.Compile(`(?i)EMAIL;TYPE=(.*WORK.*):.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)EMAIL;TYPE=(WORK|INTERNET,WORK):")
}

func parseContactAddressHome(contactData *string) string {
	re, _ := regexp.Compile(`(?i)ADR;TYPE=HOME:.*?\n`)
	result := re.FindString(*contactData)
	result = strings.Replace(result, ";;;", "", -1) // remove triple semicola
	return trimField(result, "(?i)ADR;TYPE=HOME:")
}

func parseContactAddressWork(contactData *string) string {
	re, _ := regexp.Compile(`(?i)ADR;TYPE=WORK:.*?\n`)
	result := re.FindString(*contactData)
	result = strings.Replace(result, ";;;", "", -1) // remove triple semicola
	return trimField(result, "(?i)ADR;TYPE=WORK:")
}

func parseContactBirthday(contactData *string) string {
	re, _ := regexp.Compile(`(?i)BDAY:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)BDAY:")
}

func parseContactNote(contactData *string) string {
	re, _ := regexp.Compile(`(?i)NOTE:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)NOTE:")
}

func parseContactOrg(contactData *string) string {
	re, _ := regexp.Compile(`(?i)ORG:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)ORG:")
}

func parseContactTitle(contactData *string) string {
	re, _ := regexp.Compile(`(?i)TITLE:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)TITLE:")
}

func parseContactRole(contactData *string) string {
	re, _ := regexp.Compile(`(?i)ROLE:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)ROLE:")
}

func parseContactNickname(contactData *string) string {
	re, _ := regexp.Compile(`(?i)NICKNAME:.*?\n`)
	result := re.FindString(*contactData)
	return trimField(result, "(?i)NICKNAME:")
}

func parseMain(contactData *string, contactsSlice *[]contactStruct, href, color string) {
	//fmt.Println(parseContactName(contactData))
	fullName := parseContactFullName(contactData)
	organisation := parseContactOrg(contactData)

	//if flagset["so"] {
	// TODO: How to filter with name and org?
	if (filter == "") || ((filterMatch(fullName) == true) || (filterOrgMatch(organisation) == true)) {
		data := contactStruct{
			Href:         href,
			Color:        color,
			fullName:     fullName,
			name:         parseContactName(contactData),
			title:        parseContactTitle(contactData),
			role:         parseContactRole(contactData),
			organisation: organisation,
			phoneCell:    parseContactPhoneCell(contactData),
			phoneHome:    parseContactPhoneHome(contactData),
			phoneWork:    parseContactPhoneWork(contactData),
			emailHome:    parseContactEmailHome(contactData),
			emailWork:    parseContactEmailWork(contactData),
			addressHome:  parseContactAddressHome(contactData),
			addressWork:  parseContactAddressWork(contactData),
			birthday:     parseContactBirthday(contactData),
			nickname:     parseContactNickname(contactData),
			note:         parseContactNote(contactData),
		}
		*contactsSlice = append(*contactsSlice, data)
	}
}