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