-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistry_win.go
195 lines (157 loc) · 4.29 KB
/
registry_win.go
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
// +build windows
package main
import (
"strconv"
"strings"
"golang.org/x/sys/windows/registry"
)
func setSignature(signature, style, profile string, setforall int) error {
outlookVersion, _ := getOutlookVersion()
if outlookVersion > 12 {
name := ""
if style == "new" {
name = "New Signature"
} else if style == "reply" {
name = "Reply-Forward Signature"
}
profiles := []string{}
if profile != "" {
profiles = append(profiles, profile)
} else if setforall == 1 {
outlookProfiles, _ := getOutlookProfiles(outlookVersion)
for _, op := range outlookProfiles {
profiles = append(profiles, op)
}
} else {
defaultProfile, _ := getOutlookDefaultProfile(outlookVersion)
profiles = append(profiles, defaultProfile)
}
for _, p := range profiles {
subkeys, err := getAccountSubkeys(outlookVersion, p)
if err != nil {
return err
}
for _, sk := range subkeys {
setSignatureValue(outlookVersion, sk, name, signature)
}
}
}
return nil
}
func getAccountSubkeys(outlookVersion int, profile string) ([]string, error) {
key := ""
if outlookVersion > 14 {
key = `Software\Microsoft\Office\` + strconv.Itoa(outlookVersion) + `.0\Outlook\Profiles\` + profile + `\9375CFF0413111d3B88A00104B2A6676`
} else if outlookVersion > 12 {
key = `Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\` + profile + `\9375CFF0413111d3B88A00104B2A6676`
}
k, err := registry.OpenKey(registry.CURRENT_USER, key, registry.READ)
if err != nil {
return nil, err
}
defer k.Close()
keystats, err := k.Stat()
if err != nil {
return nil, err
}
subkeycount := int(keystats.SubKeyCount)
subkeys, err := k.ReadSubKeyNames(subkeycount)
if err != nil {
return nil, err
}
accountSubkeys := []string{}
for _, subkey := range subkeys {
sk, err := registry.OpenKey(registry.CURRENT_USER, key+`\`+subkey, registry.READ)
if err != nil {
return nil, err
}
defer sk.Close()
mapiprovider, _, err := sk.GetIntegerValue("MAPI Provider")
if mapiprovider != 2 && mapiprovider != 4 {
accountSubkeys = append(accountSubkeys, key+`\`+subkey)
}
}
return accountSubkeys, nil
}
func setSignatureValue(outlookVersion int, key, name, value string) error {
k, err := registry.OpenKey(registry.CURRENT_USER, key, registry.SET_VALUE)
if err != nil {
return err
}
defer k.Close()
if outlookVersion > 14 {
err = k.SetStringValue(name, value)
if err != nil {
return err
}
} else if outlookVersion > 12 {
msValue := ""
for _, char := range msValue {
msValue += string(char) + "\x00"
}
msValue += "\x00\x00"
err = k.SetBinaryValue(name, []byte(msValue))
if err != nil {
return err
}
}
return nil
}
func getOutlookVersion() (int, error) {
k, err := registry.OpenKey(registry.CLASSES_ROOT, `Outlook.Application\CurVer`, registry.QUERY_VALUE)
if err != nil {
return 0, err
}
defer k.Close()
s, _, err := k.GetStringValue("")
if err != nil {
return 0, err
}
ssplit := strings.Split(s, ".")
version, err := strconv.Atoi(ssplit[len(ssplit)-1])
if err != nil {
return 0, err
}
return version, nil
}
func getOutlookDefaultProfile(outlookVersion int) (string, error) {
key := ""
if outlookVersion > 14 {
key = `Software\Microsoft\Office\` + strconv.Itoa(outlookVersion) + `.0\Outlook`
} else if outlookVersion > 12 {
key = `Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles`
}
k, err := registry.OpenKey(registry.CURRENT_USER, key, registry.QUERY_VALUE)
if err != nil {
return "", err
}
defer k.Close()
profile, _, err := k.GetStringValue("DefaultProfile")
if err != nil {
return "", err
}
return profile, nil
}
func getOutlookProfiles(outlookVersion int) ([]string, error) {
key := ""
if outlookVersion > 14 {
key = `Software\Microsoft\Office\` + strconv.Itoa(outlookVersion) + `.0\Outlook\Profiles`
} else if outlookVersion > 12 {
key = `Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles`
}
k, err := registry.OpenKey(registry.CURRENT_USER, key, registry.READ)
if err != nil {
return nil, err
}
defer k.Close()
keystats, err := k.Stat()
if err != nil {
return nil, err
}
subkeycount := int(keystats.SubKeyCount)
profiles, err := k.ReadSubKeyNames(subkeycount)
if err != nil {
return nil, err
}
return profiles, nil
}