1#!/usr/local/bin/perl
2
3# $Id: xmmsinfo.pl,v 1.1.1.1 2002/03/24 21:00:55 tj Exp $
4#
5# Copyright (0) 2002 Tuomas Jormola <tjormola@cc.hut.fi
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# The complete text of the GNU General Public License can be found
18# on the World Wide Web: <URL:http://www.gnu.org/licenses/gpl.html>
19#
20# $Log: xmmsinfo.pl,v $
21# Revision 1.1.1.1  2002/03/24 21:00:55  tj
22# Initial import.
23#
24#
25# TODO:
26# * Configurable string to print (%t = title, %a = artist ...)
27
28use strict;
29use Irssi;
30use Irssi::XMMSInfo;
31use vars qw($VERSION %IRSSI);
32
33# global variables
34$VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.2 $ =~ /^.+?(\d+)\.(\d+)/);
35%IRSSI = (
36	authors		=> 'Tuomas Jormola',
37	contact		=> 'tjormola@cc.hut.fi',
38	name		=> 'XMMSInfo',
39	description	=> '/xmmsinfo to tell what you\'re currently playing',
40	license		=> 'GPLv2',
41	url			=> 'http://shakti.tky.hut.fi/stuff.xml#irssi',
42	changed		=> '2006-1027T18:00+0300',
43);
44
45if(runningUnderIrssi()) {
46	Irssi::settings_add_str('misc', 'xmms_info_pipe', '/tmp/xmms-info');
47	Irssi::command_bind('xmmsinfo', 'commandXmmsInfo');
48	Irssi::print("$IRSSI{name} $VERSION loaded, /xmmsinfo -help");
49} else {
50	(my $s = $0) =~ s/.*\///;
51	$ARGV[0] || die("Usage: $s <file>\n");
52	commandXmmsInfo();
53}
54
55# command handler
56sub commandXmmsInfo {
57	my($args, $server, $target) = @_;
58
59	if(lc($args) eq "-help") {
60		Irssi::print("XMMSInfo $VERSION by $IRSSI{authors} <$IRSSI{contact}>");
61		Irssi::print("");
62		Irssi::print("Displays what your XMMS is playing using information");
63		Irssi::print("provided by the XMMS InfoPipe plugin");
64		Irssi::print("<URL:www.iki.fi/wwwwolf/code/xmms/infopipe.html");
65		Irssi::print("");
66		Irssi::print("Usage: /xmmsinfo [TARGET]");
67		Irssi::print("If TARGET is given, the info is sent there, othwerwise to");
68		Irssi::print("the current active channel/query or Irssi status window");
69		Irssi::print("if you have no channel/query window active.");
70		Irssi::print("Target can be nick name or channel name");
71		Irssi::print("");
72		Irssi::print("Configuration: /set xmms_info_pipe <file>");
73		Irssi::print("Define filename of the pipe where from the InfoPipe output is read");
74		Irssi::print("Default is /tmp/xmms-info");
75		return;
76	}
77
78	my($p) = runningUnderIrssi() ? Irssi::settings_get_str('xmms_info_pipe') : $ARGV[0];
79	my($i) = XMMSInfo->new;
80	$i->getInfo(pipe => $p);
81
82	my($o) = "XMMS: " . $i->getStatusString;
83
84	if($i->isFatalError) {
85		$o .= ": " . $i->getError;
86	} elsif($i->isXmmsRunning) {
87		my($t) = $i->infoTitle || "(unknown song)";
88		my($a) = $i->infoArtist || "(unknown artist)";
89		my($g) = lc($i->infoGenre) || "(unknown genre)";
90		my($pos) = $i->infoMinutesNow . "m" . $i->infoSecondsNowLeftover."s";
91		my($tot) = $i->infoMinutesTotal . "m" . $i->infoSecondsTotalLeftover."s";
92		my($per) = $i->infoPercentage;
93		my($b) = $i->infoBitrate . "kbps";
94		my($f) = $i->infoFrequency . "kHz";
95		$o .= " $g tune $t by $a." if ($i->isPlaying || $i->isPaused);
96		$o .= " Played $pos of total $tot ($per%)." if $i->isPlaying;
97		$o .= " [$b/$f]" if ($i->isPlaying || $i->isPaused);
98	}
99
100	if(!runningUnderIrssi()) {
101		print "$o\n";
102	} elsif($i->isFatalError || !$server || !$server->{connected} || (!$args && !$target)) {
103		Irssi::print($o);
104	} else {
105		$o =~ s/[\r\n]/ /g; # remove newline characters
106		my($t) = $args || $target->{name};
107		$server->command("msg $t $o");
108	}
109
110}
111
112sub runningUnderIrssi {
113	$0 eq '-e';
114}
115
116# END OF SCRIPT
117