a small collection of personal scripts
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.

129 lines
3.9KB

  1. # WeeChat pm and highlight notifications via notify-send
  2. #
  3. # modelled after 'WeeChat ubus notifications' by Arvid Picciani
  4. #
  5. # license: GPL3
  6. # contact: shmibs@gmail.com
  7. # history:
  8. # 1.4 another small bug-fix
  9. # 1.3 a small fix to formatting $message
  10. # 1.2 use config options
  11. # 1.1 restructured code for (greater) sanity
  12. # 1.0 first working version
  13. use strict;
  14. use warnings;
  15. use constant SCRIPT_NAME => 'notify_send';
  16. weechat::register(SCRIPT_NAME, 'shmibs', '1.1-1', 'GPL3', 'execute a user-defined system command upon highlight or private message (with smart delays to avoid spam)', '', '');
  17. # global var declarations
  18. my %pv_times;
  19. my %highlight_times;
  20. my %settings_default=(
  21. 'wait_pm' => [ '180', 'necessary time delay between private messages (seconds) for command to be executed' ],
  22. 'wait_highlight' => [ '60', 'necessary time delay between highlights (seconds) for command to be executed' ],
  23. 'ignore_nicks' => [ '', 'comma-separated list of nicks to ignore' ],
  24. 'command' => [ 'notify-send -t 2000 $type: $name', 'system command to be executed ($type, $name, and $message will be interpreted as values)' ]
  25. );
  26. my %settings=();
  27. #------------------------------------[ START CONFIGURATION ]------------------------------------
  28. sub config_changed {
  29. my ($pointer, $name, $value) = @_;
  30. $name = substr($name, length("plugins.var.perl.".SCRIPT_NAME."."), length($name));
  31. $settings{$name} = $value;
  32. return weechat::WEECHAT_RC_OK;
  33. }
  34. sub config_init{
  35. my $version = weechat::info_get("version_number", "") || 0;
  36. foreach my $option (keys %settings_default) {
  37. if (!weechat::config_is_set_plugin($option)) {
  38. weechat::config_set_plugin($option, $settings_default{$option}[0]);
  39. $settings{$option} = $settings_default{$option}[0];
  40. } else {
  41. $settings{$option} = weechat::config_get_plugin($option);
  42. }
  43. if ($version >= 0x00030500) {
  44. weechat::config_set_desc_plugin($option, $settings_default{$option}[1]." (default: \"".$settings_default{$option}[0]."\")");
  45. }
  46. }
  47. }
  48. config_init();
  49. weechat::hook_config("plugins.var.perl.".SCRIPT_NAME.".*", "config_changed", "");
  50. #-------------------------------------[ END CONFIGURATION ]-------------------------------------
  51. my @signals=qw(weechat_pv weechat_highlight);
  52. # message received hook
  53. foreach(@signals) {
  54. weechat::hook_signal($_,'new_notification','');
  55. }
  56. sub new_notification {
  57. # $_[1] is the type (either weechat_highlight, weechat_pv)
  58. # $_[2] is the actual content
  59. # get the username and message contents
  60. my $name=substr($_[2],0,index($_[2],' '));
  61. my $message=substr($_[2],index($_[2],' '));
  62. if($name eq ' *'){
  63. $name=substr($_[2],index($_[2],' *')+3);
  64. $message=substr($name,index($name,' '));
  65. $name=substr($name,0,index($name,' '));
  66. }
  67. $message =~ s/ //;
  68. $name =~ s/@|\+//;
  69. # get the type of the message
  70. my $type;
  71. if($_[1] eq 'weechat_pv') {
  72. $type='PM';
  73. } else {
  74. $type='HL';
  75. }
  76. # boolean to determine whether or not a notification should
  77. # be sent
  78. my $send='true';
  79. # ignore messages from nicks in ignore_nicks option
  80. foreach(split(/,/,$settings{'ignore_nicks'})) {
  81. $send='false' if($name eq $_);
  82. }
  83. # determine whether a notification of the same type has been
  84. # made recently. if so, ignore it
  85. if($type eq 'PM'){
  86. if(exists $pv_times{$name}) {
  87. if(time-$pv_times{$name} < int($settings{'wait_pm'})) {
  88. $send='false';
  89. }
  90. }
  91. $pv_times{$name} = time;
  92. } else {
  93. if(exists $highlight_times{$name}) {
  94. if(time-$highlight_times{$name} < int($settings{'wait_highlight'})) {
  95. $send='false';
  96. }
  97. }
  98. $highlight_times{$name} = time;
  99. }
  100. # run system command
  101. if($send eq 'true') {
  102. my @args = split(/ /,$settings{'command'});
  103. my $command = shift(@args);
  104. s/\$type/$type/g for @args;
  105. s/\$name/$name/g for @args;
  106. s/\$message/$message/g for @args;
  107. system($command, @args);
  108. }
  109. return weechat::WEECHAT_RC_OK;
  110. }