]> git.draconx.ca Git - scripts.git/blob - ircmpc.pl
Add script to convert "JWK"-format RSA keys to normal.
[scripts.git] / ircmpc.pl
1 # Copyright (C) 2008, 2010 Nick Bowler
2 #
3 # Irssi script for annoying your friends with MPD status information.
4 #
5 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
6 # This is free software: you are free to do what the fuck you want to.
7 # There is NO WARRANTY, to the extent permitted by law.
8
9 use strict;
10 use vars qw($VERSION %IRSSI);
11
12 use utf8;
13 use encoding 'utf8';
14 use IO::Socket::INET6;
15
16 use Irssi qw(
17         command_bind
18         settings_get_bool settings_add_bool
19         settings_get_int  settings_add_int
20         settings_get_str  settings_add_str
21 );
22
23 $VERSION = '0.1';
24 %IRSSI = (
25         authors     => 'Nick Bowler',
26         contact     => 'nbowler@draconx.ca',
27         name        => 'MPC/Irssi',
28         description => 'Irssi client for the Music Player Daemon.',
29         license     => 'WTFPL2',
30 );
31
32 # MPD Connection Settings
33 $ENV{"MPD_HOST"} = "localhost" unless ($ENV{"MPD_HOST"});
34 $ENV{"MPD_PORT"} = 6600        unless ($ENV{"MPD_PORT"});
35 settings_add_str("mpd", "mpd_host", $ENV{"MPD_HOST"});
36 settings_add_int("mpd", "mpd_port", $ENV{"MPD_PORT"});
37
38 # Open a connection to MPD.
39 sub mpd_open {
40         my $sock = new IO::Socket::INET6(
41                 PeerAddr => settings_get_str("mpd_host"),
42                 PeerPort => settings_get_int("mpd_port"),
43                 Proto    => 'tcp',
44         ) or do {
45                 print CLIENTERROR "failed to open MPD socket: $!.";
46                 return undef;
47         };
48         binmode($sock, ":utf8");
49
50         # Grab the MPD version.
51         if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
52                 print CLIENTERROR "failed MPD handshake: $!.";
53                 return undef;
54         }
55
56         return $sock;
57 }
58
59 sub mpd_currentsong {
60         my ($sock) = @_;
61         my %data;
62
63         # Get MPD status.
64         print $sock "status\n";
65         while (<$sock>) {
66                 last if (/^OK/);
67                 do { print CLIENTERROR "$_"; return undef } if (/^ACK/);
68
69                 if (/^(\w+): (.*)$/) {
70                         $data{$1} = $2;
71                 }
72         }
73         return %data if (!defined $data{"songid"});
74
75         # Get song info
76         print $sock "playlistid $data{songid}\n";
77         while (<$sock>) {
78                 last if (/^OK/);
79                 do { print CLIENTERROR "$_"; return undef } if (/^ACK/);
80
81                 if (/^(\w+): (.*)$/) {
82                         $data{$1} = $2;
83                 }
84         }
85
86         return %data;
87 }
88
89 sub mpd_close {
90         my ($sock) = @_;
91         print $sock "close\n";
92         close $sock;
93 }
94
95 sub fileformat {
96         ($_) = @_;
97
98         return "vorbis" if (/\.ogg$/);
99         return "flac"   if (/\.flac$/);
100         return "mp3"    if (/\.mp3$/);
101 }
102
103 sub cmd_current {
104         my ($data, $server, $witem) = @_;
105         my ($window, $local) = (Irssi::active_win());
106
107         my $sock = mpd_open() or return;
108         my %info = mpd_currentsong($sock) or return;
109         mpd_close($sock);
110
111         if ($info{'state'} eq "stop") {
112                 $window->print("MPD is currently stopped.");
113                 return;
114         }
115
116         my $text = "$info{Artist} – $info{Title}, "
117                  . "track $info{Track} of “$info{Album}” "
118                  . "($info{bitrate}kbps " . fileformat($info{'file'}) . ")";
119
120         # If the active window is not a channel or query, display locally.
121         $local = 1 unless ($witem && ($witem->{type} eq "CHANNEL"
122                                    || $witem->{type} eq "QUERY"));
123         
124         if ($local) {
125                 $window->print($text);
126         } elsif ($data) {
127                 $data =~ s/\s.*//;
128                 $witem->command("SAY $data: /me is listening to $text.");
129         } else {
130                 $witem->command("SAY /me is listening to $text.");
131         }
132 }
133
134 command_bind('mpd', 'cmd_current');