pub struct PluginHandle<'ph> { /* private fields */ }Expand description
A hexchat plugin handle, used to register hooks and interact with hexchat.
§Examples
use hexchat_unsafe_plugin::{PluginHandle};
fn init(ph: &mut PluginHandle) {
ph.register("myplug", "0.1.0", "my awesome plug");
}Implementations§
Source§impl<'ph> PluginHandle<'ph>
impl<'ph> PluginHandle<'ph>
Sourcepub fn get_name(&self) -> &str
pub fn get_name(&self) -> &str
Returns this plugin’s registered name.
§Panics
This function panics if this plugin is not registered.
Sourcepub fn get_description(&self) -> &str
pub fn get_description(&self) -> &str
Returns this plugin’s registered description.
§Panics
This function panics if this plugin is not registered.
Sourcepub fn get_version(&self) -> &str
pub fn get_version(&self) -> &str
Returns this plugin’s registered version.
§Panics
This function panics if this plugin is not registered.
Sourcepub fn ensure_valid_context<F, R>(&mut self, f: F) -> Rwhere
F: for<'a> FnOnce(ValidContext<'a, 'ph>) -> R,
pub fn ensure_valid_context<F, R>(&mut self, f: F) -> Rwhere
F: for<'a> FnOnce(ValidContext<'a, 'ph>) -> R,
Ensures the current context is valid.
§Panics
This function may panic if it’s called while hexchat is closing.
Sourcepub fn for_entry_mut<F, R>(
&mut self,
entry: &mut PluginEntryHandle<'ph>,
f: F,
) -> Rwhere
F: FnOnce(&mut PluginHandle<'ph>) -> R,
pub fn for_entry_mut<F, R>(
&mut self,
entry: &mut PluginEntryHandle<'ph>,
f: F,
) -> Rwhere
F: FnOnce(&mut PluginHandle<'ph>) -> R,
Operates on a virtual plugin.
§Examples
use hexchat_unsafe_plugin::{PluginHandle};
fn contexts_can_be_passed_around(ph: &mut PluginHandle<'_>) {
let ctx = ph.get_context();
let mut vplug = ph.plugingui_add("foo", "bar", "baz", "qux");
ph.for_entry_mut(&mut vplug, |ph| {
ph.set_context(&ctx);
ph.get_context()
});
}Sourcepub fn plugingui_add(
&self,
filename: &str,
name: &str,
description: &str,
version: &str,
) -> PluginEntryHandle<'ph>
pub fn plugingui_add( &self, filename: &str, name: &str, description: &str, version: &str, ) -> PluginEntryHandle<'ph>
Registers a virtual plugin.
Sourcepub fn get_context(&self) -> Context<'ph>
pub fn get_context(&self) -> Context<'ph>
Returns the current context.
Note: The returned context may be invalid. Use Self::set_context to
check.
Sourcepub fn set_context(&mut self, ctx: &Context<'ph>) -> bool
pub fn set_context(&mut self, ctx: &Context<'ph>) -> bool
Sets the current context.
Returns true if the context is valid, false otherwise.
Sourcepub fn with_context<F, R>(
&mut self,
ctx: &Context<'ph>,
f: F,
) -> Result<R, InvalidContextError<F>>where
F: for<'a> FnOnce(ValidContext<'a, 'ph>) -> R,
pub fn with_context<F, R>(
&mut self,
ctx: &Context<'ph>,
f: F,
) -> Result<R, InvalidContextError<F>>where
F: for<'a> FnOnce(ValidContext<'a, 'ph>) -> R,
Do something in a valid context.
Note that this changes the active context and doesn’t change it back (but that should be fine for most use-cases).
§Errors
Returns Err(InvalidContextError(f)) if the context is invalid. See
set_context. Otherwise, returns Ok(f(...)).
Note that InvalidContextError contains the original closure. This
allows you to retry, if you so wish.
Sourcepub fn hook_command<'f, F>(
&self,
cmd: &str,
pri: i32,
help: Option<&str>,
cb: F,
) -> HookHandle<'ph, 'f>
pub fn hook_command<'f, F>( &self, cmd: &str, pri: i32, help: Option<&str>, cb: F, ) -> HookHandle<'ph, 'f>
Sets a command hook.
§Panics
Panics if this is a borrowed PluginEntryHandle.
§Examples
use hexchat_unsafe_plugin::{PluginHandle, HookHandle};
fn register_commands<'ph>(ph: &mut PluginHandle<'ph>) -> Vec<HookHandle<'ph, 'ph>> {
vec![
ph.hook_command("hello-world", hexchat_unsafe_plugin::PRI_NORM, Some("prints 'Hello, World!'"), |ph, arg, arg_eol| {
ph.print("Hello, World!");
hexchat_unsafe_plugin::EAT_ALL
}),
]
}Sourcepub fn hook_server<'f, F>(
&self,
cmd: &str,
pri: i32,
cb: F,
) -> HookHandle<'ph, 'f>
pub fn hook_server<'f, F>( &self, cmd: &str, pri: i32, cb: F, ) -> HookHandle<'ph, 'f>
Sets a server hook.
§Examples
use hexchat_unsafe_plugin::{PluginHandle, HookHandle};
fn register_server_hooks<'ph>(ph: &mut PluginHandle<'ph>) -> Vec<HookHandle<'ph, 'ph>> {
vec![
ph.hook_server("PRIVMSG", hexchat_unsafe_plugin::PRI_NORM, |ph, word, word_eol| {
if word.len() > 0 && word[0].starts_with('@') {
ph.print("We have message tags!?");
}
hexchat_unsafe_plugin::EAT_NONE
}),
]
}Sourcepub fn hook_server_attrs<'f, F>(
&self,
cmd: &str,
pri: i32,
cb: F,
) -> HookHandle<'ph, 'f>where
F: Fn(&mut PluginHandle<'ph>, Word<'_>, WordEol<'_>, EventAttrs<'_>) -> Eat + 'f + RefUnwindSafe,
'f: 'ph,
pub fn hook_server_attrs<'f, F>(
&self,
cmd: &str,
pri: i32,
cb: F,
) -> HookHandle<'ph, 'f>where
F: Fn(&mut PluginHandle<'ph>, Word<'_>, WordEol<'_>, EventAttrs<'_>) -> Eat + 'f + RefUnwindSafe,
'f: 'ph,
Sets a server hook, with attributes.
Sourcepub fn hook_print<'f, F>(
&self,
name: &str,
pri: i32,
cb: F,
) -> HookHandle<'ph, 'f>
pub fn hook_print<'f, F>( &self, name: &str, pri: i32, cb: F, ) -> HookHandle<'ph, 'f>
Sets a print hook.
§Examples
use hexchat_unsafe_plugin::{PluginHandle, HookHandle};
fn register_print_hooks<'ph>(ph: &mut PluginHandle<'ph>) -> Vec<HookHandle<'ph, 'ph>> {
vec![
ph.hook_print("Channel Message", hexchat_unsafe_plugin::PRI_NORM, |ph, arg| {
if let Some(nick) = arg.get(0) {
if *nick == "KnOwN_SpAmMeR" {
return hexchat_unsafe_plugin::EAT_ALL
}
}
hexchat_unsafe_plugin::EAT_NONE
}),
]
}Sourcepub fn hook_print_attrs<'f, F>(
&self,
name: &str,
pri: i32,
cb: F,
) -> HookHandle<'ph, 'f>
pub fn hook_print_attrs<'f, F>( &self, name: &str, pri: i32, cb: F, ) -> HookHandle<'ph, 'f>
Sets a print hook, with attributes.
§Examples
use hexchat_unsafe_plugin::{PluginHandle, HookHandle};
fn register_print_hooks<'ph>(ph: &mut PluginHandle<'ph>) -> Vec<HookHandle<'ph, 'ph>> {
vec![
ph.hook_print_attrs("Channel Message", hexchat_unsafe_plugin::PRI_NORM, |ph, arg, attrs| {
if let Some(nick) = arg.get(0) {
if *nick == "KnOwN_SpAmMeR" {
return hexchat_unsafe_plugin::EAT_ALL
}
}
hexchat_unsafe_plugin::EAT_NONE
}),
]
}Sourcepub fn hook_timer<'f, F>(&self, timeout: i32, cb: F) -> HookHandle<'ph, 'f>
pub fn hook_timer<'f, F>(&self, timeout: i32, cb: F) -> HookHandle<'ph, 'f>
Sets a timer hook
§Examples
use hexchat_unsafe_plugin::{PluginHandle, HookHandle};
fn register_timers<'ph>(ph: &mut PluginHandle<'ph>) -> Vec<HookHandle<'ph, 'ph>> {
vec![
ph.hook_timer(2000, |ph| {
ph.print("timer up!");
false
}),
]
}Sourcepub fn write_fmt(&self, fmt: Arguments<'_>)
pub fn write_fmt(&self, fmt: Arguments<'_>)
Prints to this hexchat buffer.
Glue for usage of the write! macro with hexchat.
§Panics
This panics if any broken formatting trait implementations are used in
the format arguments. See also format!.
§Examples
use hexchat_unsafe_plugin::PluginHandle;
fn hello_world(ph: &mut PluginHandle<'_>) {
write!(ph, "Hello, world!");
}Sourcepub fn get_libdirfs(&self) -> Option<CString>
pub fn get_libdirfs(&self) -> Option<CString>
Returns the path to the plugin directory, used for auto-loading plugins.
The returned CStr is not guaranteed to be valid UTF-8, but local
file system encoding.
§Examples
use std::path::PathBuf;
use hexchat_unsafe_plugin::PluginHandle;
// On Unix, we can treat this as an array-of-bytes filesystem path.
#[cfg(unix)]
fn plugin_dir(ph: &PluginHandle<'_>) -> PathBuf {
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
let libdirfs = ph.get_libdirfs().expect("should exist");
OsString::from_vec(libdirfs.into_bytes()).into()
}Sourcepub fn pluginpref_set(
&self,
var: &str,
value: &str,
) -> Result<(), PluginPrefError>
pub fn pluginpref_set( &self, var: &str, value: &str, ) -> Result<(), PluginPrefError>
Sets a pluginpref.
Note: If two pluginprefs exist with the same name, but different ASCII
case, only one will be available through pluginpref_get.
§Panics
Panics if the plugin is not registered.
§Examples
use hexchat_unsafe_plugin::PluginHandle;
fn set_str(ph: &PluginHandle<'_>, val: &str) {
ph.pluginpref_set("string", val);
}
fn set_int(ph: &PluginHandle<'_>, val: i32) {
ph.pluginpref_set("int", &format!("{}", val));
}Sourcepub fn pluginpref_get(&self, var: &str) -> Result<String, PluginPrefError>
pub fn pluginpref_get(&self, var: &str) -> Result<String, PluginPrefError>
Retrieves a pluginpref.
§Panics
Panics if the plugin is not registered.
§Examples
use hexchat_unsafe_plugin::PluginHandle;
fn get_str(ph: &PluginHandle<'_>) -> String {
ph.pluginpref_get("string").unwrap_or(String::new())
}
fn get_int(ph: &PluginHandle<'_>) -> i32 {
ph.pluginpref_get("int").unwrap_or(String::new()).parse().unwrap_or(-1)
}