Update translations from 2.2.x branch
[vlc.git] / modules / misc / audioscrobbler.c
blob133829a0e97b73fc72c64758e6f0ddb12d8a0e12
1 /*****************************************************************************
2 * audioscrobbler.c : audioscrobbler submission plugin
3 *****************************************************************************
4 * Copyright © 2006-2011 the VideoLAN team
5 * $Id$
7 * Author: Rafaël Carré <funman at videolanorg>
8 * Ilkka Ollakka <ileoo at videolan org>
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.
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.
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 *****************************************************************************/
25 /* Last.fm Submissions protocol version: 1.2
26 * http://www.last.fm/api/submissions
28 * TODO: Update to new API? http://www.last.fm/api/scrobbling
30 /*****************************************************************************
31 * Preamble
32 *****************************************************************************/
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
38 #include <assert.h>
39 #include <time.h>
40 #ifdef HAVE_POLL
41 # include <poll.h>
42 #endif
44 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_interface.h>
48 #include <vlc_input.h>
49 #include <vlc_dialog.h>
50 #include <vlc_meta.h>
51 #include <vlc_md5.h>
52 #include <vlc_stream.h>
53 #include <vlc_url.h>
54 #include <vlc_network.h>
55 #include <vlc_playlist.h>
57 /*****************************************************************************
58 * Local prototypes
59 *****************************************************************************/
61 #define QUEUE_MAX 50
63 /* Keeps track of metadata to be submitted */
64 typedef struct audioscrobbler_song_t
66 char *psz_a; /**< track artist */
67 char *psz_t; /**< track title */
68 char *psz_b; /**< track album */
69 char *psz_n; /**< track number */
70 int i_l; /**< track length */
71 char *psz_m; /**< musicbrainz id */
72 time_t date; /**< date since epoch */
73 mtime_t i_start; /**< playing start */
74 } audioscrobbler_song_t;
76 struct intf_sys_t
78 audioscrobbler_song_t p_queue[QUEUE_MAX]; /**< songs not submitted yet*/
79 int i_songs; /**< number of songs */
81 input_thread_t *p_input; /**< current input thread */
82 vlc_mutex_t lock; /**< p_sys mutex */
83 vlc_cond_t wait; /**< song to submit event */
84 vlc_thread_t thread; /**< thread to submit song */
86 /* submission of played songs */
87 vlc_url_t p_submit_url; /**< where to submit data */
89 /* submission of playing song */
90 vlc_url_t p_nowp_url; /**< where to submit data */
92 char psz_auth_token[33]; /**< Authentication token */
94 /* data about song currently playing */
95 audioscrobbler_song_t p_current_song; /**< song being played */
97 mtime_t time_pause; /**< time when vlc paused */
98 mtime_t time_total_pauses; /**< total time in pause */
100 bool b_submit_nowp; /**< do we have to submit ? */
102 bool b_meta_read; /**< if we read the song's
103 * metadata already */
106 static int Open (vlc_object_t *);
107 static void Close (vlc_object_t *);
108 static void *Run (void *);
110 /*****************************************************************************
111 * Module descriptor
112 ****************************************************************************/
114 #define USERNAME_TEXT N_("Username")
115 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
116 #define PASSWORD_TEXT N_("Password")
117 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
118 #define URL_TEXT N_("Scrobbler URL")
119 #define URL_LONGTEXT N_("The URL set for an alternative scrobbler engine")
121 /* This error value is used when last.fm plugin has to be unloaded. */
122 #define VLC_AUDIOSCROBBLER_EFATAL -69
124 /* last.fm client identifier */
125 #define CLIENT_NAME PACKAGE
126 #define CLIENT_VERSION VERSION
128 vlc_module_begin ()
129 set_category(CAT_INTERFACE)
130 set_subcategory(SUBCAT_INTERFACE_CONTROL)
131 set_shortname(N_("Audioscrobbler"))
132 set_description(N_("Submission of played songs to last.fm"))
133 add_string("lastfm-username", "",
134 USERNAME_TEXT, USERNAME_LONGTEXT, false)
135 add_password("lastfm-password", "",
136 PASSWORD_TEXT, PASSWORD_LONGTEXT, false)
137 add_string("scrobbler-url", "post.audioscrobbler.com",
138 URL_TEXT, URL_LONGTEXT, false)
139 set_capability("interface", 0)
140 set_callbacks(Open, Close)
141 vlc_module_end ()
143 /*****************************************************************************
144 * DeleteSong : Delete the char pointers in a song
145 *****************************************************************************/
146 static void DeleteSong(audioscrobbler_song_t* p_song)
148 FREENULL(p_song->psz_a);
149 FREENULL(p_song->psz_b);
150 FREENULL(p_song->psz_t);
151 FREENULL(p_song->psz_m);
152 FREENULL(p_song->psz_n);
155 /*****************************************************************************
156 * ReadMetaData : Read meta data when parsed by vlc
157 *****************************************************************************/
158 static void ReadMetaData(intf_thread_t *p_this, input_thread_t *p_input)
160 intf_sys_t *p_sys = p_this->p_sys;
162 assert(p_input != NULL);
164 input_item_t *p_item = input_GetItem(p_input);
165 if (p_item == NULL)
166 return;
168 #define ALLOC_ITEM_META(a, b) do { \
169 char *psz_meta = input_item_Get##b(p_item); \
170 if (psz_meta && *psz_meta) \
171 a = vlc_uri_encode(psz_meta); \
172 free(psz_meta); \
173 } while (0)
175 vlc_mutex_lock(&p_sys->lock);
177 p_sys->b_meta_read = true;
179 ALLOC_ITEM_META(p_sys->p_current_song.psz_a, Artist);
180 if (!p_sys->p_current_song.psz_a)
182 msg_Dbg(p_this, "No artist..");
183 DeleteSong(&p_sys->p_current_song);
184 goto end;
187 ALLOC_ITEM_META(p_sys->p_current_song.psz_t, Title);
188 if (!p_sys->p_current_song.psz_t)
190 msg_Dbg(p_this, "No track name..");
191 DeleteSong(&p_sys->p_current_song);
192 goto end;
195 /* Now we have read the mandatory meta data, so we can submit that info */
196 p_sys->b_submit_nowp = true;
198 ALLOC_ITEM_META(p_sys->p_current_song.psz_b, Album);
199 ALLOC_ITEM_META(p_sys->p_current_song.psz_m, TrackID);
200 ALLOC_ITEM_META(p_sys->p_current_song.psz_n, TrackNum);
202 p_sys->p_current_song.i_l = input_item_GetDuration(p_item) / 1000000;
204 #undef ALLOC_ITEM_META
206 msg_Dbg(p_this, "Meta data registered");
208 vlc_cond_signal(&p_sys->wait);
210 end:
211 vlc_mutex_unlock(&p_sys->lock);
214 /*****************************************************************************
215 * AddToQueue: Add the played song to the queue to be submitted
216 *****************************************************************************/
217 static void AddToQueue (intf_thread_t *p_this)
219 mtime_t played_time;
220 intf_sys_t *p_sys = p_this->p_sys;
222 vlc_mutex_lock(&p_sys->lock);
224 /* Check that we have the mandatory meta data */
225 if (!p_sys->p_current_song.psz_t || !p_sys->p_current_song.psz_a)
226 goto end;
228 /* wait for the user to listen enough before submitting */
229 played_time = mdate() - p_sys->p_current_song.i_start -
230 p_sys->time_total_pauses;
231 played_time /= 1000000; /* µs → s */
233 /*HACK: it seam that the preparsing sometime fail,
234 so use the playing time as the song length */
235 if (p_sys->p_current_song.i_l == 0)
236 p_sys->p_current_song.i_l = played_time;
238 /* Don't send song shorter than 30s */
239 if (p_sys->p_current_song.i_l < 30)
241 msg_Dbg(p_this, "Song too short (< 30s), not submitting");
242 goto end;
245 /* Send if the user had listen more than 240s OR half the track length */
246 if ((played_time < 240) &&
247 (played_time < (p_sys->p_current_song.i_l / 2)))
249 msg_Dbg(p_this, "Song not listened long enough, not submitting");
250 goto end;
253 /* Check that all meta are present */
254 if (!p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
255 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t)
257 msg_Dbg(p_this, "Missing artist or title, not submitting");
258 goto end;
261 if (p_sys->i_songs >= QUEUE_MAX)
263 msg_Warn(p_this, "Submission queue is full, not submitting");
264 goto end;
267 msg_Dbg(p_this, "Song will be submitted.");
269 #define QUEUE_COPY(a) \
270 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
272 #define QUEUE_COPY_NULL(a) \
273 QUEUE_COPY(a); \
274 p_sys->p_current_song.a = NULL
276 QUEUE_COPY(i_l);
277 QUEUE_COPY_NULL(psz_n);
278 QUEUE_COPY_NULL(psz_a);
279 QUEUE_COPY_NULL(psz_t);
280 QUEUE_COPY_NULL(psz_b);
281 QUEUE_COPY_NULL(psz_m);
282 QUEUE_COPY(date);
283 #undef QUEUE_COPY_NULL
284 #undef QUEUE_COPY
286 p_sys->i_songs++;
288 /* signal the main loop we have something to submit */
289 vlc_cond_signal(&p_sys->wait);
291 end:
292 DeleteSong(&p_sys->p_current_song);
293 vlc_mutex_unlock(&p_sys->lock);
296 /*****************************************************************************
297 * PlayingChange: Playing status change callback
298 *****************************************************************************/
299 static int PlayingChange(vlc_object_t *p_this, const char *psz_var,
300 vlc_value_t oldval, vlc_value_t newval, void *p_data)
302 VLC_UNUSED(oldval);
304 intf_thread_t *p_intf = (intf_thread_t*) p_data;
305 intf_sys_t *p_sys = p_intf->p_sys;
306 input_thread_t *p_input = (input_thread_t*)p_this;
307 int state;
309 VLC_UNUSED(psz_var);
311 if (newval.i_int != INPUT_EVENT_STATE) return VLC_SUCCESS;
313 if (var_CountChoices(p_input, "video-es"))
315 msg_Dbg(p_this, "Not an audio-only input, not submitting");
316 return VLC_SUCCESS;
319 state = var_GetInteger(p_input, "state");
321 if (!p_sys->b_meta_read && state >= PLAYING_S)
323 ReadMetaData(p_intf, p_input);
324 return VLC_SUCCESS;
328 if (state >= END_S)
329 AddToQueue(p_intf);
330 else if (state == PAUSE_S)
331 p_sys->time_pause = mdate();
332 else if (p_sys->time_pause > 0 && state == PLAYING_S)
334 p_sys->time_total_pauses += (mdate() - p_sys->time_pause);
335 p_sys->time_pause = 0;
338 return VLC_SUCCESS;
341 /*****************************************************************************
342 * ItemChange: Playlist item change callback
343 *****************************************************************************/
344 static int ItemChange(vlc_object_t *p_this, const char *psz_var,
345 vlc_value_t oldval, vlc_value_t newval, void *p_data)
347 intf_thread_t *p_intf = p_data;
348 intf_sys_t *p_sys = p_intf->p_sys;
349 input_thread_t *p_input = newval.p_address;
351 VLC_UNUSED(psz_var);
352 VLC_UNUSED(oldval);
354 p_sys->b_meta_read = false;
356 if (p_sys->p_input != NULL)
358 var_DelCallback(p_sys->p_input, "intf-event", PlayingChange, p_intf);
359 vlc_object_release(p_sys->p_input);
360 p_sys->p_input = NULL;
363 if (p_input == NULL)
364 return VLC_SUCCESS;
366 input_item_t *p_item = input_GetItem(p_input);
367 if (p_item == NULL)
368 return VLC_SUCCESS;
370 if (var_CountChoices(p_input, "video-es"))
372 msg_Dbg(p_this, "Not an audio-only input, not submitting");
373 return VLC_SUCCESS;
376 p_sys->time_total_pauses = 0;
377 time(&p_sys->p_current_song.date); /* to be sent to last.fm */
378 p_sys->p_current_song.i_start = mdate(); /* only used locally */
380 p_sys->p_input = vlc_object_hold(p_input);
381 var_AddCallback(p_input, "intf-event", PlayingChange, p_intf);
383 if (input_item_IsPreparsed(p_item))
384 ReadMetaData(p_intf, p_input);
385 /* if the input item was not preparsed, we'll do it in PlayingChange()
386 * callback, when "state" == PLAYING_S */
388 return VLC_SUCCESS;
391 /*****************************************************************************
392 * Open: initialize and create stuff
393 *****************************************************************************/
394 static int Open(vlc_object_t *p_this)
396 intf_thread_t *p_intf = (intf_thread_t*) p_this;
397 intf_sys_t *p_sys = calloc(1, sizeof(intf_sys_t));
399 if (!p_sys)
400 return VLC_ENOMEM;
402 p_intf->p_sys = p_sys;
404 vlc_mutex_init(&p_sys->lock);
405 vlc_cond_init(&p_sys->wait);
407 if (vlc_clone(&p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW))
409 vlc_cond_destroy(&p_sys->wait);
410 vlc_mutex_destroy(&p_sys->lock);
411 free(p_sys);
412 return VLC_ENOMEM;
415 var_AddCallback(pl_Get(p_intf), "input-current", ItemChange, p_intf);
417 return VLC_SUCCESS;
420 /*****************************************************************************
421 * Close: destroy interface stuff
422 *****************************************************************************/
423 static void Close(vlc_object_t *p_this)
425 intf_thread_t *p_intf = (intf_thread_t*) p_this;
426 intf_sys_t *p_sys = p_intf->p_sys;
428 vlc_cancel(p_sys->thread);
429 vlc_join(p_sys->thread, NULL);
431 var_DelCallback(pl_Get(p_intf), "input-current", ItemChange, p_intf);
433 if (p_sys->p_input != NULL)
435 var_DelCallback(p_sys->p_input, "intf-event", PlayingChange, p_intf);
436 vlc_object_release(p_sys->p_input);
439 int i;
440 for (i = 0; i < p_sys->i_songs; i++)
441 DeleteSong(&p_sys->p_queue[i]);
442 vlc_UrlClean(&p_sys->p_submit_url);
443 vlc_UrlClean(&p_sys->p_nowp_url);
444 vlc_cond_destroy(&p_sys->wait);
445 vlc_mutex_destroy(&p_sys->lock);
446 free(p_sys);
449 /*****************************************************************************
450 * Handshake : Init audioscrobbler connection
451 *****************************************************************************/
452 static int Handshake(intf_thread_t *p_this)
454 char *psz_username, *psz_password;
455 char *psz_scrobbler_url;
456 time_t timestamp;
457 char psz_timestamp[21];
459 struct md5_s p_struct_md5;
461 stream_t *p_stream;
462 char *psz_handshake_url;
463 uint8_t p_buffer[1024];
464 char *p_buffer_pos;
466 int i_ret;
467 char *psz_url;
469 intf_thread_t *p_intf = (intf_thread_t*) p_this;
470 intf_sys_t *p_sys = p_this->p_sys;
472 psz_username = var_InheritString(p_this, "lastfm-username");
473 psz_password = var_InheritString(p_this, "lastfm-password");
475 /* username or password have not been setup */
476 if (EMPTY_STR(psz_username) || EMPTY_STR(psz_password))
478 free(psz_username);
479 free(psz_password);
480 return VLC_ENOVAR;
483 time(&timestamp);
485 /* generates a md5 hash of the password */
486 InitMD5(&p_struct_md5);
487 AddMD5(&p_struct_md5, (uint8_t*) psz_password, strlen(psz_password));
488 EndMD5(&p_struct_md5);
490 free(psz_password);
492 char *psz_password_md5 = psz_md5_hash(&p_struct_md5);
493 if (!psz_password_md5)
495 free(psz_username);
496 return VLC_ENOMEM;
499 snprintf(psz_timestamp, sizeof(psz_timestamp), "%"PRIu64,
500 (uint64_t)timestamp);
502 /* generates a md5 hash of :
503 * - md5 hash of the password, plus
504 * - timestamp in clear text
506 InitMD5(&p_struct_md5);
507 AddMD5(&p_struct_md5, (uint8_t*) psz_password_md5, 32);
508 AddMD5(&p_struct_md5, (uint8_t*) psz_timestamp, strlen(psz_timestamp));
509 EndMD5(&p_struct_md5);
510 free(psz_password_md5);
512 char *psz_auth_token = psz_md5_hash(&p_struct_md5);
513 if (!psz_auth_token)
515 free(psz_username);
516 return VLC_ENOMEM;
519 psz_scrobbler_url = var_InheritString(p_this, "scrobbler-url");
520 if (!psz_scrobbler_url)
522 free(psz_auth_token);
523 free(psz_username);
524 return VLC_ENOMEM;
527 i_ret = asprintf(&psz_handshake_url,
528 "http://%s/?hs=true&p=1.2&c="CLIENT_NAME"&v="CLIENT_VERSION"&u=%s&t=%s&a=%s"
529 , psz_scrobbler_url, psz_username, psz_timestamp, psz_auth_token);
531 free(psz_auth_token);
532 free(psz_scrobbler_url);
533 free(psz_username);
534 if (i_ret == -1)
535 return VLC_ENOMEM;
537 /* send the http handshake request */
538 p_stream = vlc_stream_NewURL(p_intf, psz_handshake_url);
539 free(psz_handshake_url);
541 if (!p_stream)
542 return VLC_EGENERIC;
544 /* read answer */
545 i_ret = vlc_stream_Read(p_stream, p_buffer, sizeof(p_buffer) - 1);
546 if (i_ret <= 0)
548 vlc_stream_Delete(p_stream);
549 return VLC_EGENERIC;
551 p_buffer[i_ret] = '\0';
552 vlc_stream_Delete(p_stream);
554 p_buffer_pos = strstr((char*) p_buffer, "FAILED ");
555 if (p_buffer_pos)
557 /* handshake request failed, sorry */
558 msg_Err(p_this, "last.fm handshake failed: %s", p_buffer_pos + 7);
559 return VLC_EGENERIC;
562 if (strstr((char*) p_buffer, "BADAUTH"))
564 /* authentication failed, bad username/password combination */
565 vlc_dialog_display_error(p_this,
566 _("last.fm: Authentication failed"),
567 "%s", _("last.fm username or password is incorrect. "
568 "Please verify your settings and relaunch VLC."));
569 return VLC_AUDIOSCROBBLER_EFATAL;
572 if (strstr((char*) p_buffer, "BANNED"))
574 /* oops, our version of vlc has been banned by last.fm servers */
575 msg_Err(p_intf, "This version of VLC has been banned by last.fm. "
576 "You should upgrade VLC, or disable the last.fm plugin.");
577 return VLC_AUDIOSCROBBLER_EFATAL;
580 if (strstr((char*) p_buffer, "BADTIME"))
582 /* The system clock isn't good */
583 msg_Err(p_intf, "last.fm handshake failed because your clock is too "
584 "much shifted. Please correct it, and relaunch VLC.");
585 return VLC_AUDIOSCROBBLER_EFATAL;
588 p_buffer_pos = strstr((char*) p_buffer, "OK");
589 if (!p_buffer_pos)
590 goto proto;
592 p_buffer_pos = strstr(p_buffer_pos, "\n");
593 if (!p_buffer_pos || strlen(p_buffer_pos) < 33)
594 goto proto;
595 p_buffer_pos++; /* we skip the '\n' */
597 /* save the session ID */
598 memcpy(p_sys->psz_auth_token, p_buffer_pos, 32);
599 p_sys->psz_auth_token[32] = '\0';
601 p_buffer_pos = strstr(p_buffer_pos, "http://");
602 if (!p_buffer_pos || strlen(p_buffer_pos) == 7)
603 goto proto;
605 /* We need to read the nowplaying url */
606 p_buffer_pos += 7; /* we skip "http://" */
607 psz_url = strndup(p_buffer_pos, strcspn(p_buffer_pos, "\n"));
608 if (!psz_url)
609 goto oom;
611 vlc_UrlParse(&p_sys->p_nowp_url, psz_url);
612 free(psz_url);
613 if (p_sys->p_nowp_url.psz_host == NULL ||
614 p_sys->p_nowp_url.i_port == 0)
616 vlc_UrlClean(&p_sys->p_nowp_url);
617 goto proto;
620 p_buffer_pos = strstr(p_buffer_pos, "http://");
621 if (!p_buffer_pos || strlen(p_buffer_pos) == 7)
622 goto proto;
624 /* We need to read the submission url */
625 p_buffer_pos += 7; /* we skip "http://" */
626 psz_url = strndup(p_buffer_pos, strcspn(p_buffer_pos, "\n"));
627 if (!psz_url)
628 goto oom;
630 /* parse the submission url */
631 vlc_UrlParse(&p_sys->p_submit_url, psz_url);
632 free(psz_url);
633 if (p_sys->p_submit_url.psz_host == NULL ||
634 p_sys->p_submit_url.i_port == 0)
636 vlc_UrlClean(&p_sys->p_nowp_url);
637 vlc_UrlClean(&p_sys->p_submit_url);
638 goto proto;
641 return VLC_SUCCESS;
643 oom:
644 return VLC_ENOMEM;
646 proto:
647 msg_Err(p_intf, "Handshake: can't recognize server protocol");
648 return VLC_EGENERIC;
651 static void HandleInterval(mtime_t *next, unsigned int *i_interval)
653 if (*i_interval == 0)
655 /* first interval is 1 minute */
656 *i_interval = 1;
658 else
660 /* else we double the previous interval, up to 120 minutes */
661 *i_interval <<= 1;
662 if (*i_interval > 120)
663 *i_interval = 120;
665 *next = mdate() + (*i_interval * 1000000 * 60);
668 /*****************************************************************************
669 * Run : call Handshake() then submit songs
670 *****************************************************************************/
671 static void *Run(void *data)
673 intf_thread_t *p_intf = data;
674 uint8_t p_buffer[1024];
675 int canc = vlc_savecancel();
676 bool b_handshaked = false;
677 bool b_nowp_submission_ongoing = false;
679 /* data about audioscrobbler session */
680 mtime_t next_exchange = 0; /**< when can we send data */
681 unsigned int i_interval = 0; /**< waiting interval (secs)*/
683 intf_sys_t *p_sys = p_intf->p_sys;
685 /* main loop */
686 for (;;)
688 vlc_restorecancel(canc);
689 mwait(next_exchange);
691 vlc_mutex_lock(&p_sys->lock);
692 mutex_cleanup_push(&p_sys->lock);
694 while (p_sys->i_songs == 0 && p_sys->b_submit_nowp == false)
695 vlc_cond_wait(&p_sys->wait, &p_sys->lock);
697 vlc_cleanup_pop();
698 vlc_mutex_unlock(&p_sys->lock);
699 canc = vlc_savecancel();
701 /* handshake if needed */
702 if (!b_handshaked)
704 msg_Dbg(p_intf, "Handshaking with last.fm ...");
706 switch(Handshake(p_intf))
708 case VLC_ENOMEM:
709 goto out;
711 case VLC_ENOVAR:
712 /* username not set */
713 vlc_dialog_display_error(p_intf,
714 _("Last.fm username not set"),
715 "%s", _("Please set a username or disable the "
716 "audioscrobbler plugin, and restart VLC.\n"
717 "Visit http://www.last.fm/join/ to get an account."));
718 goto out;
720 case VLC_SUCCESS:
721 msg_Dbg(p_intf, "Handshake successful :)");
722 b_handshaked = true;
723 i_interval = 0;
724 next_exchange = 0;
725 break;
727 case VLC_AUDIOSCROBBLER_EFATAL:
728 msg_Warn(p_intf, "Exiting...");
729 goto out;
731 case VLC_EGENERIC:
732 default:
733 /* protocol error : we'll try later */
734 HandleInterval(&next_exchange, &i_interval);
735 break;
737 /* if handshake failed let's restart the loop */
738 if (!b_handshaked)
739 continue;
742 msg_Dbg(p_intf, "Going to submit some data...");
743 char *psz_submit;
744 vlc_url_t *url;
745 char *psz_submit_song, *psz_submit_tmp;
747 if (asprintf(&psz_submit, "s=%s", p_sys->psz_auth_token) == -1)
748 break;
750 /* forge the HTTP POST request */
751 vlc_mutex_lock(&p_sys->lock);
753 if (p_sys->b_submit_nowp)
755 b_nowp_submission_ongoing = true;
756 url = &p_sys->p_nowp_url;
757 if (asprintf(&psz_submit_song,
758 "&a=%s"
759 "&t=%s"
760 "&b=%s"
761 "&l=%d"
762 "&n=%s"
763 "&m=%s",
764 p_sys->p_current_song.psz_a,
765 p_sys->p_current_song.psz_t,
766 p_sys->p_current_song.psz_b ? p_sys->p_current_song.psz_b : "",
767 p_sys->p_current_song.i_l,
768 p_sys->p_current_song.psz_n ? p_sys->p_current_song.psz_n : "",
769 p_sys->p_current_song.psz_m ? p_sys->p_current_song.psz_m : ""
770 ) == -1)
771 { /* Out of memory */
772 vlc_mutex_unlock(&p_sys->lock);
773 goto out;
777 else
779 url = &p_sys->p_submit_url;
780 audioscrobbler_song_t *p_song;
781 for (int i_song = 0 ; i_song < p_sys->i_songs ; i_song++)
783 p_song = &p_sys->p_queue[i_song];
784 if (asprintf(&psz_submit_song,
785 "&a%%5B%d%%5D=%s"
786 "&t%%5B%d%%5D=%s"
787 "&i%%5B%d%%5D=%u"
788 "&o%%5B%d%%5D=P"
789 "&r%%5B%d%%5D="
790 "&l%%5B%d%%5D=%d"
791 "&b%%5B%d%%5D=%s"
792 "&n%%5B%d%%5D=%s"
793 "&m%%5B%d%%5D=%s",
794 i_song, p_song->psz_a,
795 i_song, p_song->psz_t,
796 i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */
797 i_song,
798 i_song,
799 i_song, p_song->i_l,
800 i_song, p_song->psz_b ? p_song->psz_b : "",
801 i_song, p_song->psz_n ? p_song->psz_n : "",
802 i_song, p_song->psz_m ? p_song->psz_m : ""
803 ) == -1)
804 { /* Out of memory */
805 vlc_mutex_unlock(&p_sys->lock);
806 goto out;
811 psz_submit_tmp = psz_submit;
812 int print_ret = asprintf(&psz_submit, "%s%s",
813 psz_submit_tmp, psz_submit_song);
814 free(psz_submit_tmp);
815 free(psz_submit_song);
816 vlc_mutex_unlock(&p_sys->lock);
818 if (print_ret == -1)
819 { /* Out of memory */
820 goto out;
823 int i_post_socket = net_ConnectTCP(p_intf, url->psz_host,
824 url->i_port);
826 if (i_post_socket == -1)
828 /* If connection fails, we assume we must handshake again */
829 HandleInterval(&next_exchange, &i_interval);
830 b_handshaked = false;
831 free(psz_submit);
832 continue;
835 /* we transmit the data */
836 int i_net_ret = net_Printf(p_intf, i_post_socket,
837 "POST %s HTTP/1.1\r\n"
838 "Host: %s\r\n"
839 "User-Agent: "PACKAGE_NAME"/"PACKAGE_VERSION"\r\n"
840 "Connection: close\r\n"
841 "Accept-Encoding: identity\r\n"
842 "Content-Type: application/x-www-form-urlencoded\r\n"
843 "Content-Length: %zu\r\n"
844 "\r\n"
845 "%s\r\n"
846 "\r\n",
847 url->psz_path, url->psz_host, strlen(psz_submit), psz_submit);
849 free(psz_submit);
850 if (i_net_ret == -1)
852 /* If connection fails, we assume we must handshake again */
853 HandleInterval(&next_exchange, &i_interval);
854 b_handshaked = false;
855 net_Close(i_post_socket);
856 continue;
859 /* FIXME: this might wait forever */
860 struct pollfd ufd = { .fd = i_post_socket, .events = POLLIN };
861 while( poll( &ufd, 1, -1 ) == -1 );
863 /* FIXME: With TCP, you should never assume that a single read will
864 * return the entire response... */
865 i_net_ret = recv(i_post_socket, p_buffer, sizeof(p_buffer) - 1, 0);
866 if (i_net_ret <= 0)
868 /* if we get no answer, something went wrong : try again */
869 net_Close(i_post_socket);
870 continue;
873 net_Close(i_post_socket);
874 p_buffer[i_net_ret] = '\0';
876 char *failed = strstr((char *) p_buffer, "FAILED");
877 if (failed)
879 msg_Warn(p_intf, "%s", failed);
880 HandleInterval(&next_exchange, &i_interval);
881 continue;
884 if (strstr((char *) p_buffer, "BADSESSION"))
886 msg_Err(p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?");
887 b_handshaked = false;
888 HandleInterval(&next_exchange, &i_interval);
889 continue;
892 if (strstr((char *) p_buffer, "OK"))
894 if (b_nowp_submission_ongoing)
896 b_nowp_submission_ongoing = false;
897 p_sys->b_submit_nowp = false;
899 else
901 for (int i = 0; i < p_sys->i_songs; i++)
902 DeleteSong(&p_sys->p_queue[i]);
903 p_sys->i_songs = 0;
906 i_interval = 0;
907 next_exchange = 0;
908 msg_Dbg(p_intf, "Submission successful!");
910 else
912 msg_Err(p_intf, "Authentication failed, handshaking again (%s)",
913 p_buffer);
914 b_handshaked = false;
915 HandleInterval(&next_exchange, &i_interval);
918 out:
919 vlc_restorecancel(canc);
920 return NULL;