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
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use quote::quote;

use syn::parse_macro_input;

mod macro_types;

use macro_types::*;

fn generate_help(items: &Items) -> proc_macro2::TokenStream {
    let command_help = items.inner.iter().filter_map(|x| {
        if let Item::Command(command) = x {
            let help = command.help();
            Some(quote! {
                bot.send_notice(target, #help).unwrap();
            })
        } else {
            None
        }
    });

    let matcher_help = items.inner.iter().filter_map(|x| {
        if let Item::Matcher(matcher) = x {
            let help = matcher.help();
            Some(quote! {
                bot.send_notice(target, #help).unwrap();
            })
        } else {
            None
        }
    });

    let hook_help = items.inner.iter().filter_map(|x| {
        if let Item::Hook(hook) = x {
            let help = hook.help();
            Some(quote! {
                bot.send_notice(target, #help).unwrap();
            })
        } else {
            None
        }
    });

    let gen = quote! {
        let target = message.source_nickname().unwrap();

        bot.send_notice(target, "COMMANDS:").unwrap();
        #(#command_help)*

        bot.send_notice(target, "MATCHERS:").unwrap();
        #(#matcher_help)*

        bot.send_notice(target, "HOOKS:").unwrap();
        #(#hook_help)*
    };
    gen.into()
}

/// Main entrypoint to the bot
///
/// ```no_run
/// # extern crate tokio;
/// # use macros::catinator;
/// # use anyhow::Result;
/// #
/// # fn function(bot: &catinator::Bot, msg: irc::client::prelude::Message) -> Result<()> {
/// #   Ok(())
/// # }
/// #
/// #[tokio::main]
/// async fn main() {
///   catinator!(
///     hook("name", "A short description", PRIVMSG, function)
///     command("name", "A short description", function)
///     matcher("name", "A short description", r"^\[.*?\]$", function)
///   );
/// }
/// ```
///
/// # Functions
/// All the functions share a similar pattern,
/// The first two arguments are the name and description respectively,
/// the last argument is the function that gets executed.
///
/// The function must of of the following type:
/// ```
/// fn hook(bot: &catinator::Bot, msg: irc::client::prelude::Message) -> anyhow::Result<()> {
///    Ok(())
/// }
/// ```
///
/// ## async
/// You can run async functions natively by prepending your function
/// hooks etc. with the async keyword.
///
/// ```ignore
/// async hook("name", "description", COMMAND, function)
/// ```
///
/// ## hook
/// Hooks execute a function when a specific IRC Command is received,
/// this allows for great flexibility in hooking into IRC for Authentication and the likes.
///
/// ```ignore
/// hook("name", "description", COMMAND, function)
/// ```
///
/// PRIVMSG is an IRC Command like PRIVMSG or AUTHENTICATE
/// Any of the enum variants of [the irc crate](https://docs.rs/irc/0.15.0/irc/client/prelude/enum.Command.html)
/// should work.
///
/// ## command
/// A Command is command that can be executed in any PRIVMSG and is
/// prefixed with the prefix configured in the config.toml file
///
/// ```ignore
/// command("name", "description", function)
/// ```
/// Would be ":name <whatever>" in an irc channel or private message.
///
/// ## matcher
/// A matcher matches on a PRIVMSG using regex.
///
/// ```ignore
/// matcher("name", "description", r"regex", function)
/// ```
///
/// The [regex crate](https://docs.rs/regex) is used for matching, see it's documentation for details.
///
#[proc_macro]
pub fn catinator(tokens: TokenStream) -> TokenStream {
    let items = parse_macro_input!(tokens as Items);

    let hooks = items.inner.iter().filter_map(|x| {
        if let Item::Hook(hook) = x {
            Some(hook.to_call())
        } else {
            None
        }
    });

    let commands = items.inner.iter().filter_map(|x| {
        if let Item::Command(command) = x {
            Some(command.to_call())
        } else {
            None
        }
    });

    let matchers = items.inner.iter().filter_map(|x| {
        if let Item::Matcher(command) = x {
            Some(command.to_call())
        } else {
            None
        }
    });

    let matchers_regex = items.inner.iter().filter_map(|x| {
        if let Item::Matcher(matcher) = x {
            let name = &matcher.name;
            let regex = &matcher.matcher;

            let ident = Ident::new(&name.value(), Span::call_site());

            Some(quote! {
                let #ident = regex::Regex::new(#regex).unwrap();
            })
        } else {
            None
        }
    });

    let help = generate_help(&items);

    let gen = quote! {
        use std::env;

        use futures::prelude::*;
        use tracing::{info, debug, trace};

        use irc::client::prelude::*;
        use catinator::Bot;

        #(#matchers_regex)*

        info!("starting main event loop");
        let mut stream = bot.irc_client.stream().unwrap();

        while let Some(message) = stream.next().await.transpose().unwrap() {
            trace!("{:?}", message);

            let command = message.clone().command;

            #(#hooks)*

            match &command {
                Command::PRIVMSG(_target, text) => {
                    let mut word = match text.split_ascii_whitespace().next() {
                        Some(word) => word.chars(),
                        None => continue,
                    };
                    let prefix = word.next().unwrap();
                    let rest: String = word.collect();

                    if prefix == bot.config.settings.prefix {
                        if "help" == rest {
                            #help
                        }

                        #(#commands)*
                    } else {
                        #(#matchers)*
                    }
                }
                _ => (),
            }
        }
    };

    return gen.into();
}

/// Match on a privmsg and execute the function block on it
///
/// # Examples
/// ```
/// # use anyhow::Result;
/// # use irc::client::prelude::*;
/// # use macros::privmsg;
/// #
/// # pub fn hook(bot: &catinator::Bot, msg: Message) -> Result<()> {
/// privmsg!(msg, {
///     bot.send_privmsg(
///         msg.response_target().unwrap(),
///         "bla",
///     )?;
/// })
/// # }
/// ```
#[proc_macro]
pub fn privmsg(tokens: TokenStream) -> TokenStream {
    use crate::macro_types::privmsg::Item;
    let item = parse_macro_input!(tokens as Item);

    let msg = item.msg;
    let func = item.func;

    let gen = quote! {
        match &#msg.command {
            Command::PRIVMSG(target, text) => {
                #func

                Ok(())
            }
            _ => Ok(()),
        }
    };
    return gen.into();
}