]> git.vpit.fr Git - perl/scripts/xchat.git/blobdiff - Xchat/XPI/Utils.pm
Factor the GUI commands into a new utility function
[perl/scripts/xchat.git] / Xchat / XPI / Utils.pm
index ad99f0ff0162b140e40634d560a0c4a1d7fc49df..cbd9494ae1cd5a158d34dc78a32a2196eadd7ff8 100644 (file)
@@ -23,6 +23,17 @@ sub dye_nick {
  return sprintf "\003%d%s", $rcolors[$col % @rcolors], $nick;
 }
 
+my %gui_commands = (
+ 'clear'     => [ 'COLOR 0' ],
+ 'highlight' => [ 'FLASH' => 'COLOR 3' ],
+);
+
+sub gui {
+ my $commands = $gui_commands{$_[0] || 'nope'};
+ die 'Invalid command' unless defined $commands;
+ command "GUI $_" for @$commands;
+}
+
 sub save_context {
  my $cur_cxt = get_context;
  bless \$cur_cxt, 'Xchat::XPI::Utils::ContextGuard';
@@ -45,12 +56,37 @@ sub Xchat::XPI::Utils::ContextGuard::DESTROY {
  set_context ${$_[0]};
 }
 
+sub called_from_script (&) {
+ my $code  = shift;
+ my $level = 0;
+ my ($package, $filename);
+ while (1) {
+  my @frame = caller $level;
+  last unless @frame;
+  if ($frame[0] !~ /^(?:Xchat|HexChat)::XPI\b/) {
+   ($package, $filename) = @frame[0, 1];
+   last;
+  }
+  ++$level;
+ }
+ if (defined $filename) {
+  my $internal_package = Xchat::Embed::file2pkg($filename);
+  my $mock = sub { $internal_package, $package };
+  no warnings 'redefine';
+  local *Xchat::Embed::find_pkg   = $mock;
+  local *HexChat::Embed::find_pkg = $mock;
+  $code->();
+ }
+}
+
 use base qw<Exporter>;
 
 our @EXPORT         = ();
 our %EXPORT_TAGS    = ('funcs' => [ qw<
  dye_nick
+ gui
  save_context local_context
+ called_from_script
 > ]);
 our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;
 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];