diff options
author | Lars-Dominik Braun <PromyLOPh@gmail.com> | 2008-06-22 11:02:04 +0200 |
---|---|---|
committer | Lars-Dominik Braun <PromyLOPh@gmail.com> | 2008-06-22 11:02:04 +0200 |
commit | ecc7f3375b887729e45e1e838dbffde44d954b43 (patch) | |
tree | c9dda668809837311f1b8e30f90737750f74b121 /libwardrobe/src/main.c | |
parent | dc6e3c0b2686c8dbad631b4211fd0db141e5cace (diff) | |
download | pianobar-ecc7f3375b887729e45e1e838dbffde44d954b43.tar.gz pianobar-ecc7f3375b887729e45e1e838dbffde44d954b43.tar.bz2 pianobar-ecc7f3375b887729e45e1e838dbffde44d954b43.zip |
wardrobe: libwardrobe is a last.fm scrobbling library
Diffstat (limited to 'libwardrobe/src/main.c')
-rw-r--r-- | libwardrobe/src/main.c | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/libwardrobe/src/main.c b/libwardrobe/src/main.c new file mode 100644 index 0000000..f188d46 --- /dev/null +++ b/libwardrobe/src/main.c @@ -0,0 +1,310 @@ +/* +Copyright (c) 2008 Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include <time.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <curl/curl.h> + +#include "md5.h" + +typedef struct { + char *user; + char *password; + char authToken[100]; + char postUrl[1024]; + CURL *ch; +} WardrobeHandle_t; + +typedef struct { + char *artist; + char *title; + time_t started; + time_t length; +} WardrobeSong_t; + +typedef enum {WARDROBE_RET_ERR, WARDROBE_RET_OK, + WARDROBE_RET_CLIENT_BANNED, WARDROBE_RET_BADAUTH, + WARDROBE_RET_BADTIME, WARDROBE_RET_BADSESSION} WardrobeReturn_t; + +#define WARDROBE_HTTP_BUFFER_SIZE 10000 + +void WardrobeSongInit (WardrobeSong_t *ws) { + memset (ws, 0, sizeof (*ws)); +} + +/* callback for curl, writes data to buffer + * @param received data + * @param block size + * @param blocks received + * @param write data into this buffer + * @return written bytes + */ +size_t WardrobeCurlRetToVar (void *ptr, size_t size, size_t nmemb, + void *stream) { + char *charPtr = ptr; + char *streamPtr = stream; + + if (strlen (streamPtr) + nmemb > WARDROBE_HTTP_BUFFER_SIZE) { + printf ("buffer overflow...\n"); + return 0; + } else { + memcpy (streamPtr+strlen(streamPtr), charPtr, size*nmemb); + return size*nmemb; + } +} + +/* receive data from url using GET method + * @param initialized curl handle + * @param call this url + * @param put received data here, memory is allocated by this function + * @return nothing yet + */ +void WardrobeHttpGet (CURL *ch, char *url, char **retData) { + /* Let's hope nothing will be bigger than this... */ + char curlRet[WARDROBE_HTTP_BUFFER_SIZE]; + + memset (curlRet, 0, sizeof (curlRet)); + + curl_easy_setopt (ch, CURLOPT_URL, url); + curl_easy_setopt (ch, CURLOPT_WRITEFUNCTION, WardrobeCurlRetToVar); + curl_easy_setopt (ch, CURLOPT_WRITEDATA, curlRet); + + curl_easy_perform (ch); + + *retData = strdup (curlRet); +} + +/* post data to url and receive answer as string + * @param initialized curl handle + * @param call this url + * @param post this data + * @param put received data here, memory is allocated by this function + * @return nothing yet + */ +void WardrobeHttpPost (CURL *ch, char *url, char *postData, char **retData) { + /* Let's hope nothing will be bigger than this... */ + char curlRet[WARDROBE_HTTP_BUFFER_SIZE]; + + memset (curlRet, 0, sizeof (curlRet)); + curl_easy_setopt (ch, CURLOPT_URL, url); + curl_easy_setopt (ch, CURLOPT_POSTFIELDS, postData); + curl_easy_setopt (ch, CURLOPT_WRITEFUNCTION, WardrobeCurlRetToVar); + curl_easy_setopt (ch, CURLOPT_WRITEDATA, curlRet); + + curl_easy_perform (ch); + + *retData = strdup (curlRet); +} + +/* initialize wardrobe handle (setup curl, e.g.) + * @param wardrobe handle + */ +void WardrobeInit (WardrobeHandle_t *wh) { + memset (wh, 0, sizeof (*wh)); + wh->ch = curl_easy_init (); +} + +/* free () replacement that does some checks and zeros memory + * @param pointer + * @param size or 0 to disable zeroing + */ +void WardrobeFree (void *ptr, size_t size) { + if (ptr != NULL) { + if (size > 0) { + memset (ptr, 0, size); + } + free (ptr); + } +} + +/* cleanup song + * @param song + */ +void WardrobeSongDestroy (WardrobeSong_t *ws) { + WardrobeFree (ws->artist, 0); + WardrobeFree (ws->title, 0); + memset (ws, 0, sizeof (*ws)); +} + +/* cleanup wardrobe handle + * @param initialized wardrobe handle + */ +void WardrobeDestroy (WardrobeHandle_t *wh) { + WardrobeFree (wh->user, 0); + WardrobeFree (wh->password, 0); + curl_easy_cleanup (wh->ch); + memset (wh, 0, sizeof (*wh)); +} + +/* get session id from last.fm; you don't have to call this manually + * @param wardrobe handle + * @return _OK or error + */ +WardrobeReturn_t WardrobeHandshake (WardrobeHandle_t *wh) { + /* we'll use gmt */ + time_t currTime = time (NULL); + time_t currGmTime = mktime (gmtime (&currTime)); + char url[1024], tmp[100], *tmpDigest, *pwDigest, *ret; + WardrobeReturn_t fRet = WARDROBE_RET_ERR; + + tmpDigest = WardrobeMd5Calc (wh->password); + snprintf (tmp, sizeof (tmp), "%s%li", tmpDigest, currGmTime); + pwDigest = WardrobeMd5Calc (tmp); + snprintf (url, sizeof (url), "http://post.audioscrobbler.com/" + "?hs=true&p=1.2&c=tst&v=1.0&u=%s&t=%li&a=%s", wh->user, + currGmTime, pwDigest); + + WardrobeHttpGet (wh->ch, url, &ret); + + /* parse answer */ + if (memcmp (ret, "OK", 2) == 0) { + char *newlines[5]; + size_t i; + newlines[0] = ret; + /* split string */ + for (i = 1; i < sizeof (newlines) / sizeof (*newlines); i++) { + newlines[i] = strchr (newlines[i-1]+1, '\n'); + } + /* copy needed values */ + if (newlines[2] - newlines[1]-1 < sizeof (wh->authToken)) { + memcpy (wh->authToken, newlines[1]+1, newlines[2] - + newlines[1]-1); + } else { + printf ("buffer overflow!\n"); + } + if (newlines[4] - newlines[3]-1 < sizeof (wh->postUrl)) { + memcpy (wh->postUrl, newlines[3]+1, newlines[4] - + newlines[3]-1); + } else { + printf ("buffer overflow!\n"); + } + fRet = WARDROBE_RET_OK; + } else if (memcmp (ret, "BADAUTH", 7) == 0) { + fRet = WARDROBE_RET_BADAUTH; + } else if (memcmp (ret, "BADTIME", 7) == 0) { + fRet = WARDROBE_RET_BADTIME; + } else if (memcmp (ret, "BANNED", 6) == 0) { + fRet = WARDROBE_RET_CLIENT_BANNED; + } + + WardrobeFree (tmpDigest, WARDROBE_MD5_DIGEST_LEN); + WardrobeFree (pwDigest, WARDROBE_MD5_DIGEST_LEN); + WardrobeFree (ret, 0); + + return fRet; +} + +/* _really_ submit song to last.fm + * @param wardrobe handle + * @param song + * @return _OK or error + */ +WardrobeReturn_t WardrobeSendSong (WardrobeHandle_t *wh, + WardrobeSong_t *ws) { + char postContent[10000]; + char *urlencArtist, *urlencTitle, *ret; + WardrobeReturn_t fRet = WARDROBE_RET_ERR; + + urlencArtist = curl_easy_escape (wh->ch, ws->artist, 0); + urlencTitle = curl_easy_escape (wh->ch, ws->title, 0); + + snprintf (postContent, sizeof (postContent), "s=%s&a[0]=%s&t[0]=%s&" + "i[0]=%li&o[0]=P&r[0]=&l[0]=%li&b[0]=&n[0]=&m[0]=", + wh->authToken, urlencArtist, urlencTitle, ws->started, + ws->length); + + WardrobeHttpPost (wh->ch, wh->postUrl, postContent, &ret); + if (memcmp (ret, "OK", 2) == 0) { + fRet = WARDROBE_RET_OK; + } else if (memcmp (ret, "BADSESSION", 10) == 0) { + fRet = WARDROBE_RET_BADSESSION; + } + + curl_free (urlencArtist); + curl_free (urlencTitle); + WardrobeFree (ret, 0); + + return fRet; +} + +/* submit played track to last.fm + * @public yes + * @param wardrobe handle + * @param song data + * @return _OK or error + */ +WardrobeReturn_t WardrobeSubmit (WardrobeHandle_t *wh, + WardrobeSong_t *ws) { + size_t i; + WardrobeReturn_t fRet = WARDROBE_RET_ERR; + + if (strlen (wh->authToken) <= 0 && (fRet = WardrobeHandshake (wh)) != + WARDROBE_RET_OK) { + return fRet; + } + for (i = 0; i < 2; i++) { + fRet = WardrobeSendSong (wh, ws); + if (fRet == WARDROBE_RET_OK) { + return WARDROBE_RET_OK; + } else if (fRet == WARDROBE_RET_BADSESSION && + (fRet = WardrobeHandshake (wh)) != WARDROBE_RET_OK) { + return fRet; + } + } + return fRet; +} + +/* return dynamic allocated error string, don't forget to free it + * @param error int + * @return human readable error string or NULL on error + */ +char *WardrobeErrorToString (WardrobeReturn_t ret) { + switch (ret) { + case WARDROBE_RET_ERR: + return strdup ("Unknown error.\n"); + break; + + case WARDROBE_RET_OK: + return strdup ("Everything's fine :)\n"); + break; + + case WARDROBE_RET_CLIENT_BANNED: + return strdup ("Client banned. Try to update your software.\n"); + break; + + case WARDROBE_RET_BADAUTH: + return strdup ("Wrong username or password.\n"); + break; + + case WARDROBE_RET_BADTIME: + return strdup ("System time wrong. Check your system time and " + "correct it.\n"); + break; + + case WARDROBE_RET_BADSESSION: + return strdup ("Bad session. Try to login again.\n"); + break; + } + return NULL; +} |