ec7f2920108662b7c80255e09a76148ea4d61c8f
[people/pmueller/ipfire-2.x.git] / config / mpfire / mpfire.pl
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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 3 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 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use MP3::Tag;
23 use MP3::Info;
24
25 require '/var/ipfire/general-functions.pl';
26 require "${General::swroot}/lang.pl";
27 require "${General::swroot}/header.pl";
28
29 my $filename = "";
30 my %songs = "";
31 my $debug = 0;
32 my $temp;
33
34 if ($ARGV[0] eq 'scan') {
35 my $command = "find ";
36 chomp $ARGV[1];
37 $command .= "\"$ARGV[1]\"";
38 if ($ARGV[2] eq 'off'){$command .= " -maxdepth 1";}
39 $command .= " -name *.mp3";
40 my @files = `$command`;
41
42 &getExistingSongs();
43
44 foreach (@files){
45 $filename = $_;
46 chomp($filename)
47 &getSongInfo();
48 }
49 open(DATEI, ">${General::swroot}/mpfire/db/songs.db") || die "Kann Datenbank nicht speichern";
50 print DATEI %songs;
51 close(DATEI);
52 }
53
54 if ($ARGV[0] eq 'getdb') {
55 &getExistingSongs();
56 print %songs;
57 }
58 elsif ($ARGV[0] eq 'play') {
59 &checkplaylist();
60 &checkmute();
61 if ($debug){print "Yes we are called and we will play $ARGV[1]\n";}
62 system("/usr/bin/mpg123 -b 1024 --aggressive -q \"$ARGV[1]\" 2>/dev/null >/dev/null &");
63 }
64 elsif ($ARGV[0] eq 'stop') {
65 my $PID = `ps -ef \| grep "wget -qO" \| head -1 \| grep -v "sh -c" \| awk '{ print \$2 }'`;
66 if ( $PID ne '' ){
67 if ($debug){print "Stopping $PID\n";}
68 system("kill -KILL $PID");
69 my $PID = `ps -ef \| grep "mpg123 -b 1024 --aggressive -Zq -" \| head -1 \| grep -v "sh -c" \| awk '{ print \$2 }'`;
70 if ($debug){print "Killing Process $PID\n";}
71 system("kill -KILL $PID");
72 }
73 else{
74 my $PID = `ps -ef \| grep mpg123 \| grep playlist \| head -1 \| grep -v "sh -c" \| awk '{ print \$2 }'`;
75 if ($debug){print "Stopping $PID\n";}
76 system("kill -KILL $PID");
77 }
78 }
79 elsif ($ARGV[0] eq 'volup') {
80 if ($debug){print "Increasing Volume\n";}
81 system("/usr/bin/amixer set Master $ARGV[1]%+ 2>/dev/null >/dev/null");
82 system("/usr/bin/amixer set PCM $ARGV[1]%+ 2>/dev/null >/dev/null");
83 }
84 elsif ($ARGV[0] eq 'voldown') {
85 if ($debug){print "Decreasing Volume\n";}
86 system("/usr/bin/amixer set Master $ARGV[1]%- 2>/dev/null >/dev/null");
87 system("/usr/bin/amixer set PCM $ARGV[1]%- 2>/dev/null >/dev/null");
88 }
89 elsif ($ARGV[0] eq 'playall') {
90 &checkplaylist();
91 &checkmute();
92 if ($debug){print "Playing everything\n";}
93 system("/usr/bin/mpg123 -b 1024 --aggressive -Zq@ /var/ipfire/mpfire/playlist 2>/dev/null >/dev/null &");
94 }
95 elsif ($ARGV[0] eq 'pause') {
96 my $PID = `ps -ef \| grep mpg123 \| grep playlist \| head -1 \| grep -v "sh -c" \| grep -v "grep" \| awk '{ print \$2 }'`;
97 if ($debug){print "Pausing Process $PID\n";}
98 system("kill -STOP $PID");
99 }
100 elsif ($ARGV[0] eq 'resume') {
101 my $PID = `ps -ef \| grep mpg123 \| grep playlist \| head -1 \| grep -v "sh -c" \| grep -v "grep" \| awk '{ print \$2 }'`;
102 if ($debug){print "Resuming Process $PID\n";}
103 system("kill -CONT $PID");
104 }
105 elsif ($ARGV[0] eq 'next') {
106 if ($debug){print "Next Song\n";}
107 my $PID = `ps -ef | grep mpg123 | grep playlist | head -1 | awk '{ print \$2 }'`;
108 system("kill -SIGINT $PID");
109 }
110 elsif ($ARGV[0] eq 'song') {
111 &checkmute();
112 my $song = `lsof -nX \| grep mpg123 \| grep REG \| grep mem | grep mp3 \| grep -v "sh -c" \| grep -v "grep"`;
113 my @song = split(/\//,$song);
114 my $i = @song;
115 if ( $i == 0 ){
116 my $song = `ps -ef \| grep "wget -qO" \| grep -v "sh -c" \| grep -v "grep"`;
117 my @song = split(/http\:\/\//,$song);
118 my $temp = $song[1];
119 my @song = split(/ /,$temp);
120 print $song[0];
121 }
122 else { print $song[$i-1];}
123 }
124 elsif ($ARGV[0] eq 'playweb') {
125 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
126 &checkmute();
127
128 if ($proxysettings{'UPSTREAM_PROXY'}) {
129 if ($proxysettings{'UPSTREAM_USER'}) {
130 &checkm3uproxy();
131 if ($debug){print "Playing webstream\n";}
132 system("wget -qO - `wget -qO - http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@$proxysettings{'UPSTREAM_PROXY'}$ARGV[1]` | mpg123 -b 1024 --aggressive -Zq - 2>/dev/null >/dev/null &");
133 }
134 else {
135 &checkm3uproxyuser();
136 if ($debug){print "Playing webstream\n";}
137 system("wget -qO - `wget -qO - http://$proxysettings{'UPSTREAM_PROXY'}$ARGV[1]` | mpg123 -b 1024 --aggressive -Zq - 2>/dev/null >/dev/null &");}
138 } else {
139 &checkm3u();
140 if ($debug){print "Playing webstream\n";}
141 system("wget -qO - `wget -qO - http://$ARGV[1]` | mpg123 -b 1024 --aggressive -Zq - 2>/dev/null >/dev/null &");
142 }
143 }
144 elsif ($ARGV[0] eq 'volume') {
145 $temp = "Master - ";
146 $temp .= `amixer get Master \| tail -2 \| awk '{ print \$2" "\$5 }'`;
147 $temp .= "<break>PCM -";
148 $temp .= `amixer get PCM \| tail -2 \| awk '{ print \$2" "\$5 }'`;
149 print $temp;
150 }
151
152 sub getSongInfo(){
153 my $mp3 = MP3::Tag->new($filename);
154 my ($title, $track, $artist, $album, $comment, $year, $genre) = $mp3->autoinfo();
155 my $info = get_mp3info($filename);
156 $mp3->close();
157 $songs{$filename} = "|".$artist."|".$title."|".$track."|".$album."|".$year."|".$genre."|".$info->{MM}."|".$info->{SS}."|".$info->{BITRATE}."|".$info->{FREQUENCY}."|".$info->{MODE}."\n";
158 }
159
160 sub getExistingSongs(){
161 open(DATEI, "<${General::swroot}/mpfire/db/songs.db") || die "Keine Datenbank vorhanden";
162 my @Zeilen = <DATEI>;
163 close(DATEI);
164 foreach (@Zeilen){
165 my @Zeile = split(/\|/,$_);
166 $songs{$Zeile[0]} = "|".$Zeile[1]."|".$Zeile[2]."|".$Zeile[3]."|".$Zeile[4]."|".$Zeile[5]."|".$Zeile[6]."|".$Zeile[7]."|".$Zeile[8]."|".$Zeile[9]."|".$Zeile[10]."|".$Zeile[11]."\n";
167 }
168 }
169
170 sub checkplaylist(){
171 my $Datei = "/var/ipfire/mpfire/playlist";
172 my @Info = stat($Datei);
173 if ( $Info[7] eq '' || $Info[7] eq '0' ){print "There is no playlist";exit(1);}
174 }
175
176 sub checkmute(){
177 $temp = `amixer get Master \| tail -2`;
178 my @Master = split(/ /,$temp);
179 $temp = `amixer get PCM \| tail -2`;
180 my @PCM = split(/ /,$temp);
181 if ( $PCM[7] =~ /off/ ){
182 if ($debug){print "PCM was muted - umuting.\n";}
183 system("amixer set PCM toggle");
184 }
185 if ( $Master[7] =~ /off/ ){
186 if ($debug){print "Master was muted - umuting.\n";}
187 system("amixer set Master toggle");
188 }
189 }
190
191 sub checkm3u(){
192 my $Datei = system("wget -q --spider http://$ARGV[1]");
193 if ( $Datei ne '0' ){print "We are unable to get the stream";exit(1);}
194 }
195
196 sub checkm3uproxy(){
197 my $Datei = system("wget -q --spider http://$ARGV[1]");
198 if ( $Datei ne '0' ){print "We are unable to get the stream";exit(1);}
199 }
200
201 sub checkm3uproxyuser(){
202 my $Datei = system("wget -q --spider http://$ARGV[1]");
203 if ( $Datei ne '0' ){print "We are unable to get the stream";exit(1);}
204 }