html/irssiBlaster.pl


   1 # irssiBlaster 1.6
   2 # Copyright (C) 2003 legion
   3 # 
   4 # "Now Playing" (mp3blaster) in Irssi and more.
   5 # 
   6 # - mp3blaster (http://www.stack.nl/~brama/mp3blaster.html)
   7 # - irssi (http://irssi.org)
   8 # for /npsend (EXPERIMENTAL):
   9 # - lsof (ftp://vic.cc.purdue.edu/pub/tools/unix/lsof/)
  10 #
  11 # NOTE: these applications are available in any linux distribution.
  12 # 
  13 # should work with any version (i'm using irssi 0.8.8 & mp3blaster 3.2.0)
  14 # bug reports,features requests or comments -> a.lepore@email.it
  15 #
  16 # License:
  17 # This program is free software; you can redistribute it and/or modify it under
  18 # the terms of the GNU General Public License as published by the Free Software
  19 # Foundation; either version 2 of the License, or any later version. www.gnu.org
  20 #
  21 #################################################################################
  22 # *** USAGE:
  23 # 
  24 # /np			: display the "Artist - Song" played in current window,
  25 # 			  any argument is printed after the song name (i.e. you own comment).
  26 #
  27 # /npa			: like /np, but prints "Artist - Album [Year]".
  28 # 			  If there isn't an appropriate album tag,print nothing.
  29 # 			  
  30 # /anp			: /np in all channels.
  31 #
  32 # /anpa			: /npa in all channels.
  33 #
  34 # /npinfo		: display all available info for the current file.
  35 #
  36 # /cleanbar		: clean the statubar item (until the next song).
  37 # 
  38 # /npsend NICK		: *EXPERIMENTAL* (irssi often CRASH)
  39 # 			  send the current played file to NICK user.
  40 # 			  maybe it will be usable in version 2.0.
  41 # 			  
  42 # 
  43 # *** SETTINGS:
  44 #
  45 # blaster_bar ON/OFF		: statusbar item activation.
  46 # 				  ATTENTION:
  47 # 				  you also have to add the item 'blaster' to your statusbar.
  48 # 				  see: http://irssi.org/?page=docs&doc=startup-HOWTO#c12
  49 # 				  example:
  50 # 				  /statusbar window add -priority "-10" -alignment right blaster
  51 #
  52 # blaster_infos_path FILE	: the file with infos (mp3blaster -f FILE).
  53 # 				  default is ~/.infoz
  54 # 				  
  55 # blaster_bar_prefix STRING	: the bar prefix to filename. default is "playing:"
  56 # 
  57 # blaster_prefix		: the /np prefix to filename. default is "np:"
  58 # 
  59 #################################################################################
  60 # Changelog:
  61 #
  62 # 1.6:
  63 # - /npinfo.
  64 # - /cleanbar.
  65 # - /anpa.
  66 # - /npa.
  67 # - help fixes. $infofile is now /tmp/irssiblaster.
  68 # - /npsend (EXPERIMENTAL).
  69 # - /np [comment].
  70 # - /anp.
  71 # - BUGFIX: no spaces at the end of the filenames.
  72 # - added code comments.
  73 # - prefixes can be changed.
  74 # - statusbar realtime print.
  75 # - 'mp3blaster_infos_path' is now 'blaster_infos_path'.
  76 # 
  77 # 1.0:
  78 # - initial release.
  79 #
  80 # TODO:
  81 # - working /npsend
  82 # - (automatic) /cleanbar
  83 # - support for others stuff (album,time..)
  84 # - /help
  85 # - use strict;
  86 #################################################################################
  87 
  88 #use strict;
  89 use Irssi;
  90 use Irssi::TextUI;
  91 use vars qw($VERSION %IRSSI);
  92 
  93 $VERSION = '1.6';
  94 %IRSSI = (
  95 	authors		=> 'legion',
  96 	contact		=> 'a.lepore@email.it',
  97 	name		=> 'irssiBlaster',
  98 	description	=> 'Display the song played by mp3blaster in channels and statusbar. See the top of the file for usage.',
  99 	license		=> 'GNU GPLv2 or later',
 100 	changed		=> 'Fri Oct 31 12:22:08 CET 2003',
 101 );
 102 
 103 
 104 
 105 sub get_info {
 106 
 107 	my $infofile = Irssi::settings_get_str('blaster_infos_path');
 108 	open (FILE, $infofile); # open and read file with infos
 109 	@all = <FILE>;
 110 	close (FILE);
 111 
 112 	@artist = grep (/^artist/, @all); # get the lines with tag infos
 113 	@title = grep (/^title/, @all);
 114 	@album = grep (/^album/, @all);
 115 	@year = grep (/^year/, @all);
 116 	@name = grep (/^path/, @all);     # get the line with filename
 117 
 118 } ##
 119 
 120 sub get_allinfo {
 121 
 122 	my $infofile = Irssi::settings_get_str('blaster_infos_path');
 123 	open (FILE, $infofile);
 124 	@all = <FILE>;
 125 	close (FILE);
 126 
 127 	@name = grep (/^path/, @all);
 128 	$name = $name[0];
 129 	$name =~ s/^path //;
 130 	chomp $name;
 131 	@status = grep (/^status/, @all);
 132 	$status = $status[0];
 133 	$status =~ s/^status //;
 134 	chomp $status;
 135 	@artist = grep (/^artist/, @all);
 136 	$artist = $artist[0];
 137 	$artist =~ s/^artist //;
 138 	chomp $artist;
 139 	@title = grep (/^title/, @all);
 140 	$title = $title[0];
 141 	$title =~ s/^title //;
 142 	chomp $title;
 143 	@album = grep (/^album/, @all);
 144 	$album = $album[0];
 145 	$album =~ s/^album //;
 146 	chomp $album;
 147 	@year = grep (/^year/, @all);
 148 	$year = $year[0];
 149 	$year =~ s/^year //;
 150 	chomp $year;
 151 	@comment = grep (/^comment/, @all);
 152 	$comment = $comment[0];
 153 	$comment =~ s/^comment //;
 154 	chomp $comment;
 155 	@mode = grep (/^mode/, @all);
 156 	$mode = $mode[0];
 157 	$mode =~ s/^mode //;
 158 	chomp $mode;
 159 	@format = grep (/^format/, @all);
 160 	$format = $format[0];
 161 	$format =~ s/^format //;
 162 	chomp $format;
 163 	@bitrate = grep (/^bitrate/, @all);
 164 	$bitrate = $bitrate[0];
 165 	$bitrate =~ s/^bitrate //;
 166 	chomp $bitrate;
 167 	@samplerate = grep (/^samplerate/, @all);
 168 	$samplerate = $samplerate[0];
 169 	$samplerate =~ s/^samplerate //;
 170 	chomp $samplerate;
 171 	@length = grep (/^length/, @all);
 172 	$length = $length[0];
 173 	$length =~ s/^length //;
 174 	chomp $length;
 175 	@next = grep (/^next/, @all);
 176 	$next = $next[0];
 177 	$next =~ s/^next //;
 178 	chomp $next;
 179 
 180 } ##
 181 
 182 sub get_status {
 183 
 184 	my $infofile = Irssi::settings_get_str('blaster_infos_path');
 185 	open (FILE, $infofile);
 186 	@all = <FILE>;
 187 	close (FILE);
 188 
 189 	@status = grep (/^status/, @all);
 190 } ##
 191 
 192 sub get_tag_info {
 193 
 194 	$artist = $artist[0]; # is an one-element array
 195 	$artist =~ s/^artist //; # remove prefixes
 196 	chomp $artist;           # remove last char (for correct printing)
 197 	$title = $title[0];
 198 	$title =~ s/^title //;
 199 	chomp $title;
 200 	$album = $album[0];
 201 	$album =~ s/^album //;
 202 	chomp $album;
 203 	$year = $year[0];
 204 	$year =~ s/^year //;
 205 	chomp $year;
 206 
 207 	$prefix = Irssi::settings_get_str('blaster_prefix');
 208 	$barprefix = Irssi::settings_get_str('blaster_bar_prefix');
 209 
 210 } ##
 211 
 212 sub get_name_info {
 213 
 214 	$name = $name[0];
 215 	$name =~ s/^path //;  # remove prefix
 216 	$name =~ s/\.mp3$//i; # remove extensions
 217 	$name =~ s/\.ogg$//i;
 218 	$name =~ s/_/ /g;     # change underscores to spaces
 219 	chomp $name;
 220 
 221 	$prefix = Irssi::settings_get_str('blaster_prefix');
 222 	$barprefix = Irssi::settings_get_str('blaster_bar_prefix');
 223 
 224 } ##
 225 
 226 sub noinfo_error {
 227 
 228 	my $infofile = Irssi::settings_get_str('blaster_infos_path');
 229 	# print help if the info file is not valid
 230 	Irssi::print(
 231 	"%9IrssiBlaster:%_ \"$infofile\" is not a valid info file. %9Make sure%_ %Rmp3blaster -f $infofile%n %9is running!!!%_\n".
 232 	"%9IrssiBlaster:%_ (Hint: put %9alias mp3blaster='mp3blaster -f $infofile'%_ in your ~/.bashrc )"
 233 	, MSGLEVEL_CRAP);
 234 
 235 } ##
 236 
 237 
 238 
 239 
 240 sub cmd_np { # /np stuff
 241 
 242 get_info;
 243 
 244 if (@artist && @title) { # if file has a an id3tag
 245 
 246 	get_tag_info;
 247 	
 248 	my ($comment, $server, $witem) = @_; # np: blabla in current window (copied from other scripts..)
 249 	if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
 250 		$witem->command("me $prefix $artist - $title $comment");
 251 	}
 252 	else {
 253 		Irssi::print("$prefix $artist - $title $comment", MSGLEVEL_CRAP); # or print in client level if no active channel/query
 254 	}
 255 }
 256 
 257 elsif (@name) { # if there isn't id3tag we use the filename
 258 
 259 	get_name_info;
 260 	
 261 	my ($comment, $server, $witem) = @_;
 262 	if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
 263 		$witem->command("me $prefix $name $comment");
 264 	}
 265 	else {
 266 		Irssi::print("$prefix $name $comment", MSGLEVEL_CRAP);
 267 	}
 268 }
 269 	
 270 else { noinfo_error; }
 271 
 272 } ##
 273 
 274 sub cmd_npall { # /anp stuff
 275 
 276 get_info;
 277 
 278 if (@artist && @title) {
 279 
 280 	get_tag_info;
 281 
 282 	my ($comment, $server, $witem) = @_;
 283 	Irssi::command("foreach channel /me $prefix $artist - $title $comment");
 284 }
 285 
 286 elsif (@name) {
 287 	
 288 	get_name_info;
 289 
 290 	my ($comment, $server, $witem) = @_;
 291 	Irssi::command("foreach channel /me $prefix $name $comment");
 292 }
 293 
 294 else { noinfo_error; }
 295 
 296 } ##
 297 
 298 sub cmd_npalbum { # /npa stuff
 299 
 300 if (@artist && @album) {
 301 		
 302 	get_tag_info;
 303 
 304 	if ($year) { $year = "[$year]"; }
 305 
 306 	my ($comment, $server, $witem) = @_;
 307 	if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
 308 		$witem->command("me $prefix $artist - $album $year $comment");
 309 	}
 310 	else {
 311 		Irssi::print("$prefix $artist - $album $year $comment", MSGLEVEL_CRAP);
 312 	}
 313 }
 314 else {
 315 	Irssi::print("%9IrssiBlaster:%_ filename has no album tag.", MSGLEVEL_CRAP);
 316 }
 317 } ##
 318 
 319 sub cmd_npalbumall { # /anpa stuff
 320 
 321 get_info;
 322 
 323 if (@artist && @album) {
 324 
 325 	get_tag_info;
 326 
 327 	if ($year) { $year = "[$year]"; }
 328 
 329 	my ($comment, $server, $witem) = @_;
 330 	Irssi::command("foreach channel /me $prefix $artist - $album $year $comment");
 331 }
 332 else {
 333         Irssi::print("%9IrssiBlaster:%_ filename has no album tag.", MSGLEVEL_CRAP);
 334 }
 335 } ##
 336 
 337 sub cmd_info {
 338 
 339 get_allinfo;
 340 
 341 $tot = $length/60; # calculating minutes:seconds
 342 @tot = split(/\./, $tot);
 343 $min = $tot[0];
 344 $sec = $min*60;
 345 $secs = $length-$sec;
 346 
 347 Irssi::print("\n%9IrssiBlaster - File Info:%_", MSGLEVEL_CRAP);
 348 Irssi::print("%9F%_ile%9:%_ $name", MSGLEVEL_CRAP);
 349 Irssi::print("%9S%_tatus%9:%_ $status", MSGLEVEL_CRAP);
 350 if ($artist) { Irssi::print("%9A%_rtist%9:%_ $artist", MSGLEVEL_CRAP); }
 351 if ($title) { Irssi::print("%9T%_itle%9:%_ $title", MSGLEVEL_CRAP); }
 352 if ($album) { Irssi::print("%9A%_lbum%9:%_ $album", MSGLEVEL_CRAP); }
 353 if ($year) { Irssi::print("%9Y%_ear%9:%_ $year", MSGLEVEL_CRAP); }
 354 if ($comment) { Irssi::print("%9C%_omment%9:%_ $comment", MSGLEVEL_CRAP); }
 355 Irssi::print("%9-%_----------%9-%_", MSGLEVEL_CRAP);
 356 if ($secs =~ /^.{1}$/) { Irssi::print("%9L%_ength%9:%_ $min\:0$secs", MSGLEVEL_CRAP); }
 357 else { Irssi::print("%9L%_ength%9:%_ $min\:$secs", MSGLEVEL_CRAP); }
 358 if ($format =~ /0$/) { Irssi::print("%9F%_iletype%9:%_ $format (Ogg/Vorbis?)", MSGLEVEL_CRAP); }
 359 else { Irssi::print("%9F%_iletype%9:%_ $format", MSGLEVEL_CRAP); }
 360 Irssi::print("%9R%_ate%9:%_ $bitrate\kb/$samplerate\Khz", MSGLEVEL_CRAP);
 361 if ($mode) { Irssi::print("%9M%_ode%9:%_ $mode", MSGLEVEL_CRAP); }
 362 if ($next) { Irssi::print("%9N%_ext in playlist%9:%_ $next", MSGLEVEL_CRAP); }
 363 
 364 } ##
 365 
 366 #######################################################################################
 367 
 368 sub bar_np { # statusbar stuff
 369 
 370 my ($item, $get_size_only) = @_;
 371 
 372 my $bar_activation = Irssi::settings_get_str('blaster_bar');
 373 if ($bar_activation =~ /^on$/i) { # display in bar only if /set blaster_bar = ON
 374 
 375 get_info;
 376 
 377 if (@artist && @title) {
 378 
 379 	get_tag_info;
 380 	
 381 	# print in statusbar
 382 	$item->default_handler($get_size_only, "{sb $barprefix $artist - $title}", undef, 1);
 383 }
 384 
 385 elsif (@name) {
 386 
 387 	get_name_info;
 388 	
 389 	$item->default_handler($get_size_only, "{sb $barprefix $name}", undef, 1);
 390 }
 391 
 392 else { 
 393 	$item->default_handler($get_size_only, undef, undef, 1);
 394 }
 395 }
 396 } ##
 397 
 398 sub refresh {
 399 	Irssi::statusbar_items_redraw('blaster'); # refresh statusbar
 400 	Irssi::statusbars_recreate_items();
 401 } ##
 402 
 403 sub cmd_cleanbar { # /cleanbar stuff
 404 
 405 my $infofile = Irssi::settings_get_str('blaster_infos_path');
 406 unlink $infofile;
 407 
 408 } ##
 409 
 410 sub cmd_send { # /npsend stuff
 411 
 412 	get_info;
 413 
 414 	my @name = grep (/^path/, @all);
 415 	my $name = $name[0];
 416 	$name =~ s/path //;
 417 	chomp $name;
 418 
 419 	# get the full path of the file from 'lsof' (i have lsof 4.64)
 420 	my @open_files = grep (/$name$/, `lsof -c mp3blaste -F n`);
 421 	$open_files[0] =~ s/^n//;
 422 	my $filename = $open_files[0];
 423 	chomp $filename;
 424 
 425 	my ($target, $server, $witem) = @_;
 426 	$server->command("DCC SEND $target \"$filename\""); # /dcc send
 427 
 428 } ##
 429 
 430 
 431 Irssi::settings_add_str('irssiBlaster', 'blaster_infos_path', '/tmp/irssiblaster'); # register settings
 432 Irssi::settings_add_str('irssiBlaster', 'blaster_prefix', 'np:');
 433 Irssi::settings_add_str('irssiBlaster', 'blaster_bar_prefix', 'playing:');
 434 Irssi::settings_add_str('irssiBlaster', 'blaster_bar', 'OFF');
 435 Irssi::command_bind('np', 'cmd_np'); # register /commands
 436 Irssi::command_bind('anp', 'cmd_npall');
 437 Irssi::command_bind('npa', 'cmd_npalbum');
 438 Irssi::command_bind('anpa', 'cmd_npalbumall');
 439 Irssi::command_bind('npinfo', 'cmd_info');
 440 Irssi::command_bind('cleanbar', 'cmd_cleanbar');
 441 Irssi::command_bind('npsend', 'cmd_send');
 442 Irssi::statusbar_item_register('blaster', undef, 'bar_np'); # register statusbar item
 443 Irssi::timeout_add(1000, 'refresh', undef); # refresh every 1 second