forked from phin1x/go-ipp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cups-client.go
271 lines (205 loc) · 8.16 KB
/
cups-client.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
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
package ipp
import "strings"
type CUPSClient struct {
*IPPClient
}
func NewCUPSClient(host string, port int, username, password string, useTLS bool) *CUPSClient {
ippClient := NewIPPClient(host, port, username, password, useTLS)
return &CUPSClient{ippClient}
}
func (c *CUPSClient) GetDevices() (map[string]Attributes, error) {
req := NewRequest(OperationCupsGetDevices, 1)
resp, err := c.SendRequest(c.getHttpUri("", nil), req)
if err != nil {
return nil, err
}
printerNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers {
printerNameMap[printerAttributes["device-uri"][0].Value.(string)] = printerAttributes
}
return printerNameMap, nil
}
func (c *CUPSClient) MoveJob(jobID int, destPrinter string) error {
req := NewRequest(OperationCupsMoveJob, 1)
req.OperationAttributes["job-uri"] = c.getJobUri(jobID)
req.PrinterAttributes["job-printer-uri"] = c.getPrinterUri(destPrinter)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
return err
}
func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error {
req := NewRequest(OperationCupsMoveJob, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(srcPrinter)
req.PrinterAttributes["job-printer-uri"] = c.getPrinterUri(destPrinter)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
return err
}
func (c *CUPSClient) GetPPDs() (map[string]Attributes, error) {
req := NewRequest(OperationCupsGetPpds, 1)
resp, err := c.SendRequest(c.getHttpUri("", nil), req)
if err != nil {
return nil, err
}
ppdNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers {
ppdNameMap[printerAttributes["ppd-name"][0].Value.(string)] = printerAttributes
}
return ppdNameMap, nil
}
func (c *CUPSClient) AcceptJobs(printer string) error {
req := NewRequest(OperationCupsAcceptJobs, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) RejectJobs(printer string) error {
req := NewRequest(OperationCupsRejectJobs, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) AddPrinterToClass(class, printer string) error {
attributes, err := c.GetPrinterAttributes(class, []string{"member-uris"})
if err != nil && !IsNotExistsError(err) {
return err
}
memberURIList := make([]string, 0)
if !IsNotExistsError(err) {
for _, member := range attributes["member-uris"] {
memberString := strings.Split(member.Value.(string), "/")
printerName := memberString[len(memberString)-1]
if printerName == printer {
return nil
}
memberURIList = append(memberURIList, member.Value.(string))
}
}
memberURIList = append(memberURIList, c.getPrinterUri(printer))
req := NewRequest(OperationCupsAddModifyClass, 1)
req.OperationAttributes["printer-uri"] = c.getClassUri(class)
req.PrinterAttributes["member-uris"] = memberURIList
_, err = c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error {
attributes, err := c.GetPrinterAttributes(class, []string{"member-uris"})
if err != nil {
return err
}
memberURIList := make([]string, 0)
for _, member := range attributes["member-uris"] {
memberString := strings.Split(member.Value.(string), "/")
printerName := memberString[len(memberString)-1]
if printerName != printer {
memberURIList = append(memberURIList, member.Value.(string))
}
}
if len(memberURIList) == 0 {
return c.DeleteClass(class)
}
req := NewRequest(OperationCupsAddModifyClass, 1)
req.OperationAttributes["printer-uri"] = c.getClassUri(class)
req.PrinterAttributes["member-uris"] = memberURIList
_, err = c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) DeleteClass(class string) error {
req := NewRequest(OperationCupsDeleteClass, 1)
req.OperationAttributes["printer-uri"] = c.getClassUri(class)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, errorPolicy ErrorPolicy, information, location string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(name)
req.OperationAttributes["ppd-name"] = ppd
req.OperationAttributes["printer-is-shared"] = shared
req.PrinterAttributes["printer-state-reason"] = "none"
req.PrinterAttributes["device-uri"] = deviceURI
req.PrinterAttributes["printer-info"] = information
req.PrinterAttributes["printer-location"] = location
req.PrinterAttributes["printer-error-policy"] = string(errorPolicy)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) SetPrinterPPD(printer, ppd string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
req.OperationAttributes["ppd-name"] = ppd
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) SetPrinterDeviceURI(printer, deviceURI string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
req.PrinterAttributes["device-uri"] = deviceURI
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
req.OperationAttributes["printer-is-shared"] = shared
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy ErrorPolicy) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
req.PrinterAttributes["printer-error-policy"] = string(errorPolicy)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) SetPrinterInformation(printer, information string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
req.PrinterAttributes["printer-info"] = information
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) SetPrinterLocation(printer, location string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
req.PrinterAttributes["printer-location"] = location
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) DeletePrinter(printer string) error {
req := NewRequest(OperationCupsDeletePrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err
}
func (c *CUPSClient) GetPrinters(attributes []string) (map[string]Attributes, error) {
req := NewRequest(OperationCupsGetPrinters, 1)
if attributes == nil {
req.OperationAttributes["requested-attributes"] = DefaultPrinterAttributes
} else {
req.OperationAttributes["requested-attributes"] = append(attributes, "printer-name")
}
resp, err := c.SendRequest(c.getHttpUri("", nil), req)
if err != nil {
return nil, err
}
printerNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers {
printerNameMap[printerAttributes["printer-name"][0].Value.(string)] = printerAttributes
}
return printerNameMap, nil
}
func (c *CUPSClient) GetClasses(attributes []string) (map[string]Attributes, error) {
req := NewRequest(OperationCupsGetClasses, 1)
if attributes == nil {
req.OperationAttributes["requested-attributes"] = DefaultClassAttributes
} else {
req.OperationAttributes["requested-attributes"] = append(attributes, "printer-name")
}
resp, err := c.SendRequest(c.getHttpUri("", nil), req)
if err != nil {
return nil, err
}
printerNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers {
printerNameMap[printerAttributes["printer-name"][0].Value.(string)] = printerAttributes
}
return printerNameMap, nil
}