-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgandimail
executable file
·129 lines (99 loc) · 3.31 KB
/
gandimail
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
#!/usr/bin/env ruby
require 'xmlrpc/client'
require 'optparse'
class ZlibParserDecorator
def initialize(parser)
@parser = parser
end
def parseMethodResponse(responseText)
@parser.parseMethodResponse(Zlib::GzipReader.new(StringIO.new(responseText)).read)
end
def parseMethodCall(*args)
@parser.parseMethodCall(*args)
end
end
def main()
server = XMLRPC::Client.new2('https://rpc.gandi.net/xmlrpc/')
server.http_header_extra = { "Accept-Encoding" => "gzip" }
server.set_parser ZlibParserDecorator.new(server.send(:parser))
## 24-character API needs to be set as an environment variable
apikey = ENV['GANDI_API_KEY']
options = get_command_line_options
if options[:version]
api_version = server.call("version.info", apikey)
puts "Using API version #{api_version['api_version']}"
exit
elsif options[:list]
puts get_current_forwards(apikey, server, options)
exit
elsif options[:create]
new_forward = create_new_forward(apikey, server, options)
puts "Added new forward: #{new_forward}"
exit
elsif options[:delete]
del_forward = delete_existing_forward(apikey, server, options)
puts "Deleted forward: #{del_forward}"
exit
end
end
def delete_existing_forward(apikey, server, options)
source = options[:from]
return server.call("domain.forward.delete", apikey, options[:fqdn], source)
end
def create_new_forward(apikey, server, options)
source = options[:from]
dests = {'destinations' => [options[:to]]}
return server.call("domain.forward.create", apikey, options[:fqdn], source, dests)
end
def get_current_forwards(apikey, server, options)
return server.call("domain.forward.list", apikey, options[:fqdn])
end
def get_domains(apikey, server)
domain_list = server.call("domain.list", apikey)
fqdn_list = []
domain_list.each do |d|
fqdn_list << d["fqdn"]
end
return fqdn_list
end
def get_command_line_options
options = {}
# special case for personal use :)
# can (and should!) be overridden with -f "fqdn.com"
options[:fqdn] = "isaacdontjelindell.com"
opts = OptionParser.new do |opts|
opts.banner = "Usage: gandimail [options]"
opts.on("-v", "--version", "show Gandi API version") do
options[:version] = true
end
opts.on("-c", "--create 'FROM [email protected]'", "create a new forwarding address") do |s|
addresses = s.split
options[:create] = true
options[:from] = addresses[0]
options[:to] = addresses[1]
end
opts.on("-d", "--delete FROM", "delete an existing forwarding address") do |from|
options[:delete] = true
options[:from] = from
end
opts.on("-l", "--list", "list all existing forwards") do
options[:list] = true
end
opts.on("-f", "--fqdn FQDN", "specify the domain to administrate") do |fqdn|
options[:fqdn] = fqdn
end
opts.on_tail("-h", "--help", "show this message") do
puts opts
exit
end
end
begin
opts.parse!
rescue OptionParser::InvalidOption => e
puts e
puts opts
exit
end
return options
end
main()