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