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