#!/usr/bin/perl =pod (inb4 this is the worst way to comment) mpd.pl by charlie shit is so custom vars: %ARTIST = artist %ALBUM = album %TITLE = song name %BITRATE = bitrate %TIME = current time/total time (yes this actually works :) %PLAYER = a string (just "MPD") %RCOL = a random color errata: clean up vars & fix CAPS LOCK (oopsies) released under the WTFPL enjoy~ ALSO: some tips for irssi: ^C# (control+c) is to start a color sequence, (this is alternatively, \x03#) to do this in vim, enter visual mode (control+v) and type ^C (control+c) then a corresponding number color codes are here http://irssi.org/documentation/formats ^O (control+o) is to clear color (this is \x0F) =cut use strict; use IO::Socket; use Date::Format; use Irssi; use vars qw{$VERSION %IRSSI %MPD}; $VERSION = "0.9"; %IRSSI = ( name => "mpd", license => "wtfpl", description => "Now playing script", ); my @faces = ("〜( ̄▽ ̄〜)", "(〜 ̄▽ ̄)〜", "(ノ゚-゚)ノ☆", "(`ー´)", "ヽ(・ω・)ノ", "( ゚∀゚)アハハ八八ノヽノヽノヽノ \ / \/ \", "( ゚ ヮ゚)", "◕ ◡ ◕", "キターーーー(^ヮ^)ーーーー!!!!", "┐('~`;)┌", "(っ⌒‿⌒)っ", "(づ。◕‿‿◕。)づ", "(屮゜Д゜)屮", "(つ・∀・)つ", "(´ω`)っ旦~~", "ヽ(´ー`)ノ♥ヽ(´ー`)ノ", "(☞゚∀゚)☞", "( ´・‿-) ~ ♥", "(@皿@)", "( ゚ ー ゚)", "(~° ▽ °)~", "(´・ω・)", "◔ ◡ ◔", "〜( ̄▽ ̄)〜", "(·¯◡◡¯)", "キタ━━━━━━(゚∀゚)━━━━━━ッ!!!!!", "(☞゚ヮ゚)☞" ); my @separators = qw/⇛ ⇨ ➡ ➤ 〜 ●/; my @colors = qw/2 3 4 5 6 7 9 10 11 12 13/; sub pnp { # print now playing my($MSG, $ITEM) = @_; if ($ITEM) { $ITEM->print($MSG); } else { Irssi::print($MSG); } } sub np { my($data, $server, $ITEM) = @_; # Settings $MPD{"PORT"} = Irssi::settings_get_str("mpd_port"); $MPD{"HOST"} = Irssi::settings_get_str("mpd_host"); $MPD{"TIME"} = Irssi::settings_get_str("mpd_timeout"); $MPD{"FORM"} = Irssi::settings_get_str("mpd_format"); my $socket = IO::Socket::INET->new( Proto => "tcp", PeerPort => $MPD{"PORT"}, PeerAddr => $MPD{"HOST"}, timeout => $MPD{"TIME"} ); if (not $socket) { pnp("No MPD listening at ". $MPD{"HOST"} .":". $MPD{"PORT"} .".", $ITEM); return; } $MPD{"PLAYER"} = "MPD"; $MPD{"STATUS"} = ""; $MPD{"ARTIST"} = ""; $MPD{"TITLE"} = ""; $MPD{"FACE"} = $faces[rand @faces]; my $ans = ""; my $STR = ""; print $socket "status\n"; # ask "status" from MPD while (not $ans =~ /^(OK$|ACK)/) { $ans = <$socket>; if ($ans =~ /state: (.+)$/) { $MPD{"STATUS"} = $1; } } if ($MPD{"STATUS"} eq "stop") { pnp("No song playing in MPD.", $ITEM); close $socket; return; } print $socket "currentsong\n"; # ask "currentsong" from MPD $ans = ""; # reset $ans while (not $ans =~ /^(OK$|ACK)/) { $ans = <$socket>; if ($ans =~ /Artist: (.+)$/) { $MPD{"ARTIST"} = "$1"; } elsif ($ans =~ /Title: (.+)$/) { $MPD{"TITLE"} = "$1"; } elsif ($ans =~ /Album: (.+)$/) { $MPD{"ALBUM"} = "$1"; } } print $socket "status\n"; # ask for status again $ans = ""; # reset $ans while (not $ans =~ /^(OK$|ACK)/) { $ans = <$socket>; if ($ans =~ /^bitrate: (.+)$/) { $MPD{"BITRATE"} = $1; } elsif ($ans =~ /time: (.+)$/) { my $topsec = (split /:/, $1)[0]; my $botsec = (split /:/, $1)[1]; my $telp = time2str("%M:%S",$topsec); my $ttol = time2str("%M:%S",$botsec); my $tophour = int($topsec/3600); my $bothour = int($botsec/3600); if ($tophour > 0) { $telp = $tophour.":".$telp; } if ($bothour > 0) { $ttol = $bothour.":".$ttol; } $MPD{"TIME"} = "$telp/$ttol"; } } close $socket; if ($MPD{"ARTIST"} eq "" and $MPD{"TITLE"} eq "") { $STR = $MPD{"ALTT"}; } else { $STR = $MPD{"FORM"}; } # begin plastic surgery $STR =~ s/\%FACE/$MPD{"FACE"}/g; while($STR =~ /\%RCOL/) { $MPD{"COLOR"} = $colors[rand @colors]; $STR =~ s/\%RCOL/\x03$MPD{"COLOR"}/; } $STR =~ s/\%RSEP/$separators[rand @separators]/g; $STR =~ s/\%ARTIST/$MPD{"ARTIST"}/g; $STR =~ s/\%TITLE/$MPD{"TITLE"}/g; $STR =~ s/\%ALBUM/$MPD{"ALBUM"}/g; $STR =~ s/\%BITRATE/$MPD{"BITRATE"}kbps/g; $STR =~ s/\%TIME/$MPD{"TIME"}/g; $STR =~ s/\%PLAYER/$MPD{"PLAYER"}/g; if ($ITEM && ($ITEM->{type} eq "CHANNEL" || $ITEM->{type} eq "QUERY")) { if($MPD{"FORM"} =~ /^\//) { $ITEM->command($STR); # if your np stars with /me } else { $ITEM->command("MSG ". $ITEM->{name} ." $STR"); # if not } } else { Irssi::print("You're not in a channel."); } } Irssi::settings_add_str("mpd", "mpd_host", "thinkcentre"); # MPD host Irssi::settings_add_str("mpd", "mpd_port", "6600"); # MPD port Irssi::settings_add_str("mpd", "mpd_timeout", "5"); # 5 second timeout Irssi::settings_add_str("mpd", "mpd_format", "/me NP: %ARTIST - %TITLE (%TIME / %BITRATE) %FACE"); # a simple nowplaying with some color Irssi::command_bind np => \&np; # our /np