2019-03-10 17:43:47 +00:00
|
|
|
package ipp
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
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 {
|
2019-06-08 22:03:50 +00:00
|
|
|
printerNameMap[printerAttributes[PrinterAttributeDeviceURI][0].Value.(string)] = printerAttributes
|
2019-03-15 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return printerNameMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) MoveJob(jobID int, destPrinter string) error {
|
|
|
|
req := NewRequest(OperationCupsMoveJob, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributeJobURI] = c.getJobUri(jobID)
|
|
|
|
req.PrinterAttributes[PrinterAttributeJobPrinterURI] = c.getPrinterUri(destPrinter)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error {
|
|
|
|
req := NewRequest(OperationCupsMoveJob, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(srcPrinter)
|
|
|
|
req.PrinterAttributes[PrinterAttributeJobPrinterURI] = c.getPrinterUri(destPrinter)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, 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 {
|
2019-06-08 22:03:50 +00:00
|
|
|
ppdNameMap[printerAttributes[OperationAttributePPDName][0].Value.(string)] = printerAttributes
|
2019-03-15 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ppdNameMap, nil
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:40:08 +00:00
|
|
|
func (c *CUPSClient) AcceptJobs(printer string) error {
|
2019-03-15 15:20:02 +00:00
|
|
|
req := NewRequest(OperationCupsAcceptJobs, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:40:08 +00:00
|
|
|
func (c *CUPSClient) RejectJobs(printer string) error {
|
2019-03-15 15:20:02 +00:00
|
|
|
req := NewRequest(OperationCupsRejectJobs, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) AddPrinterToClass(class, printer string) error {
|
2019-06-08 22:03:50 +00:00
|
|
|
attributes, err := c.GetPrinterAttributes(class, []string{PrinterAttributeMemberURIs})
|
2019-03-17 12:40:08 +00:00
|
|
|
if err != nil && !IsNotExistsError(err) {
|
2019-03-15 15:20:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
memberURIList := make([]string, 0)
|
|
|
|
|
2019-03-17 12:40:08 +00:00
|
|
|
if !IsNotExistsError(err) {
|
2019-06-08 22:03:50 +00:00
|
|
|
for _, member := range attributes[PrinterAttributeMemberURIs] {
|
2019-03-17 12:40:08 +00:00
|
|
|
memberString := strings.Split(member.Value.(string), "/")
|
|
|
|
printerName := memberString[len(memberString)-1]
|
2019-03-15 15:20:02 +00:00
|
|
|
|
2019-03-17 12:40:08 +00:00
|
|
|
if printerName == printer {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-15 15:20:02 +00:00
|
|
|
|
2019-03-17 12:40:08 +00:00
|
|
|
memberURIList = append(memberURIList, member.Value.(string))
|
|
|
|
}
|
2019-03-15 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memberURIList = append(memberURIList, c.getPrinterUri(printer))
|
|
|
|
|
|
|
|
req := NewRequest(OperationCupsAddModifyClass, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getClassUri(class)
|
|
|
|
req.PrinterAttributes[PrinterAttributeMemberURIs] = memberURIList
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err = c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error {
|
2019-06-08 22:03:50 +00:00
|
|
|
attributes, err := c.GetPrinterAttributes(class, []string{PrinterAttributeMemberURIs})
|
2019-03-15 15:20:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
memberURIList := make([]string, 0)
|
|
|
|
|
2019-06-08 22:03:50 +00:00
|
|
|
for _, member := range attributes[PrinterAttributeMemberURIs] {
|
2019-03-15 15:20:02 +00:00
|
|
|
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)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getClassUri(class)
|
|
|
|
req.PrinterAttributes[PrinterAttributeMemberURIs] = memberURIList
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err = c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) DeleteClass(class string) error {
|
|
|
|
req := NewRequest(OperationCupsDeleteClass, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getClassUri(class)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:40:08 +00:00
|
|
|
func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, errorPolicy ErrorPolicy, information, location string) error {
|
2019-03-15 15:20:02 +00:00
|
|
|
req := NewRequest(OperationCupsAddModifyPrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(name)
|
|
|
|
req.OperationAttributes[OperationAttributePPDName] = ppd
|
|
|
|
req.OperationAttributes[OperationAttributePrinterIsShared] = shared
|
|
|
|
req.PrinterAttributes[PrinterAttributePrinterStateReason] = "none"
|
|
|
|
req.PrinterAttributes[PrinterAttributeDeviceURI] = deviceURI
|
|
|
|
req.PrinterAttributes[PrinterAttributePrinterInfo] = information
|
|
|
|
req.PrinterAttributes[PrinterAttributePrinterLocation] = location
|
|
|
|
req.PrinterAttributes[PrinterAttributePrinterErrorPolicy] = string(errorPolicy)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) SetPrinterPPD(printer, ppd string) error {
|
|
|
|
req := NewRequest(OperationCupsAddModifyPrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
|
|
|
req.OperationAttributes[OperationAttributePPDName] = ppd
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) SetPrinterDeviceURI(printer, deviceURI string) error {
|
|
|
|
req := NewRequest(OperationCupsAddModifyPrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
|
|
|
req.PrinterAttributes[PrinterAttributeDeviceURI] = deviceURI
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error {
|
|
|
|
req := NewRequest(OperationCupsAddModifyPrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
|
|
|
req.OperationAttributes[OperationAttributePrinterIsShared] = shared
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:40:08 +00:00
|
|
|
func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy ErrorPolicy) error {
|
2019-03-15 15:20:02 +00:00
|
|
|
req := NewRequest(OperationCupsAddModifyPrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
|
|
|
req.PrinterAttributes[PrinterAttributePrinterErrorPolicy] = string(errorPolicy)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) SetPrinterInformation(printer, information string) error {
|
|
|
|
req := NewRequest(OperationCupsAddModifyPrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
|
|
|
req.PrinterAttributes[PrinterAttributePrinterInfo] = information
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) SetPrinterLocation(printer, location string) error {
|
|
|
|
req := NewRequest(OperationCupsAddModifyPrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
|
|
|
req.PrinterAttributes[PrinterAttributePrinterLocation] = location
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, err := c.SendRequest(c.getHttpUri("admin", ""), req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) DeletePrinter(printer string) error {
|
|
|
|
req := NewRequest(OperationCupsDeletePrinter, 1)
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
|
2019-03-15 15:20:02 +00:00
|
|
|
|
|
|
|
_, 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 {
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributeRequestedAttributes] = DefaultPrinterAttributes
|
2019-03-15 15:20:02 +00:00
|
|
|
} else {
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributeRequestedAttributes] = append(attributes, PrinterAttributePrinterName)
|
2019-03-15 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.SendRequest(c.getHttpUri("", nil), req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
printerNameMap := make(map[string]Attributes)
|
|
|
|
|
|
|
|
for _, printerAttributes := range resp.Printers {
|
2019-06-08 22:03:50 +00:00
|
|
|
printerNameMap[printerAttributes[PrinterAttributePrinterName][0].Value.(string)] = printerAttributes
|
2019-03-15 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return printerNameMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CUPSClient) GetClasses(attributes []string) (map[string]Attributes, error) {
|
|
|
|
req := NewRequest(OperationCupsGetClasses, 1)
|
|
|
|
|
|
|
|
if attributes == nil {
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributeRequestedAttributes] = DefaultClassAttributes
|
2019-03-15 15:20:02 +00:00
|
|
|
} else {
|
2019-06-08 22:03:50 +00:00
|
|
|
req.OperationAttributes[OperationAttributeRequestedAttributes] = append(attributes, PrinterAttributePrinterName)
|
2019-03-15 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.SendRequest(c.getHttpUri("", nil), req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
printerNameMap := make(map[string]Attributes)
|
|
|
|
|
|
|
|
for _, printerAttributes := range resp.Printers {
|
2019-06-08 22:03:50 +00:00
|
|
|
printerNameMap[printerAttributes[PrinterAttributePrinterName][0].Value.(string)] = printerAttributes
|
2019-03-15 15:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return printerNameMap, nil
|
|
|
|
}
|