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