mqttv5-cli 0.27.1

Superior CLI tool for MQTT v5.0 - unified client and broker commands with ergonomic input design
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
#![allow(clippy::large_futures)]
#![allow(clippy::struct_excessive_bools)]

use anyhow::{Context, Result};
use clap::Args;
use dialoguer::{Input, Select};
use mqtt5::client::auth_handlers::{JwtAuthHandler, ScramSha256AuthHandler};
use mqtt5::time::Duration;
#[cfg(feature = "codec")]
use mqtt5::{CodecRegistry, DeflateCodec, GzipCodec};
use mqtt5::{
    ConnectOptions, ConnectionEvent, Message, MqttClient, ProtocolVersion, PublishOptions, QoS,
    WillMessage,
};
use std::io::{self, Read};
use std::path::PathBuf;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use tokio::signal;
use tokio::sync::Notify;
use tracing::{debug, info, warn};

use super::parsers::{
    calculate_wait_until, duration_secs_to_u32, parse_duration_millis, parse_duration_secs,
    parse_stream_strategy,
};

#[derive(Args)]
pub struct PubCommand {
    /// MQTT topic to publish to
    #[arg(long, short, env = "MQTT5_TOPIC")]
    pub topic: Option<String>,

    /// Message to publish
    #[arg(long, short, env = "MQTT5_MESSAGE")]
    pub message: Option<String>,

    /// Read message from file
    #[arg(long, short, env = "MQTT5_FILE")]
    pub file: Option<String>,

    /// Read message from stdin
    #[arg(long, env = "MQTT5_STDIN")]
    pub stdin: bool,

    /// Full broker URL for TLS/WebSocket/QUIC (e.g., <mqtts://host:8883>, <wss://host/mqtt>)
    #[arg(long, short = 'U', conflicts_with_all = &["host", "port"], env = "MQTT5_URL")]
    pub url: Option<String>,

    /// Broker hostname (builds mqtt:// URL, use --url for TLS/WebSocket/QUIC)
    #[arg(long, short = 'H', default_value = "localhost", env = "MQTT5_HOST")]
    pub host: String,

    /// Broker port (used with --host)
    #[arg(long, short, default_value = "1883", env = "MQTT5_PORT")]
    pub port: u16,

    /// Quality of Service level (0, 1, or 2)
    #[arg(long, short, value_parser = parse_qos, env = "MQTT5_QOS")]
    pub qos: Option<QoS>,

    /// Retain message
    #[arg(long, short, env = "MQTT5_RETAIN")]
    pub retain: bool,

    /// Message expiry interval in seconds (0 = no expiry)
    #[arg(long, env = "MQTT5_MESSAGE_EXPIRY_INTERVAL")]
    pub message_expiry_interval: Option<u32>,

    /// Topic alias (1-65535) for repeated publishing to same topic
    #[arg(long, env = "MQTT5_TOPIC_ALIAS")]
    pub topic_alias: Option<u16>,

    /// Response topic for request/response pattern (MQTT 5.0)
    #[arg(long, env = "MQTT5_RESPONSE_TOPIC")]
    pub response_topic: Option<String>,

    /// Correlation data for request/response pattern (MQTT 5.0, hex-encoded)
    #[arg(long, env = "MQTT5_CORRELATION_DATA")]
    pub correlation_data: Option<String>,

    /// Wait for response after publishing (requires --response-topic)
    #[arg(long, env = "MQTT5_WAIT_RESPONSE")]
    pub wait_response: bool,

    /// Timeout when waiting for response (e.g., 30s, 1m) (default: 30s)
    #[arg(long, default_value = "30", value_parser = parse_duration_secs, env = "MQTT5_TIMEOUT")]
    pub timeout: u64,

    /// Number of responses to wait for (default: 1, 0 = unlimited until timeout)
    #[arg(long, default_value = "1", env = "MQTT5_RESPONSE_COUNT")]
    pub response_count: u32,

    /// Output format for responses: raw, json, verbose
    #[arg(long, default_value = "raw", value_parser = ["raw", "json", "verbose"], env = "MQTT5_OUTPUT_FORMAT")]
    pub output_format: String,

    /// Username for authentication
    #[arg(long, short, env = "MQTT5_USERNAME")]
    pub username: Option<String>,

    /// Password for authentication
    #[arg(long, short = 'P', env = "MQTT5_PASSWORD")]
    pub password: Option<String>,

    /// Authentication method: password, scram, jwt (default: password)
    #[arg(long, value_parser = ["password", "scram", "jwt"], env = "MQTT5_AUTH_METHOD")]
    pub auth_method: Option<String>,

    /// JWT token for JWT authentication
    #[arg(long, env = "MQTT5_JWT_TOKEN")]
    pub jwt_token: Option<String>,

    /// Client ID
    #[arg(long, short, env = "MQTT5_CLIENT_ID")]
    pub client_id: Option<String>,

    /// Skip prompts and use defaults/fail if required args missing
    #[arg(long, env = "MQTT5_NON_INTERACTIVE")]
    pub non_interactive: bool,

    /// Don't clean start (resume existing session)
    #[arg(long = "no-clean-start", env = "MQTT5_NO_CLEAN_START")]
    pub no_clean_start: bool,

    /// Session expiry interval (e.g., 1h, 30m) (0 = expire on disconnect)
    #[arg(long, value_parser = parse_duration_secs, env = "MQTT5_SESSION_EXPIRY")]
    pub session_expiry: Option<u64>,

    /// Keep alive interval (e.g., 60s, 1m) (default: 60s)
    #[arg(long, short = 'k', default_value = "60", value_parser = parse_duration_secs, env = "MQTT5_KEEP_ALIVE")]
    pub keep_alive: u64,

    /// MQTT protocol version (3.1.1 or 5, default: 5)
    #[arg(long, value_parser = parse_protocol_version, env = "MQTT5_PROTOCOL_VERSION")]
    pub protocol_version: Option<ProtocolVersion>,

    /// Will topic (last will and testament)
    #[arg(long, env = "MQTT5_WILL_TOPIC")]
    pub will_topic: Option<String>,

    /// Will message payload
    #[arg(long, env = "MQTT5_WILL_MESSAGE")]
    pub will_message: Option<String>,

    /// Will `QoS` level (0, 1, or 2)
    #[arg(long, value_parser = parse_qos, env = "MQTT5_WILL_QOS")]
    pub will_qos: Option<QoS>,

    /// Will retain flag
    #[arg(long, env = "MQTT5_WILL_RETAIN")]
    pub will_retain: bool,

    /// TLS certificate file (PEM format) for secure connections
    #[arg(long, env = "MQTT5_CERT")]
    pub cert: Option<PathBuf>,

    /// TLS private key file (PEM format) for secure connections
    #[arg(long, env = "MQTT5_KEY")]
    pub key: Option<PathBuf>,

    /// TLS CA certificate file (PEM format) for server verification
    #[arg(long, env = "MQTT5_CA_CERT")]
    pub ca_cert: Option<PathBuf>,

    /// Skip certificate verification for TLS/QUIC connections (insecure, for testing only)
    #[arg(long, env = "MQTT5_INSECURE")]
    pub insecure: bool,

    /// Will delay interval (e.g., 5m, 1h)
    #[arg(long, value_parser = parse_duration_secs, env = "MQTT5_WILL_DELAY")]
    pub will_delay: Option<u64>,

    /// Keep connection alive after publishing (for testing will messages)
    #[arg(long, hide = true)]
    pub keep_alive_after_publish: bool,

    /// Enable automatic reconnection when broker disconnects
    #[arg(long, env = "MQTT5_AUTO_RECONNECT")]
    pub auto_reconnect: bool,

    /// QUIC stream strategy (control-only, per-publish, per-topic, per-subscription)
    #[arg(long, value_parser = parse_stream_strategy, env = "MQTT5_QUIC_STREAM_STRATEGY")]
    pub quic_stream_strategy: Option<mqtt5::transport::StreamStrategy>,

    /// Enable `MQoQ` flow headers for stream state tracking
    #[arg(long, env = "MQTT5_QUIC_FLOW_HEADERS")]
    pub quic_flow_headers: bool,

    /// Flow expiration interval (e.g., 5m, 1h) (default: 5m)
    #[arg(long, default_value = "300", value_parser = parse_duration_secs, env = "MQTT5_QUIC_FLOW_EXPIRE")]
    pub quic_flow_expire: u64,

    /// Maximum concurrent QUIC streams
    #[arg(long, env = "MQTT5_QUIC_MAX_STREAMS")]
    pub quic_max_streams: Option<usize>,

    /// Enable QUIC datagrams for unreliable transport
    #[arg(long, env = "MQTT5_QUIC_DATAGRAMS")]
    pub quic_datagrams: bool,

    /// QUIC connection timeout (e.g., 30s, 1m) (default: 30s)
    #[arg(long, default_value = "30", value_parser = parse_duration_secs, env = "MQTT5_QUIC_CONNECT_TIMEOUT")]
    pub quic_connect_timeout: u64,

    /// Enable QUIC 0-RTT early data for faster reconnections
    #[arg(long, env = "MQTT5_QUIC_EARLY_DATA")]
    pub quic_early_data: bool,

    /// Delay before publishing (e.g., 5s, 1m30s)
    #[arg(long, value_parser = parse_duration_secs, env = "MQTT5_DELAY")]
    pub delay: Option<u64>,

    /// Repeat publishing N times (0 = infinite until Ctrl+C)
    #[arg(long, env = "MQTT5_REPEAT")]
    pub repeat: Option<u64>,

    /// Interval between repeated publishes in ms (e.g., 1000, 1s, 500ms)
    #[arg(long, value_parser = parse_duration_millis, requires = "repeat", env = "MQTT5_INTERVAL")]
    pub interval: Option<u64>,

    /// Schedule publish at specific time (e.g., 14:30, 14:30:00, 2025-01-15T14:30:00)
    #[arg(long, conflicts_with = "delay", env = "MQTT5_AT")]
    pub at: Option<String>,

    /// OpenTelemetry OTLP endpoint (e.g., `http://localhost:4317`)
    #[cfg(feature = "opentelemetry")]
    #[arg(long, env = "MQTT5_OTEL_ENDPOINT")]
    pub otel_endpoint: Option<String>,

    /// OpenTelemetry service name (default: mqttv5-pub)
    #[cfg(feature = "opentelemetry")]
    #[arg(long, default_value = "mqttv5-pub", env = "MQTT5_OTEL_SERVICE_NAME")]
    pub otel_service_name: String,

    /// OpenTelemetry sampling ratio (0.0-1.0, default: 1.0)
    #[cfg(feature = "opentelemetry")]
    #[arg(long, default_value = "1.0", env = "MQTT5_OTEL_SAMPLING")]
    pub otel_sampling: f64,

    /// Compress payload using codec (gzip, deflate)
    #[cfg(feature = "codec")]
    #[arg(long, value_parser = ["gzip", "deflate"], env = "MQTT5_CODEC")]
    pub codec: Option<String>,

    /// Codec compression level (0-9, default: 6)
    #[cfg(feature = "codec")]
    #[arg(
        long,
        default_value = "6",
        requires = "codec",
        env = "MQTT5_CODEC_LEVEL"
    )]
    pub codec_level: u32,

    /// Minimum payload size for compression in bytes (default: 128)
    #[cfg(feature = "codec")]
    #[arg(
        long,
        default_value = "128",
        requires = "codec",
        env = "MQTT5_CODEC_MIN_SIZE"
    )]
    pub codec_min_size: usize,
}

fn parse_qos(s: &str) -> Result<QoS, String> {
    match s {
        "0" => Ok(QoS::AtMostOnce),
        "1" => Ok(QoS::AtLeastOnce),
        "2" => Ok(QoS::ExactlyOnce),
        _ => Err(format!("QoS must be 0, 1, or 2, got: {s}")),
    }
}

fn parse_protocol_version(s: &str) -> Result<ProtocolVersion, String> {
    match s {
        "3.1.1" | "311" | "4" => Ok(ProtocolVersion::V311),
        "5" | "5.0" => Ok(ProtocolVersion::V5),
        _ => Err(format!("Invalid protocol version: {s}. Use '3.1.1' or '5'")),
    }
}

fn prompt_topic_and_qos(cmd: &mut PubCommand) -> Result<(String, QoS)> {
    if cmd.topic.is_none() && !cmd.non_interactive {
        let topic = Input::<String>::new()
            .with_prompt("MQTT topic (e.g., sensors/temperature, home/status)")
            .interact()
            .context("Failed to get topic input")?;
        cmd.topic = Some(topic);
    }

    let topic = cmd.topic.take().ok_or_else(|| {
        anyhow::anyhow!("Topic is required. Use --topic or run without --non-interactive")
    })?;

    if topic.is_empty() {
        anyhow::bail!("Topic cannot be empty");
    }
    if topic.contains("//") {
        anyhow::bail!(
            "Invalid topic '{}' - cannot have empty segments\nDid you mean '{}'?",
            topic,
            topic.replace("//", "/")
        );
    }
    if topic.ends_with('/') {
        anyhow::bail!(
            "Invalid topic '{}' - cannot end with '/'\nDid you mean '{}'?",
            topic,
            topic.trim_end_matches('/')
        );
    }

    if cmd.wait_response && cmd.response_topic.is_none() {
        anyhow::bail!("--response-topic is required when using --wait-response");
    }

    let qos = if cmd.qos.is_none() && !cmd.non_interactive {
        let qos_options = vec![
            "0 (At most once - fire and forget)",
            "1 (At least once - acknowledged)",
            "2 (Exactly once - assured)",
        ];
        let selection = Select::new()
            .with_prompt("Quality of Service level")
            .items(&qos_options)
            .default(0)
            .interact()
            .context("Failed to get QoS selection")?;

        match selection {
            1 => QoS::AtLeastOnce,
            2 => QoS::ExactlyOnce,
            _ => QoS::AtMostOnce,
        }
    } else {
        cmd.qos.unwrap_or(QoS::AtMostOnce)
    };

    if cmd.wait_response && qos == QoS::AtMostOnce {
        warn!("Using --wait-response with QoS 0 may be unreliable; consider using -q 1 or -q 2");
    }

    Ok((topic, qos))
}

fn build_connect_options(cmd: &PubCommand, client_id: &str) -> ConnectOptions {
    let mut options = ConnectOptions::new(client_id.to_owned())
        .with_clean_start(!cmd.no_clean_start)
        .with_keep_alive(Duration::from_secs(cmd.keep_alive));

    if cmd.auto_reconnect {
        options = options.with_automatic_reconnect(true);
    }

    if let Some(version) = cmd.protocol_version {
        options = options.with_protocol_version(version);
    }

    if let Some(expiry) = cmd.session_expiry {
        options = options.with_session_expiry_interval(duration_secs_to_u32(expiry));
    }

    options
}

async fn configure_auth(
    client: &MqttClient,
    options: &mut ConnectOptions,
    cmd: &PubCommand,
) -> Result<()> {
    match cmd.auth_method.as_deref() {
        Some("scram") => {
            let username = cmd.username.clone().ok_or_else(|| {
                anyhow::anyhow!("--username is required for SCRAM authentication")
            })?;
            let password = cmd.password.clone().ok_or_else(|| {
                anyhow::anyhow!("--password is required for SCRAM authentication")
            })?;
            *options = std::mem::take(options)
                .with_credentials(username.clone(), Vec::new())
                .with_authentication_method("SCRAM-SHA-256");

            let handler = ScramSha256AuthHandler::new(username, password);
            client.set_auth_handler(handler).await;
            debug!("SCRAM-SHA-256 authentication configured");
        }
        Some("jwt") => {
            let token = cmd
                .jwt_token
                .clone()
                .ok_or_else(|| anyhow::anyhow!("--jwt-token is required for JWT authentication"))?;
            *options = std::mem::take(options).with_authentication_method("JWT");

            let handler = JwtAuthHandler::new(token);
            client.set_auth_handler(handler).await;
            debug!("JWT authentication configured");
        }
        Some("password") | None => {
            if let (Some(username), Some(password)) = (cmd.username.clone(), cmd.password.clone()) {
                *options =
                    std::mem::take(options).with_credentials(username, password.into_bytes());
            } else if let Some(username) = cmd.username.clone() {
                *options = std::mem::take(options).with_credentials(username, Vec::new());
            }
        }
        Some(other) => anyhow::bail!("Unknown auth method: {other}"),
    }
    Ok(())
}

fn configure_will(options: &mut ConnectOptions, cmd: &PubCommand) {
    if let Some(topic) = cmd.will_topic.clone() {
        let payload = cmd.will_message.clone().unwrap_or_default();
        let mut will = WillMessage::new(topic, payload.into_bytes()).with_retain(cmd.will_retain);

        if let Some(qos) = cmd.will_qos {
            will = will.with_qos(qos);
        }

        if let Some(delay) = cmd.will_delay {
            will.properties.will_delay_interval = Some(duration_secs_to_u32(delay));
        }

        *options = std::mem::take(options).with_will(will);
    }
}

async fn configure_quic_transport(client: &MqttClient, cmd: &PubCommand) {
    if let Some(strategy) = cmd.quic_stream_strategy {
        client.set_quic_stream_strategy(strategy).await;
        debug!("QUIC stream strategy: {:?}", strategy);
    }
    if cmd.quic_flow_headers {
        client.set_quic_flow_headers(true).await;
        debug!("QUIC flow headers enabled");
    }
    client
        .set_quic_flow_expire(std::time::Duration::from_secs(cmd.quic_flow_expire))
        .await;
    if let Some(max) = cmd.quic_max_streams {
        client.set_quic_max_streams(Some(max)).await;
        debug!("QUIC max streams: {max}");
    }
    if cmd.quic_datagrams {
        client.set_quic_datagrams(true).await;
        debug!("QUIC datagrams enabled");
    }
    client
        .set_quic_connect_timeout(Duration::from_secs(cmd.quic_connect_timeout))
        .await;
    if cmd.quic_early_data {
        client.set_quic_early_data(true).await;
        debug!("QUIC 0-RTT early data enabled");
    }
}

async fn configure_tls_certs(
    client: &MqttClient,
    broker_url: &str,
    cmd: &PubCommand,
) -> Result<()> {
    let is_secure = broker_url.starts_with("ssl://")
        || broker_url.starts_with("mqtts://")
        || broker_url.starts_with("quics://");
    let has_certs = cmd.cert.is_some() || cmd.key.is_some() || cmd.ca_cert.is_some();

    if is_secure && has_certs {
        let cert_pem = if let Some(cert_path) = &cmd.cert {
            Some(std::fs::read(cert_path).with_context(|| {
                format!("Failed to read certificate file: {}", cert_path.display())
            })?)
        } else {
            None
        };
        let key_pem = if let Some(key_path) = &cmd.key {
            Some(
                std::fs::read(key_path)
                    .with_context(|| format!("Failed to read key file: {}", key_path.display()))?,
            )
        } else {
            None
        };
        let ca_pem = if let Some(ca_path) = &cmd.ca_cert {
            Some(std::fs::read(ca_path).with_context(|| {
                format!("Failed to read CA certificate file: {}", ca_path.display())
            })?)
        } else {
            None
        };

        client.set_tls_config(cert_pem, key_pem, ca_pem).await;
    }
    Ok(())
}

async fn setup_response_subscription(
    client: &MqttClient,
    cmd: &PubCommand,
    correlation_data: Option<Vec<u8>>,
) -> Result<(Arc<AtomicU32>, Arc<Notify>)> {
    let received_count = Arc::new(AtomicU32::new(0));
    let done_notify = Arc::new(Notify::new());

    if cmd.wait_response {
        let response_topic = cmd.response_topic.as_ref().unwrap().clone();
        let expected_correlation = correlation_data;
        let target_count = cmd.response_count;
        let output_format = cmd.output_format.clone();
        let received_clone = received_count.clone();
        let done_clone = done_notify.clone();

        client
            .subscribe(&response_topic, move |msg: Message| {
                if let Some(ref expected) = expected_correlation {
                    match &msg.properties.correlation_data {
                        Some(received) if received == expected => {}
                        _ => return,
                    }
                }

                format_response_message(&output_format, &msg);

                let count = received_clone.fetch_add(1, Ordering::Relaxed) + 1;
                if target_count > 0 && count >= target_count {
                    done_clone.notify_one();
                }
            })
            .await?;

        debug!(
            "SUBACK received for '{}', subscription ready before publish",
            response_topic
        );
    }

    Ok((received_count, done_notify))
}

fn format_response_message(output_format: &str, msg: &Message) {
    match output_format {
        "json" => {
            if let Ok(json) = serde_json::from_slice::<serde_json::Value>(&msg.payload) {
                println!(
                    "{}",
                    serde_json::to_string_pretty(&json).unwrap_or_default()
                );
            } else {
                println!("{}", String::from_utf8_lossy(&msg.payload));
            }
        }
        "verbose" => {
            println!("Topic: {}", msg.topic);
            if let Some(corr) = &msg.properties.correlation_data {
                println!("Correlation: {}", hex::encode(corr));
            }
            println!("Payload: {}", String::from_utf8_lossy(&msg.payload));
            println!("---");
        }
        _ => {
            println!("{}", String::from_utf8_lossy(&msg.payload));
        }
    }
}

async fn publish_loop(
    client: &MqttClient,
    topic: &str,
    message: &str,
    cmd: &PubCommand,
    qos: QoS,
    correlation_data: Option<&Vec<u8>>,
) -> Result<()> {
    let has_properties = cmd.retain
        || cmd.message_expiry_interval.is_some()
        || cmd.topic_alias.is_some()
        || cmd.response_topic.is_some()
        || correlation_data.is_some();

    let repeat_count = cmd.repeat.unwrap_or(1);
    let interval_millis = cmd.interval.unwrap_or(1000);
    let infinite_repeat = cmd.repeat == Some(0);
    let mut iteration = 0u64;

    loop {
        iteration += 1;

        if has_properties {
            publish_with_properties(client, topic, message, cmd, qos, correlation_data).await?;
        } else {
            publish_simple(client, topic, message, qos).await?;
        }

        print_publish_result(cmd, iteration, topic, qos);

        if !infinite_repeat && iteration >= repeat_count {
            break;
        }

        tokio::select! {
            () = tokio::time::sleep(std::time::Duration::from_millis(interval_millis)) => {}
            _ = signal::ctrl_c() => {
                println!("\n✓ Interrupted after {iteration} publishes");
                client.disconnect().await?;
                return Ok(());
            }
        }
    }

    Ok(())
}

async fn publish_with_properties(
    client: &MqttClient,
    topic: &str,
    message: &str,
    cmd: &PubCommand,
    qos: QoS,
    correlation_data: Option<&Vec<u8>>,
) -> Result<()> {
    let mut options = PublishOptions {
        qos,
        retain: cmd.retain,
        ..Default::default()
    };
    options.properties.message_expiry_interval = cmd.message_expiry_interval;
    options.properties.topic_alias = cmd.topic_alias;
    options
        .properties
        .response_topic
        .clone_from(&cmd.response_topic);
    options.properties.correlation_data = correlation_data.cloned();
    client
        .publish_with_options(topic, message.as_bytes(), options)
        .await?;
    Ok(())
}

async fn publish_simple(client: &MqttClient, topic: &str, message: &str, qos: QoS) -> Result<()> {
    match qos {
        QoS::AtMostOnce => {
            client.publish(topic, message.as_bytes()).await?;
        }
        QoS::AtLeastOnce => {
            client.publish_qos1(topic, message.as_bytes()).await?;
        }
        QoS::ExactlyOnce => {
            client.publish_qos2(topic, message.as_bytes()).await?;
        }
    }
    Ok(())
}

fn print_publish_result(cmd: &PubCommand, iteration: u64, topic: &str, qos: QoS) {
    if cmd.repeat.is_some() {
        println!(
            "✓ Published message {} to '{}' (QoS {})",
            iteration, topic, qos as u8
        );
    } else {
        println!("✓ Published message to '{}' (QoS {})", topic, qos as u8);
    }
    if cmd.retain {
        println!("  Message retained on broker");
    }
}

async fn wait_for_response(
    cmd: &PubCommand,
    done_notify: Arc<Notify>,
    received_count: Arc<AtomicU32>,
    client: &MqttClient,
) -> Result<()> {
    if !cmd.wait_response {
        return Ok(());
    }

    let timeout_secs = cmd.timeout;
    let target_count = cmd.response_count;

    println!(
        "Waiting for response on '{}'...",
        cmd.response_topic.as_ref().unwrap()
    );

    tokio::select! {
        () = done_notify.notified() => {
        }
        () = tokio::time::sleep(std::time::Duration::from_secs(timeout_secs)) => {
            let count = received_count.load(Ordering::Relaxed);
            if count == 0 {
                client.disconnect().await?;
                anyhow::bail!("Timeout: no response received within {timeout_secs}s");
            } else if target_count > 0 && count < target_count {
                eprintln!(
                    "Warning: timeout after receiving {count} of {target_count} expected responses"
                );
            }
        }
        _ = signal::ctrl_c() => {
            println!("\n✓ Interrupted");
        }
    }

    Ok(())
}

async fn keep_alive_loop(client: &MqttClient, auto_reconnect: bool) -> Result<()> {
    info!("Keeping connection alive (--keep-alive-after-publish)");

    if auto_reconnect {
        client
            .on_connection_event(move |event| match event {
                ConnectionEvent::Connecting => {
                    info!("Connecting to broker...");
                }
                ConnectionEvent::Connected { session_present } => {
                    if session_present {
                        info!("✓ Reconnected (session present)");
                    } else {
                        info!("✓ Reconnected (new session)");
                    }
                }
                ConnectionEvent::Disconnected { .. } => {
                    warn!("âš  Disconnected from broker, attempting reconnection...");
                }
                ConnectionEvent::Reconnecting { attempt } => {
                    info!("Reconnecting (attempt {})...", attempt);
                }
                ConnectionEvent::ReconnectFailed { error } => {
                    warn!("âš  Reconnection failed: {error}");
                }
            })
            .await?;
    }

    match signal::ctrl_c().await {
        Ok(()) => {
            println!("\n✓ Received Ctrl+C, disconnecting...");
        }
        Err(err) => {
            anyhow::bail!("Unable to listen for shutdown signal: {err}");
        }
    }

    Ok(())
}

pub async fn execute(mut cmd: PubCommand, verbose: bool, debug: bool) -> Result<()> {
    #[cfg(feature = "opentelemetry")]
    let has_otel = cmd.otel_endpoint.is_some();

    #[cfg(not(feature = "opentelemetry"))]
    let has_otel = false;

    if !has_otel {
        crate::init_basic_tracing(verbose, debug);
    }

    let (topic, qos) = prompt_topic_and_qos(&mut cmd)?;

    let message = get_message_content(&mut cmd)
        .await
        .context("Failed to get message content")?;

    let broker_url = cmd
        .url
        .take()
        .unwrap_or_else(|| format!("mqtt://{}:{}", cmd.host, cmd.port));
    debug!("Connecting to broker: {}", broker_url);

    let client_id = cmd
        .client_id
        .take()
        .unwrap_or_else(|| format!("mqttv5-pub-{}", rand::rng().random::<u32>()));
    let client = MqttClient::new(&client_id);

    #[cfg(feature = "opentelemetry")]
    if let Some(endpoint) = &cmd.otel_endpoint {
        use mqtt5::telemetry::TelemetryConfig;
        let telemetry_config = TelemetryConfig::new(&cmd.otel_service_name)
            .with_endpoint(endpoint)
            .with_sampling_ratio(cmd.otel_sampling);
        mqtt5::telemetry::init_tracing_subscriber(&telemetry_config)?;
        info!(
            "OpenTelemetry enabled: endpoint={}, service={}, sampling={}",
            endpoint, cmd.otel_service_name, cmd.otel_sampling
        );
    }

    let mut options = build_connect_options(&cmd, &client_id);
    configure_auth(&client, &mut options, &cmd).await?;
    configure_will(&mut options, &cmd);

    #[cfg(feature = "codec")]
    configure_codec(&mut options, &cmd)?;

    if cmd.insecure {
        client.set_insecure_tls(true).await;
        info!("Insecure TLS mode enabled (certificate verification disabled)");
    }

    configure_quic_transport(&client, &cmd).await;
    configure_tls_certs(&client, &broker_url, &cmd).await?;

    info!("Connecting to {}...", broker_url);
    let result = client
        .connect_with_options(&broker_url, options)
        .await
        .context("Failed to connect to MQTT broker")?;

    if result.session_present {
        println!("✓ Resumed existing session");
    }

    info!("Publishing to topic '{}'...", topic);

    if cmd.topic_alias == Some(0) {
        anyhow::bail!("Topic alias must be between 1 and 65535, got: 0");
    }

    let correlation_data: Option<Vec<u8>> = if cmd.wait_response && cmd.correlation_data.is_none() {
        Some(format!("rr-{}", rand::rng().random::<u64>()).into_bytes())
    } else if let Some(ref hex_data) = cmd.correlation_data {
        Some(hex::decode(hex_data).context("Invalid hex in --correlation-data")?)
    } else {
        None
    };

    let (received_count, done_notify) =
        setup_response_subscription(&client, &cmd, correlation_data.clone()).await?;

    await_scheduled_delay(&cmd).await?;

    publish_loop(
        &client,
        &topic,
        &message,
        &cmd,
        qos,
        correlation_data.as_ref(),
    )
    .await?;

    wait_for_response(&cmd, done_notify, received_count, &client).await?;

    if cmd.keep_alive_after_publish {
        keep_alive_loop(&client, cmd.auto_reconnect).await?;
    }

    client.disconnect().await?;

    Ok(())
}

async fn await_scheduled_delay(cmd: &PubCommand) -> Result<()> {
    if let Some(delay_secs) = cmd.delay {
        info!(
            "Waiting {} before publishing...",
            humantime::format_duration(std::time::Duration::from_secs(delay_secs))
        );
        tokio::time::sleep(std::time::Duration::from_secs(delay_secs)).await;
    } else if let Some(ref at_time) = cmd.at {
        let wait_duration = calculate_wait_until(at_time)?;
        if wait_duration.as_secs() > 0 {
            info!(
                "Scheduled for {}, waiting {}...",
                at_time,
                humantime::format_duration(wait_duration)
            );
            tokio::time::sleep(wait_duration).await;
        }
    }
    Ok(())
}

#[cfg(feature = "codec")]
fn configure_codec(options: &mut ConnectOptions, cmd: &PubCommand) -> Result<()> {
    if let Some(ref codec_name) = cmd.codec {
        let registry = CodecRegistry::new();
        match codec_name.as_str() {
            "gzip" => {
                registry.register(
                    GzipCodec::new()
                        .with_level(cmd.codec_level)
                        .with_min_size(cmd.codec_min_size),
                );
                registry.set_default("application/gzip")?;
                debug!(
                    "Gzip codec enabled: level={}, min_size={}",
                    cmd.codec_level, cmd.codec_min_size
                );
            }
            "deflate" => {
                registry.register(
                    DeflateCodec::new()
                        .with_level(cmd.codec_level)
                        .with_min_size(cmd.codec_min_size),
                );
                registry.set_default("application/x-deflate")?;
                debug!(
                    "Deflate codec enabled: level={}, min_size={}",
                    cmd.codec_level, cmd.codec_min_size
                );
            }
            _ => anyhow::bail!("Unknown codec: {codec_name}"),
        }
        *options = std::mem::take(options).with_codec_registry(Arc::new(registry));
    }
    Ok(())
}

async fn get_message_content(cmd: &mut PubCommand) -> Result<String> {
    if cmd.stdin {
        debug!("Reading message from stdin");
        let mut buffer = String::new();
        io::stdin()
            .read_to_string(&mut buffer)
            .context("Failed to read from stdin")?;
        return Ok(buffer.trim().to_string());
    }

    if let Some(file_path) = &cmd.file {
        debug!("Reading message from file: {}", file_path);
        let content = tokio::fs::read_to_string(file_path)
            .await
            .with_context(|| format!("Failed to read file: {file_path}"))?;
        return Ok(content.trim().to_string());
    }

    if let Some(message) = &cmd.message {
        return Ok(message.clone());
    }

    if !cmd.non_interactive {
        let message = Input::<String>::new()
            .with_prompt("Message content")
            .interact()
            .context("Failed to get message input")?;
        return Ok(message);
    }

    anyhow::bail!("Message content is required. Use one of:\n  --message \"your message\"\n  --file message.txt\n  --stdin\n  Or run without --non-interactive to be prompted");
}

use rand::Rng;