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