-
Notifications
You must be signed in to change notification settings - Fork 4
/
rtxmlsrv.rb
executable file
·300 lines (275 loc) · 8.24 KB
/
rtxmlsrv.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
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
#!/usr/bin/ruby
## XML RPC service to provide a cross-platform API for
## RT ticket creation/maintenance. Essentially just a wrapper
## around the rt/client library.
require "rubygems" # so we can load gems
require "rt_client/rt_client" # rt-client REST library
require "xmlrpc/server" # that's what we're doing
require "date" # for parsing arbitrary date formats
PORT=8080
MAX_CONN=50
# extend the Hash class to
# translate string keys into symbol keys
class Hash # :nodoc:
def remapkeys!
n = Hash.new
self.each_key do |key|
n[key.to_sym] = self[key]
end
self.replace(n)
n = nil
$stderr.puts self.map { |k,v| "#{k} => #{v}" }
self
end
end
class TicketSrv
def initialize
end
INTERFACE = XMLRPC::interface("rt") {
meth 'string add_watcher(struct)','Calls RT_Client::add_watcher'
meth 'array attachments(struct)','Calls RT_Client::attachments'
meth 'string comment(struct)','Calls RT_Client::comment'
meth 'string correspond(struct)','Calls RT_Client::correspond'
meth 'string create(struct)','Calls RT_Client::create'
meth 'string create_user(struct)','Calls RT_Client::create_user'
meth 'string edit(struct)','Calls RT_Client::edit'
meth 'string edit_or_create_user(struct)','Calls RT_Client::edit_or_create_user'
meth 'struct get_attachment(struct)','Calls RT_Client::get_attachment'
meth 'struct history(struct)','Calls RT_Client::history (long form)'
meth 'struct history_item(struct)','Calls RT_Client::history_item'
meth 'array list(struct)','Calls RT_Client::list'
meth 'array query(struct)','Calls RT_Client::query (long form)'
meth 'struct show(struct)','Calls RT_Client::show'
}
# Allows watchers to be added via RT_Client::add_watcher
# You need to pass :id, :addr, and optionally :type
def add_watcher(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.add_watcher(struct)
rt = nil
val
end
# Gets a list of attachments via RT_Client::attachments
# You need to pass :id, and optionally :unnamed
def attachments(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
rt = RT_Client.new
val = rt.attachments(struct)
rt = nil
val
end
# Adds comments to tickets via RT_Client::comment
def comment(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.comment(struct)
rt = nil
val
end
# Allows new tickets to be created via RT_Client::correspond
def correspond(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.correspond(struct)
rt = nil
val
end
# Allows new tickets to be created via RT_Client::create
def create(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.create(struct)
rt = nil
val
end
# Allows new users to be created via RT_Client::create_user
def create_user(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.create_user(struct)
rt = nil
val
end
# Find RT user details from email address via RT_Cleint::usersearch
def usersearch(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.usersearch(struct)
rt = nil
val
end
# Allows new users to be edited or created if they don't exist
def edit_or_create_user(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.edit_or_create_user(struct)
rt = nil
val
end
# Allows existing ticket to be modified via RT_Client::edit
def edit(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.edit(struct)
rt = nil
val
end
# Retrieves attachments via RT_Client::get_attachment
def get_attachment(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.get_attachment(struct)
rt = nil
val
end
# Gets the history of a ticket via RT_Client::history
def history(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.history(struct)
rt = nil
val
end
# Gets a single history item via RT_Client::history_item
def history_item(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.history_item(struct)
rt = nil
val
end
# Gets a list of tickets via RT_Client::list
def list(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.list(struct)
rt = nil
val
end
# Gets a list of tickets via RT_Client::query
def query(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.query(struct)
rt = nil
val
end
# Gets detail (minus history/attachments) via RT_Client::show
def show(struct)
struct.remapkeys!
if struct.has_key? :user and struct.has_key? :pass
rt = RT_Client.new(:user => struct[:user], :pass => struct[:pass])
struct.delete(:user)
struct.delete(:pass)
else
rt = RT_Client.new
end
val = rt.show(struct)
rt = nil
val
end
end # class TicketSrv
pid = fork do
Signal.trap('HUP','IGNORE')
# set up a log file
logfile = File.dirname(__FILE__) + "/ticketsrv.log"
accfile = File.dirname(__FILE__) + "/access.log"
acc = File.open(accfile,"a+")
$stderr.reopen acc # redirect $stderr to the log as well
# determine the IP address to listen on and create the server
sock = Socket.getaddrinfo(Socket.gethostname,PORT,Socket::AF_INET,Socket::SOCK_STREAM)
$s = XMLRPC::Server.new(sock[0][1], sock[0][3], MAX_CONN, logfile)
$s.set_parser(XMLRPC::XMLParser::XMLStreamParser.new)
$s.add_handler(TicketSrv::INTERFACE, TicketSrv.new)
$s.add_introspection
$s.serve # start serving
$stderr.reopen STDERR
acc.close
end
Process.detach(pid)