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