forked from poljar/rust-weechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweechat.rs
More file actions
711 lines (626 loc) · 20.9 KB
/
weechat.rs
File metadata and controls
711 lines (626 loc) · 20.9 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Main weechat module
#[cfg(feature = "async")]
use std::future::Future;
use std::{
ffi::{CStr, CString},
panic::PanicHookInfo,
path::PathBuf,
ptr, vec,
};
#[cfg(feature = "async")]
pub use async_task::Task;
use backtrace::Backtrace;
use libc::{c_char, c_int};
use weechat_sys::t_weechat_plugin;
#[cfg(feature = "async")]
use crate::executor::WeechatExecutor;
use crate::LossyCString;
/// An iterator over the arguments of a Weechat command, yielding a String value
/// for each argument.
pub struct Args {
iter: vec::IntoIter<String>,
}
/// A Weechat prefix, can be prepended to a message to notify the message
/// category.
pub enum Prefix {
/// Prefix for an error message.
Error,
/// Prefix for a networking related message.
Network,
/// Prefix for a `/me` action type of message.
Action,
/// Prefix for a message notifying that an user has joined the chat.
Join,
/// Prefix for a message notifying that an user has left the chat.
Quit,
}
impl Prefix {
fn as_str(&self) -> &str {
match self {
Prefix::Error => "error",
Prefix::Network => "network",
Prefix::Action => "action",
Prefix::Join => "join",
Prefix::Quit => "quit",
}
}
}
impl Args {
/// Create an Args object from the underlying weechat C types.
/// Expects the strings in argv to be valid utf8, if not invalid UTF-8
/// sequences are replaced with the replacement character.
///
/// # Safety
///
/// This should never be called by the user, this is called internally but
/// needs to be public because it's used in the macro expansion of the
/// plugin init method.
#[doc(hidden)]
pub unsafe fn new(argc: c_int, argv: *mut *mut c_char) -> Args {
let argc = argc as isize;
let args: Vec<String> = (0..argc)
.map(|i| {
let cstr = unsafe { CStr::from_ptr(*argv.offset(i) as *const libc::c_char) };
String::from_utf8_lossy(cstr.to_bytes()).to_string()
})
.collect();
Args { iter: args.into_iter() }
}
}
impl std::fmt::Debug for Args {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.iter.clone()).finish()
}
}
impl Iterator for Args {
type Item = String;
fn next(&mut self) -> Option<String> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl ExactSizeIterator for Args {
fn len(&self) -> usize {
self.iter.len()
}
}
impl DoubleEndedIterator for Args {
fn next_back(&mut self) -> Option<String> {
self.iter.next_back()
}
}
static mut WEECHAT: Option<Weechat> = None;
static mut WEECHAT_THREAD_ID: Option<std::thread::ThreadId> = None;
/// Main Weechat struct that encapsulates common weechat API functions.
/// It has a similar API as the weechat script API.
pub struct Weechat {
pub(crate) ptr: *mut t_weechat_plugin,
}
impl Weechat {
/// Create a Weechat object from a C t_weechat_plugin pointer.
///
/// # Arguments
///
/// * `ptr` - C pointer of the weechat plugin.
///
/// # Safety
///
/// This should never be called by the user. This is called internally.
#[doc(hidden)]
pub unsafe fn init_from_ptr(ptr: *mut t_weechat_plugin) -> Weechat {
assert!(!ptr.is_null());
WEECHAT = Some(Weechat { ptr });
WEECHAT_THREAD_ID = Some(std::thread::current().id());
std::panic::set_hook(Box::new(Weechat::panic_hook));
#[cfg(feature = "async")]
WeechatExecutor::start();
Weechat { ptr }
}
fn panic_hook(info: &PanicHookInfo) {
let current_thread = std::thread::current();
let weechat_thread = Weechat::thread_id();
let current_thread_id = current_thread.id();
let thread_name = current_thread.name().unwrap_or("Unnamed");
let backtrace = std::env::var("RUST_BACKTRACE").map(|v| v == "1").unwrap_or(false);
if current_thread_id == weechat_thread {
if backtrace {
let bt = Backtrace::new();
Weechat::print(&format!(
"{}Panic in the main Weechat thread: {}\n{:?}",
Weechat::prefix(Prefix::Error),
info,
bt,
));
} else {
Weechat::print(&format!(
"{}Panic in the main Weechat thread: {}",
Weechat::prefix(Prefix::Error),
info,
));
}
} else {
#[cfg(feature = "async")]
{
let bt = if backtrace { Some(Backtrace::new()) } else { None };
if current_thread_id != weechat_thread {
Weechat::spawn_from_thread(Weechat::thread_panic(
thread_name.to_string(),
info.to_string(),
bt,
))
}
}
#[cfg(not(feature = "async"))]
{
println!("thread '{thread_name}' panicked: {info}");
}
}
}
#[cfg(feature = "async")]
async fn thread_panic(thread_name: String, message: String, backtrace: Option<Backtrace>) {
if let Some(backtrace) = backtrace {
Weechat::print(&format!(
"{}Thread '{}{}{}' {}\n{:?}",
Weechat::prefix(Prefix::Error),
Weechat::color("red"),
thread_name,
Weechat::color("reset"),
message,
backtrace,
));
} else {
Weechat::print(&format!(
"{}Thread '{}{}{}' {}.",
Weechat::prefix(Prefix::Error),
Weechat::color("red"),
thread_name,
Weechat::color("reset"),
message
));
}
}
/// Free internal plugin data.
/// # Safety
///
/// This should never be called by the user. This is called internally.
#[doc(hidden)]
pub unsafe fn free() {
#[cfg(feature = "async")]
WeechatExecutor::free();
}
pub(crate) fn from_ptr(ptr: *mut t_weechat_plugin) -> Weechat {
assert!(!ptr.is_null());
Weechat { ptr }
}
/// Get the Weechat plugin.
///
/// # Safety
///
/// It is generally safe to call this method, the plugin pointer is valid
/// for the durration of the plugin lifetime. The problem is that many
/// Weechat objects need to have a lifetime bound to a Weechat context
/// object that is only valid for the duration of a callback.
///
/// Since this one will have a static lifetime, objects that are fetched
/// from this object may have a longer lifetime than they should.
#[allow(clippy::self_named_constructors)]
pub unsafe fn weechat() -> &'static mut Weechat {
match WEECHAT {
Some(ref mut w) => w,
None => panic!("Plugin wasn't initialized correctly"),
}
}
#[inline]
pub(crate) fn get(&self) -> &t_weechat_plugin {
unsafe { &*self.ptr }
}
/// Write a message in WeeChat log file (weechat.log).
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
pub fn log(msg: &str) {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let log_printf = weechat.get().log_printf.unwrap();
let fmt = LossyCString::new("%s");
let msg = LossyCString::new(msg);
unsafe {
log_printf(fmt.as_ptr(), msg.as_ptr());
}
}
/// Display a message on the core weechat buffer.
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
pub fn print(msg: &str) {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
#[cfg(not(weechat410))]
let printf_datetime_tags = weechat.get().printf_datetime_tags.unwrap();
#[cfg(weechat410)]
let printf_datetime_tags = weechat.get().printf_date_tags.unwrap();
let fmt = LossyCString::new("%s");
let msg = LossyCString::new(msg);
unsafe {
printf_datetime_tags(
ptr::null_mut(),
0,
#[cfg(not(weechat410))]
0,
ptr::null(),
fmt.as_ptr(),
msg.as_ptr(),
);
}
}
fn thread_id() -> std::thread::ThreadId {
*unsafe {
#[allow(static_mut_refs)]
WEECHAT_THREAD_ID.as_ref().expect(
"Weechat main thread ID wasn't found, plugin \
wasn't correctly initialized",
)
}
}
pub(crate) fn check_thread() {
#[allow(static_mut_refs)]
let weechat_thread_id = unsafe {
WEECHAT_THREAD_ID.as_ref().expect(
"Weechat main thread ID wasn't found, plugin \
wasn't correctly initialized",
)
};
if std::thread::current().id() != *weechat_thread_id {
panic!(
"Weechat methods can be only called from the main Weechat \
thread."
)
}
}
/// Return a string color code for display.
///
/// # Arguments
///
/// `color_name` - name of the color
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
pub fn color(color_name: &str) -> &str {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let weechat_color = weechat.get().color.unwrap();
let color_name = LossyCString::new(color_name);
unsafe {
let color = weechat_color(color_name.as_ptr());
CStr::from_ptr(color).to_str().expect("Weechat returned a non UTF-8 string")
}
}
/// Return a string color pair for display.
///
/// # Arguments
///
/// `foreground_color` - Name of the color that should be used for the
/// foreground.
///
/// `background_color` - Name of the color that should be used for the
/// background.
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
pub fn color_pair(foreground_color: &str, background_color: &str) -> String {
Weechat::color(&format!("{foreground_color},{background_color}")).to_string()
}
/// Retrieve a prefix value
///
/// # Arguments:
///
/// `prefix` - The name of the prefix.
///
/// Valid prefixes are:
/// * error
/// * network
/// * action
/// * join
/// * quit
///
/// An empty string will be returned if the prefix is not found
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
pub fn prefix(prefix: Prefix) -> String {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let prefix_fn = weechat.get().prefix.unwrap();
let prefix = LossyCString::new(prefix.as_str());
unsafe {
CStr::from_ptr(prefix_fn(prefix.as_ptr()))
.to_str()
.expect("Weechat returned a non UTF-8 string")
.to_string()
}
}
/// Get some info from Weechat or a plugin.
///
/// # Arguments
///
/// * `name` - name the info
///
/// * `arguments` - arguments for the info
pub fn info_get(name: &str, arguments: &str) -> Option<String> {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let info_get = weechat.get().info_get.unwrap();
let info_name = LossyCString::new(name);
let arguments = LossyCString::new(arguments);
unsafe {
let info = info_get(weechat.ptr, info_name.as_ptr(), arguments.as_ptr());
if info.is_null() {
None
} else {
Some(CStr::from_ptr(info).to_string_lossy().to_string())
}
}
}
/// Remove WeeChat colors from a string.
///
/// # Arguments
///
/// * `string` - The string that should be stripped from Weechat colors.
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
pub fn remove_color(string: &str) -> String {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let string = LossyCString::new(string);
let remove_color = weechat.get().string_remove_color.unwrap();
let string = unsafe {
let ptr = remove_color(string.as_ptr(), ptr::null());
CString::from_raw(ptr)
};
string.to_string_lossy().to_string()
}
/// Evaluate a Weechat expression and return the result.
///
/// # Arguments
///
/// * `expression` - The expression that should be evaluated.
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
//
// TODO: Add hashtable options
// TODO: This needs better docs and examples.
pub fn eval_string_expression(expression: &str) -> Result<String, ()> {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let string_eval_expression = weechat.get().string_eval_expression.unwrap();
let expr = LossyCString::new(expression);
unsafe {
let result = string_eval_expression(
expr.as_ptr(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
);
if result.is_null() {
Err(())
} else {
Ok(CStr::from_ptr(result).to_string_lossy().to_string())
}
}
}
/// Get the Weechat homedir.
pub fn home_dir() -> PathBuf {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let eval_path_home = weechat.get().string_eval_path_home.unwrap();
let path = LossyCString::new("%h");
let path = unsafe {
let result =
eval_path_home(path.as_ptr(), ptr::null_mut(), ptr::null_mut(), ptr::null_mut());
if result.is_null() {
panic!("Returned null while evaluating the Weechat home dir");
} else {
CStr::from_ptr(result).to_string_lossy().to_string()
}
};
PathBuf::from(path)
}
/// Replace a leading `~` with the home directory.
///
/// If the string does not start with `~`, the same string is returned.
pub fn expand_home(string: &str) -> String {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let expand = weechat.get().string_expand_home.unwrap();
let string = LossyCString::new(string);
let string = unsafe {
let result = expand(string.as_ptr());
if result.is_null() {
panic!("Returned null while expanding the home dir");
} else {
CStr::from_ptr(result).to_string_lossy().to_string()
}
};
string
}
/// Execute a modifier.
///
/// A modifier takes a string and modifies it in some way, Weechat has a
/// list of defined modifiers. For example to parse a string with some color
/// format (ansi, irc...) and to convert it to another format.
///
/// Returns the modified string or an empty error if the string couldn't be
/// modified.
///
/// # Arguments
///
/// * `modifier` - The name of a modifier. The list of modifiers can be
/// found in the official
/// [Weechat documentation](https://weechat.org/files/doc/stable/weechat_plugin_api.en.html#_hook_modifier_exec).
///
/// * `modifier_data` - Data that will be passed to the modifier, this
/// depends on the modifier that was chosen, consult the list of modifiers
/// in the Weechat documentation.
///
/// * `input_string` - The string that should be modified.
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
pub fn execute_modifier(
modifier: &str,
modifier_data: &str,
input_string: &str,
) -> Result<String, ()> {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let exec = weechat.get().hook_modifier_exec.unwrap();
let modifier = LossyCString::new(modifier);
let modifier_data = LossyCString::new(modifier_data);
let input_string = LossyCString::new(input_string);
unsafe {
let result =
exec(weechat.ptr, modifier.as_ptr(), modifier_data.as_ptr(), input_string.as_ptr());
if result.is_null() {
Err(())
} else {
Ok(CStr::from_ptr(result).to_string_lossy().to_string())
}
}
}
/// Update the content of a bar item, by calling its build callback.
///
/// # Arguments
///
/// * `name` - The name of the bar item that should be updated.
pub fn bar_item_update(name: &str) {
Weechat::check_thread();
let weechat = unsafe { Weechat::weechat() };
let bar_item_update = weechat.get().bar_item_update.unwrap();
let name = LossyCString::new(name);
unsafe { bar_item_update(name.as_ptr()) }
}
/// Spawn a new `Future` on the main Weechat thread.
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread or if
/// the method is called in a buffer close callback when Weechat is shutting
/// down.
///
/// The buffer close callback gets called after the plugin is dropped and
/// the executor will be stopped at that point.
///
/// # Example
///
/// ```no_run
/// use weechat::Weechat;
/// use async_std::channel::{bounded as channel, Receiver};
/// use futures::executor::block_on;
///
/// pub async fn task(receiver: Receiver<String>) {
/// loop {
/// match receiver.recv().await {
/// Ok(m) => {
/// Weechat::print(&format!("Received message: {}", m));
/// },
/// Err(e) => {
/// Weechat::print(
/// &format!("Error receiving on channel {:?}", e)
/// );
/// return;
/// }
/// }
/// }
/// }
///
/// let (tx, rx) = channel(1000);
///
/// Weechat::spawn(task(rx));
/// block_on(tx.send("Hello world".to_string()));
/// ```
#[cfg(feature = "async")]
pub fn spawn<F>(future: F) -> Task<F::Output>
where
F: Future + 'static,
F::Output: 'static,
{
Weechat::check_thread();
WeechatExecutor::spawn(future).expect("Executor isn't running anymore")
}
/// Spawn a new `Future` on the main Weechat thread, checking if the
/// executor is running.
///
/// # Panics
///
/// Panics if the method is not called from the main Weechat thread.
///
/// # Example
///
/// ```no_run
/// use weechat::Weechat;
/// use async_std::channel::{bounded as channel, Receiver};
/// use futures::executor::block_on;
///
/// pub async fn task(receiver: Receiver<String>) {
/// loop {
/// match receiver.recv().await {
/// Ok(m) => {
/// Weechat::print(&format!("Received message: {}", m));
/// },
/// Err(e) => {
/// Weechat::print(
/// &format!("Error receiving on channel {:?}", e)
/// );
/// return;
/// }
/// }
/// }
/// }
///
/// let (tx, rx) = channel(1000);
///
/// if let Some(task) = Weechat::spawn_checked(task(rx)) {
/// block_on(tx.send("Hello world".to_string()));
/// }
/// ```
#[cfg(feature = "async")]
pub fn spawn_checked<F>(future: F) -> Option<Task<F::Output>>
where
F: Future + 'static,
F::Output: 'static,
{
Weechat::check_thread();
WeechatExecutor::spawn(future)
}
/// Spawn a new `Future` on the main Weechat thread.
///
/// This can be called from any thread and will execute the future on the
/// main Weechat thread.
#[cfg(feature = "async")]
pub fn spawn_from_thread<F>(future: F)
where
F: Future<Output = ()> + Send + 'static,
{
WeechatExecutor::spawn_from_non_main(future)
}
#[cfg(feature = "async")]
pub(crate) fn spawn_buffer_cb<F>(buffer_name: String, future: F) -> Task<F::Output>
where
F: Future + 'static,
F::Output: 'static,
{
WeechatExecutor::spawn_buffer_cb(buffer_name, future)
}
}