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
#![recursion_limit = "256"]

extern crate proc_macro;
use proc_macro2::{Ident, Literal};
use std::collections::HashMap;

use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{parse_macro_input, Error, LitStr};

use quote::quote;

struct WeechatPluginInfo {
    plugin: syn::Ident,
    name: (usize, Literal),
    author: (usize, Literal),
    description: (usize, Literal),
    version: (usize, Literal),
    license: (usize, Literal),
}

enum WeechatVariable {
    Name(syn::LitStr),
    Author(syn::LitStr),
    Description(syn::LitStr),
    Version(syn::LitStr),
    License(syn::LitStr),
}

impl WeechatVariable {
    #[allow(clippy::wrong_self_convention)]
    fn to_pair(string: &LitStr) -> (usize, Literal) {
        let mut bytes = string.value().into_bytes();
        // Push a null byte since this goes to the C side.
        bytes.push(0);

        (bytes.len(), Literal::byte_string(&bytes))
    }

    fn as_pair(&self) -> (usize, Literal) {
        match self {
            WeechatVariable::Name(string) => WeechatVariable::to_pair(string),
            WeechatVariable::Author(string) => WeechatVariable::to_pair(string),
            WeechatVariable::Description(string) => WeechatVariable::to_pair(string),
            WeechatVariable::Version(string) => WeechatVariable::to_pair(string),
            WeechatVariable::License(string) => WeechatVariable::to_pair(string),
        }
    }

    fn default_literal() -> (usize, Literal) {
        let bytes = vec![0];
        (bytes.len(), Literal::byte_string(&bytes))
    }
}

impl Parse for WeechatVariable {
    fn parse(input: ParseStream) -> Result<Self> {
        let key: Ident = input.parse()?;
        input.parse::<syn::Token![:]>()?;
        let value = input.parse()?;

        match key.to_string().to_lowercase().as_ref() {
            "name" => Ok(WeechatVariable::Name(value)),
            "author" => Ok(WeechatVariable::Author(value)),
            "description" => Ok(WeechatVariable::Description(value)),
            "version" => Ok(WeechatVariable::Version(value)),
            "license" => Ok(WeechatVariable::License(value)),
            _ => Err(Error::new(
                key.span(),
                "expected one of name, author, description, version or license",
            )),
        }
    }
}

impl Parse for WeechatPluginInfo {
    fn parse(input: ParseStream) -> Result<Self> {
        let plugin: syn::Ident = input.parse().map_err(|_e| {
            Error::new(
                input.span(),
                "a struct that implements the Plugin trait needs to be given",
            )
        })?;
        input.parse::<syn::Token![,]>()?;

        let args: Punctuated<WeechatVariable, syn::Token![,]> =
            input.parse_terminated(WeechatVariable::parse)?;
        let mut variables = HashMap::new();

        for arg in args.pairs() {
            let variable = arg.value();
            match variable {
                WeechatVariable::Name(_) => variables.insert("name", *variable),
                WeechatVariable::Author(_) => variables.insert("author", *variable),
                WeechatVariable::Description(_) => variables.insert("description", *variable),
                WeechatVariable::Version(_) => variables.insert("version", *variable),
                WeechatVariable::License(_) => variables.insert("license", *variable),
            };
        }

        Ok(WeechatPluginInfo {
            plugin,
            name: variables.remove("name").map_or_else(
                || {
                    Err(Error::new(
                        input.span(),
                        "the name of the plugin needs to be defined",
                    ))
                },
                |v| Ok(v.as_pair()),
            )?,
            author: variables
                .remove("author")
                .map_or_else(WeechatVariable::default_literal, |v| v.as_pair()),
            description: variables
                .remove("description")
                .map_or_else(WeechatVariable::default_literal, |v| v.as_pair()),
            version: variables
                .remove("version")
                .map_or_else(WeechatVariable::default_literal, |v| v.as_pair()),
            license: variables
                .remove("license")
                .map_or_else(WeechatVariable::default_literal, |v| v.as_pair()),
        })
    }
}

/// Register a struct that implements the `Plugin` trait as a Weechat plugin.
///
/// This configures the Weechat init and end method as well as additonal plugin
/// metadata.
///
/// # Example
/// ```
/// # use weechat::{plugin, Args, Weechat, Plugin};
/// # struct SamplePlugin;
/// # impl Plugin for SamplePlugin {
/// #    fn init(weechat: &Weechat, _args: Args) -> Result<Self, ()> {
/// #        Ok(SamplePlugin)
/// #    }
/// # }
/// plugin!(
///     SamplePlugin,
///     name: "rust_sample",
///     author: "poljar",
///     description: "",
///     version: "0.1.0",
///     license: "MIT"
/// );
/// ```
#[proc_macro]
pub fn plugin(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let WeechatPluginInfo {
        plugin,
        name,
        author,
        description,
        version,
        license,
    } = parse_macro_input!(input as WeechatPluginInfo);

    let (name_len, name) = name;
    let (author_len, author) = author;
    let (description_len, description) = description;
    let (license_len, license) = license;
    let (version_len, version) = version;

    let result = quote! {
        #[doc(hidden)]
        #[no_mangle]
        pub static weechat_plugin_api_version: [u8; weechat::weechat_sys::WEECHAT_PLUGIN_API_VERSION_LENGTH] =
            *weechat::weechat_sys::WEECHAT_PLUGIN_API_VERSION;

        #[doc(hidden)]
        #[no_mangle]
        pub static weechat_plugin_name: [u8; #name_len] = *#name;

        #[doc(hidden)]
        #[no_mangle]
        pub static weechat_plugin_author: [u8; #author_len] = *#author;

        #[doc(hidden)]
        #[no_mangle]
        pub static weechat_plugin_description: [u8; #description_len] = *#description;

        #[doc(hidden)]
        #[no_mangle]
        pub static weechat_plugin_version: [u8; #version_len] = *#version;

        #[doc(hidden)]
        #[no_mangle]
        pub static weechat_plugin_license: [u8; #license_len] = *#license;

        #[doc(hidden)]
        static mut __PLUGIN: Option<#plugin> = None;

        /// This function is called when plugin is loaded by WeeChat.
        ///
        /// # Safety
        /// This function needs to be an extern C function and it can't be
        /// mangled, otherwise Weechat will not find the symbol.
        #[doc(hidden)]
        #[no_mangle]
        pub unsafe extern "C" fn weechat_plugin_init(
            plugin: *mut weechat::weechat_sys::t_weechat_plugin,
            argc: weechat::libc::c_int,
            argv: *mut *mut weechat::libc::c_char,
        ) -> weechat::libc::c_int {
            let weechat = unsafe {
                Weechat::init_from_ptr(plugin)
            };
            let args = Args::new(argc, argv);
            match <#plugin as ::weechat::Plugin>::init(&weechat, args) {
                Ok(p) => {
                    unsafe {
                        __PLUGIN = Some(p);
                    }
                    return weechat::weechat_sys::WEECHAT_RC_OK;
                }
                Err(_e) => {
                    return weechat::weechat_sys::WEECHAT_RC_ERROR;
                }
            }
        }

        /// This function is called when plugin is unloaded by WeeChat.
        ///
        /// # Safety
        /// This function needs to be an extern C function and it can't be
        /// mangled, otherwise Weechat will not find the symbol.
        #[doc(hidden)]
        #[no_mangle]
        pub unsafe extern "C" fn weechat_plugin_end(
            _plugin: *mut weechat::weechat_sys::t_weechat_plugin
        ) -> weechat::libc::c_int {
            unsafe {
                __PLUGIN = None;
                Weechat::free();
            }
            weechat::weechat_sys::WEECHAT_RC_OK
        }

        impl #plugin {
            /// Get a reference to our created plugin.
            ///
            /// # Panic
            ///
            /// Panics if this is called before the plugin `init()` method is
            /// done.
            pub fn get() -> &'static mut #plugin {
                unsafe {
                    match &mut __PLUGIN {
                        Some(p) => p,
                        None => panic!("Weechat plugin isn't initialized"),
                    }
                }
            }
        }
    };

    result.into()
}