]> git.sesse.net Git - vlc/blob - modules/misc/audioscrobbler.c
wait INTERVAL if needed on submitting, and don't try to submit data
[vlc] / modules / misc / audioscrobbler.c
1 /*****************************************************************************
2  * audioscrobbler.c : audioscrobbler submission plugin
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Rafaël Carré <funman at videolan org>
8  *          Kenneth Ostby <kenneo -at- idi -dot- ntnu -dot- no>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /* audioscrobbler protocol version: 1.1 
26  * http://audioscrobbler.net/wiki/Protocol1.1
27  * */
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32
33
34 #if defined( WIN32 )
35 #include <time.h>
36 #endif
37
38 #include <vlc/vlc.h>
39 #include <vlc_interface.h>
40 #include <vlc_meta.h>
41 #include <vlc_md5.h>
42 #include <vlc_block.h>
43 #include <vlc_stream.h>
44 #include <vlc_url.h>
45 #include <vlc_network.h>
46 #include <vlc_interface.h>
47 #include <vlc_playlist.h>
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52
53 /* Keeps track of metadata to be submitted */
54 typedef struct audioscrobbler_song_t
55 {
56     char        *psz_a;                /* track artist     */
57     char        *psz_t;                /* track title      */
58     char        *psz_b;                /* track album      */
59     int         i_l;                   /* track length     */
60     char        *psz_m;                /* musicbrainz id   */
61     char        *psz_i;                /* date             */
62     time_t      time_playing;          /* date (epoch)     */
63 } audioscrobbler_song_t;
64
65
66 /* Queue to be submitted to server, 10 songs max */
67 typedef struct audioscrobbler_queue_t
68 {
69     audioscrobbler_song_t   **p_queue;      /* contains up to 10 songs        */
70     int                     i_songs_nb;     /* number of songs                */
71     void                    *p_next_queue;  /* if queue full, pointer to next */
72 } audioscrobbler_queue_t;
73
74 struct intf_sys_t
75 {
76     audioscrobbler_queue_t  *p_first_queue;     /* 1st queue              */
77     vlc_mutex_t             lock;               /* p_sys mutex            */
78
79     /* data about audioscrobbler session */
80     time_t                  time_next_exchange; /* when can we send data? */
81     char                    *psz_submit_host;   /* where to submit data ? */
82     int                     i_submit_port;      /* at which port ?        */
83     char                    *psz_submit_file;   /* in which file ?        */
84     char                    *psz_username;      /* last.fm username       */
85     vlc_bool_t              b_handshaked;       /* did we handshake ?     */
86     char                    *psz_response_md5;  /* md5 response to use    */
87
88     /* data about song currently playing */
89     audioscrobbler_song_t   *p_current_song;    /* song being played      */
90     time_t                  time_pause;         /* time when vlc paused   */
91     time_t                  time_total_pauses;  /* sum of time in pause   */
92     vlc_bool_t              b_queued;           /* has it been queud ?    */
93     vlc_bool_t              b_metadata_read;    /* did we read metadata ? */
94     vlc_bool_t              b_paused;           /* is vlc paused ?        */
95     vlc_bool_t              b_waiting_meta;     /* we need fetched data?  */
96 };
97
98 intf_sys_t *p_sys_global;     /* to retrieve p_sys in Run() thread */
99
100 static int  Open        ( vlc_object_t * );
101 static void Close       ( vlc_object_t * );
102 static void Run         ( intf_thread_t * );
103 static int ItemChange   ( vlc_object_t *, const char *, vlc_value_t,
104                                 vlc_value_t, void * );
105 static int PlayingChange( vlc_object_t *, const char *, vlc_value_t,
106                                 vlc_value_t, void * );
107 static int AddToQueue   ( intf_thread_t *p_this );
108 static int Handshake    ( intf_thread_t *p_sd );
109 static int ReadMetaData ( intf_thread_t *p_this );
110 void DeleteQueue        ( audioscrobbler_queue_t *p_queue );
111
112 /*****************************************************************************
113  * Module descriptor
114  ****************************************************************************/
115
116 #define USERNAME_TEXT       N_("Username")
117 #define USERNAME_LONGTEXT   N_("The username of your last.fm account")
118 #define PASSWORD_TEXT       N_("Password")
119 #define PASSWORD_LONGTEXT   N_("The password of your last.fm account")
120
121 /* if something goes wrong, we wait at least one minute before trying again */
122 #define DEFAULT_INTERVAL 60
123
124 /* last.fm client identifier */
125 #define CLIENT_NAME     PACKAGE
126 #define CLIENT_VERSION  VERSION
127
128 /* HTTP POST request : to submit data */
129 #define    POST_REQUEST "POST /%s HTTP/1.1\n"                               \
130                         "Accept-Encoding: identity\n"                       \
131                         "Content-length: %d\n"                              \
132                         "Connection: close\n"                               \
133                         "Content-type: application/x-www-form-urlencoded\n" \
134                         "Host: %s\n"                                        \
135                         "User-agent: VLC Media Player/%s\r\n"               \
136                         "\r\n"                                              \
137                         "%s\r\n"                                            \
138                         "\r\n"
139
140 /* data to submit */
141 #define POST_DATA       "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s&b%%5B%d%%5D=%s" \
142                         "&m%%5B%d%%5D=%s&l%%5B%d%%5D=%d&i%%5B%d%%5D=%s"
143 #define HTTPPOST_MAXLEN 2048
144
145 vlc_module_begin();
146     set_category( CAT_INTERFACE );
147     set_subcategory( SUBCAT_INTERFACE_CONTROL );
148     set_shortname( N_( "Audioscrobbler" ) );
149     set_description( N_("Audioscrobbler submission Plugin") );
150     add_string( "lastfm-username", "", NULL,
151                 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
152     add_password( "lastfm-password", "", NULL,
153                 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
154     set_capability( "interface", 0 );
155     set_callbacks( Open, Close );
156 vlc_module_end();
157
158 /*****************************************************************************
159  * Open: initialize and create stuff
160  *****************************************************************************/
161
162 static int Open( vlc_object_t *p_this )
163 {
164     playlist_t      *p_playlist;
165     intf_thread_t   *p_intf     = ( intf_thread_t* ) p_this;
166     intf_sys_t      *p_sys      = malloc( sizeof( intf_sys_t ) );
167
168 #define MEM_ERROR \
169     free( p_sys->p_current_song ); \
170     free( p_sys->p_first_queue ); \
171     free( p_sys->psz_response_md5 ); \
172     free( p_sys ); \
173     return VLC_ENOMEM;
174
175     if( !p_sys )
176     {
177         MEM_ERROR
178     }
179
180     p_intf->p_sys = p_sys;
181
182     vlc_mutex_init( p_this, &p_sys->lock );
183
184     p_sys_global = p_sys;
185     p_sys->psz_submit_host = NULL;
186     p_sys->psz_submit_file = NULL;
187     p_sys->b_handshaked = VLC_FALSE;
188     p_sys->time_next_exchange = time( NULL );
189     p_sys->psz_username = NULL;
190     p_sys->b_paused = VLC_FALSE;
191
192 #define MALLOC_CHECK( a ) \
193     if( !a ) { \
194         vlc_mutex_destroy( &p_sys->lock ); \
195         MEM_ERROR \
196     }
197
198     /* md5 response is 32 chars, + final \0 */
199     p_sys->psz_response_md5 = malloc( 33 );
200     MALLOC_CHECK( p_sys->psz_response_md5 )
201
202     p_sys->p_first_queue = malloc( sizeof( audioscrobbler_queue_t ) );
203     MALLOC_CHECK( p_sys->p_first_queue )
204
205     p_sys->p_current_song = malloc( sizeof( audioscrobbler_song_t ) );
206     MALLOC_CHECK( p_sys->p_current_song )
207     time( &p_sys->p_current_song->time_playing );
208
209     /* queues can't contain more than 10 songs */
210     p_sys->p_first_queue->p_queue =
211         malloc( 10 * sizeof( audioscrobbler_song_t ) );
212     MALLOC_CHECK( p_sys->p_current_song )
213
214     p_sys->p_first_queue->i_songs_nb = 0;
215     p_sys->p_first_queue->p_next_queue = NULL;
216
217     p_playlist = pl_Yield( p_intf );
218     PL_LOCK;
219     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
220     PL_UNLOCK;
221     pl_Release( p_playlist );
222
223     p_intf->pf_run = Run;
224
225     return VLC_SUCCESS;
226 #undef MEM_ERROR
227 #undef MALLOC_CHECK
228 }
229
230 /*****************************************************************************
231  * Close: destroy interface stuff
232  *****************************************************************************/
233 static void Close( vlc_object_t *p_this )
234 {
235     audioscrobbler_queue_t      *p_current_queue, *p_next_queue;
236     playlist_t                  *p_playlist;
237     input_thread_t              *p_input;
238     intf_thread_t               *p_intf = ( intf_thread_t* ) p_this;
239     intf_sys_t                  *p_sys  = p_intf->p_sys;
240
241     p_playlist = pl_Yield( p_intf );
242     PL_LOCK;
243     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
244
245     p_input = p_playlist->p_input;
246     if ( p_input )
247     {
248         vlc_object_yield( p_input );
249         var_DelCallback( p_input, "state", PlayingChange, p_intf );
250         vlc_object_release( p_input );
251     }
252
253     PL_UNLOCK;
254     pl_Release( p_playlist );
255
256     vlc_mutex_lock ( &p_sys->lock );
257     p_current_queue = p_sys->p_first_queue;
258     vlc_mutex_unlock ( &p_sys->lock );
259
260     while( ( p_current_queue->i_songs_nb == 10 ) &&
261         ( p_current_queue->p_next_queue != NULL ) )
262     {
263         p_next_queue = p_current_queue->p_next_queue;
264         DeleteQueue( p_current_queue );
265         free( p_current_queue );
266         p_current_queue = p_next_queue;
267     }
268
269     DeleteQueue( p_current_queue );
270     free( p_current_queue );
271
272     vlc_mutex_lock ( &p_sys->lock );
273     free( p_sys->psz_username );
274     free( p_sys->p_current_song );
275     free( p_sys->psz_submit_host );
276     free( p_sys->psz_submit_file );
277     free( p_sys->psz_response_md5 );
278     vlc_mutex_unlock ( &p_sys->lock );
279     vlc_mutex_destroy( &p_sys->lock );
280     free( p_sys );
281 }
282
283 /*****************************************************************************
284  * Run : call Handshake() then submit songs
285  *****************************************************************************/
286 static void Run( intf_thread_t *p_this )
287 {
288     char                    *psz_submit         = NULL;
289     char                    *psz_submit_song    = NULL;
290     int                     i_net_ret;
291     int                     i_song;
292     playlist_t              *p_playlist;
293     uint8_t                 *p_buffer           = NULL;
294     char                    *p_buffer_pos       = NULL;
295     audioscrobbler_queue_t  *p_first_queue;
296     int                     i_post_socket;
297     time_t                  played_time;
298
299     p_this->p_sys = p_sys_global;
300     intf_sys_t *p_sys = p_this->p_sys;
301
302     #define MEM_ERROR \
303         free( psz_submit ); \
304         free( psz_submit_song ); \
305         free( p_buffer ); \
306         msg_Err( p_this, "Out of memory" ); \
307         return;
308
309     psz_submit = malloc( HTTPPOST_MAXLEN );
310     psz_submit_song = malloc( HTTPPOST_MAXLEN );
311     p_buffer = ( uint8_t* ) malloc( 1024 );
312
313     if( !psz_submit || !psz_submit_song || !p_buffer )
314     {
315         MEM_ERROR
316     }
317
318     /* main loop */
319     while( !p_this->b_die )
320     {
321         /* verify if there is data to submit 
322          * and if waiting interval is elapsed */
323         if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
324             ( time( NULL ) >= p_sys->time_next_exchange ) )
325         {
326             /* handshake if needed */
327             if( p_sys->b_handshaked == VLC_FALSE )
328             {
329                 msg_Dbg( p_this, "Handshaking with last.fm ..." );
330  
331                 switch( Handshake( p_this ) )
332                 {
333                     case VLC_ENOMEM:
334                         MEM_ERROR
335                         break;
336
337                     case VLC_ENOVAR:
338                         /* username not set */
339                         vlc_mutex_unlock ( &p_sys->lock );
340                         intf_UserFatal( p_this, VLC_FALSE,
341                             _("Last.fm username not set"),
342                             _("Please set an username or disable "
343                             "audioscrobbler plugin, and then restart VLC.\n"
344                             "Visit https://www.last.fm/join/ to get an account")
345                         );
346                         free( psz_submit );
347                         free( psz_submit_song );
348                         free( p_buffer );
349                         return;
350                         break;
351
352                     case VLC_SUCCESS:
353                         msg_Dbg( p_this, "Handshake successfull :)" );
354                         vlc_mutex_lock ( &p_sys->lock );
355                         p_sys->b_handshaked = VLC_TRUE;
356                         vlc_mutex_unlock ( &p_sys->lock );
357                         break;
358
359                     case VLC_EGENERIC:
360                     default:
361                         /* protocol error : we'll try later */
362                         vlc_mutex_lock ( &p_sys->lock );
363                         time( &p_sys->time_next_exchange );
364                         p_sys->time_next_exchange += DEFAULT_INTERVAL;
365                         vlc_mutex_unlock ( &p_sys->lock );
366                         break;
367                 }
368                 /* handshake is done or failed, lets start from 
369                  * beginning to check it out and wait INTERVAL if needed
370                  */
371                 continue;
372             }
373
374             msg_Dbg( p_this, "Going to submit some data..." );
375             vlc_mutex_lock ( &p_sys->lock );
376
377             snprintf( psz_submit, HTTPPOST_MAXLEN, "u=%s&s=%s",
378                 p_sys->psz_username, p_sys->psz_response_md5 );
379
380             /* forge the HTTP POST request */
381             for (i_song = 0 ; i_song < p_sys->p_first_queue->i_songs_nb ;
382                 i_song++ )
383             {
384                 snprintf( psz_submit_song, HTTPPOST_MAXLEN -1, POST_DATA,
385                     i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
386                     i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
387                     i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
388                     i_song, p_sys->p_first_queue->p_queue[i_song]->psz_m,
389                     i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
390                     i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
391                 );
392                 strncat( psz_submit, psz_submit_song, HTTPPOST_MAXLEN - 1 );
393             }
394
395             i_post_socket = net_ConnectTCP( p_this,
396                 p_sys->psz_submit_host, p_sys->i_submit_port);
397
398             if ( i_post_socket == -1 )
399             {
400                 /* If connection fails, we assume we must handshake again */
401                 time( &p_sys->time_next_exchange );
402                 p_sys->time_next_exchange += DEFAULT_INTERVAL;
403                 p_sys->b_handshaked = VLC_FALSE;
404                 vlc_mutex_unlock( &p_sys->lock );
405                 continue;
406             }
407
408             /* we transmit the data */
409             i_net_ret = net_Printf(
410                 VLC_OBJECT(p_this), i_post_socket, NULL,
411                 POST_REQUEST, p_sys->psz_submit_file,
412                 strlen( psz_submit ), p_sys->psz_submit_file,
413                 VERSION, psz_submit
414             );
415
416             if ( i_net_ret == -1 )
417             {
418                 /* If connection fails, we assume we must handshake again */
419                 time( &p_sys->time_next_exchange );
420                 p_sys->time_next_exchange += DEFAULT_INTERVAL;
421                 p_sys->b_handshaked = VLC_FALSE;
422                 vlc_mutex_unlock( &p_sys->lock );
423                 continue;
424             }
425
426             memset( p_buffer, '\0', 1024 );
427
428             i_net_ret = net_Read( p_this, i_post_socket, NULL,
429                         p_buffer, 1024, VLC_FALSE );
430             if ( i_net_ret <= 0 )
431             {
432                 /* if we get no answer, something went wrong : try again */
433                 vlc_mutex_unlock( &p_sys->lock );
434                 continue;
435             }
436
437             net_Close( i_post_socket );
438
439             /* record interval */
440             p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
441             if ( p_buffer_pos )
442             {
443                 time( &p_sys->time_next_exchange );
444                 p_sys->time_next_exchange += atoi( p_buffer_pos +
445                                             strlen( "INTERVAL " ) );
446             }
447
448             p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
449             if ( p_buffer_pos )
450             {
451                 /* woops, submission failed */
452                 msg_Dbg( p_this, "%s", p_buffer_pos );
453                 vlc_mutex_unlock ( &p_sys->lock );
454                 continue;
455             }
456
457             p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
458             if ( p_buffer_pos )
459             {
460                 msg_Dbg( p_this, "Authentification failed, handshaking again" );
461                 p_sys->b_handshaked = VLC_FALSE;
462                 vlc_mutex_unlock ( &p_sys->lock );
463                 continue;
464             }
465
466             p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
467             if ( p_buffer_pos )
468             {
469                 if ( p_sys->p_first_queue->i_songs_nb == 10 )
470                 {
471                     /* if there are more than one queue, delete the 1st */
472                     p_first_queue = p_sys->p_first_queue->p_next_queue;
473                     DeleteQueue( p_sys->p_first_queue );
474                     free( p_sys->p_first_queue );
475                     p_sys->p_first_queue = p_first_queue;
476                 }
477                 else
478                 {
479                     DeleteQueue( p_sys->p_first_queue );
480                     p_sys->p_first_queue->i_songs_nb = 0;
481                 }
482                 msg_Dbg( p_this, "Submission successfull!" );
483             }
484             vlc_mutex_unlock ( &p_sys->lock );
485         } /* data transmission finished or skipped */
486
487         msleep( INTF_IDLE_SLEEP );
488
489         p_playlist = pl_Yield( p_this );
490         PL_LOCK;
491         if( p_playlist->request.i_status == PLAYLIST_STOPPED )
492         {
493             /* if we stopped, we won't submit playing song */
494             vlc_mutex_lock( &p_sys->lock );
495             p_sys->b_queued = VLC_TRUE;
496             p_sys->b_metadata_read = VLC_TRUE;
497             vlc_mutex_unlock( &p_sys->lock );
498         }
499         PL_UNLOCK;
500         pl_Release( p_playlist );
501
502         vlc_mutex_lock( &p_sys->lock );
503         if( p_sys->b_metadata_read == VLC_FALSE )
504         {
505             /* we read the metadata of the playing song */
506             time( &played_time );
507             played_time -= p_sys->p_current_song->time_playing;
508             played_time -= p_sys->time_total_pauses;
509
510             vlc_mutex_unlock( &p_sys->lock );
511
512             if( played_time > 20 )
513             /* wait at least 20 secondes before reading the meta data
514              * since the songs must be at least 30s */
515             {
516                 if ( ReadMetaData( p_this ) == VLC_ENOMEM )
517                 {
518                     MEM_ERROR
519                 }
520             }
521         }
522         else
523         {
524             /* we add the playing song into the queue */
525             if( ( p_sys->b_queued == VLC_FALSE )
526                 && ( p_sys->b_paused == VLC_FALSE ) )
527             {
528                 vlc_mutex_unlock( &p_sys->lock );
529                 if( AddToQueue( p_this ) == VLC_ENOMEM )
530                 {
531                     MEM_ERROR
532                 }
533             }
534             else
535             {
536                 vlc_mutex_unlock( &p_sys->lock );
537             }
538         }
539     }
540 #undef MEM_ERROR
541 }
542
543 /*****************************************************************************
544  * PlayingChange: Playing status change callback
545  *****************************************************************************/
546 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
547                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
548 {
549     intf_thread_t   *p_intf = ( intf_thread_t* ) p_data;
550     intf_sys_t      *p_sys  = p_intf->p_sys;
551
552     (void)p_this; (void)psz_var; (void)oldval;
553
554     /* don't bother if song has already been queued */
555     if( p_sys->b_queued == VLC_TRUE )
556         return VLC_SUCCESS;
557
558     vlc_mutex_lock( &p_sys->lock );
559
560     if( newval.i_int == PAUSE_S )
561     {
562         time( &p_sys->time_pause );
563         p_sys->b_paused = VLC_TRUE;
564     }
565
566     else if( newval.i_int == PLAYING_S )
567     {
568         p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
569         p_sys->b_paused = VLC_FALSE;
570     }
571
572     vlc_mutex_unlock( &p_sys->lock );
573
574     return VLC_SUCCESS;
575 }
576
577 /*****************************************************************************
578  * ItemChange: Playlist item change callback
579  *****************************************************************************/
580 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
581                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
582 {
583     playlist_t          *p_playlist;
584     input_thread_t      *p_input    = NULL;
585     time_t              epoch;
586     struct tm           *epoch_tm;
587     char                psz_date[20];
588     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
589     intf_sys_t          *p_sys      = p_intf->p_sys;
590     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
591
592     p_playlist = pl_Yield( p_intf );
593     PL_LOCK;
594     p_input = p_playlist->p_input;
595
596     if( !p_input )
597     {
598         PL_UNLOCK;
599         pl_Release( p_playlist );
600
601         vlc_mutex_lock( &p_sys->lock );
602
603         p_sys->b_queued = VLC_TRUE;
604         p_sys->b_metadata_read = VLC_TRUE;
605
606         vlc_mutex_unlock( &p_sys->lock );
607
608         return VLC_SUCCESS;
609     }
610
611     vlc_object_yield( p_input );
612     PL_UNLOCK;
613     pl_Release( p_playlist );
614
615     var_AddCallback( p_input, "state", PlayingChange, p_intf );
616
617     vlc_mutex_lock ( &p_sys->lock );
618
619     /* reset pause counter */
620     p_sys->time_total_pauses = 0;
621
622     /* we'll read metadata when it's present */
623     p_sys->b_metadata_read = VLC_FALSE;
624     p_sys->b_waiting_meta = VLC_FALSE;
625
626     time( &epoch );
627     epoch_tm = gmtime( &epoch );
628     snprintf( psz_date, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
629         epoch_tm->tm_year+1900, epoch_tm->tm_mon+1, epoch_tm->tm_mday,
630         epoch_tm->tm_hour, epoch_tm->tm_min, epoch_tm->tm_sec );
631
632     p_sys->p_current_song->psz_i = encode_URI_component( psz_date );
633     p_sys->p_current_song->time_playing = epoch;
634
635     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
636     p_sys->b_paused = ( p_input->b_dead || !psz_name )
637                       ? VLC_TRUE : VLC_FALSE;
638     free( psz_name );
639
640     vlc_mutex_unlock( &p_sys->lock );
641
642     vlc_object_release( p_input );
643     return VLC_SUCCESS;
644 }
645
646 /*****************************************************************************
647  * AddToQueue: Add the played song to the queue to be submitted
648  *****************************************************************************/
649 static int AddToQueue ( intf_thread_t *p_this )
650 {
651     int                         i_songs_nb;
652     time_t                      played_time;
653     audioscrobbler_queue_t      *p_queue        = NULL,
654                                 *p_next_queue   = NULL;
655     intf_sys_t                  *p_sys          = p_this->p_sys;
656
657     /* wait for the user to listen enough before submitting */
658     time ( &played_time );
659     vlc_mutex_lock( &p_sys->lock );
660
661     played_time -= p_sys->p_current_song->time_playing;
662     played_time -= p_sys->time_total_pauses;
663
664     #define NO_SUBMISSION \
665         p_sys->b_queued = VLC_TRUE; \
666         vlc_mutex_unlock( &p_sys->lock ); \
667         return VLC_SUCCESS;
668
669     if( ( played_time < 240 ) &&
670         ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
671     {
672         //msg_Dbg( p_this, "Song not listened long enough -> waiting" );
673         vlc_mutex_unlock( &p_sys->lock );
674         return VLC_SUCCESS;
675     }
676
677     if( p_sys->p_current_song->i_l < 30 )
678     {
679         msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
680         NO_SUBMISSION
681     }
682
683     if( !*p_sys->p_current_song->psz_a || !*p_sys->p_current_song->psz_t )
684     {
685         msg_Dbg( p_this, "Missing artist or title -> not submitting" );
686         NO_SUBMISSION
687     }
688
689     msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
690
691     /* go to last queue */
692     p_queue = p_sys->p_first_queue;
693     while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
694         p_queue = p_queue->p_next_queue;
695
696     i_songs_nb = p_queue->i_songs_nb;
697
698     #define MALLOC_CHECK( a ) \
699         if( !a ) { \
700             vlc_mutex_unlock( &p_sys->lock ); \
701             return VLC_ENOMEM; \
702         }
703
704     if( i_songs_nb == 10 )
705     {
706         p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
707         MALLOC_CHECK( p_next_queue );
708         p_queue->p_next_queue = p_next_queue;
709         p_queue = p_next_queue;
710         i_songs_nb = 0;
711         p_queue->i_songs_nb = i_songs_nb;
712     }
713
714     p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
715     MALLOC_CHECK( p_queue->p_queue[i_songs_nb] );
716
717     #define QUEUE_COPY( a ) \
718         p_queue->p_queue[i_songs_nb]->a = p_sys->p_current_song->a
719
720     QUEUE_COPY(i_l);
721     QUEUE_COPY(psz_a);
722     QUEUE_COPY(psz_t);
723     QUEUE_COPY(psz_b);
724     QUEUE_COPY(psz_m);
725     QUEUE_COPY(psz_i);
726
727     p_queue->i_songs_nb++;
728     p_sys->b_queued = VLC_TRUE;
729
730     vlc_mutex_unlock( &p_sys->lock );
731
732     return VLC_SUCCESS;
733 #undef QUEUE_COPY
734 #undef MALLOC_CHECK
735 #undef NO_SUBMISSION
736 }
737
738 /*****************************************************************************
739  * Handshake : Init audioscrobbler connection
740  *****************************************************************************/
741 static int Handshake( intf_thread_t *p_this )
742 {
743     char                *psz_password           = NULL;
744     struct md5_s        *p_struct_md5           = NULL;
745     char                *psz_password_md5       = NULL;
746     char                *ps_challenge_md5       = NULL;
747     stream_t            *p_stream;
748     char                *psz_handshake_url      = NULL;
749     uint8_t             *p_buffer               = NULL;
750     char                *p_buffer_pos           = NULL; 
751     char                *psz_url_parser         = NULL;
752     char                *psz_buffer_substring;
753     int                 i_url_pos, i;
754
755     intf_thread_t       *p_intf                 = ( intf_thread_t* ) p_this;
756     intf_sys_t          *p_sys                  = p_this->p_sys;
757
758     vlc_mutex_lock ( &p_sys->lock );
759
760     #define MEM_ERROR \
761         free( p_buffer ); \
762         free( p_struct_md5 ); \
763         free( ps_challenge_md5 ); \
764         vlc_mutex_unlock( &p_sys->lock ); \
765         return VLC_ENOMEM;
766
767     #define PROTOCOL_ERROR \
768         free( p_buffer ); \
769         vlc_mutex_unlock( &p_sys->lock ); \
770         return VLC_EGENERIC;
771
772     #define MALLOC_CHECK( a ) \
773         if( !a ) \
774         { \
775             MEM_ERROR \
776         }
777
778     p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
779     MALLOC_CHECK( p_sys->psz_username )
780
781     /* username has not been setup, ignoring */
782     if ( !*p_sys->psz_username )
783         return VLC_ENOVAR;
784
785     psz_handshake_url = malloc( 1024 );
786     MALLOC_CHECK( p_sys->psz_username )
787
788     snprintf( psz_handshake_url, 1024,
789         "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
790         CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
791
792     /* send the http handshake request */
793     p_stream = stream_UrlNew( p_intf, psz_handshake_url);
794
795     free( psz_handshake_url );
796
797     if( !p_stream )
798     {
799         vlc_mutex_unlock ( &p_sys->lock );
800         return VLC_EGENERIC;
801     }
802
803     p_buffer = ( uint8_t* ) calloc( 1, 1024 );
804     if ( !p_buffer )
805     {
806         stream_Delete( p_stream );
807         MEM_ERROR
808     }
809
810     /* read answer */
811     if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
812     {
813         stream_Delete( p_stream );
814         PROTOCOL_ERROR
815     }
816
817     stream_Delete( p_stream );
818
819     /* record interval before next submission */
820     p_buffer_pos = strstr( ( char* ) p_buffer, "INTERVAL" );
821     if ( p_buffer_pos )
822     {
823         time( &p_sys->time_next_exchange );
824         p_sys->time_next_exchange +=
825                 atoi( p_buffer_pos + strlen( "INTERVAL " ) );
826     }
827
828     p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED" );
829     if ( p_buffer_pos )
830     {
831         /* handshake request failed, sorry */
832         msg_Dbg( p_this, "%s", p_buffer_pos );
833         PROTOCOL_ERROR
834     }
835
836     p_buffer_pos = strstr( ( char* ) p_buffer, "BADUSER" );
837     if ( p_buffer_pos )
838     {
839         /* username does not exist on the server */
840         intf_UserFatal( p_this, VLC_FALSE, _("Bad last.fm Username"),
841             _("last.fm username is incorrect, please verify your settings")
842         );
843         PROTOCOL_ERROR
844     }
845
846     p_buffer_pos = strstr( ( char* ) p_buffer, "UPDATE" );
847     if ( p_buffer_pos )
848     {
849         /* protocol has been updated, time to update the code */
850         msg_Dbg( p_intf, "Protocol updated : plugin may be outdated" );
851         msg_Dbg( p_intf, "%s", p_buffer_pos );
852     }
853
854     else
855     {
856         p_buffer_pos = strstr( ( char* ) p_buffer, "UPTODATE" );
857         if ( !p_buffer_pos )
858         {
859             msg_Dbg( p_intf, "Can't recognize server protocol" );
860             PROTOCOL_ERROR
861         }
862     }
863
864     psz_buffer_substring = strstr( p_buffer_pos, "\n" );
865     if( ( psz_buffer_substring == NULL ) || \
866             ( strlen( psz_buffer_substring + 1 ) < 32 ) )
867     {
868         msg_Dbg( p_intf, "Can't recognize server protocol" );
869         PROTOCOL_ERROR
870     }
871     else
872     {
873         ps_challenge_md5 = malloc( 32 );
874         MALLOC_CHECK( ps_challenge_md5 )
875         memcpy( ps_challenge_md5, psz_buffer_substring + 1, 32 );
876     }
877
878     p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
879
880     /* free old information */
881     free( p_sys->psz_submit_host );
882     free( p_sys->psz_submit_file );
883
884     psz_url_parser = p_buffer_pos + strlen( "http://" );
885     i_url_pos = strcspn( psz_url_parser, ":" );
886
887     p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
888     MALLOC_CHECK( p_sys->psz_submit_host )
889
890     p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
891
892     psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
893     i_url_pos = strcspn( psz_url_parser, "\n" );
894     p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
895     MALLOC_CHECK( p_sys->psz_submit_file )
896
897     free(p_buffer);
898
899     p_struct_md5 = malloc( sizeof( struct md5_s ) );
900     MALLOC_CHECK( p_struct_md5 )
901
902     psz_password = config_GetPsz(p_this, "lastfm-password");
903     MALLOC_CHECK( psz_password )
904
905     /* generates a md5 hash of the password */
906     InitMD5( p_struct_md5 );
907     AddMD5( p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
908     EndMD5( p_struct_md5 );
909
910     free( psz_password );
911
912     psz_password_md5 = malloc ( 33 );
913     MALLOC_CHECK( psz_password_md5 )
914
915     for ( i = 0; i < 4; i++ )
916     {
917         sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
918             p_struct_md5->p_digest[i] & 0xff,
919             ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
920             ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
921             p_struct_md5->p_digest[i] >> 24
922         );
923     }
924
925     /* generates a md5 hash of :
926      * - md5 hash of the password, plus
927      * - md5 challenge sent by last.fm server
928      */
929     InitMD5( p_struct_md5 );
930     AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
931     AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
932     EndMD5( p_struct_md5 );
933
934     free( ps_challenge_md5 );
935     free( psz_password_md5 );
936
937     for ( i = 0; i < 4; i++ )
938     {
939         sprintf( &p_sys->psz_response_md5[8*i], "%02x%02x%02x%02x",
940             p_struct_md5->p_digest[i] & 0xff,
941             ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
942             ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
943             p_struct_md5->p_digest[i] >> 24
944         );
945     }
946
947     p_sys->psz_response_md5[32] = '\0';
948
949     vlc_mutex_unlock ( &p_sys->lock );
950
951     return VLC_SUCCESS;
952 #undef MEM_ERROR
953 #undef PROTOCOL_ERROR
954 #undef MALLOC_CHECK
955 }
956
957 /*****************************************************************************
958  * DeleteQueue : Free all songs from an audioscrobbler_queue_t
959  *****************************************************************************/
960 void DeleteQueue( audioscrobbler_queue_t *p_queue )
961 {
962     int     i;
963
964     for( i = 0; i < p_queue->i_songs_nb; i++ )
965     {
966         free( p_queue->p_queue[i]->psz_a );
967         free( p_queue->p_queue[i]->psz_b );
968         free( p_queue->p_queue[i]->psz_t );
969         free( p_queue->p_queue[i]->psz_i );
970         free( p_queue->p_queue[i] );
971     }
972 }
973
974 /*****************************************************************************
975  * ReadMetaData : Read meta data when parsed by vlc
976  * or wait for fetching if unavailable
977  *****************************************************************************/
978 static int ReadMetaData( intf_thread_t *p_this )
979 {
980     playlist_t          *p_playlist;
981     input_thread_t      *p_input        = NULL;
982     vlc_value_t         video_val;
983
984     char                *psz_title      = NULL;
985     char                *psz_artist     = NULL;
986     char                *psz_album      = NULL;
987     char                *psz_trackid    = NULL;
988     int                 i_length        = -1;
989     vlc_bool_t          b_waiting;
990     intf_sys_t          *p_sys          = p_this->p_sys;
991     int                 i_status;
992
993     p_playlist = pl_Yield( p_this );
994     PL_LOCK;
995     p_input = p_playlist->p_input;
996
997     if( !p_input )
998     {
999         PL_UNLOCK;
1000         pl_Release( p_playlist );
1001         return( VLC_SUCCESS );
1002     }
1003
1004     vlc_object_yield( p_input );
1005     PL_UNLOCK;
1006     pl_Release( p_playlist );
1007
1008     var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
1009     if( ( video_val.i_int > 0 ) || \
1010         ( input_GetItem( p_input )->i_type == ITEM_TYPE_NET ) )
1011     {
1012         msg_Dbg( p_this, "Not an audio only local file -> no submission");
1013         vlc_object_release( p_input );
1014
1015         vlc_mutex_lock( &p_sys->lock );
1016         p_sys->b_queued = VLC_TRUE;
1017         p_sys->b_metadata_read = VLC_TRUE;
1018         vlc_mutex_unlock( &p_sys->lock );
1019
1020         return VLC_SUCCESS;
1021     }
1022
1023     #define FREE_INPUT_AND_CHARS \
1024         vlc_object_release( p_input ); \
1025         free( psz_title ); \
1026         free( psz_artist ); \
1027         free( psz_album ); \
1028         free( psz_trackid );
1029
1030     #define WAIT_METADATA_FETCHING( a ) \
1031         if ( b_waiting == VLC_TRUE ) \
1032         { \
1033             a = calloc( 1, 1 ); \
1034         } \
1035         else \
1036         { \
1037             vlc_object_release( p_input ); \
1038             vlc_mutex_lock( &p_sys->lock ); \
1039             p_sys->b_waiting_meta = VLC_TRUE; \
1040             vlc_mutex_unlock( &p_sys->lock ); \
1041             free( psz_artist ); \
1042             return VLC_SUCCESS; \
1043         }
1044
1045     char *psz_meta;
1046     #define ALLOC_ITEM_META( a, b ) \
1047         psz_meta = input_item_Get##b( input_GetItem( p_input ) ); \
1048         if( psz_meta ) \
1049         { \
1050             a = encode_URI_component( psz_meta ); \
1051             if( !a ) \
1052             { \
1053                 free( psz_meta ); \
1054                 FREE_INPUT_AND_CHARS \
1055                 return VLC_ENOMEM; \
1056             } \
1057             free( psz_meta ); \
1058         }
1059
1060     i_status = input_GetItem(p_input)->p_meta->i_status;
1061
1062     vlc_mutex_lock( &p_sys->lock );
1063     b_waiting = p_sys->b_waiting_meta;
1064     vlc_mutex_unlock( &p_sys->lock );
1065
1066     if( i_status & ( !b_waiting ? ITEM_PREPARSED : ITEM_META_FETCHED ) )
1067     {
1068         ALLOC_ITEM_META( psz_artist, Artist )
1069         else
1070         {
1071             msg_Dbg( p_this, "No artist.." );
1072             WAIT_METADATA_FETCHING( psz_artist )
1073         }
1074         psz_meta = input_item_GetName( input_GetItem( p_input ) );
1075         if( psz_meta )
1076         {
1077             psz_title = encode_URI_component( psz_meta );
1078             if( !psz_title )
1079             {
1080                 free( psz_meta );
1081                 FREE_INPUT_AND_CHARS
1082                 return VLC_ENOMEM;
1083             }
1084             free( psz_meta );
1085         }
1086         else
1087         {
1088             msg_Dbg( p_this, "No track name.." );
1089             WAIT_METADATA_FETCHING( psz_title );
1090         }
1091
1092         ALLOC_ITEM_META( psz_album, Album )
1093         else
1094             psz_album = calloc( 1, 1 );
1095
1096         ALLOC_ITEM_META( psz_trackid, TrackID )
1097         else
1098             psz_trackid = calloc( 1, 1 );
1099
1100         i_length = input_item_GetDuration( input_GetItem( p_input ) ) / 1000000;
1101
1102         vlc_mutex_lock ( &p_sys->lock );
1103
1104         p_sys->p_current_song->psz_a    = strdup( psz_artist );
1105         p_sys->p_current_song->psz_t    = strdup( psz_title );
1106         p_sys->p_current_song->psz_b    = strdup( psz_album );
1107         p_sys->p_current_song->psz_m    = strdup( psz_trackid );
1108         p_sys->p_current_song->i_l      = i_length;
1109         p_sys->b_queued                 = VLC_FALSE;
1110         p_sys->b_metadata_read          = VLC_TRUE;
1111
1112         vlc_mutex_unlock( &p_sys->lock );
1113
1114         msg_Dbg( p_this, "Meta data registered, waiting to be queued" );
1115     }
1116    
1117     FREE_INPUT_AND_CHARS 
1118     return VLC_SUCCESS;
1119 #undef FREE_INPUT_AND_CHARS
1120 #undef ALLOC_ITEM_META
1121 #undef WAIT_METADATA_FETCHING
1122 }