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