Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1154 lines
37 KiB

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