]> git.r.bdr.sh - rbdr/dotfiles/blame - weechat/perl/chanmon.pl
Update yum to dnf
[rbdr/dotfiles] / weechat / perl / chanmon.pl
CommitLineData
aca63701
BB
1#
2# chanmon.pl - Channel Monitoring for weechat 0.3.0
3# Version 2.3.1
4#
5# Add 'Channel Monitor' buffer/bar that you can position to show IRC channel
6# messages in a single location without constantly switching buffers
7# i.e. In a seperate window beneath the main channel buffer
8#
9# Usage:
10# /chanmon [help] | [monitor [channel [server]]] | [dynmon] | [clean default|orphan|all]
11# Command wrapper for chanmon commands
12#
13# /chmonitor [channel] [server] is used to toggle a channel monitoring on and off, this
14# can be used in the channel buffer for the channel you wish to toggle, or be given
15# with arguments e.g. /monitor #weechat freenode
16#
17# /dynmon is used to toggle 'Dynamic Channel Monitoring' on and off, this
18# will automagically stop monitoring the current active buffer, without
19# affecting regular settings (Default is off)
20#
21# /chanclean default|orphan|all will clean the config section of default 'on' entries,
22# channels you are no longer joined, or both
23#
24# /set plugins.var.perl.chanmon.alignment
25# The config setting "alignment" can be changed to;
26# "channel", "schannel", "nchannel", "channel,nick", "schannel,nick", "nchannel,nick"
27# to change how the monitor appears
28# The 'channel' value will show: "#weechat"
29# The 'schannel' value will show: "6"
30# The 'nchannel' value will show: "6:#weechat"
31#
32# /set plugins.var.perl.chanmon.short_names
33# Setting this to 'on' will trim the network name from chanmon, ala buffers.pl
34#
35# /set plugins.var.perl.chanmon.merge_private
36# Setting this to 'on' will merge private messages to chanmon's display
37#
38# /set plugins.var.perl.chanmon.color_buf
39# This turns colored buffer names on or off, you can also set a single fixed color by using a weechat color name.
40# This *must* be a valid color name, or weechat will likely do unexpected things :)
41#
42# /set plugins.var.perl.chanmon.show_aways
43# Toggles showing the Weechat away messages
44#
45# /set plugins.var.perl.chanmon.logging
46# Toggles logging status for chanmon buffer (default: off)
47#
48# /set plugins.var.perl.chanmon.output
49# Changes where output method of chanmon; takes either "bar" or "buffer" (default; buffer)
50# /set plugins.var.perl.chanmon.bar_lines
51# Changes the amount of lines the output bar will hold.
52# (Only appears once output has been set to bar, defaults to 10)
53#
54# /set plugins.var.perl.chanmon.nick_prefix
55# /set plugins.var.perl.chanmon.nick_suffix
56# Sets the prefix and suffix chars in the chanmon buffer
57# (Defaults to <> if nothing set, and blank if there is)
58#
59# servername.#channel
60# servername is the internal name for the server (set when you use /server add)
61# #channel is the channel name, (where # is whatever channel type that channel happens to be)
62#
63# Ideal set up:
64# Split the layout 70/30 (or there abouts) horizontally and load
65# Optionally, make the status and input lines only show on active windows
66#
67# /window splith 70 --> open the chanmon buffer
68# /set weechat.bar.status.conditions "active"
69# /set weechat.bar.input.conditions "active"
70
71# History:
72# 2012-04-15, KenjiE20 <longbow@longbowslair.co.uk>:
73# v2.3.1: -fix: Colour tags in bar timestamp string, bar error fixes from highmon
74# 2012-02-28, KenjiE20 <longbow@longbowslair.co.uk>:
75# v2.3: -feature: Added merge_private option to display private messages (default: off)
76# 2010-12-22, KenjiE20 <longbow@longbowslair.co.uk>:
77# v2.2: -change: Use API instead of config to find channel colours, ready for 0.3.4 and 256 colours
78# 2010-12-05, KenjiE20 <longbow@longbowslair.co.uk>:
79# v2.1.3: -change: /monitor is now /chmonitor to avoid command conflicts (thanks m4v)
80# (/chanmon monitor remains the same)
81# -fix: Add command list to inbuilt help
82# 2010-09-30, KenjiE20 <longbow@longbowslair.co.uk>:
83# v2.1.2: -fix: logging config was not correctly toggling back on (thanks to sleo for noticing)
84# 2010-09-20, m4v <lambdae2@gmail.com>:
85# v2.1.1: -fix: chanmon wasn't detecting buffers displayed on more than one window
86# 2010-08-27, KenjiE20 <longbow@longbowslair.co.uk>:
87# v2.1: -feature: Add 'nchannel' option to alignment to display buffer and name
88# 2010-04-25, KenjiE20 <longbow@longbowslair.co.uk>:
89# v2.0: Release as version 2.0
90# 2010-04-24, KenjiE20 <longbow@longbowslair.co.uk>:
91# -fix: No longer using hard-coded detection for ACTION and
92# TOPIC messages. Use config settings for ACTION printing
93# 2010-04-15, KenjiE20 <longbow@longbowslair.co.uk>:
94# v1.9: Rewrite for v2.0
95# -feature: /monitor takes arguments
96# -feature: Added /chanclean for config cleanup
97# -feature: Buffer logging option (default: off)
98# -feature: Selectable output (Bar/Buffer (default))
99# -feature: /chanmon is now a command wrapper for all commands
100# /help chanmon gives command help
101# /chanmon help gives config help
102# -code change: Made more subs to shrink the code down in places
103# -fix: Stop chanmon attempting to double load/hook
104# 2010-02-10, m4v <lambdae2@gmail.com>:
105# v1.7.1: -fix: chanmon was leaking infolists, changed how chanmon
106# detects if the buffer is displayed or not.
107# 2010-01-25, KenjiE20 <longbow@longbowslair.co.uk>:
108# v1.7: -fixture: Let chanmon be aware of nick_prefix/suffix
109# and allow custom prefix/suffix for chanmon buffer
110# (Defaults to <> if nothing set, and blank if there is)
111# -fix: Make dynamic monitoring aware of multiple windows
112# rather than just the active buffer
113# (Thanks to m4v for these)
114# 2009-09-07, KenjiE20 <longbow@longbowslair.co.uk>:
115# v1.6: -feature: colored buffer names
116# -change: chanmon version sync
117# 2009-09-05, KenjiE20 <longbow@longbowslair.co.uk>:
118# v1.5: -fix: disable buffer highlight
119# 2009-09-02, KenjiE20 <longbow@longbowslair.co.uk>:
120# v.1.4.1 -change: Stop unsightly text block on '/help'
121# 2009-08-10, KenjiE20 <longbow@longbowslair.co.uk>:
122# v1.4: -feature: In-client help added
123# -fix: Added missing help entries
124# Fix remaining ugly vars
125# 2009-07-09, KenjiE20 <longbow@longbowslair.co.uk>:
126# v.1.3.3 -fix: highlight on the channel monitor when someone /me highlights
127# 2009-07-04, KenjiE20 <longbow@longbowslair.co.uk>:
128# v.1.3.2 -fix: use new away_info tag instead of ugly regexp for away detection
129# -code: cleanup old raw callback arguement variables to nice neat named ones
130# 2009-07-04, KenjiE20 <longbow@longbowslair.co.uk>:
131# v.1.3.1 -feature(tte): Hide /away messages by default, change 'show_aways' to get them back
132# 2009-07-01, KenjiE20 <longbow@longbowslair.co.uk>:
133# v.1.3 -feature(tte): Mimic buffers.pl 'short_names'
134# 2009-06-29, KenjiE20 <longbow@longbowslair.co.uk>:
135# v.1.2.1 -fix: let the /monitor message respect the alignment setting
136# 2009-06-19, KenjiE20 <longbow@longbowslair.co.uk>:
137# v.1.2 -feature(tte): Customisable alignment
138# Thanks to 'FreakGaurd' for the idea
139# 2009-06-14, KenjiE20 <longbow@longbowslair.co.uk>:
140# v.1.1.2 -fix: don't assume chanmon buffer needs creating
141# fixes crashing with /upgrade
142# 2009-06-13, KenjiE20 <longbow@longbowslair.co.uk>:
143# v.1.1.1 -code: change from True/False to on/off for weechat consistency
144# Settings WILL NEED to be changed manually from previous versions
145# 2009-06-13, KenjiE20 <longbow@longbowslair.co.uk>:
146# v1.1: -feature: Dynamic Channel Monitoring,
147# don't display messages from active channel buffer
148# defaults to Disabled
149# Thanks to 'sjohnson' for the idea
150# -fix: don't set config entries for non-channels
151# -fix: don't assume all channels are #
152# 2009-06-12, KenjiE20 <longbow@longbowslair.co.uk>:
153# v1.0.1: -fix: glitch with tabs in IRC messages
154# 2009-06-10, KenjiE20 <longbow@longbowslair.co.uk>:
155# v1.0: Initial Public Release
156
157# Copyright (c) 2009 by KenjiE20 <longbow@longbowslair.co.uk>
158#
159# This program is free software; you can redistribute it and/or modify
160# it under the terms of the GNU General Public License as published by
161# the Free Software Foundation; either version 3 of the License, or
162# (at your option) any later version.
163#
164# This program is distributed in the hope that it will be useful,
165# but WITHOUT ANY WARRANTY; without even the implied warranty of
166# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
167# GNU General Public License for more details.
168#
169# You should have received a copy of the GNU General Public License
170# along with this program. If not, see <http://www.gnu.org/licenses/>.
171#
172
173@bar_lines = ();
174@bar_lines_time = ();
175# Replicate info earlier for in-client help
176$chanmonhelp = weechat::color("bold")."/chanmon [help] | [monitor [channel [server]]] | [dynmon] | [clean default|orphan|all]".weechat::color("-bold")."
177Command wrapper for chanmon commands
178
179".weechat::color("bold")."/chmonitor [channel] [server]".weechat::color("-bold")." is used to toggle a channel monitoring on and off, this
180 can be used in the channel buffer for the channel you wish to toggle, or be given with arguments e.g. /monitor #weechat freenode
181
182".weechat::color("bold")."/dynmon".weechat::color("-bold")." is used to toggle 'Dynamic Channel Monitoring' on and off, this will automagically stop monitoring the current active buffer, without affecting regular settings (Default is off)
183
184".weechat::color("bold")."/chanclean".weechat::color("-bold")." default|orphan|all will clean the config section of default 'on' entries, channels you are no longer joined, or both
185
186".weechat::color("bold")."/set plugins.var.perl.chanmon.alignment".weechat::color("-bold")."
187 The config setting \"alignment\" can be changed to;
188 \"channel\", \"schannel\", \"nchannel\", \"channel,nick\", \"schannel,nick\", \"nchannel,nick\"
189 to change how the monitor appears
190 The 'channel' value will show: \"#weechat\"
191 The 'schannel' value will show: \"6\"
192 The 'nchannel' value will show: \"6:#weechat\"
193
194".weechat::color("bold")."/set plugins.var.perl.chanmon.short_names".weechat::color("-bold")."
195 Setting this to 'on' will trim the network name from chanmon, ala buffers.pl
196
197".weechat::color("bold")."/set plugins.var.perl.chanmon.merge_private".weechat::color("-bold")."
198 Setting this to 'on' will merge private messages to chanmon's display
199
200".weechat::color("bold")."/set plugins.var.perl.chanmon.color_buf".weechat::color("-bold")."
201 This turns colored buffer names on or off, you can also set a single fixed color by using a weechat color name.
202 This ".weechat::color("bold")."must".weechat::color("-bold")." be a valid color name, or weechat will likely do unexpected things :)
203
204".weechat::color("bold")."/set plugins.var.perl.chanmon.show_aways".weechat::color("-bold")."
205 Toggles showing the Weechat away messages
206
207".weechat::color("bold")."/set plugins.var.perl.chanmon.logging".weechat::color("-bold")."
208 Toggles logging status for chanmon buffer (default: off)
209
210".weechat::color("bold")."/set plugins.var.perl.chanmon.output".weechat::color("-bold")."
211 Changes where output method of chanmon; takes either \"bar\" or \"buffer\" (default; buffer)
212".weechat::color("bold")."/set plugins.var.perl.chanmon.bar_lines".weechat::color("-bold")."
213 Changes the amount of lines the output bar will hold.
214 (Only appears once output has been set to bar, defaults to 10)
215
216".weechat::color("bold")."/set plugins.var.perl.chanmon.nick_prefix".weechat::color("-bold")."
217".weechat::color("bold")."/set plugins.var.perl.chanmon.nick_suffix".weechat::color("-bold")."
218 Sets the prefix and suffix chars in the chanmon buffer
219 (Defaults to <> if nothing set, and blank if there is)
220
221".weechat::color("bold")."servername.#channel".weechat::color("-bold")."
222 servername is the internal name for the server (set when you use /server add)
223 #channel is the channel name, (where # is whatever channel type that channel happens to be)
224
225".weechat::color("bold")."Ideal set up:".weechat::color("-bold")."
226Split the layout 70/30 (or there abouts) horizontally and load
227Optionally, make the status and input lines only show on active windows
228
229".weechat::color("bold")."/window splith 70 --> open the chanmon buffer".weechat::color("-bold")."
230".weechat::color("bold")."/set weechat.bar.status.conditions \"active\"".weechat::color("-bold")."
231".weechat::color("bold")."/set weechat.bar.input.conditions \"active\"".weechat::color("-bold");
232# Print verbose help
233sub print_help
234{
235 weechat::print("", "\t".weechat::color("bold")."Chanmon Help".weechat::color("-bold")."\n\n");
236 weechat::print("", "\t".$chanmonhelp);
237 return weechat::WEECHAT_RC_OK;
238}
239
240# Bar item build
241sub chanmon_bar_build
242{
243 # Get max lines
244 $max_lines = weechat::config_get_plugin("bar_lines");
245 $max_lines = $max_lines ? $max_lines : 10;
246 $str = '';
247 $align_num = 0;
248 $count = 0;
249 # Keep lines within max
250 while ($#bar_lines > $max_lines)
251 {
252 shift(@bar_lines);
253 shift(@bar_lines_time);
254 }
255 # So long as we have some lines, build a string
256 if (@bar_lines)
257 {
258 # Build loop
259 foreach(@bar_lines)
260 {
261 # Find max align needed
262 $prefix_num = (index(weechat::string_remove_color($_, ""), " | "));
263 $align_num = $prefix_num if ($prefix_num > $align_num);
264 }
265 foreach(@bar_lines)
266 {
267 # Get align for this line
268 $prefix_num = (index(weechat::string_remove_color($_, ""), " | "));
269
270 # Make string
271 $str = $str.$bar_lines_time[$count]." ".(" " x ($align_num - $prefix_num)).$_."\n";
272 # Increment count for sync with time list
273 $count++;
274 }
275 }
276 return $str;
277}
278
279# Make a new bar
280sub chanmon_bar_open
281{
282 # Make the bar item
283 weechat::bar_item_new("chanmon", "chanmon_bar_build", "");
284
285 $chanmon_bar = weechat::bar_new ("chanmon", "off", 100, "root", "", "bottom", "vertical", "vertical", 0, 0, "default", "cyan", "default", "on", "chanmon");
286
287 return weechat::WEECHAT_RC_OK;
288}
289# Close bar
290sub chanmon_bar_close
291{
292 # Find if bar exists
293 $chanmon_bar = weechat::bar_search("chanmon");
294 # If is does, close it
295 if ($chanmon_bar ne "")
296 {
297 weechat::bar_remove($chanmon_bar);
298 }
299
300 # Find if bar item exists
301 $chanmon_bar_item = weechat::bar_item_search("chanmon_bar");
302 # If is does, close it
303 if ($chanmon_bar_item ne "")
304 {
305 weechat::bar_remove($chanmon_bar_item);
306 }
307
308 @bar_lines = ();
309 return weechat::WEECHAT_RC_OK;
310}
311
312# Make a new buffer
313sub chanmon_buffer_open
314{
315 # Search for pre-existing buffer
316 $chanmon_buffer = weechat::buffer_search("perl", "chanmon");
317
318 # Make a new buffer
319 if ($chanmon_buffer eq "")
320 {
321 $chanmon_buffer = weechat::buffer_new("chanmon", "chanmon_buffer_input", "", "chanmon_buffer_close", "");
322 }
323
324 # Turn off notify, highlights
325 if ($chanmon_buffer ne "")
326 {
327 weechat::buffer_set($chanmon_buffer, "notify", "0");
328 weechat::buffer_set($chanmon_buffer, "highlight_words", "-");
329 weechat::buffer_set($chanmon_buffer, "title", "Channel Monitor");
330 # Set no_log
331 if (weechat::config_get_plugin("logging") eq "off")
332 {
333 weechat::buffer_set($chanmon_buffer, "localvar_set_no_log", "1");
334 }
335 }
336 return weechat::WEECHAT_RC_OK;
337}
338# Buffer input has no action
339sub chanmon_buffer_input
340{
341 return weechat::WEECHAT_RC_OK;
342}
343# Close up
344sub chanmon_buffer_close
345{
346 $chanmon_buffer = "";
347 return weechat::WEECHAT_RC_OK;
348}
349
350# Chanmon command wrapper
351sub chanmon_command_cb
352{
353 $data = $_[0];
354 $buffer = $_[1];
355 $args = $_[2];
356 my $cmd = '';
357 my $arg = '';
358
359 if ($args ne "")
360 {
361 # Split argument up
362 @arg_array = split(/ /,$args);
363 # Take first as command
364 $cmd = shift(@arg_array);
365 # Rebuild string to pass to subs
366 if (@arg_array)
367 {
368 $arg = join(" ", @arg_array);
369 }
370 }
371
372 # Help command
373 if ($cmd eq "" || $cmd eq "help")
374 {
375 print_help();
376 }
377 # /monitor command
378 elsif ($cmd eq "monitor")
379 {
380 chanmon_toggle($data, $buffer, $arg);
381 }
382 # /dynmon command
383 elsif ($cmd eq "dynmon")
384 {
385 chanmon_dyn_toggle();
386 }
387 # /chanclean command
388 elsif ($cmd eq "clean")
389 {
390 chanmon_config_clean($data, $buffer, $arg);
391 }
392 return weechat::WEECHAT_RC_OK;
393}
394
395# Clean up config entries
396sub chanmon_config_clean
397{
398 $data = $_[0];
399 $buffer = $_[1];
400 $args = $_[2];
401
402 # Don't do anything if bad option given
403 if ($args ne "default" && $args ne "orphan" && $args ne "all")
404 {
405 weechat::print("", "\tchanmon.pl: Unknown option");
406 return weechat::WEECHAT_RC_OK;
407 }
408
409 @chans = ();
410 # Load an infolist of chanmon options
411 $infolist = weechat::infolist_get("option", "", "*chanmon*");
412 while (weechat::infolist_next($infolist))
413 {
414 $name = weechat::infolist_string($infolist, "option_name");
415 $name =~ s/perl\.chanmon\.(\w*)\.([#&\+!])(.*)/$1.$2$3/;
416 if ($name =~ /^(.*)\.([#&\+!])(.*)$/)
417 {
418 $action = 0;
419 # Clean up all 'on's
420 if ($args eq "default" || $args eq "all")
421 {
422 # If value in config is "on"
423 if (weechat::config_get_plugin($name) eq "on")
424 {
425 # Unset and if successful flag as changed
426 $rc = weechat::config_unset_plugin($name);
427 if ($rc eq weechat::WEECHAT_CONFIG_OPTION_UNSET_OK_REMOVED)
428 {
429 $action = 1;
430 }
431 }
432 }
433 # Clean non joined
434 if ($args eq "orphan" || $args eq "all")
435 {
436 # If we can't find the buffer for this entry
437 if (weechat::buffer_search("irc", $name) eq "")
438 {
439 # Unset and if successful flag as changed
440 $rc = weechat::config_unset_plugin($name);
441 if ($rc eq weechat::WEECHAT_CONFIG_OPTION_UNSET_OK_REMOVED)
442 {
443 $action = 1;
444 }
445 }
446 }
447 # Add changed entry names to list
448 push (@chans, $name) if ($action);
449 }
450 }
451 weechat::infolist_free($infolist);
452 # If channels were cleaned from config
453 if (@chans)
454 {
455 # If only one entry
456 if (@chans == 1)
457 {
458 $str = "\tchanmon.pl: Cleaned ".@chans." entry from the config:";
459 }
460 else
461 {
462 $str = "\tchanmon.pl: Cleaned ".@chans." entries from the config:";
463 }
464 # Build a list of channels
465 foreach(@chans)
466 {
467 $str = $str." ".$_;
468 }
469 # Print what happened
470 weechat::print("",$str);
471 }
472 # Config seemed to be clean
473 else
474 {
475 weechat::print("", "\tchanmon.pl: No entries removed");
476 }
477 return weechat::WEECHAT_RC_OK;
478}
479
480# Check config elements
481sub chanmon_config_init
482{
483 # Alignment default
484 if (!(weechat::config_is_set_plugin ("alignment")))
485 {
486 weechat::config_set_plugin("alignment", "channel");
487 }
488 if (weechat::config_get_plugin("alignment") eq "")
489 {
490 weechat::config_set_plugin("alignment", "none");
491 }
492
493 # Dynmon default
494 if (!(weechat::config_is_set_plugin ("dynamic")))
495 {
496 weechat::config_set_plugin("dynamic", "off");
497 }
498
499 # Short name default
500 if (!(weechat::config_is_set_plugin ("short_names")))
501 {
502 weechat::config_set_plugin("short_names", "off");
503 }
504
505 # Coloured names default
506 if (!(weechat::config_is_set_plugin ("color_buf")))
507 {
508 weechat::config_set_plugin("color_buf", "on");
509 }
510
511 # Away message default
512 if (!(weechat::config_is_set_plugin ("show_aways")))
513 {
514 weechat::config_set_plugin("show_aways", "off");
515 }
516
517 # chanmon log default
518 if (!(weechat::config_is_set_plugin ("logging")))
519 {
520 weechat::config_set_plugin("logging", "off");
521 }
522
523 # Output default
524 if (!(weechat::config_is_set_plugin ("output")))
525 {
526 weechat::config_set_plugin("output", "buffer");
527 }
528
529 # Private message merging
530 if (!(weechat::config_is_set_plugin ("merge_private")))
531 {
532 weechat::config_set_plugin("merge_private", "off");
533 }
534
535 # Check for exisiting prefix/suffix chars, and setup accordingly
536 $prefix = weechat::config_get("irc.look.nick_prefix");
537 $prefix = weechat::config_string($prefix);
538 $suffix = weechat::config_get("irc.look.nick_suffix");
539 $suffix = weechat::config_string($suffix);
540
541 if (!(weechat::config_is_set_plugin("nick_prefix")))
542 {
543 if ($prefix eq "" && $suffix eq "")
544 {
545 weechat::config_set_plugin("nick_prefix", "<");
546 }
547 else
548 {
549 weechat::config_set_plugin("nick_prefix", "");
550 }
551 }
552
553 if (!(weechat::config_is_set_plugin("nick_suffix")))
554 {
555 if ($prefix eq "" && $suffix eq "")
556 {
557 weechat::config_set_plugin("nick_suffix", ">");
558 }
559 else
560 {
561 weechat::config_set_plugin("nick_suffix", "");
562 }
563 }
564}
565
566# Get config updates
567sub chanmon_config_cb
568{
569 $point = $_[0];
570 $name = $_[1];
571 $value = $_[2];
572
573 $name =~ s/^plugins\.var\.perl\.chanmon\.//;
574
575 # Set logging on buffer
576 if ($name eq "logging")
577 {
578 # Search for pre-existing buffer
579 $chanmon_buffer = weechat::buffer_search("perl", "chanmon");
580 if ($value eq "off")
581 {
582 weechat::buffer_set($chanmon_buffer, "localvar_set_no_log", "1");
583 }
584 else
585 {
586 weechat::buffer_set($chanmon_buffer, "localvar_del_no_log", "");
587 }
588 }
589 # Output changer
590 elsif ($name eq "output")
591 {
592 if ($value eq "bar")
593 {
594 # Search for pre-existing buffer
595 $chanmon_buffer = weechat::buffer_search("perl", "chanmon");
596 # Close if it exists
597 if ($chanmon_buffer ne "")
598 {
599 weechat::buffer_close($chanmon_buffer)
600 }
601
602 # Output bar lines default
603 if (!(weechat::config_is_set_plugin ("bar_lines")))
604 {
605 weechat::config_set_plugin("bar_lines", "10");
606 }
607 # Make a bar if doesn't exist
608 chanmon_bar_open();
609 }
610 elsif ($value eq "buffer")
611 {
612 # If a bar exists, close it
613 chanmon_bar_close();
614 # Open buffer
615 chanmon_buffer_open();
616 }
617
618 }
619 return weechat::WEECHAT_RC_OK;
620}
621
622# Toggle dynamic monitoring on/off
623sub chanmon_dyn_toggle
624{
625 if (weechat::config_get_plugin("dynamic") eq "off")
626 {
627 weechat::config_set_plugin("dynamic", "on");
628 chanmon_print("Dynamic Channel Monitoring Enabled");
629 return weechat::WEECHAT_RC_OK;
630 }
631 elsif (weechat::config_get_plugin("dynamic") eq "on")
632 {
633 weechat::config_set_plugin("dynamic", "off");
634 chanmon_print("Dynamic Channel Monitoring Disabled");
635 return weechat::WEECHAT_RC_OK;
636 }
637}
638
639# Set up weechat hooks / commands
640sub chanmon_hook
641{
642 weechat::hook_print("", "", "", 0, "chanmon_new_message", "");
643 weechat::hook_command("chmonitor", "Toggles monitoring for a channel", "[channel [server]]", " channel: What channel to toggle monitoring for\n server: Internal server name, if channel is on more than one server", "%(irc_channels) %(irc_servers)", "chanmon_toggle", "");
644 weechat::hook_command("dynmon", "Toggles 'dynamic' monitoring (auto-disable monitoring for current channel)", "", "", "", "chanmon_dyn_toggle", "");
645 weechat::hook_command("chanclean", "Chanmon config clean up", "default|orphan|all", " default: Cleans all config entries with the default \"on\" value\n orphan: Cleans all config entries for channels you aren't currently joined\n all: Does both defaults and orphan", "default|orphan|all", "chanmon_config_clean", "");
646
647 weechat::hook_command("chanmon", "Chanmon help", "[help] | [monitor [channel [server]]] | [dynmon] | [clean default|orphan|all]", " help: Print help for chanmon\n monitor: Toggles monitoring for a channel (/chmonitor)\n dynmon: Toggles 'dynamic' monitoring (auto-disable monitoring for current channel) (/dynmon)\n clean: Chanmon config clean up (/chanclean)", "help || monitor %(irc_channels) %(irc_servers) || dynmon || clean default|orphan|all", "chanmon_command_cb", "");
648
649 weechat::hook_config("plugins.var.perl.chanmon.*", "chanmon_config_cb", "");
650}
651
652# Main body, Callback for hook_print
653sub chanmon_new_message
654{
655 my $net = "";
656 my $chan = "";
657 my $nick = "";
658 my $outstr = "";
659 my $window_displayed = "";
660 my $dyncheck = "0";
661
662# DEBUG point
663# $string = "\t"."0: ".$_[0]." 1: ".$_[1]." 2: ".$_[2]." 3: ".$_[3]." 4: ".$_[4]." 5: ".$_[5]." 6: ".$_[6]." 7: ".$_[7];
664# weechat::print("", "\t".$string);
665
666 $cb_datap = $_[0];
667 $cb_bufferp = $_[1];
668 $cb_date = $_[2];
669 $cb_tags = $_[3];
670 $cb_disp = $_[4];
671 $cb_high = $_[5];
672 $cb_prefix = $_[6];
673 $cb_msg = $_[7];
674
675 # Only work on messages and topic notices
676 if ($cb_tags =~ /irc_privmsg/ || $cb_tags =~ /irc_topic/)
677 {
678 # Check buffer name is an IRC channel or private message when enabled
679 $bufname = weechat::buffer_get_string($cb_bufferp, 'name');
680 if ($bufname =~ /(.*)\.([#&\+!])(.*)/ || (weechat::config_get_plugin("merge_private") eq "on" && $cb_tags =~ /notify_private/))
681 {
682 # Are we running on this channel
683 if (weechat::config_get_plugin($bufname) ne "off" && $cb_disp eq "1")
684 {
685 # Are we running dynamically
686 if (weechat::config_get_plugin("dynamic") eq "on")
687 {
688 # Check if this buffer is shown in a window somewhere
689 $window_displayed = weechat::buffer_get_integer($cb_bufferp, "num_displayed");
690 if ($window_displayed ne 0)
691 {
692 # Stop running
693 return weechat::WEECHAT_RC_OK;
694 }
695 }
696
697 # Format nick
698 # Line isn't action or topic notify
699 if (!($cb_tags =~ /irc_action/) && !($cb_tags =~ /irc_topic/))
700 {
701 # Highlight
702 if ($cb_high eq "1")
703 {
704 # Strip nick colour
705 $uncolnick = weechat::string_remove_color($cb_prefix, "");
706 # Format nick
707 $nick = " ".weechat::config_get_plugin("nick_prefix").weechat::color("chat_highlight").$uncolnick.weechat::color("reset").weechat::config_get_plugin("nick_suffix");
708 }
709 # Normal line
710 else
711 {
712 # Format nick
713 $nick = " ".weechat::config_get_plugin("nick_prefix").$cb_prefix.weechat::color("reset").weechat::config_get_plugin("nick_suffix");
714 }
715 }
716 # Topic line
717 elsif ($cb_tags =~ /irc_topic/)
718 {
719
720 $nick = " ".$cb_prefix.weechat::color("reset");
721 }
722 # Action line
723 else
724 {
725 # Highlight
726 if ($cb_high eq "1")
727 {
728 $uncolnick = weechat::string_remove_color($cb_prefix, "");
729 $nick = weechat::color("chat_highlight").$uncolnick.weechat::color("reset");
730 }
731 # Normal line
732 else
733 {
734 $nick = $cb_prefix.weechat::color("reset");
735 }
736 }
737 # Send to output
738 chanmon_print ($cb_msg, $cb_bufferp, $nick);
739 }
740 }
741 }
742 # Special outgoing ACTION & away_info catcher
743 elsif ($cb_tags eq "" || $cb_tags =~ /away_info/ && weechat::config_get_plugin("show_aways") eq "on" )
744 {
745 # Check buffer name is an IRC channel or private message when enabled
746 $bufname = weechat::buffer_get_string($cb_bufferp, 'name');
747 if ($bufname =~ /(.*)\.([#&\+!])(.*)/ || (weechat::config_get_plugin("merge_private") eq "on" && $cb_tags =~ /notify_private/))
748 {
749 # Are we running dynamically
750 if (weechat::config_get_plugin("dynamic") eq "on")
751 {
752 # Check if this buffer is shown in a window somewhere
753 $window_displayed = weechat::buffer_get_integer($cb_bufferp, "num_displayed");
754 if ($window_displayed eq 1)
755 {
756 # Stop running
757 return weechat::WEECHAT_RC_OK;
758 }
759 }
760
761 $net = $1;
762 $mynick = weechat::info_get("irc_nick", $net);
763 if ($cb_msg =~ $mynick)
764 {
765 $action_colour = weechat::color(weechat::config_string(weechat::config_get("weechat.color.chat_prefix_action")));
766 $action_prefix = weechat::config_string(weechat::config_get("weechat.look.prefix_action"));
767 $nick_self_colour = weechat::color(weechat::config_string(weechat::config_get("weechat.color.chat_nick_self")));
768 $nick = $action_colour.$action_prefix.$nick_self_colour.$nick.weechat::color("reset");
769 # Send to output
770 chanmon_print ($cb_msg, $cb_bufferp, $nick);
771 }
772 }
773 }
774 return weechat::WEECHAT_RC_OK;
775}
776
777# Output formatter and printer takes (msg bufpointer nick)
778sub chanmon_print
779{
780 $cb_msg = $_[0];
781 my $cb_bufferp = $_[1] if ($_[1]);
782 my $nick = $_[2] if ($_[2]);
783
784 #Normal channel message
785 if ($cb_bufferp && $nick)
786 {
787 # Format buffer name
788 $bufname = format_buffer_name($cb_bufferp);
789
790 # If alignment is #channel | nick msg
791 if (weechat::config_get_plugin("alignment") eq "channel")
792 {
793 $nick =~ s/\s(.*)/$1/;
794 # Build string
795 $outstr = $bufname."\t".$nick." ".$cb_msg;
796 }
797 # or if it is channel number | nick msg
798 elsif (weechat::config_get_plugin("alignment") eq "schannel")
799 {
800 $nick =~ s/\s(.*)/$1/;
801 # Use channel number instead
802 $bufname = weechat::color("chat_prefix_buffer").weechat::buffer_get_integer($cb_bufferp, 'number').weechat::color("reset");
803 # Build string
804 $outstr = $bufname."\t".$nick." ".$cb_msg;
805 }
806 # or if it is number:#channel | nick msg
807 elsif (weechat::config_get_plugin("alignment") eq "nchannel")
808 {
809 $nick =~ s/\s(.*)/$1/;
810 # Place channel number in front of formatted name
811 $bufname = weechat::color("chat_prefix_buffer").weechat::buffer_get_integer($cb_bufferp, 'number').":".weechat::color("reset").$bufname;
812 # Build string
813 $outstr = $bufname."\t".$nick." ".$cb_msg;
814 }
815 # or if it is #channel nick | msg
816 elsif (weechat::config_get_plugin("alignment") eq "channel,nick")
817 {
818 # Build string
819 $outstr = $bufname.":".$nick."\t".$cb_msg;
820 }
821 # or if it is channel number nick | msg
822 elsif (weechat::config_get_plugin("alignment") eq "schannel,nick")
823 {
824 # Use channel number instead
825 $bufname = weechat::color("chat_prefix_buffer").weechat::buffer_get_integer($cb_bufferp, 'number').weechat::color("reset");
826 # Build string
827 $outstr = $bufname.":".$nick."\t".$cb_msg;
828 }
829 # or if it is number:#channel nick | msg
830 elsif (weechat::config_get_plugin("alignment") eq "nchannel,nick")
831 {
832 # Place channel number in front of formatted name
833 $bufname = weechat::color("chat_prefix_buffer").weechat::buffer_get_integer($cb_bufferp, 'number').":".weechat::color("reset").$bufname;
834 # Build string
835 $outstr = $bufname.":".$nick."\t".$cb_msg;
836 }
837 # or finally | #channel nick msg
838 else
839 {
840 # Build string
841 $outstr = "\t".$bufname.":".$nick." ".$cb_msg;
842 }
843 }
844 # chanmon channel toggle message
845 elsif ($cb_bufferp && !$nick)
846 {
847 # Format buffer name
848 $bufname = format_buffer_name($cb_bufferp);
849
850 # If alignment is #channel * | *
851 if (weechat::config_get_plugin("alignment") =~ /channel/)
852 {
853 # If it's actually channel number * | *
854 if (weechat::config_get_plugin("alignment") =~ /schannel/)
855 {
856 # Use channel number instead
857 $bufname = weechat::color("chat_prefix_buffer").weechat::buffer_get_integer($cb_bufferp, 'number').weechat::color("reset");
858 }
859 # Or if it's actually number:#channel * | *
860 if (weechat::config_get_plugin("alignment") =~ /nchannel/)
861 {
862 # Place channel number in front of formatted name
863 $bufname = weechat::color("chat_prefix_buffer").weechat::buffer_get_integer($cb_bufferp, 'number').":".weechat::color("reset").$bufname;
864 }
865 $outstr = $bufname."\t".$cb_msg;
866 }
867 # or if alignment is | *
868 else
869 {
870 $outstr = $bufname.": ".$cb_msg;
871 }
872 }
873 # chanmon dynmon
874 elsif (!$cb_bufferp && !$nick)
875 {
876 $outstr = "\t".$cb_msg;
877 }
878
879 # Send string to buffer
880 if (weechat::config_get_plugin("output") eq "buffer")
881 {
882 # Search for and confirm buffer
883 $chanmon_buffer = weechat::buffer_search("perl", "chanmon");
884 # Print
885 weechat::print($chanmon_buffer, $outstr);
886 }
887 elsif (weechat::config_get_plugin("output") eq "bar")
888 {
889 # Add time string
890 use POSIX qw(strftime);
891 $time = strftime(weechat::config_string(weechat::config_get("weechat.look.buffer_time_format")), localtime);
892 # Colourise
893 if ($time =~ /\$\{\w+\}/) # Coloured string
894 {
895 while ($time =~ /\$\{(\w+)\}/)
896 {
897 $color = weechat::color($1);
898 $time =~ s/\$\{\w+\}/$color/;
899 }
900 }
901 else # Default string
902 {
903 $colour = weechat::color(weechat::config_string(weechat::config_get("weechat.color.chat_time_delimiters")));
904 $reset = weechat::color("reset");
905 $time =~ s/(\d*)(.)(\d*)/$1$colour$2$reset$3/g;
906 }
907 # Push updates to bar lists
908 push (@bar_lines_time, $time);
909
910 # Change tab char
911 $delim = weechat::color(weechat::config_string(weechat::config_get("weechat.color.chat_delimiters")))." | ".weechat::color("reset");
912 $outstr =~ s/\t/$delim/;
913
914 push (@bar_lines, $outstr);
915 # Trigger update
916 weechat::bar_item_update("chanmon");
917 }
918}
919
920# Start the output display
921sub chanmon_start
922{
923 if (weechat::config_get_plugin("output") eq "buffer")
924 {
925 chanmon_buffer_open();
926 }
927 elsif (weechat::config_get_plugin("output") eq "bar")
928 {
929 chanmon_bar_open();
930 }
931}
932
933# Takes two optional args (channel server), toggles monitoring on/off
934sub chanmon_toggle
935{
936 $data = $_[0];
937 $buffer = $_[1];
938 $args = $_[2];
939
940 # Check if we've been told what channel to act on
941 if ($args ne "")
942 {
943 # Split argument up
944 @arg_array = split(/ /,$args);
945 # Check if a server was given
946 if ($arg_array[1])
947 {
948 # Find matching
949 $bufp = weechat::buffer_search("irc", $arg_array[1].".".$arg_array[0]);
950 }
951 else
952 {
953 $found_chans = 0;
954 # Loop through defined servers
955 $infolist = weechat::infolist_get("buffer", "", "");
956 while (weechat::infolist_next($infolist))
957 {
958 # Only interesting in IRC buffers
959 if (weechat::infolist_string($infolist, "plugin_name") eq "irc")
960 {
961 # Find buffers that maych
962 $sname = weechat::infolist_string($infolist, "short_name");
963 if ($sname eq $arg_array[0])
964 {
965 $found_chans++;
966 $bufp = weechat::infolist_pointer($infolist, "pointer");
967 }
968 }
969 }
970 weechat::infolist_free($infolist);
971 # If the infolist found more than one channel, halt as we need to know which one
972 if ($found_chans > 1)
973 {
974 weechat::print("", "Channel name is not unique, please define server");
975 return weechat::WEECHAT_RC_OK;
976 }
977 }
978 # Something didn't return right
979 if ($bufp eq "")
980 {
981 weechat::print("", "Could not find buffer");
982 return weechat::WEECHAT_RC_OK;
983 }
984 }
985 else
986 {
987 # Get pointer from where we are
988 $bufp = weechat::current_buffer();
989 }
990 # Get buffer name
991 $bufname = weechat::buffer_get_string($bufp, 'name');
992 # Test if buffer is an IRC channel
993 if ($bufname =~ /(.*)\.([#&\+!])(.*)/)
994 {
995 if (weechat::config_get_plugin($bufname) eq "off")
996 {
997 # If currently off, set on
998 weechat::config_set_plugin($bufname, "on");
999
1000 # Send to output formatter
1001 chanmon_print("Channel Monitoring Enabled", $bufp);
1002 return weechat::WEECHAT_RC_OK;
1003 }
1004 elsif (weechat::config_get_plugin($bufname) eq "on" || weechat::config_get_plugin($bufname) eq "")
1005 {
1006 # If currently on, set off
1007 weechat::config_set_plugin($bufname, "off");
1008
1009 # Send to output formatter
1010 chanmon_print("Channel Monitoring Disabled", $bufp);
1011 return weechat::WEECHAT_RC_OK;
1012 }
1013 }
1014}
1015
1016# Takes a buffer pointer and returns a formatted name
1017sub format_buffer_name
1018{
1019 $cb_bufferp = $_[0];
1020 $bufname = weechat::buffer_get_string($cb_bufferp, 'name');
1021
1022 # Set colour from buffer name
1023 if (weechat::config_get_plugin("color_buf") eq "on")
1024 {
1025 # Determine what colour to use
1026 $color = weechat::info_get("irc_nick_color", $bufname);
1027 if (!$color)
1028 {
1029 $color = 0;
1030 @char_array = split(//,$bufname);
1031 foreach $char (@char_array)
1032 {
1033 $color += ord($char);
1034 }
1035 $color %= 10;
1036 $color = sprintf "weechat.color.chat_nick_color%02d", $color+1;
1037 $color = weechat::config_get($color);
1038 $color = weechat::config_string($color);
1039 $color = weechat::color($color);
1040 }
1041
1042 # Private message just show network
1043 if (weechat::config_get_plugin("merge_private") eq "on" && weechat::buffer_get_string($cb_bufferp, "localvar_type") eq "private")
1044 {
1045 $bufname = weechat::buffer_get_string($cb_bufferp, "localvar_server");
1046 }
1047 # Format name to short or 'nicename'
1048 elsif (weechat::config_get_plugin("short_names") eq "on")
1049 {
1050 $bufname = weechat::buffer_get_string($cb_bufferp, 'short_name');
1051 }
1052 else
1053 {
1054 $bufname =~ s/(.*)\.([#&\+!])(.*)/$1$2$3/;
1055 }
1056
1057 # Build a coloured string
1058 $bufname = $color.$bufname.weechat::color("reset");
1059 }
1060 # User set colour name
1061 elsif (weechat::config_get_plugin("color_buf") ne "off")
1062 {
1063 # Private message just show network
1064 if (weechat::config_get_plugin("merge_private") eq "on" && weechat::buffer_get_string($cb_bufferp, "localvar_type") eq "private")
1065 {
1066 $bufname = weechat::buffer_get_string($cb_bufferp, "localvar_server");
1067 }
1068 # Format name to short or 'nicename'
1069 elsif (weechat::config_get_plugin("short_names") eq "on")
1070 {
1071 $bufname = weechat::buffer_get_string($cb_bufferp, 'short_name');
1072 }
1073 else
1074 {
1075 $bufname =~ s/(.*)\.([#&\+!])(.*)/$1$2$3/;
1076 }
1077
1078 $color = weechat::config_get_plugin("color_buf");
1079 $bufname = weechat::color($color).$bufname.weechat::color("reset");
1080 }
1081 # Stick with default colour
1082 else
1083 {
1084 # Private message just show network
1085 if (weechat::config_get_plugin("merge_private") eq "on" && weechat::buffer_get_string($cb_bufferp, "localvar_type") eq "private")
1086 {
1087 $bufname = weechat::buffer_get_string($cb_bufferp, "localvar_server");
1088 }
1089 # Format name to short or 'nicename'
1090 elsif (weechat::config_get_plugin("short_names") eq "on")
1091 {
1092 $bufname = weechat::buffer_get_string($cb_bufferp, 'short_name');
1093 }
1094 else
1095 {
1096 $bufname =~ s/(.*)\.([#&\+!])(.*)/$1$2$3/;
1097 }
1098 }
1099
1100 return $bufname;
1101}
1102
1103# Check result of register, and attempt to behave in a sane manner
1104if (!weechat::register("chanmon", "KenjiE20", "2.3.1", "GPL3", "Channel Monitor", "", ""))
1105{
1106 # Double load
1107 weechat::print ("", "\tChanmon is already loaded");
1108 return weechat::WEECHAT_RC_OK;
1109}
1110else
1111{
1112 # Start everything
1113 chanmon_hook();
1114 chanmon_config_init();
1115 chanmon_start();
1116}