-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddalias.rb
executable file
·193 lines (143 loc) · 3.67 KB
/
addalias.rb
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
#!ruby
#
# script with various mailcow API utilities
#
# Axel Roest, 2023
require 'json'
require 'net/http'
load 'secrets.rb'
load 'aliases.rb'
def getMailboxes(domain)
if domain == "" then
domain = "demo.mailcow.email"
end
path = "/api/v1/get/mailbox/all/#{domain}"
url = $BaseURL + path
net = Net::HTTP.new($BaseURL, 443)
net.use_ssl = true
request = Net::HTTP::Get.new(path)
request['X-API-Key'] = $READKEY
headers =
{
'X-API-Key' => $READKEY
}
response = net.request(request)
puts response.body if response.is_a?(Net::HTTPSuccess)
end
def getAliases(id)
if id == "" then
id = "all"
end
path = "/api/v1/get/alias/#{id}"
url = $BaseURL + path
puts path
net = Net::HTTP.new($BaseURL, 443)
net.use_ssl = true
request = Net::HTTP::Get.new(path)
request['X-API-Key'] = $READKEY
response = net.request(request)
puts response.body if response.is_a?(Net::HTTPSuccess)
end
def getDomainAliases(mailbox)
if mailbox == "" then
mailbox = "all"
end
path = "/api/v1/get/alias/#{mailbox}"
url = $BaseURL + path
puts path
net = Net::HTTP.new($BaseURL, 443)
net.use_ssl = true
request = Net::HTTP::Get.new(path)
request['X-API-Key'] = $READKEY
response = net.request(request)
puts response.body if response.is_a?(Net::HTTPSuccess)
end
def addAlias(aliasEmail, mailbox)
if !aliasEmail || !mailbox then
puts "Error: aliasEmail or mailbox cannot be nil"
end
path = "/api/v1/add/alias"
url = $BaseURL + path
puts path
net = Net::HTTP.new($BaseURL, 443)
net.use_ssl = true
request = Net::HTTP::Post.new(path)
request['X-API-Key'] = $EDITKEY
body = {
"active": "1",
"address": aliasEmail,
"goto": mailbox
}.to_json
# jsonBody = JSON.generate(body)
request.body = body
request.content_type = 'application/json'
response = net.request(request)
puts response.body
end
def getBlacklist(domain)
path = "/api/v1/get/policy_bl_domain/#{domain}"
url = $BaseURL + path
puts path
net = Net::HTTP.new($BaseURL, 443)
net.use_ssl = true
request = Net::HTTP::Get.new(path)
request['X-API-Key'] = $READKEY
response = net.request(request)
puts response.body if response.is_a?(Net::HTTPSuccess)
end
def addBlacklist(domain, email)
if !domain || !email then
puts "Error: domain or email cannot be nil"
end
path = "/api/v1/add/domain-policy"
url = $BaseURL + path
puts path
net = Net::HTTP.new($BaseURL, 443)
net.use_ssl = true
request = Net::HTTP::Post.new(path)
request['X-API-Key'] = $EDITKEY
body = {
"domain": domain,
"object_from": email,
"object_list": "bl"
}.to_json
request.body = body
request.content_type = 'application/json'
response = net.request(request)
puts response.body
end
def addBlacklistList(domain, emails)
emails.each do |email|
addBlacklist(domain,email)
end
end
def addAliasesFromHash(aliasHash)
aliasHash.each do |address, mailboxes|
puts "#{address} -> #{mailboxes}"
addAlias(address, mailboxes)
sleep 0.2
end
end
def getDKIM(domain)
path = "/api/v1/get/dkim/#{domain}"
url = $BaseURL + path
puts path
net = Net::HTTP.new($BaseURL, 443)
net.use_ssl = true
request = Net::HTTP::Get.new(path)
request['X-API-Key'] = $READKEY
request['accept'] = "application/json"
response = net.request(request)
# puts response.body if response.is_a?(Net::HTTPSuccess)
record = JSON.parse(response.body)
puts record['dkim_txt']
end
getMailboxes(nil)
# getDomainAliases("all")
# addAlias("[email protected]", "[email protected]")
# this function adds the hash with aliases (from aliases.rb)
# addAliasesFromHash($aliases)
# getBlacklist("example.com")
# addBlacklist("example.com", "[email protected]")
# addBlacklistList("example.com", $emails)
# getDKIM("example.com")