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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
//! This file holds the Hexchat struct used to interface with Hexchat. Its
//! fields are the actual callbacks provided by Hexchat. When Hexchat
//! loads this library, the Hexchat pointer is stored and used by casting it
//! to the struct contained in this file. These native function pointers are
//! private to this crate, and a more Rust-friendly API is provided through
//! this Hexchat interface.

use libc::{c_int, c_char, c_void, time_t};
use std::ptr;
use std::ffi::CString;
use std::fmt;
use std::fmt::Debug;
use std::ops::FnMut;
use std::ptr::null;
use std::str;

use crate::callback_data::{CallbackData, TimerCallbackOnce};
use crate::context::Context;
use crate::{hexchat_callbacks::*, HexchatError};
#[cfg(feature = "threadsafe")]
use crate::hexchat_entry_points::PHEXCHAT;
use crate::hook::Hook;
use crate::list_iterator::ListIterator;
use crate::plugin::Plugin;
use crate::user_data::*;
use crate::utils::*;
#[cfg(feature = "threadsafe")]
use crate::threadsafe_hexchat::*;

/// Value used in example from the Hexchat Plugin Interface doc web page.
const MAX_PREF_VALUE_SIZE: usize =  512;

/// Value specified on the [Hexchat Plugin Interface web page]
/// (https://hexchat.readthedocs.io/en/latest/plugins.html).
const MAX_PREF_LIST_SIZE : usize = 4096;

// hexchat_send_modes, hexchat_event_attrs_free, pluginpref_delete,

/// The priorty for a given callback invoked by Hexchat.
pub enum Priority {
    Highest     =  127,
    High        =   64,
    Norm        =    0,
    Low         =  -64,
    Lowest      = -128,
}

/// The return value for client plugin callbacks.
pub enum Eat {
    None        =    0,
    Hexchat     =    1,
    Plugin      =    2,
    All         =    3,
}

/// File descriptor types.
pub enum FD {
    Read        =    1,
    Write       =    2,
    Exception   =    4,
    NotSocket   =    8,
}

/// Used by the `hexthat.strip()` function to determine what to strip from the
/// target string.
pub enum StripFlags {
    StripMIrcColors     = 1,
    StripTextAttributes = 2,
    StripBoth           = 3,
}

/// This is the rust-facing Hexchat API. Each method has a corresponding
/// C function pointer which they wrap and marshal data/from.
///
impl Hexchat {

    /// Returns a thread-safe wrapper for `Hexchat` that exposes thread-safe
    /// methods wrapping several of `Hexchat`s methods.
    ///
    #[cfg(feature = "threadsafe")]
    pub fn threadsafe(&self) -> ThreadSafeHexchat {
        ThreadSafeHexchat::new(unsafe { &*PHEXCHAT })
    }

    /// Prints the string passed to it to the active Hexchat window.
    /// # Arguments
    /// * `text` - The text to print.
    ///
    pub fn print(&self, text: &str) {
        let text = str2cstring(text);
        unsafe { (self.c_print)(self, text.as_ptr()); }
    }

    /// Invokes the Hexchat command specified by `command`.
    /// # Arguments
    /// * `command` - The Hexchat command to invoke.
    ///
    pub fn command(&self, command: &str) {
        let command = str2cstring(command);
        unsafe { (self.c_command)(self, command.as_ptr()); }
    }

    /// Registeres a command callback with Hexchat. This will add a user
    /// invocable slash "/" command that can be seen when listing `/help`.
    /// The callback can be a static function, or a closure, that has the form:
    /// ```
    ///     FnMut(&Hexchat, &[String], &[String], &UserData)
    ///     -> Eat
    /// ```
    /// Note that the callback parameters include a reference to the `Hexchat`
    /// object as a convenience. This differs from the C interface which doesn't
    /// include it.
    ///
    /// # Arguments
    /// * `name`        - The name of the event that invokes the callback.
    /// * `pri`         - The priority of the callback.
    /// * `callback`    - The static function or closure to register.
    /// * `help`        - Help text displayed by Hexchat for the command.
    /// * `user_data`   - Data passed back to the callback when invoked.
    ///
    /// # Returns
    /// A `Hook` object associated with the callback.
    ///
    pub fn hook_command<F: 'static>(&self,
                                    name        : &str,
                                    pri         : Priority,
                                    callback    : F,
                                    help        : &str,
                                    user_data   : UserData)
        -> Hook
    where
        F: FnMut(&Hexchat, &[String], &[String], &UserData)
           -> Eat
    {
        let hook = Hook::new();
        let ud   = Box::new(
                    CallbackData::new_command_data(
                                      Box::new(callback),
                                      user_data,
                                      hook.clone()));

        let ud   = Box::into_raw(ud) as *mut c_void;

        hook.set_cbd(ud);

        let help = if !help.is_empty() {
            help
        } else {
            "No help available for this command."
        };
        let name = str2cstring(name);
        let help = str2cstring(help);
        unsafe {
            // TODO - Consider making an empty help string cause a NULL to be
            //        used as hook_command()'s 5th argument.
            hook.set((self.c_hook_command)(self,
                                           name.as_ptr(),
                                           pri as i32,
                                           c_callback,
                                           help.as_ptr(),
                                           ud));
        }
        hook
    }

    /// Registers a callback to be called when a certain server event occurs.
    /// For any of these functions, more information can be found at
    /// [Hexchat Plugin Interface](https://hexchat.readthedocs.io/en/latest/plugins.html)
    /// The callback needs to be compatible with this signature:
    ///  ```
    ///  FnMut(&Hexchat, &[String], &[String], &UserData)
    ///  -> Eat
    ///  ```
    /// # Arguments
    /// * `name`        - The name of the event to listen for.
    /// * `pri`         - The priority of the callback.
    /// * `callback`    - The callback to invoke when the event occurs.
    /// * `user_data`   - The user data that gets passed back to the callback
    ///                   when it's invoked.
    /// # Returns
    /// * A `Hook` object that can be used to deregister the callback. It
    ///   doesn't need to be retained if not needed.
    ///
    pub fn hook_server<F: 'static>(&self,
                                   name        : &str,
                                   pri         : Priority,
                                   callback    : F,
                                   user_data   : UserData)
        -> Hook
    where
        F: FnMut(&Hexchat, &[String], &[String], &UserData)
           -> Eat
    {
        let hook = Hook::new();
        let ud   = Box::new(
                    CallbackData::new_command_data(
                                      Box::new(callback),
                                      user_data,
                                      hook.clone()
                                  ));
        let ud = Box::into_raw(ud) as *mut c_void;

        hook.set_cbd(ud);
        let name = str2cstring(name);
        unsafe {
            hook.set((self.c_hook_server)(self,
                                          name.as_ptr(),
                                          pri as i32,
                                          c_callback,
                                          ud));
        }
        hook
    }

    /// Registers a callback to be called when a given print event occurs. This
    /// can be any of the text events listed under Settings > Text Events.
    /// Callback needs to be compatible with this signature:
    /// ```
    /// FnMut(&Hexchat, &[String], &UserData) -> Eat
    /// ```
    /// # Arguments
    /// * `name`        - The name of the event to listen for.
    /// * `pri`         - The priority of the callback.
    /// * `callback`    - The callback to invoke when the event occurs.
    /// * `user_data`   - The user data that gets passed back to the callback
    ///                   when it's invoked.
    /// # Returns
    /// * A `Hook` object that can be used to deregister the callback. It
    ///   doesn't need to be retained if not needed.
    ///
    pub fn hook_print<F: 'static>(&self,
                                  event_name  : &str,
                                  pri         : Priority,
                                  callback    : F,
                                  user_data   : UserData)
        -> Hook
    where
        F: FnMut(&Hexchat, &[String], &UserData) -> Eat
    {
        let hook = Hook::new();
        let ud   = Box::new(
                    CallbackData::new_print_data(
                                      Box::new(callback),
                                      user_data,
                                      hook.clone()
                                  ));
        let ud = Box::into_raw(ud) as *mut c_void;

        hook.set_cbd(ud);
        let event_name = str2cstring(event_name);
        unsafe {
            hook.set((self.c_hook_print)(self,
                                         event_name.as_ptr(),
                                         pri as i32,
                                         c_print_callback,
                                         ud));
        }
        hook
    }

    /// Registers a callback to be called when a given print event occurs.
    /// The callback will be invoked with an `EventAttrs` object containing
    /// a `time_t` value for the event. The callback needs to be compatible
    /// with this signature:
    /// ```
    /// FnMut(&Hexchat, &[String], &EventAttrs, &UserData)
    /// -> Eat
    /// ```
    /// # Arguments
    /// * `name`        - The name of the event to listen for.
    /// * `pri`         - The priority of the callback.
    /// * `callback`    - The callback to invoke when the event occurs.
    /// * `user_data`   - The user data that gets passed back to the callback
    ///                   when it's invoked.
    /// # Returns
    /// * A `Hook` object that can be used to deregister the callback. It
    ///   doesn't need to be retained if not needed.
    ///
    pub fn hook_print_attrs<F: 'static>(&self,
                                        name        : &str,
                                        pri         : Priority,
                                        callback    : F,
                                        user_data   : UserData)
        -> Hook
    where
        F: FnMut(&Hexchat, &[String], &EventAttrs, &UserData)
           -> Eat
    {
        let hook = Hook::new();
        let ud   = Box::new(
                    CallbackData::new_print_attrs_data(
                                      Box::new(callback),
                                      user_data,
                                      hook.clone()
                                  ));
        let ud = Box::into_raw(ud) as *mut c_void;

        hook.set_cbd(ud);
        let name = str2cstring(name);
        unsafe {
            hook.set((self.c_hook_print_attrs)(self,
                                               name.as_ptr(),
                                               pri as i32,
                                               c_print_attrs_callback,
                                               ud));
        }
        hook
    }


    /// Sets up a callback to be invoked every `timeout` milliseconds. The
    /// callback needs to be compatible with:
    /// ```
    /// FnMut(&Hexchat, &UserData) -> i32
    /// ```
    /// # Arguments
    /// * `timeout`     - The timeout in milliseconds.
    /// * `callback`    - The `FnOnce()` callback.
    /// * `user_data`   - User data included with the callback and passed back
    ///                   to the callback during invocation.
    /// # Returns
    /// * A `Hook` object that is can be used to deregister the callback.
    ///
    pub fn hook_timer<F: 'static>(&self,
                                  timeout   : i64,
                                  callback  : F,
                                  user_data : UserData)
        -> Hook
    where
        F: FnMut(&Hexchat, &UserData) -> i32
    {
        let hook = Hook::new();
        let ud   = Box::new(CallbackData::new_timer_data(
                                            Box::new(callback),
                                            user_data,
                                            hook.clone()
                                        ));
        let ud = Box::into_raw(ud) as *mut c_void;

        hook.set_cbd(ud);

        unsafe {
            hook.set((self.c_hook_timer)(self,
                                         timeout as c_int,
                                         c_timer_callback,
                                         ud));
        }
        hook
    }

    /// This is a special case feature, used internally to enable other threads
    /// to invoke callbacks on the main thread. This function isn't exported
    /// with the rest of the functions of this class.
    /// # Arguments
    /// * `timeout`     - The timeout in milliseconds.
    /// * `callback`    - The `FnOnce()` callback.
    /// * `user_data`   - User data included with the callback and passed back
    ///                   to the callback during invocation.
    /// # Returns
    /// * A `Hook` object that is used to deregister the callback after it's
    ///   invoked.
    ///
    #[allow(dead_code)]
    pub (crate)
    fn hook_timer_once(&self,
                       timeout   : i64,
                       callback  : Box<TimerCallbackOnce>,
                       user_data : UserData)
        -> Hook
    {
        // TODO - Put the function signatures somewhere logical (?)

        let hook = Hook::new();
        let ud   = Box::new(CallbackData::new_timer_once_data(
                                            callback,
                                            user_data,
                                            hook.clone()
                                        ));
        let ud = Box::into_raw(ud) as *mut c_void;

        hook.set_cbd(ud);

        unsafe {
            hook.set((self.c_hook_timer)(self,
                                         timeout as c_int,
                                         c_timer_callback_once,
                                         ud));
        }
        hook
    }

    /// Registers a callback to be called after the given timeout.
    pub fn hook_fd<F: 'static>(&self,
                               fd        : i32,
                               flags     : i32,
                               callback  : F,
                               user_data : UserData)
        -> Hook
    where
        F: FnMut(&Hexchat, i32, i32, &UserData) -> Eat
    {
        let hook = Hook::new();
        let ud   = Box::new(CallbackData::new_fd_data(
                                            Box::new(callback),
                                            user_data,
                                            hook.clone()
                                        ));
        let ud = Box::into_raw(ud) as *mut c_void;

        hook.set_cbd(ud);

        unsafe {
            hook.set((self.c_hook_fd)(self,
                                      fd as c_int,
                                      flags as c_int,
                                      c_fd_callback,
                                      ud));
        }
        hook
    }

    /// Unhooks any Hook that was returned from a callback registration.
    /// Ownership of the user_data is transferred to the caller.
    /// Note: Hexchat unhooks all hooks automatically when a plugin is unloaded,
    /// so the client plugin doesn't have to in that case.
    /// # Arguments
    /// * `hook` - The callback hook to deregister with Hexchat.
    /// # Returns
    /// * The user data that was registered with the callback using one of the
    ///   hook commands. Ownership of this object is transferred to the caller.
    ///
    pub fn unhook(&self, hook: &mut Hook) -> UserData {
        hook.unhook()
    }


    /// Issues one of the Hexchat IRC events. The command works for any of the
    /// events listed in Settings > Text Events dialog.
    /// # Arguments
    /// * `event_name`  - The name of the Hexchat text event to send.
    /// * `var_args`    - A slice of `&str`'s containing the event's arguments.
    /// # Returns
    /// * On success, `Ok(())` is returned; otherwise, `Err(<HexchatError>)`.
    ///
    pub fn emit_print(&self, event_name: &str, var_args: &[&str])
        -> Result<(), HexchatError>
    {
        let event_attrs = EventAttrs { server_time_utc: 0 };
        self.emit_print_impl(0, &event_attrs, event_name, var_args)
    }


    /// Issues one of the Hexchat IRC events. The command works for any of the
    /// events listed in Settings > Text Events dialog.
    /// # Arguments
    /// * `event_attrs` - A reference to an `EventAttrs` struct.
    /// * `event_name`  - The name of the Hexchat text event to send.
    /// * `var_args`    - A slice of `&str`'s containing the event's arguments.
    /// # Returns
    /// * On success, `Ok(())` is returned; otherwise, `Err(<HexchatError>)`.
    ///
    pub fn emit_print_attrs(&self,
                            event_attrs : &EventAttrs,
                            event_name  : &str,
                            var_args    : &[&str])
        -> Result<(), HexchatError>
    {
        self.emit_print_impl(1, event_attrs, event_name, var_args)
    }

    /// Issues one of the Hexchat IRC events. Called internally by the public
    /// commands, `emit_print()` and `emit_print_attrs()`. The command works
    /// for any of the events listed in Settings > Text Events dialog.
    /// # Arguments
    /// * `ver`         - 0 to invoke `hc.c_emit_print()`, 1 to invoke
    ///                   `hc.c_emit_print_attrs()`.
    /// * `event_attrs` - A reference to an `EventAttrs` struct.
    /// * `event_name`  - The name of the Hexchat text event to send.
    /// * `var_args`    - A slice of `&str`'s containing the event's arguments.
    /// # Returns
    /// * On success, `Ok(())` is returned; otherwise, `Err(<HexchatError>)`.
    ///
    fn emit_print_impl(&self,
                       ver          : i32,
                       event_attrs  : &EventAttrs,
                       event_name   : &str,
                       var_args     : &[&str])
        -> Result<(), HexchatError>
    {
        let mut args   = vec![];
        let     name   = str2cstring(event_name);
        let     va_len = var_args.len();

        // We don't know if there are 6 items in var_args - so Clippy's
        // suggestion would fail. This range loop is fine.
        #[allow(clippy::needless_range_loop)]
        for i in 0..6 {
            args.push(str2cstring(if i < va_len { var_args[i] } else { "" }));
        }

        // TODO - If empty strings don't suffice as a nop param, then construct
        //        another vector containing pointers and pad with nulls.
        unsafe {
            use HexchatError::*;

            if ver == 0 {
                let result = (self.c_emit_print)(
                                    self,
                                    name.as_ptr(), args[0].as_ptr(),
                                    args[1].as_ptr(), args[2].as_ptr(),
                                    args[3].as_ptr(), args[4].as_ptr(),
                                    args[5].as_ptr(), null::<c_char>());
                if result > 0 {
                    Ok(())
                } else {
                    Err(CommandFailed(format!("`.emit_print(\"{}\", {:?})` \
                                              failed. Check the event name \
                                              and data for errors.",
                                              event_name, var_args)))
                }
            } else {
                let result = (self.c_emit_print_attrs)(
                                    self, event_attrs,
                                    name.as_ptr(), args[0].as_ptr(),
                                    args[1].as_ptr(), args[2].as_ptr(),
                                    args[3].as_ptr(), args[4].as_ptr(),
                                    args[5].as_ptr(), null::<c_char>());
                if result > 0 {
                    Ok(())
                } else {
                    Err(CommandFailed(format!(
                                      "`.emit_print_attrs(\"{}\", {:?})` \
                                      failed. Check the event name \
                                      and data for errors.",
                                      event_name, var_args)))
                }
            }
        }
    }

    /// Compares two nicknames, returning a similar value to `strcmp()`.
    /// If they're equal (0), s1 < s2 (<0 - negative), or s1 > s2 (>0 positive).
    /// # Arguments
    /// * `s1` - The first nickname to compare.
    /// * `s2` - The second.
    /// # Returns
    /// * If the first non-matching character is of lesser value for `s1`, a
    ///   negative value is returned; if `s1`'s char is greater, then a non-0
    ///   postive value is returned. 0 is returned if they match.
    ///
    pub fn nickcmp(&self, s1: &str, s2: &str) -> i32 {
        let s1 = str2cstring(s1);
        let s2 = str2cstring(s2);
        unsafe {
            (self.c_nickcmp)(self, s1.as_ptr(), s2.as_ptr())
        }
    }

    /// Converts a string with text attributes and IRC colors embedded into
    /// a plain text string. Either IRC colors, or text attributes (or both)
    /// can be stripped out of the string.
    /// # Arguments
    /// * `text`    - The string to strip.
    /// * `flags`   - One of the `StripFlags` cases (`StripMIrcColors`,
    ///               `StripTextAttributes`, `StripBoth`).
    /// # Returns
    /// * `Some(<stripped-string>)` or `None` if the operation failed.
    ///
    pub fn strip(&self, text: &str, flags: StripFlags) -> Option<String> {
        let length = text.len() as i32;
        let text  = str2cstring(text);
        let result = unsafe {
            (self.c_strip)(self, text.as_ptr(), length, flags as i32)
        };
        if !result.is_null() {
            let stripped = pchar2string(result);
            unsafe { (self.c_free)(self, result as *const c_void); }
            Some(stripped)
        } else { None }
    }

    /// Sets the currently active context to that bound to the  `Context`
    /// object. The contexts are essentially the channels the user is in
    /// and has open tabs/windows to them. The `Context` object itself has
    /// a `.set()` method that can be invoked directly, which this command
    /// invokes.
    /// # Arguments
    /// * `context` - The `Context` to make the currently active context.
    /// # Returns
    /// * A result (`Result<(), HexchatError>) where `Ok(())` indicates
    ///   the context has been switched, and a `HexchatError` if it didn't.
    ///
    pub fn set_context(&self, context: &Context) -> Result<(), HexchatError> {
        context.set()
    }

    /// Returns a `Context` object bound to the requested server/channel.
    /// The object provides methods like `print()` that will execute the
    /// Hexchat print command in that tab/window related to the context.
    /// The `Context::find()` can also be invoked to find a context.
    /// # Arguments
    /// * `network`  - The network (e.g. "freenode") of the context.
    /// * `channel`  - The channel name for the context (e.g. "##rust").
    /// # Returns
    /// *  the context was found, i.e. if the user is joined to the channel
    ///    specified currently, a `Some(<Context>)` is returned with the
    ///    context object; `None` otherwise.
    ///
    pub fn find_context(&self, network: &str, channel: &str)
        -> Option<Context>
    {
        Context::find(network, channel)
    }

    /// Returns a `Context` object for the current context (Hexchat tab/window
    /// currently visible in the app). This object can be used to invoke
    /// the Hexchat API within the context the object is bound to. Also,
    /// `Context::get()` will return a context object for the current context.
    /// # Returns
    /// * The `Context` for the currently active context. This usually means
    ///   the channel window the user has visible in the GUI.
    ///
    pub fn get_context(&self) -> Option<Context> {
        Context::get()
    }

    /// Retrieves the info data with the given `id`. It returns None on failure
    /// and `Some(String)` on success. All information is returned as String
    /// data - even the "win_ptr"/"gtkwin_ptr" values, which can be parsed
    /// and cast to pointers.
    /// # Arguments
    /// * `id` - The name/identifier for the information needed. A list of
    ///          the names for some of these can be found on the Hexchat
    ///          Plugin Interface page under `hexchat_get_info()`. These include
    ///          "channel", "network", "topic", etc.
    /// # Returns
    /// * `Some(<String>)` is returned with the string value of the info
    ///   requested. `None` is returned if there is no info with the requested
    ///   `id`.
    ///
    pub fn get_info(&self, id: &str) -> Option<String> {
        let idstr = str2cstring(id);
        let info  = unsafe { (self.c_get_info)(self, idstr.as_ptr()) };
        if !info.is_null() {
            match id {
                "win_ptr"  | "gtkwin_ptr"  => {
                    Some((info as u64).to_string())
                },
                _ => {
                    Some(pchar2string(info))
                },
            }
        } else { None }
    }

    /// Returns the requested pref value, or None if it doesn't exist. These
    /// are settings specific to Hexchat itself. It's possible to get the
    /// user's input box text cursor position via this command with
    /// "state_cursor", for instance. Other preferences can be listed with the
    /// `/set` command.
    /// # Arguments
    /// * name - The name of the pref to read.
    /// # Returns
    /// * `Some(PrefValue)` if the pref exists, `None` otherwise.
    ///
    pub fn get_prefs(&self, name: &str) -> Option<PrefValue> {
        let namecstr = str2cstring(name);
        unsafe {
            let mut str_ptr: *const c_char = ptr::null();
            let mut int_loc: c_int = 0;
            let result = (self.c_get_prefs)(self,
                                            namecstr.as_ptr(),
                                            &mut str_ptr,
                                            &mut int_loc);
            match result {
                1 => { Some(StringVal(pchar2string(str_ptr))) },
                2 => { Some(IntegerVal(int_loc as i32)) },
                3 => { Some(BoolVal( int_loc != 0 )) },
                _ => { None },
            }
        }
    }

    /// Creates an iterator for the requested Hexchat list. This is modeled
    /// after how Hexchat implements the listing feature: rather than load
    /// all the list items up front, an internal list pointer is advanced
    /// to the current item, and the fields of which are accessible through
    /// the iterator's `.get_field()` function. List iterators can also be
    /// created by invoking `ListIterator::new(<list-name>)`. See the Hexchat
    /// Plugin Interface web page for more information on the related lists.
    /// # Arguments
    /// * `name` - The name of the list to iterate over.
    /// # Returns
    /// * If the list exists, `Some(ListIterator)` is returned; `None`
    ///   otherwise.
    ///
    pub fn list_get(&self, name: &str) -> Option<ListIterator> {
        ListIterator::new(name)
    }

    /// Writes a variable name and value to a configuration file maintained
    /// by Hexchat for your plugin. These can be accessed later using
    /// `pluginpref_get()`. *A character representing the type of the pref is
    /// prepended to the value output to the config file. `pluginpref_get()`
    /// uses this when reading back values from the config file to return the
    /// correct variant of `PrefValue`.*
    /// # Arguments
    /// * `name`    - The name of the pref to set.
    /// * `value`   - The value to set - an instance of one of the `PrefValue`
    ///               types (`StringVal, IntVal, or BoolVal`).
    /// # Returns
    /// * `true` if the operation succeeds, `false` otherwise.
    ///
    pub fn pluginpref_set(&self, name: &str, value: PrefValue) -> bool {
        let sval = value.simple_ser();
        let namecstr = str2cstring(name);
        let svalcstr = CString::new(sval.clone()).unwrap();
        if sval.len() > MAX_PREF_VALUE_SIZE {
            panic!("`hexchat.pluginpref_set({}, <overflow>)`: the value \
                    exceeds the max allowable size of {:?} bytes.",
                   name, MAX_PREF_VALUE_SIZE);
        }
        unsafe {
            (self.c_pluginpref_set_str)(self,
                                        namecstr.as_ptr(),
                                        svalcstr.as_ptr()) > 0
        }
    }

    /// Retrieves, from a config file that Hexchat manages for your plugin,
    /// the value for the named variable that had been previously created using
    /// `pluginpref_set()`.
    /// # Arguments
    /// * `name` - The name of the pref to load.
    /// # Returns
    /// * `Some(<PrefValue>)` holding the value of the requested pref if it
    ///   exists, `None` otherwise.
    ///
    pub fn pluginpref_get(&self, name: &str) -> Option<PrefValue> {
        let mut buf = [0i8; MAX_PREF_VALUE_SIZE];
        let name = str2cstring(name);
        if unsafe { (self.c_pluginpref_get_str)(self,
                                                name.as_ptr(),
                                                buf.as_mut_ptr()) > 0 }
        {
            let sval = pchar2string(buf.as_ptr());
            Some(PrefValue::simple_deser(&sval))
        } else { None }
    }

    /// Returns a list of all the plugin pref variable names your plugin
    /// registered using `pluginpref_set()`. `pluginpref_get()` can be invoked
    /// with each item to get their values.
    /// # Returns
    /// * `Some(Vec<String>)` if prefs exist for the plugin, `None` otherwise.
    ///    The vector contains the names of the prefs registered.
    ///
    pub fn pluginpref_list(&self) -> Option<Vec<String>> {
        let mut buf = [0i8; MAX_PREF_LIST_SIZE];
        if unsafe { (self.c_pluginpref_list)(self, buf.as_mut_ptr()) > 0 } {
            let s = pchar2string(buf.as_ptr());
            if !s.is_empty() {
                let mut v = vec![];
                for name in s.split(',') {
                    if !name.is_empty() {
                        v.push(name.to_string());
                    }
                }
                Some(v)
            } else { None }
        } else { None }
    }

    /// Adds a dummy entry in Hexchat's list of plugins. The "plugin" registered
    /// using this command is visible in the "Plugins and Scripts" dialog and
    /// using other slash "/" commands; however, that's all it does. This
    /// command is useful when embedding a script interpreter that loads
    /// scripts as plugin code. Each script thus loaded can be visible to the
    /// user as a plugin. If writing a native plugin, you don't need to be
    /// concerned with this command as your plugin's info is registered during
    /// init from the `PluginInfo` object provided by your `plugin_get_info()`
    /// function.
    /// # Arguments
    /// * `filename`    - This can be the name of a script or binary.
    /// * `name`        - The name of the plugin.
    /// * `desc`        - The description of the plugin.
    /// * `version`     - A version string.
    /// # Returns
    /// * A new `Plugin` object that represents the plugin entry in Hexchat.
    ///   It can be used to deregister the plugin, and it (or a clone of it)
    ///   needs to be retained; otherwise, the plugin entry will be removed
    ///   when the last copy of the `Plugin` object goes out of scope.
    ///
    pub fn plugingui_add(&self,
                         filename : &str,
                         name     : &str,
                         desc     : &str,
                         version  : &str)
        -> Plugin
    {
        Plugin::new(filename, name, desc, version)
    }

    /// Removes the dummy plugin entry from the Hexchat environment. The
    /// dummy plugin would have been registered using `hexchat.plugingui_add()`.
    ///
    pub fn plugingui_remove(&self, plugin: &Plugin) {
        plugin.remove();
    }
}

/// Represents the values that can be accessed using the prefs functions of
/// the `Hexchat` object (`hc.pluginpref_get()`, `hc.pluginpref_get()`, etc.).
/// The enumeration enables the typing of the values stored and retrieved.
///
#[derive(Debug)]
pub enum PrefValue {
    StringVal(String),
    IntegerVal(i32),
    BoolVal(bool),
}
use PrefValue::*;

impl PrefValue {
    pub fn str(&self) -> String {
        match self {
            StringVal(s) => { s.clone() },
            IntegerVal(i) => { i.to_string() },
            BoolVal(b) => { b.to_string() },
        }
    }
    pub fn int(&self) -> i32 {
        match self {
            StringVal(s) => {
                if let Ok(v) = s.parse::<i32>() { v } else { 0 }
            },
            IntegerVal(i) => { *i },
            BoolVal(b) => { if *b { 1 } else { 0 } },
        }
    }

    pub fn bool(&self) -> bool {
        match self {
            StringVal(s) => {
                if let Ok(v) = s.parse::<bool>() { v } else { false }
            },
            IntegerVal(i) => { *i != 0 },
            BoolVal(b) => { *b },
        }
    }

    /// Simple config file value serialization into string.
    /// The string produced can be written to the config file Hexchat maintains.
    /// A type character is prepended ('s', 'i', or 'b').
    ///
    fn simple_ser(&self) -> String {
        match self {
            StringVal(s) => {
                let mut sstr = s.clone();
                sstr.insert(0, 's');
                sstr
            },
            IntegerVal(i) => {
                let mut istr = i.to_string();
                istr.insert(0, 'i');
                istr
            },
            BoolVal(b) => {
                let mut bstr = b.to_string();
                bstr.insert(0, 'b');
                bstr
            },
        }
    }
    /// Simple config file value deserialization from a string to a `PrefValue`.
    /// Treats the first character of the string read in from the config file
    /// as the type, which it then discards and parses the rest of the string
    /// to return the correct variant of `PrefValue`.
    ///
    fn simple_deser(s: &str) -> PrefValue {
        if s.len() > 1 {
            match &s[0..1] {
                "s" => {
                    StringVal(s.to_string())
                },
                "i" => {
                    if let Ok(v) = s[1..].parse::<i32>() {
                        IntegerVal(v)
                    } else { StringVal(s.to_string()) }
                },
                "b" => {
                    if let Ok(v) = s[1..].parse::<bool>() {
                        BoolVal(v)
                    } else { StringVal(s.to_string()) }
                },
                _ => { StringVal(s.to_string()) },
            }
        } else {
            StringVal(s.to_string())
        }
    }
}

impl From<PrefValue> for String {
    fn from(pv: PrefValue) -> String {
        pv.str()
    }
}

impl From<PrefValue> for i32 {
    fn from(pv: PrefValue) -> i32 {
        pv.int()
    }
}

impl From<PrefValue> for bool {
    fn from(pv: PrefValue) -> bool {
        pv.bool()
    }
}

impl From<String> for PrefValue {
    fn from(s: String) -> PrefValue {
        StringVal(s)
    }
}

impl From<i32> for PrefValue {
    fn from(i: i32) -> PrefValue {
        IntegerVal(i)
    }
}

impl From<bool> for PrefValue {
    fn from(b: bool) -> PrefValue {
        BoolVal(b)
    }
}

// Apply the same attribute to all items in the block, but don't compile it as
// an actual block.
macro_rules! apply_attrib {
    (#![$attr:meta] $( $it:item )*) => { $( #[$attr] $it )* }
}

apply_attrib! {
#![allow(non_camel_case_types)]

/// Some types used by the C struct below.
pub (crate) type hexchat_hook        = c_void;
pub (crate) type hexchat_list        = c_void;
pub (crate) type hexchat_context     = c_void;
#[allow(dead_code)]
pub (crate) type hexchat_event_attrs = c_void;

/// Mirrors the callback function pointer of Hexchat.
type C_Callback      = extern "C"
                       fn(word       : *const *const c_char,
                          word_eol   : *const *const c_char,
                          user_data  : *mut c_void
                         ) -> c_int;

/// Mirrors the print callback function pointer of Hexchat.
type C_PrintCallback = extern "C"
                       fn(word       : *const *const c_char,
                          user_data  : *mut c_void
                         ) -> c_int;

/// Mirrors the timer callback function pointer of Hexchat.
type C_TimerCallback = extern "C"
                       fn(user_data  : *mut c_void
                         ) -> c_int;

/// Mirrors the print attr callback function pointer of Hexchat.
type C_AttrCallback  = extern "C"
                       fn(word       : *const *const c_char,
                          attrs      : *const EventAttrs,
                          user_data  : *mut c_void
                         ) -> c_int;

/// Mirrors the FD related callback function pointer of Hexchat.
type C_FDCallback    = extern "C"
                       fn(fd         : c_int,
                          flags      : c_int,
                          udata      : *mut c_void
                         ) -> c_int;
}

/// Mirrors the C struct for `hexchat_event_attrs`. It holds the timestamps
/// for the callback invocations for callbacks registered using
/// `hexchat_print_attrs()`, and similar commands.
#[repr(C)]
pub struct EventAttrs {
    pub server_time_utc : time_t
}

impl fmt::Debug for Hexchat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Hexchat")
            .field("raw_function_pointers", &"...")
            .finish()
    }
}

/// This struct mirrors the C Hexchat struct passed to the plugin from
/// Hexchat when the plugin is loaded. Hexchat's API is implemented as a struct
/// holding callbacks to its native functions. *Don't modify* this struct,
/// unless there has been a change to the layout of it in the Hexchat C code
/// base.
#[repr(C)]
pub struct Hexchat {
    pub (crate)
    c_hook_command       : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char,
                              pri    : c_int,
                              cb     : C_Callback,
                              help   : *const c_char,
                              udata  : *mut c_void
                             ) -> *const hexchat_hook,
    pub (crate)
    c_hook_server        : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char,
                              pri    : c_int,
                              cb     : C_Callback,
                              udata  : *mut c_void
                             ) -> *const hexchat_hook,
    pub (crate)
    c_hook_print         : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char,
                              pri    : c_int,
                              cb     : C_PrintCallback,
                              udata  : *mut c_void
                             ) -> *const hexchat_hook,
    pub (crate)
    c_hook_timer         : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              timeout: c_int,
                              cb     : C_TimerCallback,
                              udata  : *mut c_void
                             ) -> *const hexchat_hook,
    pub (crate)
    c_hook_fd            : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              fd     : c_int,
                              flags  : c_int,
                              cb     : C_FDCallback,
                              udata  : *mut c_void
                             ) -> *const hexchat_hook,
    pub (crate)
    c_unhook             : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              hook   : *const hexchat_hook,
                             ) -> *const c_void,
    pub (crate)
    c_print              : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              text   : *const c_char),
    pub (crate)
    c_printf             : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              text   : *const c_char,
                              ...),
    pub (crate)
    c_command            : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              command: *const c_char),
    pub (crate)
    c_commandf           : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              command: *const c_char,
                              ...),
    pub (crate)
    c_nickcmp            : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              s1     : *const c_char,
                              s2     : *const c_char
                             ) -> c_int,
    pub (crate)
    c_set_context        : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              ctx    : *const hexchat_context
                             ) -> c_int,
    pub (crate)
    c_find_context       : unsafe extern "C"
                           fn(hp         : *const Hexchat,
                              srv_name   : *const c_char,
                              channel    : *const c_char,
                             ) -> *const hexchat_context,
    pub (crate)
    c_get_context        : unsafe extern "C"
                           fn(hp: *const Hexchat
                             ) -> *const hexchat_context,
    pub (crate)
    c_get_info           : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              id     : *const c_char,
                             ) -> *const c_char,
    pub (crate)
    c_get_prefs          : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char,
                              string : *mut *const c_char,
                              integer: *mut c_int
                             ) -> c_int,
    pub (crate)
    c_list_get           : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char
                             ) -> *const hexchat_list,
    pub (crate)
    c_list_free          : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              hclist : *const hexchat_list
                             ),
    pub (crate)
    c_list_fields        : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char
                             ) -> *const *const c_char,
    pub (crate)
    c_list_next          : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              hclist : *const hexchat_list
                             ) -> c_int,
    pub (crate)
    c_list_str           : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              hclist : *const hexchat_list,
                              field  : *const c_char
                             ) -> *const c_char,
    pub (crate)
    c_list_int           : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              hclist : *const hexchat_list,
                              field  : *const c_char
                             ) -> c_int,
    pub (crate)
    c_plugingui_add      : unsafe extern "C"
                           fn(hp         : *const Hexchat,
                              filename   : *const c_char,
                              name       : *const c_char,
                              desc       : *const c_char,
                              version    : *const c_char,
                              reserved   : *const c_char
                             ) -> *const c_void,
    pub (crate)
    c_plugingui_remove   : unsafe extern "C"
                           fn(hp: *const Hexchat, handle: *const c_void),
    pub (crate)
    c_emit_print         : unsafe extern "C"
                           fn(hp         : *const Hexchat,
                              event_name : *const c_char,
                              ...
                             ) -> c_int,
    pub (crate)
    c_read_fd            : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              src    : *const c_void,
                              buf    : *mut c_char,
                              len    : *mut c_int
                              ) -> c_int,
    pub (crate)
    c_list_time          : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              hclist : *const hexchat_list,
                              name   : *const c_char
                             ) -> time_t,
    pub (crate)
    c_gettext            : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              msgid  : *const c_char
                             ) -> *const c_char,
    pub (crate)
    c_send_modes         : unsafe extern "C"
                           fn(hp             : *const Hexchat,
                              targets        : *const *const c_char,
                              n_targets      : c_int,
                              modes_per_line : c_int,
                              sign           : c_char,
                              mode           : c_char
                             ),
    pub (crate)
    c_strip              : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              string : *const c_char,
                              len    : c_int,
                              flags  : c_int
                             ) -> *const c_char,
    pub (crate)
    c_free               : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              ptr    : *const c_void,
                             ),
    pub (crate)
    c_pluginpref_set_str : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              var    : *const c_char,
                              value  : *const c_char
                             ) -> c_int,
    pub (crate)
    c_pluginpref_get_str : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              var    : *const c_char,
                              dest   : *mut c_char
                             ) -> c_int,
    pub (crate)
    c_pluginpref_set_int : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              var    : *const c_char,
                              value  : c_int
                             ) -> c_int,
    pub (crate)
    c_pluginpref_get_int : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              var    : *const c_char
                             ) -> c_int,
    pub (crate)
    c_pluginpref_delete  : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              var    : *const c_char
                             ) -> c_int,
    pub (crate)
    c_pluginpref_list    : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              dest   : *mut c_char
                             ) -> c_int,
    pub (crate)
    c_hook_server_attrs  : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char,
                              pri    : c_int,
                              cb     : C_AttrCallback,
                              udata  : *const c_void
                             ) -> *const hexchat_hook,
    pub (crate)
    c_hook_print_attrs   : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              name   : *const c_char,
                              pri    : c_int,
                              cb     : C_AttrCallback,
                              udata  : *const c_void
                             ) -> *const hexchat_hook,
    pub (crate)
    c_emit_print_attrs   : unsafe extern "C"
                           fn(hp         : *const Hexchat,
                              attrs      : *const EventAttrs,
                              event_name : *const c_char,
                              ...
                             ) -> c_int,
    pub (crate)
    c_event_attrs_create : unsafe extern "C"
                           fn(hp: *const Hexchat) -> *mut EventAttrs,
    pub (crate)
    c_event_attrs_free   : unsafe extern "C"
                           fn(hp     : *const Hexchat,
                              attrs  : *mut EventAttrs
                             ),
}