x-haui

mocnp.pl

This is a nowplaying script for moc and irssi. It also enables you to control moc from irssi.


Usage:

/mocnp prints the currently played song in the active channel or the status-window.
/play starts playing, if moc's in state "pause"
/pause pause the current song
/start starts moc if it's not running or if it's in mode "stop"
/stop stops moc
/next next song
/prev previous song
/shuffle turns shuffle on/off (requires >=mocp 2.5)
/repeat turns repeat on/off (requires >=mocp 2.5)

Download

Source

  1 #!/usr/bin/perl -w
  2 #    This program is free software; you can redistribute it and/or modify
  3 #    it under the terms of the GNU General Public License as published by
  4 #    the Free Software Foundation; either version 3 of the License, or
  5 #    (at your option) any later version.
  6 #
  7 #    This program is distributed in the hope that it will be useful,
  8 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10 #    GNU General Public License for more details.
 11 #
 12 #    You should have received a copy of the GNU General Public License
 13 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 14 #
 15 
 16 #       simple moc now playing script for irssi
 17 #       
 18 #       a patch for moc is available here
 19 #       ./scripts/misc/patch
 20 #
 21 use strict;
 22 use Irssi;
 23 use vars qw($VERSION %IRSSI);
 24 $VERSION = '1.00';
 25 %IRSSI = (
 26     authors     => 'Haui',
 27     contact     => 'haui45@web.de',
 28     name        => 'mocnp',
 29     description => 'Irssi moc now playing script',
 30     license     => 'GPL',
 31 );
 32 
 33 my @array;
 34 my %info;
 35 my $string;
 36 my $in;
 37 
 38 #uncomment this, if you're sure of having moc installed
 39 `which mocp` or die "fatal....mocp executable not found! Get it from http://moc.daper.net";
 40 
 41 # set $pidfile manually if this fails....
 42 my $home = $ENV{"HOME"} || die "couldn't determine your home directory..." ;
 43 my $pidfile = $home . "/.moc/pid";
 44 
 45 
 46 sub irssi_stuff {
 47         # server - the active server in window
 48         # witem - the active window item (eg. channel, query)
 49         #         or undef if the window is empty
 50         my ($data, $server, $witem) = @_;
 51         if ($data) {
 52                 help();
 53                 return ;
 54         }
 55         if ((my $ret = moc()) < 0){
 56                 Irssi::print("mocp isn't playing") if ($ret eq -2);
 57                 Irssi::print("moc's not running, use /start to start the server") if ($ret eq -1);
 58                 return;
 59         }
 60         $string =~ s#`#'#g;
 61         if (!$server || !$server->{connected}) {
 62                 Irssi::print("Not connected to server");
 63                 return;
 64         }
 65         elsif ($witem && ($witem->{type} eq "CHANNEL" ||
 66                         $witem->{type} eq "QUERY")) {
 67                 # there's query/channel active in window
 68                 $witem->command("ME $string");
 69         } else {
 70                 #print songinfo even if you're viewing the server-window
 71                 Irssi::print("No active channel/query in window");
 72                 Irssi::print("moc $string");
 73         }
 74 }
 75 # retrieve infos about the currently played song by parsing `mocp -i` & write it to %info
 76 sub moc {
 77         #check if moc is running
 78         if (checkplaying() eq -1) {
 79                 return -1;
 80         }
 81 
 82         @array = `mocp -i`;
 83         foreach my $line (@array){
 84                 if ($line =~ m/^State:/){
 85                         $info{"state"} = $line;
 86                         $info{"state"} =~ s/State: //;
 87                         chomp $info{"state"};
 88                 }
 89                 if ($line =~ m/^SongTitle:/){
 90                         $info{"title"} = $line;
 91                         $info{"title"} =~ s/SongTitle: //;
 92                         chomp $info{"title"};
 93                 }
 94                 if ($line =~ m/^Artist/){
 95                         $info{"artist"} = $line;
 96                         $info{"artist"} =~ s/Artist: //;
 97                         chomp $info{"artist"};
 98                 }
 99                 if ($line =~ m/^Album/){
100                         $info{"album"} = $line;
101                         $info{"album"} =~ s/Album: //;
102                         chomp $info{"album"};
103                 }
104                 if ($line =~ m/^TotalTime/){
105                         $info{"total"} = $line;
106                         $info{"total"} =~ s/TotalTime: //;
107                         chomp $info{"total"};
108                 }
109                 if ($line =~ m/^CurrentTime/){
110                         $info{"current"} = $line;
111                         $info{"current"} =~ s/CurrentTime:[ ]*//;
112                         chomp $info{"current"};
113                 }
114                 if ($line =~ m/^Bitrate:/){
115                         $info{"bitrate"} = $line;
116                         $info{"bitrate"} =~ s/Bitrate: //;
117                         chomp $info{"bitrate"};
118                 }
119                 if ($line =~ m/^Rate:/){
120                         $info{"rate"} = $line;
121                         $info{"rate"} =~ s/Rate: //;
122                         chomp $info{"rate"};
123                 }
124                 if ($line =~ m/^Shuffle:/){
125                         $info{"shuffle"} = $line;
126                         $info{"shuffle"} =~ s/Shuffle: //;
127                         chomp $info{"shuffle"};
128                 }
129                 if ($line =~ m/^Repeat:/){
130                         $info{"repeat"} = $line;
131                         $info{"repeat"} =~ s/Repeat: //;
132                         chomp $info{"repeat"};
133                 }
134                 if ($line =~ m/^File: /) {
135                         $info{"file"} = $line;
136                         chomp($info{'file'});
137                         $info{"file"} =~ s/File: //;
138                         chomp $info{"file"};
139                         # it's an internetstream
140                         if ($info{"file"} =~ m/^http:\/\//){
141                                 $in = 1;
142                         }
143                         $info{'file'} =~ s#`#\\`#g;
144                         $info{"type"} = `file \"$info{'file'}\"`;  #filetype
145                         $info{'file'} =~ s#\\`#`#g;
146                         my $tmp = -s $info{'file'};
147                         $info{"size"} = sprintf("%.2f MB", ((($tmp) / 1024) / 1024)) ;
148                 }
149 
150         }
151         if ($info{'type'} =~ /mp3/i){
152                 $info{'type'} = "mp3";
153         }
154         elsif ($info{'type'} =~ /ogg/i){
155                 $info{'type'} = "ogg";
156         }
157         elsif ($info{'type'} =~ /wav/i){
158                 $info{'type'} = "wav";
159         }
160         elsif ($info{'type'} =~ /wma/i){
161                 $info{'type'} = "wma";
162         }
163         else{
164                 $info{'type'} = "unknown";
165         }
166 
167         if (($info{"state"} =~ m/STOP|PAUSE/)){
168                 return -1 if ($info{'state'} =~ m/STOP/);
169                 return -2 if ($info{'state'} =~ m/PAUSE/);
170         }
171 
172         if ($in eq 1){
173                 stream();
174                 return;
175         }
176 
177         $string = "is currently playing: $info{'artist'} - $info{'title'} on \"$info{'album'}\" | [$info{'current'}/$info{'total'}] " .
178                         "| [$info{'bitrate'}] | [$info{'rate'}] | [$info{'size'}] | [$info{'type'}]" ;
179 
180 }
181 
182 sub checkplaying {
183         return -1 unless (-e $pidfile);
184         return 0;
185 }
186 
187 sub stream {
188         $string = "is currently playing: $info{'artist'} - $info{'title'} | [$info{'current'}] " .
189                         "| [$info{'bitrate'}] | [$info{'rate'}] | [$info{'file'}] | [Streaming...]" ;
190         $in = 0;
191 }
192 
193 sub next {
194         if (checkplaying() == -1){
195                 Irssi::print "moc's not running, use /start to start the server";
196                 return;
197         }
198         system("mocp -f");
199         return 0;
200 }
201 
202 sub prev {
203         if (checkplaying() == -1) {
204                 Irssi::print "moc's not running, use /start to start the server";
205                 return;
206         }
207         system("mocp -r");
208         return 0;
209 }
210 
211 sub shuffle {
212         my ($data, $server, $witem) = @_;
213         if (checkplaying() == -1){
214                 Irssi::print "moc's not running, use /start to start the server";
215                 return;
216         }
217         system("mocp -t shuffle &>/dev/null");
218         moc();
219         return unless (defined ($info{'shuffle'}));
220         if (!$server || !$server->{connected}) {
221                 Irssi::print ("Shuffle: $info{'shuffle'}");
222         }
223         elsif ($witem && ($witem->{type} eq "CHANNEL" ||
224                         $witem->{type} eq "QUERY")) {
225                 # there's query/channel active in window
226                 $witem->command("echo Shuffle: $info{'shuffle'}");
227         }
228         else{
229                 Irssi::print ("Shuffle: $info{'shuffle'}");
230         }
231 
232         return 0;
233 }
234 
235 sub stop {
236         if (checkplaying() == -1){
237                 Irssi::print "moc's not running, use /start to start the server";
238                 return;
239         }
240         system("mocp -P");
241         return 0;
242 }
243 
244 sub play {
245         if (checkplaying() == -1) {
246                 Irssi::print "moc's not running, use /start to start the server";
247                 return;
248         }
249         system("mocp -U");
250         return 0;
251 }
252 
253 sub start {
254         system("mocp -p &> /dev/null");
255         return 0;
256 }
257 
258 sub repeat {
259         my ($data, $server, $witem) = @_;
260         if (moc() == -1){
261                 Irssi::print "moc's not running, use /start to start the server";
262                 return;
263         }
264         system("mocp -t repeat &>/dev/null");
265         moc();
266         return unless (defined ($info{'repeat'}));
267         if (!$server || !$server->{connected}) {
268                 Irssi::print ("Repeat: $info{'repeat'}");
269         }
270         elsif ($witem && ($witem->{type} eq "CHANNEL" ||
271                         $witem->{type} eq "QUERY")) {
272                 # there's query/channel active in window
273                 $witem->command("echo Repeat: $info{'repeat'}");
274         }
275         else{
276                 Irssi::print ("Repeat: $info{'repeat'}");
277         }
278         return 0;
279 
280 }
281 sub help {
282         Irssi::print(" mocp help: \n" .
283                         " /mocnp     -  prints the song currently played by moc in the current channel/query\n" .
284                         " /next      -  next song\n" .
285                         " /prev      -  previous song\n" .
286                         " /shuffle   -  Turns shuffle on/off (requires >=mocp 2.5)\n" .
287                         " /repeat    -  Turns repeat on/off (requires >=mocp 2.5)\n" .
288                         " /start     -  Starts moc if it's not running or if it's in mode \"stop\"\n" .
289                         " /play      -  Play... \n" .
290                         " /pause     -  Pause the current song\n" .
291                         " /stop      -  Stop moc\n" .
292                         " /mocp help -  Display this help \n" .
293                         "Please note that notifications about shuffle/repeat will only work with a patched version of moc...");
294 
295         return;
296 }
297 Irssi::command_bind('mocnp', 'irssi_stuff');
298 Irssi::command_bind('next', 'next');
299 Irssi::command_bind('prev', 'prev');
300 Irssi::command_bind('shuffle', 'shuffle');
301 Irssi::command_bind('repeat', 'repeat');
302 Irssi::command_bind('play', 'play');
303 Irssi::command_bind('start', 'start');
304 Irssi::command_bind('pause', 'stop');
305 Irssi::command_bind('stop', 'stop');
306 Irssi::command_bind('mocnp help', 'help');