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
//! Objects of the `Context` class represent Hexchat contexts, which are
//! associated with channels the user is currently in. These are usually
//! associated with an open window/tab in their client GUI. The `Context`
//! objects provide a convenient set of commands that mirror those in the
//! main `Hexchat` class, but when executed they perform their operation in
//! the window/tab/channel that the `Context` is bound to. The network and
//! server strings are used internally to acquire context pointers, which
//! are then used to switch context for a command operation and switch back
//! to the previously active context. On each command a check is performed to
//! ensure the `Context` is still valid. If that fails a `AcquisitionFailed`
//! error is returned with the network/channel strings as data.
use std::error;
use std::fmt;
use std::ffi::CString;
use std::rc::Rc;
use crate::hexchat::{Hexchat, hexchat_context, HexchatError};
use crate::hexchat_entry_points::PHEXCHAT;
use crate::list_iterator::ListIterator;
use crate::utils::*;
use ContextError::*;
use HexchatError::*;
#[derive(Debug)]
struct ContextData {
hc : &'static Hexchat,
network : CString,
channel : CString,
}
/// Any channel in Hexchat has an associated IRC network name and channel name.
/// The network name and channel name are closely associated with the Hexchat
/// concept of contexts. Hexchat contexts can also be thought of as the
/// tabs, or windows, open in the UI that have the user joined to their various
/// "chat rooms". To access a specific chat window in Hexchat, its context
/// can be acquired and used. This library's `Context` objects represent the
/// Hexchat contexts and can be used to interact with the specific
/// channels/windows/tabs that he user has open. For instance if your plugin
/// needs to output only to specific channels, rather than the default window
/// (which is the one currently open) - it can acquire the appropriate context
/// using `Context::find("some-network", "some-channel")`, and use the object
/// returned to invoke a command, `context.command("SAY hello!")`, or print,
/// `context.print("Hello!")`, or perform other operations.
///
#[derive(Debug, Clone)]
pub struct Context {
data : Rc<ContextData>,
}
impl Context {
/// This will create a new `Context` object holding an internal pointer to
/// the requested network/channel, if it exists. The object will be
/// returned as a `Some<Context>` if the context is found, or `None` if
/// not.
pub fn find(network: &str, channel: &str) -> Option<Self> {
let csnetwork = str2cstring(network);
let cschannel = str2cstring(channel);
let hc = unsafe { &*PHEXCHAT };
let context_ptr;
unsafe {
context_ptr = (hc.c_find_context)(hc,
csnetwork.as_ptr(),
cschannel.as_ptr());
}
if !context_ptr.is_null() {
let ctx = Context {
data: Rc::new(
ContextData {
hc,
network : csnetwork,
channel : cschannel,
})};
Some(ctx)
} else {
None
}
}
/// This will create a new `Context` that represents the currently active
/// context (window/tab, channel/network) open on the user's screen. A
/// `Result<Context, ()>` is returned with either the context, or an
/// error result if it coulnd't be obtained.
pub fn get() -> Option<Self> {
unsafe {
let hc = &*PHEXCHAT;
let ctx_ptr = (hc.c_get_context)(hc);
if !ctx_ptr.is_null() {
let nwstr = str2cstring("network");
let chstr = str2cstring("channel");
let network = (hc.c_get_info)(hc, nwstr.as_ptr());
let channel = (hc.c_get_info)(hc, chstr.as_ptr());
let ctx = Context {
data: Rc::new(
ContextData {
hc,
network : pchar2cstring(network),
channel : pchar2cstring(channel),
})
};
Some(ctx)
} else{
None
}
}
}
/// Private method to try and acquire a context pointer for a `Context`
/// object. Contexts can go bad in Hexchat: if the user shuts a tab/window
/// or leaves a channel, using a context associated with that channel
/// is no longer valid. Or the Hexchat client could disconnect; in which
/// case, using old context pointers can cause unexpected problems.
/// So `Context` objects need to reacquire the pointer for each command
/// invocation. If successful, `Ok(ptr)` is returned with the pointer value;
/// `AcquisitionFailed(network, channel)` otherwise.
#[inline]
fn acquire(&self) -> Result<*const hexchat_context, ContextError> {
let data = &*self.data;
let ptr = unsafe {
(data.hc.c_find_context)(data.hc,
data.network.as_ptr(),
data.channel.as_ptr())
};
if !ptr.is_null() {
Ok(ptr)
} else {
Err(AcquisitionFailed(cstring2string(&data.network),
cstring2string(&data.channel)))
}
}
/// Sets the currently active context to the context the `Context` object
/// points to internally.
///
pub fn set(&self) -> Result<(), ContextError> {
let data = &*self.data;
unsafe {
let ptr = self.acquire()?;
if (data.hc.c_set_context)(data.hc, ptr) > 0 {
Ok(())
} else { Err(OperationFailed(".set() failed.".to_string())) }
}
}
/// Prints the message to the `Context` object's Hexchat context. This is
/// how messages can be printed to Hexchat windows apart from the currently
/// active one.
///
pub fn print(&self, message: &str) -> Result<(), ContextError> {
let data = &*self.data;
unsafe {
let ptr = self.acquire()?;
let prior = (data.hc.c_get_context)(data.hc);
(data.hc.c_set_context)(data.hc, ptr);
data.hc.print(message);
(data.hc.c_set_context)(data.hc, prior);
Ok(())
}
}
/// Issues a print event to the context held by the `Context` object.
///
pub fn emit_print(&self, event_name: &str, var_args: &[&str])
-> Result<(), ContextError>
{
let data = &*self.data;
unsafe {
let ptr = self.acquire()?;
let prior = (data.hc.c_get_context)(data.hc);
(data.hc.c_set_context)(data.hc, ptr);
let result = data.hc.emit_print(event_name, var_args);
(data.hc.c_set_context)(data.hc, prior);
if let Err(CommandFailed(msg)) = result {
Err(OperationFailed(msg))
} else {
Ok(())
}
}
}
/// Issues a command in the context held by the `Context` object.
///
pub fn command(&self, command: &str) -> Result<(), ContextError> {
let data = &*self.data;
unsafe {
let ptr = self.acquire()?;
let prior = (data.hc.c_get_context)(data.hc);
(data.hc.c_set_context)(data.hc, ptr);
data.hc.command(command);
(data.hc.c_set_context)(data.hc, prior);
Ok(())
}
}
/// Gets information from the channel/window that the `Context` object
/// holds an internal pointer to.
///
pub fn get_info(&self, list: &str) -> Result<Option<String>, ContextError>
{
let data = &*self.data;
unsafe {
let ptr = self.acquire()?;
let prior = (data.hc.c_get_context)(data.hc);
(data.hc.c_set_context)(data.hc, ptr);
let result = data.hc.get_info(list);
(data.hc.c_set_context)(data.hc, prior);
Ok(result)
}
}
/// Gets a `ListIterator` from the context held by the `Context` object.
/// If the list doesn't exist, the `OK()` result will contain `None`;
/// otherwise it will hold the `listIterator` object for the requested
/// list.
///
pub fn list_get(&self, list: &str)
-> Result<Option<ListIterator>, ContextError>
{
let data = &*self.data;
unsafe {
let ptr = self.acquire()?;
let prior = (data.hc.c_get_context)(data.hc);
(data.hc.c_set_context)(data.hc, ptr);
let iter = ListIterator::new(list);
(data.hc.c_set_context)(data.hc, prior);
Ok(iter)
}
}
}
impl fmt::Display for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let data = &*self.data;
let network = cstring2string(&data.network);
let channel = cstring2string(&data.channel);
write!(f, "Context(\"{}\", \"{}\")", network, channel)
}
}
/// The `Context` functions may encounter an error when invoked depending on
/// whether the network name and channel name they're bound to are currently
/// valid.
/// # Variants
/// * `AcquisitionFailed` - The function was unable to acquire the desired
/// context associated with its network and channel
/// names.
/// * `OperationFailed` - The context acquisition succeeded, but there is
/// some problem with the action being performed,
/// for instance the requested list for
/// `ctx.get_listiter("foo")` doesn't exist.
#[derive(Debug, Clone)]
pub enum ContextError {
AcquisitionFailed(String, String),
OperationFailed(String),
ContextDropped(String),
}
impl error::Error for ContextError {}
impl fmt::Display for ContextError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AcquisitionFailed(network, channel) => {
write!(f, "AcquisitionFailed(\"{}\", \"{}\")",
network, channel)
},
OperationFailed(reason) => {
write!(f, "OperationFailed(\"{}\")", reason)
},
ContextDropped(reason) => {
write!(f, "ContextDropped(\"{}\")", reason)
},
}
}
}
/*
// See the following for the methods to implement:
// https://doc.rust-lang.org/src/std/error.rs.html#48-153
impl error::Error for ContextError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Option::from(match self {
AcquisitionFailed(network, channel, err) => {
match err {
Some(err) => { err },
None => { None },
}
},
OperationFailed(msg) => { None },
})
}
}
*/