/var/www/www.irssi.org-old/scripts/html/listen.pl


   1 use Irssi;
   2 use Irssi::Irc;
   3 
   4 use vars qw($VERSION %IRSSI);
   5 $VERSION = "0.2";
   6 %IRSSI = ( 
   7            authors         => "Csaba Nagy",
   8 	   contact         => "lordpyre\@negerno.hu",
   9 	   name            => "listen",
  10 	   description     => "A simple mp3 display script that will display what mp3 you are playing in which software (mpg123, xmms, mp3blaster, etc) to your active channel or to a query window.",
  11 	   license         => "GNU GPLv2 or later",
  12 	   changed         => "Tue Nov 26 19:55:04 CET 2002"
  13 );
  14 
  15 # Usage: 1, load the script
  16 # 	 2, personalize the settings
  17 # 	 	- listen_use_action -> if "on" the script will issue an action 
  18 # 	 		to let otherones know what you are listening to
  19 # 	 		if "off" it will use a simple msg
  20 # 	 	- listen_prefix -> the output of the script will look like:
  21 # 	 		'/me $listen_prefix $listen_tagorder' if the
  22 # 	 		mp3file has idtags. otherwise the output will be:
  23 # 	 		'/me $listen_prefix $mp3filename'
  24 # 	 	- listen_tagorder -> the perfect order of the tags? ;)
  25 # 	 		for example: '%ARTIST (%ALBUM) - %TITLE (%PLAYER)'
  26 # 	 		you can specify: %TITLE, %ALBUM, %ARTIST, %GENRE,
  27 # 	 				 %COMMENT, %PLAYER
  28 # 	 3, use /listen
  29 # 	 4, have phun =)
  30 #
  31 # Programs needed:
  32 # 	- lsof - ftp://vic.cc.purdue.edu/pub/tools/unix/lsof
  33 # 	- id3 - http://frantica.lly.org/~rcw/id3/
  34 # LordPyre
  35 
  36 
  37 # list of supported mp3 players
  38 # if you would like to use the script with other players, just type these
  39 # name into the list below... it will probably work :)
  40 @mp3players=("mpg123", "mpg321", "xmms", "mp3blaster", "alsaplayer");
  41 
  42 ################## PLZ DON'T CHANGE ANYTHING BELOW THIS LINE ##################
  43 # or do it on your own risk!! 
  44 
  45 sub default_values {
  46 	$mp3player="nope";
  47 	$mp3file="nope";
  48 	%idtag=("Title",  "Unknown Title",
  49 		"Album",  "Unknown Album",
  50 		"Artist", "Unknown Artist",
  51 		"Genre",  "Unknown Genre",
  52 		"Comment","No Comment");
  53 }
  54 
  55 sub getmp3filename {
  56 	open(CSOCS,$_[0]);
  57 	GECMO: while (<CSOCS>) {
  58 		chop;
  59 		(@line) = split(/\s/,$_);
  60 		# we check wheter the mp3file returned by lsof has been opened
  61 		# with a known mp3player or not
  62 		HMM: foreach $w (@mp3players) {
  63 			# if yes we save it, and leave
  64 			if ($w =~ /^$line[0]/) {
  65 				$mp3player=$w;
  66 				last HMM;
  67 				}
  68 			}
  69 		# if we have found one player 'turned on', we don't have to
  70 		# check the other results of lsof, so we can leave
  71 		if ($mp3player ne "nope") {
  72 			$mp3file=$line[$#line];
  73 			last GECMO;
  74 			}
  75 		}
  76 	close(CSOCS);
  77 }
  78 
  79 sub getmp3proces {
  80 	# most of the players put the file into the memory at first,
  81 	# let's try to catch it there, first
  82 	getmp3filename("/usr/sbin/lsof -d mem | grep -i .mp3|");
  83 	# if we didn't find anything there, we check the fds for mp3s
  84 	if ($mp3player eq "nope") {
  85 		getmp3filename("/usr/sbin/lsof -d 1-15 | grep -i .mp3|");
  86 	}
  87 	
  88 	# hmm are we listening to anything?
  89 	if ($mp3player eq "nope") {
  90 		Irssi::print("Hmm are you listening to anything? (possibly not supported mp3player)");
  91 		return 0;
  92 	}
  93 	
  94 	# the only problem can happen to us, if the string we got from lsof
  95 	# isn't a real mp3file (this may happen for example if there are \x20
  96 	# chars in the filename). so let's check it!
  97 	if (!(-e $mp3file && -r $mp3file)) { 
  98 		Irssi::print("Damn! Nonexistent filename. (maybe spaces in it?)");
  99 		return 0;
 100 	}
 101 	return 1;
 102 }
 103 
 104 sub getmp3idtags {
 105 	# getting the idtags from file
 106 	open(ID3GECMO, "/usr/bin/id3 -R -l \"$mp3file\" |");
 107 	while (<ID3GECMO>) {
 108 		chop;
 109 		foreach $kulcs (keys %idtag) {
 110 			if ($_=~ /^$kulcs/) {
 111 		        	s/^$kulcs://; s/\s*$//;	s/^\s*//;
 112 				if ($_)	{ $idtag{$kulcs}=$_; }
 113 				}
 114 			}
 115 		}
 116 	close(ID3GECMO);
 117 }
 118 
 119 sub do_listen {
 120 
 121 	#setting up variables
 122 	my ($data, $server, $witem) = @_;
 123 	default_values();
 124 	if (!getmp3proces()) { return };
 125 	getmp3idtags();
 126 
 127 	# if there's no usable idtag in the mp3 we use the filename
 128 	if (($idtag{"Artist"} eq "Unknow Artist") && ($idtag{"Title"} eq "Unknown Title")) { 
 129 		$outtext=$mp3filename;
 130 	} else {
 131 		# if the file is tagged we parse over the tagorder
 132 		$outtext=Irssi::settings_get_str("listen_tagorder");
 133 		foreach $w (keys %idtag) {
 134 			$outtext=~s/%$w/$idtag{$w}/i;
 135 			}
 136 		$outtext=~s/%player/$mp3player/i;
 137 	}
 138 	
 139 	$prefix=Irssi::settings_get_str("listen_prefix");
 140 	
 141 	if (Irssi::settings_get_bool("listen_use_action")) {
 142 		$outtext="ME ".$prefix." ".$outtext;
 143 	} else {
 144 		$outtext="MSG ".$witem->{name}." ".$prefix." ".$outtext;
 145 		}
 146 	# let's write the result to everyone
 147         if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
 148         	$witem->command($outtext);
 149 		}
 150 }
 151 
 152 # setting irssi enviroments
 153 Irssi::command_bind("listen", "do_listen");
 154 Irssi::settings_add_bool("listen","listen_use_action",1);
 155 Irssi::settings_add_str("listen","listen_prefix","is listening to");
 156 Irssi::settings_add_str("listen","listen_tagorder","%ARTIST (%ALBUM) - %TITLE (%PLAYER)");
 157 
 158 print CLIENTCRAP "%B>>%n ".$IRSSI{name}." v".$VERSION." loaded... (command: /listen)";