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
//! # `weechat`
//!
//! This crate implements high level bindings for the Weechat plugin API.
//!
//! The bindings make it possible to create powerful Weechat plugins using Rust.
//!
//! ```no_run
//! use weechat::{
//!    buffer::Buffer,
//!    plugin, Args, Weechat, Plugin,
//! };
//!
//! struct HelloWorld;
//!
//! impl Plugin for HelloWorld {
//!     fn init(_: &Weechat, _: Args) -> Result<Self, ()> {
//!         Weechat::print("Hello from Rust");
//!         Ok(Self)
//!     }
//! }
//!
//! impl Drop for HelloWorld {
//!     fn drop(&mut self) {
//!         Weechat::print("Bye from Rust");
//!     }
//! }
//!
//! plugin!(
//!     HelloWorld,
//!     name: "hello",
//!     author: "Damir Jelić <poljar@termina.org.uk>",
//!     description: "Simple hello world Rust plugin",
//!     version: "1.0.0",
//!     license: "MIT"
//! );
//! ```

#![deny(missing_docs)]
#![cfg_attr(feature = "docs", feature(doc_cfg))]

use std::ffi::CString;

#[cfg(feature = "async")]
mod executor;
mod hashtable;
mod hdata;
mod weechat;

#[cfg(feature = "config_macro")]
#[macro_use]
mod config_macros;

#[cfg(feature = "config_macro")]
pub use paste;
#[cfg(feature = "config_macro")]
pub use strum;

pub mod buffer;
pub mod config;
pub mod hooks;
pub mod infolist;

pub use crate::weechat::{Args, Weechat};

pub use libc;
pub use weechat_macro::plugin;
pub use weechat_sys;

/// Weechat plugin trait.
///
/// Implement this trait over your struct to implement a Weechat plugin. The
/// init method will get called when Weechat loads the plugin, while the
///
/// Drop method will be called when Weechat unloads the plugin.
pub trait Plugin: Sized {
    /// The initialization method for the plugin.
    ///
    /// This will be called when Weechat loads the pluign.
    ///
    /// # Arguments
    ///
    /// * `weechat` - A borrow to a Weechat object that will be valid during the
    ///     duration of the init callback.
    ///
    /// * `args` - Arguments passed to the plugin when it is loaded.
    fn init(weechat: &Weechat, args: Args) -> Result<Self, ()>;
}

#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(r#async)))]
pub use executor::Task;

/// Status values for Weechat callbacks
pub enum ReturnCode {
    /// The callback returned successfully.
    Ok = weechat_sys::WEECHAT_RC_OK as isize,
    /// The callback returned successfully and the command will not be executed
    /// after the callback.
    OkEat = weechat_sys::WEECHAT_RC_OK_EAT as isize,
    /// The callback returned with an error.
    Error = weechat_sys::WEECHAT_RC_ERROR as isize,
}

pub(crate) struct LossyCString;

impl LossyCString {
    #[allow(clippy::new_ret_no_self)]
    pub(crate) fn new<T: AsRef<str>>(t: T) -> CString {
        match CString::new(t.as_ref()) {
            Ok(cstr) => cstr,
            Err(_) => CString::new(t.as_ref().replace('\0', "")).expect("string has no nulls"),
        }
    }
}