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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//!
//! Command-line IPP utility to print a document or get printer status
//!

#![allow(clippy::result_large_err)]

use std::{
    fs,
    io::{self, BufReader},
    path::PathBuf,
    time::Duration,
};

use clap::Parser;

use ipp::{prelude::*, util};

fn new_client(uri: Uri, params: &IppParams) -> io::Result<IppClient> {
    let mut builder = IppClient::builder(uri).ignore_tls_errors(params.ignore_tls_errors);
    if let Some(timeout) = params.timeout {
        builder = builder.request_timeout(Duration::from_secs(timeout));
    }

    for param in &params.headers {
        if let Some((k, v)) = param.split_once('=') {
            builder = builder.http_header(k, v);
        }
    }

    for cert in &params.ca_certs {
        builder = builder.ca_cert(fs::read(cert)?);
    }

    Ok(builder.build())
}

fn new_payload(cmd: &IppPrintCmd) -> io::Result<IppPayload> {
    let payload = match cmd.file {
        Some(ref filename) => IppPayload::new(BufReader::new(fs::File::open(filename)?)),
        None => IppPayload::new(BufReader::new(io::stdin())),
    };
    Ok(payload)
}

fn dump_attributes(response: &IppRequestResponse, tag: DelimiterTag) {
    for group in response.attributes().groups_of(tag) {
        let mut values = group.attributes().values().collect::<Vec<_>>();

        values.sort_by_key(|&a| a.name());

        for v in values {
            println!("{}: {}", v.name(), v.value());
        }
        println!();
    }
}

fn do_print_job(params: &IppParams, cmd: IppPrintCmd) -> Result<(), IppError> {
    let client = new_client(cmd.uri.parse()?, params)?;

    if !cmd.no_check_state {
        let operation = IppOperationBuilder::get_printer_attributes(client.uri().clone()).build();
        let response = client.send(operation)?;
        if !util::is_printer_ready(&response)? {
            return Err(IppError::PrinterNotReady);
        }
    }

    let payload = new_payload(&cmd).map_err(IppError::from)?;

    let mut builder = IppOperationBuilder::print_job(client.uri().clone(), payload);
    if let Some(jobname) = cmd.job_name {
        builder = builder.job_title(jobname);
    }
    if let Some(username) = cmd.user_name {
        builder = builder.user_name(username);
    }

    for arg in cmd.options {
        if let Some((k, v)) = arg.split_once('=') {
            builder = builder.attribute(IppAttribute::new(k, v.parse().unwrap()));
        }
    }

    let response = client.send(builder.build())?;

    let status = response.header().status_code();
    if !status.is_success() {
        return Err(IppError::StatusError(status));
    }

    dump_attributes(&response, DelimiterTag::JobAttributes);

    Ok(())
}

fn do_status(params: &IppParams, cmd: IppStatusCmd) -> Result<(), IppError> {
    let client = new_client(cmd.uri.parse()?, params)?;

    let operation = IppOperationBuilder::get_printer_attributes(client.uri().clone())
        .attributes(&cmd.attributes)
        .build();

    let response = client.send(operation)?;

    let status = response.header().status_code();
    if !status.is_success() {
        return Err(IppError::StatusError(status));
    }

    dump_attributes(&response, DelimiterTag::PrinterAttributes);

    Ok(())
}

fn do_purge_jobs(params: &IppParams, cmd: IppPurgeCmd) -> Result<(), IppError> {
    let client = new_client(cmd.uri.parse()?, params)?;

    let mut builder = IppOperationBuilder::purge_jobs(client.uri().clone());

    if let Some(username) = cmd.user_name {
        builder = builder.user_name(username);
    }

    let operation = builder.build();

    let response = client.send(operation)?;

    let status = response.header().status_code();
    if !status.is_success() {
        return Err(IppError::StatusError(status));
    }

    dump_attributes(&response, DelimiterTag::OperationAttributes);

    Ok(())
}

fn do_cancel_job(params: &IppParams, cmd: IppCancelCmd) -> Result<(), IppError> {
    let client = new_client(cmd.uri.parse()?, params)?;

    let mut builder = IppOperationBuilder::cancel_job(client.uri().clone(), cmd.job_id);

    if let Some(username) = cmd.user_name {
        builder = builder.user_name(username);
    }

    let operation = builder.build();

    let response = client.send(operation)?;

    let status = response.header().status_code();
    if !status.is_success() {
        return Err(IppError::StatusError(status));
    }

    dump_attributes(&response, DelimiterTag::OperationAttributes);

    Ok(())
}

fn do_get_job(params: &IppParams, cmd: IppGetJobCmd) -> Result<(), IppError> {
    let client = new_client(cmd.uri.parse()?, params)?;

    let mut builder = IppOperationBuilder::get_job_attributes(client.uri().clone(), cmd.job_id);

    if let Some(username) = cmd.user_name {
        builder = builder.user_name(username);
    }

    let operation = builder.build();

    let response = client.send(operation)?;

    let status = response.header().status_code();
    if !status.is_success() {
        return Err(IppError::StatusError(status));
    }

    dump_attributes(&response, DelimiterTag::JobAttributes);

    Ok(())
}

fn do_get_all_jobs(params: &IppParams, cmd: IppGetAllJobsCmd) -> Result<(), IppError> {
    let client = new_client(cmd.uri.parse()?, params)?;

    let mut builder = IppOperationBuilder::get_jobs(client.uri().clone());

    if let Some(username) = cmd.user_name {
        builder = builder.user_name(username);
    }

    let operation = builder.build();

    let response = client.send(operation)?;

    let status = response.header().status_code();
    if !status.is_success() {
        return Err(IppError::StatusError(status));
    }

    dump_attributes(&response, DelimiterTag::JobAttributes);

    Ok(())
}

#[derive(Parser)]
#[clap(about = "IPP print utility", name = "ipputil", rename_all = "kebab-case")]
struct IppParams {
    #[clap(
        long = "ignore-tls-errors",
        short = 'i',
        global = true,
        help = "Ignore TLS handshake errors"
    )]
    ignore_tls_errors: bool,

    #[clap(
        long = "ca-cert",
        short = 'c',
        global = true,
        help = "One or more additional CA certs in PEM or DER format"
    )]
    ca_certs: Vec<PathBuf>,

    #[clap(
        long = "timeout",
        short = 't',
        global = true,
        help = "Request timeout in seconds, default = no timeout"
    )]
    timeout: Option<u64>,

    #[clap(long = "header", short = 'H', help = "Extra HTTP headers in key=value format")]
    headers: Vec<String>,

    #[clap(subcommand)]
    command: IppCommand,
}

#[derive(Parser)]
enum IppCommand {
    #[clap(name = "print", about = "Print file to an IPP printer")]
    PrintJob(IppPrintCmd),
    #[clap(name = "status", about = "Get status of an IPP printer")]
    Status(IppStatusCmd),
    #[clap(name = "cancel-job", about = "Cancel job from an IPP printer")]
    CancelJob(IppCancelCmd),
    #[clap(name = "get-job", about = "Get job attributes from an IPP printer")]
    GetJob(IppGetJobCmd),
    #[clap(name = "purge-jobs", about = "Purge all jobs from an IPP printer")]
    PurgeJobs(IppPurgeCmd),
    #[clap(name = "get-all-jobs", about = "Get pending jobs from an IPP printer")]
    GetAllJobs(IppGetAllJobsCmd),
}

#[derive(Parser, Clone)]
#[clap(rename_all = "kebab-case")]
struct IppPrintCmd {
    #[clap(help = "Printer URI")]
    uri: String,

    #[clap(
        long = "no-check-state",
        short = 'n',
        help = "Do not check printer state before printing"
    )]
    no_check_state: bool,

    #[clap(
        long = "file",
        short = 'f',
        help = "Input file name to print [default: standard input]"
    )]
    file: Option<PathBuf>,

    #[clap(long = "job-name", short = 'j', help = "Job name to send as job-name attribute")]
    job_name: Option<String>,

    #[clap(
        long = "user-name",
        short = 'u',
        help = "User name to send as requesting-user-name attribute"
    )]
    user_name: Option<String>,

    #[clap(long = "option", short = 'o', help = "Extra IPP job attributes in key=value format")]
    options: Vec<String>,
}

#[derive(Parser, Clone)]
#[clap(rename_all = "kebab-case")]
struct IppStatusCmd {
    #[clap(help = "Printer URI")]
    uri: String,

    #[clap(long = "attribute", short = 'a', help = "Attributes to query, default is to get all")]
    attributes: Vec<String>,
}

#[derive(Parser, Clone)]
#[clap(rename_all = "kebab-case")]
struct IppPurgeCmd {
    #[clap(help = "Printer URI")]
    uri: String,
    #[clap(
        long = "user-name",
        short = 'u',
        help = "User name to send as requesting-user-name attribute"
    )]
    user_name: Option<String>,
}

#[derive(Parser, Clone)]
#[clap(rename_all = "kebab-case")]
struct IppCancelCmd {
    #[clap(help = "Printer URI")]
    uri: String,
    #[clap(long = "job-id", short = 'j', help = "Job ID to cancel")]
    job_id: i32,
    #[clap(
        long = "user-name",
        short = 'u',
        help = "User name to send as requesting-user-name attribute"
    )]
    user_name: Option<String>,
}

#[derive(Parser, Clone)]
#[clap(rename_all = "kebab-case")]
struct IppGetJobCmd {
    #[clap(help = "Printer URI")]
    uri: String,
    #[clap(long = "job-id", short = 'j', help = "Job ID to get attributes for")]
    job_id: i32,
    #[clap(
        long = "user-name",
        short = 'u',
        help = "User name to send as requesting-user-name attribute"
    )]
    user_name: Option<String>,
}

#[derive(Parser, Clone)]
#[clap(rename_all = "kebab-case")]
struct IppGetAllJobsCmd {
    #[clap(help = "Printer URI")]
    uri: String,
    #[clap(
        long = "user-name",
        short = 'u',
        help = "User name to send as requesting-user-name attribute"
    )]
    user_name: Option<String>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let params = IppParams::parse();

    match params.command {
        IppCommand::Status(ref cmd) => do_status(&params, cmd.clone())?,
        IppCommand::PrintJob(ref cmd) => do_print_job(&params, cmd.clone())?,
        IppCommand::CancelJob(ref cmd) => do_cancel_job(&params, cmd.clone())?,
        IppCommand::GetJob(ref cmd) => do_get_job(&params, cmd.clone())?,
        IppCommand::PurgeJobs(ref cmd) => do_purge_jobs(&params, cmd.clone())?,
        IppCommand::GetAllJobs(ref cmd) => do_get_all_jobs(&params, cmd.clone())?,
    }
    Ok(())
}