]> git.sesse.net Git - vlc/blob - modules/misc/audioscrobbler.c
* implemented the previously committed playmode buttons and fixed the playmode saving
[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     audioscrobbler_queue_t      *p_current_queue, *p_next_queue;
254     input_thread_t              *p_input;
255     intf_thread_t               *p_intf = (intf_thread_t* ) p_this;
256     intf_sys_t                  *p_sys = p_intf->p_sys;
257     playlist_t                  *p_playlist = pl_Yield( p_intf );
258
259     p_playlist = pl_Yield( p_intf );
260     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
261
262     PL_LOCK;
263     p_input = p_playlist->p_input;
264     if( p_input ) vlc_object_yield( p_input );
265     PL_UNLOCK;
266
267     if( p_input )
268     {
269         var_DelCallback( p_input, "state", PlayingChange, p_intf );
270     }
271
272     pl_Release( p_playlist );
273
274     vlc_mutex_lock ( &p_sys->lock );
275     p_current_queue = p_sys->p_first_queue;
276     vlc_mutex_unlock ( &p_sys->lock );
277
278     while( ( p_current_queue->i_songs_nb == 10 ) &&
279         ( p_current_queue->p_next_queue != NULL ) )
280     {
281         p_next_queue = p_current_queue->p_next_queue;
282         DeleteQueue( p_current_queue );
283         free( p_current_queue );
284         p_current_queue = p_next_queue;
285     }
286
287     DeleteQueue( p_current_queue );
288     free( p_current_queue );
289
290     vlc_mutex_lock ( &p_sys->lock );
291     if ( p_sys->psz_username )
292     {
293         free( p_sys->psz_username );
294     }
295
296     free( p_sys->p_current_song );
297     vlc_mutex_unlock ( &p_sys->lock );
298     vlc_mutex_destroy( &p_sys->lock );
299     free( p_sys );
300 }
301
302 /*****************************************************************************
303  * Run : Handshake with audioscrobbler, then submit songs
304  *****************************************************************************/
305 static void Run( intf_thread_t *p_this )
306 {
307     char                    *psz_submit_string = NULL;
308     int                     i_handshake;
309     int                     i_netprintf;
310     int                     i_song;
311     playlist_t              *p_playlist;
312     uint8_t                 *p_buffer = NULL;
313     char                    *p_buffer_pos = NULL;
314     time_t                  played_time;
315     audioscrobbler_queue_t  *p_first_queue;
316     intf_sys_t              *p_sys;
317
318     p_this->p_sys = p_sys_global;
319     p_sys = p_this->p_sys;
320     while( !p_this->b_die )
321     {
322         if( p_sys->b_handshaked == VLC_FALSE )
323         {
324             if ( time( NULL ) >=
325                 ( p_sys->time_last_interval + p_sys->i_interval ) )
326             {
327                 msg_Dbg( p_this, "Handshaking with last.fm ..." );
328                 i_handshake = Handshake( p_this );
329
330                 if( i_handshake == VLC_ENOMEM )
331                 {
332                     msg_Err( p_this, "Out of memory" );
333                     return;
334                 }
335
336                 else if( i_handshake == VLC_ENOVAR )
337                 /* username not set */
338                 {
339                     msg_Dbg( p_this, "Set an username then restart vlc" );
340                     vlc_mutex_unlock ( &p_sys->lock );
341                     return;
342                 }
343
344                 else if( i_handshake == VLC_SUCCESS )
345                 {
346                     msg_Dbg( p_this, "Handshake successfull :)" );
347                     vlc_mutex_lock ( &p_sys->lock );
348                     p_sys->b_handshaked = VLC_TRUE;
349                     vlc_mutex_unlock ( &p_sys->lock );
350                 }
351
352                 else
353                 {
354                     vlc_mutex_lock ( &p_sys->lock );
355                     p_sys->i_interval = DEFAULT_INTERVAL;
356                     time( &p_sys->time_last_interval );
357                     vlc_mutex_unlock ( &p_sys->lock );
358                 }
359             }
360         }
361
362         else
363         {
364             if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
365                 ( time( NULL ) >=
366                 ( p_sys->time_last_interval + p_sys->i_interval )  ) )
367             {
368                 msg_Dbg( p_this, "Going to submit some data..." );
369                 vlc_mutex_lock ( &p_sys->lock );
370                 psz_submit_string = malloc( 2048 * sizeof( char ) );
371
372                 if (!psz_submit_string)
373                 {
374                     msg_Err( p_this, "Out of memory" );
375                     vlc_mutex_unlock ( &p_sys->lock );
376                     return;
377                 }
378
379                 for (i_song = 0; i_song < p_sys->p_first_queue->i_songs_nb ;
380                     i_song++ )
381                 {
382                     snprintf( psz_submit_string, 2048, POST_DATA,
383                         p_sys->psz_username, p_sys->psz_response_md5,
384                         i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
385                         i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
386                         i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
387                         i_song,
388                         i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
389                         i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
390                     );
391                 }
392
393                 p_sys->i_post_socket = net_ConnectTCP( p_this,
394                     p_sys->psz_submit_host, p_sys->i_submit_port);
395
396                 i_netprintf = net_Printf(
397                     VLC_OBJECT(p_this), p_sys->i_post_socket, NULL,
398                     POST_REQUEST, p_sys->psz_submit_file,
399                     strlen( psz_submit_string), p_sys->psz_submit_file,
400                     VERSION, psz_submit_string
401                 );
402
403                 if ( i_netprintf == -1 )
404                 {
405                 /* If connection fails, we assume we must handshake again */
406                     p_sys->i_interval = DEFAULT_INTERVAL;
407                     time( &p_sys->time_last_interval );
408                     p_sys->b_handshaked = VLC_FALSE;
409                     vlc_mutex_unlock ( &p_sys->lock );
410                     return;
411                 }
412
413                 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
414                 if ( !p_buffer )
415                 {
416                     msg_Err( p_this, "Out of memory" );
417                     vlc_mutex_unlock ( &p_sys->lock );
418                     return;
419                 }
420
421                 net_Read( p_this, p_sys->i_post_socket, NULL,
422                           p_buffer, 1024, VLC_FALSE );
423                 net_Close( p_sys->i_post_socket );
424
425                 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
426
427                 if ( p_buffer_pos )
428                 {
429                     p_sys->i_interval = atoi( p_buffer_pos +
430                                               strlen( "INTERVAL " ) );
431                     time( &p_sys->time_last_interval );
432                 }
433
434                 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
435
436                 if ( p_buffer_pos )
437                 {
438                     msg_Err( p_this, p_buffer_pos );
439                     vlc_mutex_unlock ( &p_sys->lock );
440                     return;
441                 }
442
443                 p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
444
445                 if ( p_buffer_pos )
446                 {
447                     msg_Err( p_this,
448                              "Authentification failed, handshaking again" );
449                     p_sys->b_handshaked = VLC_FALSE;
450                     vlc_mutex_unlock ( &p_sys->lock );
451                     return;
452                 }
453
454                 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
455
456                 if ( p_buffer_pos )
457                 {
458                     if ( p_sys->p_first_queue->i_songs_nb == 10 )
459                     {
460                         p_first_queue = p_sys->p_first_queue->p_next_queue;
461                         DeleteQueue( p_sys->p_first_queue );
462                         free( p_sys->p_first_queue );
463                         p_sys->p_first_queue = p_first_queue;
464                     }
465                     else
466                     {
467                         DeleteQueue( p_sys->p_first_queue );
468                         p_sys->p_first_queue->i_songs_nb = 0;
469                     }
470                     msg_Dbg( p_this, "Submission successfull!" );
471                 }
472                 vlc_mutex_unlock ( &p_sys->lock );
473             }
474         }
475         msleep( INTF_IDLE_SLEEP );
476
477         p_playlist = pl_Yield( p_this );
478         PL_LOCK;
479         if( p_playlist->request.i_status == PLAYLIST_STOPPED )
480         {
481             PL_UNLOCK;
482             /* if we stopped, we won't submit playing song */
483             vlc_mutex_lock( &p_sys->lock );
484             p_sys->b_queued = VLC_TRUE;
485             p_sys->b_metadata_read = VLC_TRUE;
486             vlc_mutex_unlock( &p_sys->lock );
487         }
488         else
489         {
490             PL_UNLOCK;
491         }
492
493         pl_Release( p_playlist );
494         vlc_mutex_lock( &p_sys->lock );
495
496         if( p_sys->b_metadata_read == VLC_FALSE )
497         {
498             time( &played_time );
499             played_time -= p_sys->p_current_song->time_playing;
500             played_time -= p_sys->time_total_pauses;
501
502
503             vlc_mutex_unlock( &p_sys->lock );
504
505             /* ok now we can read meta data */
506             if( played_time > 10 )
507             {
508                 ReadMetaData( p_this );
509             }
510         }
511         else
512         {
513             if( ( p_sys->b_queued == VLC_FALSE )
514                 && ( p_sys->b_paused == VLC_FALSE ) )
515             {
516                 vlc_mutex_unlock( &p_sys->lock );
517                 if( AddToQueue( p_this ) == VLC_ENOMEM )
518                 {
519                     msg_Err( p_this, "Out of memory" );
520                     return;
521                 }
522             }
523             else
524             {
525                 vlc_mutex_unlock( &p_sys->lock );
526             }
527         }
528     }
529 }
530
531 /*****************************************************************************
532  * PlayingChange: Playing status change callback
533  *****************************************************************************/
534 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
535                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
536 {
537     intf_thread_t       *p_intf;
538     intf_sys_t          *p_sys;
539
540     p_intf = ( intf_thread_t* ) p_data;
541     p_sys = p_intf->p_sys;
542
543     (void)p_this; (void)psz_var; (void)oldval;
544
545     vlc_mutex_lock( &p_sys->lock );
546
547     if( newval.i_int == PAUSE_S )
548     {
549         time( &p_sys->time_pause );
550         p_sys->b_paused = VLC_TRUE;
551     }
552
553     if( newval.i_int == PLAYING_S )
554     {
555         p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
556         p_sys->b_paused = VLC_TRUE;
557     }
558
559     vlc_mutex_unlock( &p_sys->lock );
560
561     return VLC_SUCCESS;
562 }
563
564 /*****************************************************************************
565  * ItemChange: Playlist item change callback
566  *****************************************************************************/
567 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
568                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
569 {
570     playlist_t          *p_playlist;
571     input_thread_t      *p_input = NULL;
572     intf_thread_t       *p_intf;
573     intf_sys_t          *p_sys;
574
575     time_t              epoch;
576     struct tm           *epoch_tm;
577
578     char                psz_date[20];
579
580     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
581
582     p_intf = ( intf_thread_t* ) p_data;
583     p_sys = p_intf->p_sys;
584
585     p_playlist = pl_Yield( p_intf );
586     PL_LOCK;
587     p_input = p_playlist->p_input;
588
589     if( !p_input )
590     {
591         PL_UNLOCK;
592         pl_Release( p_playlist );
593         vlc_mutex_lock( &p_sys->lock );
594
595         /* we won't read p_input */
596         p_sys->b_queued = VLC_TRUE;
597         p_sys->b_metadata_read = VLC_TRUE;
598
599         vlc_mutex_unlock( &p_sys->lock );
600         return VLC_SUCCESS;
601     }
602
603     vlc_object_yield( p_input );
604     PL_UNLOCK;
605     pl_Release( p_playlist );
606
607     var_AddCallback( p_input, "state", PlayingChange, p_intf );
608
609     vlc_mutex_lock ( &p_sys->lock );
610
611     /* reset pause counter */
612     p_sys->time_total_pauses = 0;
613
614     /* we save the p_input value to delete the callback later */
615     p_sys->p_input = p_input;
616
617     /* we'll read after to be sure it's present */
618     p_sys->b_metadata_read = VLC_FALSE;
619
620     p_sys->b_queued = VLC_TRUE;
621
622     time( &epoch );
623     epoch_tm = gmtime( &epoch );
624     snprintf( psz_date, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
625         epoch_tm->tm_year+1900, epoch_tm->tm_mon+1, epoch_tm->tm_mday,
626         epoch_tm->tm_hour, epoch_tm->tm_min, epoch_tm->tm_sec );
627
628     p_sys->p_current_song->psz_i = encode_URI_component( psz_date );
629     p_sys->p_current_song->time_playing = epoch;
630
631     p_sys->b_paused = ( p_input->b_dead || !p_input->input.p_item->psz_name )
632                       ? VLC_TRUE : VLC_FALSE;
633
634     vlc_mutex_unlock( &p_sys->lock );
635
636     vlc_object_release( p_input );
637     return VLC_SUCCESS;
638 }
639
640 /*****************************************************************************
641  * AddToQueue: Add the played song to the queue to be submitted
642  *****************************************************************************/
643 static int AddToQueue ( intf_thread_t *p_this )
644 {
645     int                         i_songs_nb;
646     time_t                      played_time;
647     audioscrobbler_queue_t      *p_queue = NULL, *p_next_queue = NULL;
648     intf_sys_t                  *p_sys;
649
650     p_sys = p_this->p_sys;
651
652     /* has it been queued already ? is it long enough ? */
653     vlc_mutex_lock( &p_sys->lock );
654     if( p_sys->p_current_song->i_l < 30 )
655     {
656         msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
657         p_sys->b_queued = VLC_TRUE;
658         vlc_mutex_unlock ( &p_sys->lock );
659         return VLC_SUCCESS;
660     }
661
662     /* wait for the user to listen enough before submitting */
663     time ( &played_time );
664     played_time -= p_sys->p_current_song->time_playing;
665     played_time -= p_sys->time_total_pauses;
666
667     if( ( played_time < 240 )
668         && ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
669     {
670         vlc_mutex_unlock ( &p_sys->lock );
671         return VLC_SUCCESS;
672     }
673
674     msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
675
676     p_queue = p_sys->p_first_queue;
677
678     while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
679     {
680         p_queue = p_queue->p_next_queue;
681     }
682
683     i_songs_nb = p_queue->i_songs_nb;
684
685     if( i_songs_nb == 10 )
686     {
687         p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
688         if( !p_next_queue )
689         {
690             vlc_mutex_unlock ( &p_sys->lock );
691             return VLC_ENOMEM;
692         }
693         p_queue->p_next_queue = p_next_queue;
694         i_songs_nb = 0;
695         p_queue = p_next_queue;
696         p_queue->i_songs_nb = i_songs_nb;
697     }
698
699     p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
700
701     p_queue->p_queue[i_songs_nb]->i_l = p_sys->p_current_song->i_l;
702
703     p_queue->p_queue[i_songs_nb]->psz_a =
704         strdup( p_sys->p_current_song->psz_a );
705
706     p_queue->p_queue[i_songs_nb]->psz_t =
707         strdup( p_sys->p_current_song->psz_t );
708
709     p_queue->p_queue[i_songs_nb]->psz_b =
710         strdup( p_sys->p_current_song->psz_b );
711
712     p_queue->p_queue[i_songs_nb]->psz_i =
713         strdup( p_sys->p_current_song->psz_i );
714
715     p_queue->i_songs_nb++;
716     p_sys->b_queued = VLC_TRUE;
717
718     vlc_mutex_unlock( &p_sys->lock );
719
720     return VLC_SUCCESS;
721 }
722
723 /*****************************************************************************
724  * Handshake : Init audioscrobbler connection
725  *****************************************************************************/
726 static int Handshake( intf_thread_t *p_this )
727 {
728     char                *psz_password = NULL;
729     struct md5_s        *p_struct_md5 = NULL;
730     char                *psz_password_md5 = NULL;
731     char                *ps_challenge_md5 = NULL;
732
733     stream_t            *p_stream;
734     char                *psz_handshake_url = NULL;
735
736     uint8_t             *p_buffer = NULL;
737     char                *p_buffer_pos = NULL;
738     char                *psz_buffer_substring = NULL;
739     char                *psz_url_parser = NULL;
740     int                 i_url_pos, i;
741
742     char                *b1, *b2, *b3, *b4;
743
744     intf_thread_t       *p_intf;
745     intf_sys_t          *p_sys;
746
747     p_intf = ( intf_thread_t* ) p_this;
748     p_sys = p_this->p_sys;
749
750     vlc_mutex_lock ( &p_sys->lock );
751
752     p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
753     if ( !p_sys->psz_username )
754     {
755         goto memerror;
756     }
757
758
759     if ( !*p_sys->psz_username )
760     {
761         msg_Info( p_this, "You have to set an username! "
762          "Visit https://www.last.fm/join/" );
763         return VLC_ENOVAR;
764     }
765
766     psz_handshake_url = malloc( 1024 );
767     if ( !psz_handshake_url )
768     {
769         goto memerror;
770     }
771
772     snprintf( psz_handshake_url, 1024,
773         "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
774         CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
775
776     p_stream = stream_UrlNew( p_intf, psz_handshake_url);
777
778     free( psz_handshake_url );
779
780     if( !p_stream )
781     {
782         p_sys->i_interval = DEFAULT_INTERVAL;
783         time( &p_sys->time_last_interval );
784         vlc_mutex_unlock ( &p_sys->lock );
785         return VLC_EGENERIC;
786     }
787
788     p_buffer = ( uint8_t* ) calloc( 1, 1024 );
789     if ( !p_buffer )
790     {
791         stream_Delete( p_stream );
792         goto memerror;
793     }
794
795     if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
796     {
797         stream_Delete( p_stream );
798         free( p_buffer );
799         vlc_mutex_unlock ( &p_sys->lock );
800         return VLC_EGENERIC;
801     }
802
803     stream_Delete( p_stream );
804
805     p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
806
807     if ( p_buffer_pos )
808     {
809         p_sys->i_interval = atoi( p_buffer_pos + strlen( "INTERVAL " ) );
810         time( &p_sys->time_last_interval );
811     }
812
813     p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
814
815     if ( p_buffer_pos )
816     {
817         msg_Info( p_this, p_buffer_pos );
818         free( p_buffer );
819         vlc_mutex_unlock ( &p_sys->lock );
820         return VLC_EGENERIC;
821     }
822
823     p_buffer_pos = strstr( ( char * ) p_buffer, "BADUSER" );
824
825     if ( p_buffer_pos )
826     {
827         msg_Info( p_intf, "Username is incorrect" );
828         free( p_buffer );
829         vlc_mutex_unlock ( &p_sys->lock );
830         return VLC_EGENERIC;
831     }
832
833     p_buffer_pos = strstr( ( char * ) p_buffer, "UPDATE" );
834
835     if ( p_buffer_pos )
836     {
837         msg_Dbg( p_intf, "Protocol updated" );
838         msg_Dbg( p_intf, p_buffer_pos );
839     }
840
841     else
842     {
843         p_buffer_pos = strstr( ( char * ) p_buffer, "UPTODATE" );
844         if ( !p_buffer_pos )
845         {
846             msg_Dbg( p_intf, "Protocol error" );
847             free( p_buffer );
848             vlc_mutex_unlock ( &p_sys->lock );
849             return VLC_EGENERIC;
850         }
851     }
852
853     psz_buffer_substring = strndup( strstr( p_buffer_pos, "\n" ) + 1, 32 );
854     if ( !psz_buffer_substring )
855     {
856         goto memerror;
857     }
858     else
859     {
860         ps_challenge_md5 = malloc( sizeof( char ) * 32 );
861         if ( !ps_challenge_md5 )
862         {
863             goto memerror;
864         }
865         memcpy( ps_challenge_md5, psz_buffer_substring, 32 );
866         free( psz_buffer_substring );
867     }
868
869     p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
870
871     if ( p_sys->psz_submit_host != NULL )
872     {
873         free( p_sys->psz_submit_host );
874     }
875
876     if ( p_sys->psz_submit_file != NULL )
877     {
878         free( p_sys->psz_submit_file );
879     }
880
881     psz_url_parser = p_buffer_pos + strlen( "http://" );
882
883     i_url_pos = strcspn( psz_url_parser, ":" );
884     p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
885
886     p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
887
888     psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
889     i_url_pos = strcspn( psz_url_parser, "\n" );
890     p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
891
892     free(p_buffer);
893
894     p_struct_md5 = malloc( sizeof( struct md5_s ) );
895     if( !p_struct_md5 )
896     {
897         goto memerror;
898     }
899
900     psz_password = config_GetPsz(p_this, "lastfm-password");
901     if ( !psz_password )
902     {
903          goto memerror;
904     }
905
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 * sizeof( char ) );
913     if ( !psz_password_md5 )
914     {
915         goto memerror;
916     }
917
918     for ( i = 0; i < 4; i++ )
919     {
920     /* TODO check that this works on every arch/platform (uint32_t to char) */
921         b1 = hexa( p_struct_md5->p_digest[i] % 256 );
922         b2 = hexa( ( p_struct_md5->p_digest[i] / 256 ) % 256 );
923         b3 = hexa( ( p_struct_md5->p_digest[i] / 65536 ) % 256 );
924         b4 = hexa( p_struct_md5->p_digest[i] / 16777216 );
925         sprintf( &psz_password_md5[8*i], "%s%s%s%s", b1, b2, b3, b4 );
926         free( b1 );
927         free( b2 );
928         free( b3 );
929         free( b4 );
930     }
931
932     strlwr( psz_password_md5 );
933
934     InitMD5( p_struct_md5 );
935     AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
936     AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
937     EndMD5( p_struct_md5 );
938
939     free( ps_challenge_md5 );
940     free( psz_password_md5 );
941
942     for ( i = 0; i < 4; i++ )
943     {
944         b1 = hexa( p_struct_md5->p_digest[i] % 256 );
945         b2 = hexa( ( p_struct_md5->p_digest[i] / 256 ) % 256 );
946         b3 = hexa( ( p_struct_md5->p_digest[i] / 65536 ) % 256 );
947         b4 = hexa( p_struct_md5->p_digest[i] / 16777216 );
948         sprintf( &p_sys->psz_response_md5[8*i],"%s%s%s%s", b1, b2, b3, b4 );
949         free( b1 );
950         free( b2 );
951         free( b3 );
952         free( b4 );
953     }
954
955     p_sys->psz_response_md5[32] = 0;
956
957     strlwr( p_sys->psz_response_md5 );
958
959     vlc_mutex_unlock ( &p_sys->lock );
960
961     return VLC_SUCCESS;
962
963 memerror:
964     free( p_buffer );
965     free( p_struct_md5 );
966     free( psz_buffer_substring );
967
968     vlc_mutex_unlock ( &p_sys->lock );
969     return VLC_ENOMEM;
970 }
971
972 /*****************************************************************************
973  * hexa : Converts a byte to a string in its hexadecimal value
974  *****************************************************************************/
975 char *hexa( short int i )
976 {
977     char        *res = calloc( 3 , sizeof( char ) );
978
979     ((i/16) < 10) ? res[0] = (i / 16) + '0' : ( res[0] = (i/16) + 'a' - 10 );
980     ((i%16) < 10) ? res[1] = (i % 16) + '0' : ( res[1] = (i%16) + 'a' - 10 );
981
982     return res;
983 }
984 /*****************************************************************************
985  * strlwr : Converts a string to lower case
986  *****************************************************************************/
987 #if !defined(strlwr) && !defined( WIN32 )
988 char* strlwr(char *psz_string)
989 {
990     while ( *psz_string )
991     {
992         *psz_string++ = tolower( *psz_string );
993     }
994     return psz_string;
995 }
996 #endif
997
998 /*****************************************************************************
999  * DeleteQueue : Free all songs from an audioscrobbler_queue_t
1000  *****************************************************************************/
1001 void DeleteQueue( audioscrobbler_queue_t *p_queue )
1002 {
1003     int     i;
1004
1005     for( i = 0; i < p_queue->i_songs_nb; i++ )
1006     {
1007         free( p_queue->p_queue[i]->psz_a );
1008         free( p_queue->p_queue[i]->psz_b );
1009         free( p_queue->p_queue[i]->psz_t );
1010         free( p_queue->p_queue[i]->psz_i );
1011         free( p_queue->p_queue[i] );
1012     }
1013 }
1014
1015 /*****************************************************************************
1016  * ReadMetaData : Puts current song's meta data in p_sys->p_current_song
1017  *****************************************************************************/
1018 static int ReadMetaData( intf_thread_t *p_this )
1019 {
1020     playlist_t          *p_playlist;
1021     char                *psz_title = NULL;
1022     char                *psz_artist = NULL;
1023     char                *psz_album = NULL;
1024     int                 i_length = -1;
1025     input_thread_t      *p_input = NULL;
1026     vlc_value_t         video_val;
1027     intf_sys_t          *p_sys;
1028
1029     p_sys = p_this->p_sys;
1030     p_playlist = pl_Yield( p_this );
1031     PL_LOCK;
1032     p_input = p_playlist->p_input;
1033
1034     if( !p_input )
1035     {
1036         return VLC_SUCCESS;
1037     }
1038
1039     vlc_object_yield( p_input );
1040     PL_UNLOCK;
1041     pl_Release( p_playlist );
1042
1043     if ( p_input->input.p_item->i_type == ITEM_TYPE_NET )
1044     {
1045         msg_Dbg( p_this, "We play a stream -> no submission");
1046         goto no_submission;
1047     }
1048
1049     var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
1050     if( video_val.i_int > 0 )
1051     {
1052         msg_Dbg( p_this, "We play a video -> no submission");
1053         goto no_submission;
1054     }
1055
1056     if ( p_input->input.p_item->p_meta->psz_artist )
1057     {
1058         psz_artist = encode_URI_component(
1059             p_input->input.p_item->p_meta->psz_artist );
1060         if ( !psz_artist )
1061         {
1062             goto error;
1063         }
1064     }
1065     else
1066     {
1067         msg_Dbg( p_this, "No artist.." );
1068         goto no_submission;
1069     }
1070
1071     if ( p_input->input.p_item->p_meta->psz_album )
1072     {
1073         psz_album = encode_URI_component(
1074             p_input->input.p_item->p_meta->psz_album );
1075         if ( !psz_album )
1076         {
1077             goto error;
1078         }
1079     }
1080     else
1081     {
1082         msg_Dbg( p_this, "No album.." );
1083         goto no_submission;
1084     }
1085
1086     if ( p_input->input.p_item->psz_name )
1087     {
1088         psz_title = encode_URI_component( p_input->input.p_item->psz_name );
1089         if ( !psz_title )
1090         {
1091             goto error;
1092         }
1093     }
1094     else
1095     {
1096         msg_Dbg( p_this, "No track name.." );
1097         goto no_submission;
1098     }
1099
1100     i_length = p_input->input.p_item->i_duration / 1000000;
1101
1102     vlc_object_release( p_input );
1103
1104     vlc_mutex_lock ( &p_sys->lock );
1105
1106     p_sys->p_current_song->psz_a = strdup( psz_artist );
1107     p_sys->p_current_song->psz_t = strdup( psz_title );
1108     p_sys->p_current_song->psz_b = strdup( psz_album );
1109     p_sys->p_current_song->i_l = i_length;
1110     p_sys->b_queued = VLC_FALSE;
1111     p_sys->b_metadata_read = VLC_TRUE;
1112
1113     vlc_mutex_unlock ( &p_sys->lock );
1114
1115     msg_Dbg( p_this, "Meta data registered, waiting to be queued" );
1116
1117     free( psz_title );
1118     free( psz_artist );
1119     free( psz_album );
1120
1121     return VLC_SUCCESS;
1122
1123 error:
1124     free( psz_artist );
1125     free( psz_album );
1126     free( psz_title );
1127     return VLC_ENOMEM;
1128
1129 no_submission:
1130     vlc_object_release( p_input );
1131
1132     free( psz_title );
1133     free( psz_artist );
1134     free( psz_album );
1135
1136     vlc_mutex_lock( &p_sys->lock );
1137     p_sys->b_queued = VLC_TRUE;
1138     p_sys->b_metadata_read = VLC_TRUE;
1139     vlc_mutex_unlock( &p_sys->lock );
1140
1141     return VLC_SUCCESS;
1142 }