static int psync_p2p_get_download_token(psync_fileid_t fileid, const unsigned char *filehashhex, uint64_t fsize, unsigned char **token, size_t *tlen){ binparam params[]={P_STR("auth", psync_my_auth), P_NUM("fileid", fileid), P_NUM("filesize", fsize), P_LSTR(PSYNC_CHECKSUM, filehashhex, PSYNC_HASH_DIGEST_HEXLEN), P_LSTR("keydata", psync_rsa_public_bin->data, psync_rsa_public_bin->datalen)}; psync_socket *api; binresult *res; const binresult *ctoken; *token=NULL; /* especially for gcc */ *tlen=0; api=psync_apipool_get(); if (unlikely_log(!api)) return PSYNC_NET_TEMPFAIL; res=send_command(api, "getfileownershiptoken", params); if (unlikely_log(!res)){ psync_apipool_release_bad(api); return PSYNC_NET_TEMPFAIL; } psync_apipool_release(api); if (unlikely_log(psync_find_result(res, "result", PARAM_NUM)->num!=0)){ psync_free(res); return PSYNC_NET_PERMFAIL; } ctoken=psync_find_result(res, "token", PARAM_STR); *token=psync_malloc(ctoken->length+1); memcpy(*token, ctoken->str, ctoken->length+1); *tlen=ctoken->length; psync_free(res); return PSYNC_NET_OK; }
static void psync_notifications_set_current_list(binresult *res, const char *thumbpath){ binresult *ores; const binresult *notifications, *thumb; pnotification_callback_t cb; uint32_t cntnew, cnttotal, i; notifications=psync_find_result(res, "notifications", PARAM_ARRAY); cnttotal=notifications->length; debug(D_NOTICE, "got list with %u notifications", (unsigned)cnttotal); cntnew=0; for (i=0; i<cnttotal; i++){ if (psync_find_result(notifications->array[i], "isnew", PARAM_BOOL)->num) cntnew++; thumb=psync_check_result(notifications->array[i], "thumb", PARAM_HASH); if (thumb && thumbpath) psync_notifications_download_thumb(thumb, thumbpath); } pthread_mutex_lock(&ntf_mutex); ores=ntf_processed_result; if (ntf_processing==2){ ntf_processed_result=NULL; psync_free(res); } else ntf_processed_result=res; ntf_processing=0; cb=ntf_callback; pthread_mutex_unlock(&ntf_mutex); psync_free(ores); if (cb){ debug(D_NOTICE, "calling notification callback, cnt=%u, newcnt=%u", (unsigned)cnttotal, (unsigned)cntnew); cb(cnttotal, cntnew); } }
static void psync_notifications_download_thumb(const binresult *thumb, const char *thumbpath){ const char *path, *filename, *host; char *filepath, *tmpfilepath, *buff; char cookie[128]; psync_http_socket *sock; psync_stat_t st; psync_file_t fd; int rd; rd=-1; path=psync_find_result(thumb, "path", PARAM_STR)->str; filename=strrchr(path, '/'); if (unlikely_log(!filename++)) return; filepath=psync_strcat(thumbpath, PSYNC_DIRECTORY_SEPARATOR, filename, NULL); if (!psync_stat(filepath, &st)){ debug(D_NOTICE, "skipping download of %s as it already exists", filename); goto err0; } tmpfilepath=psync_strcat(filepath, ".part", NULL); debug(D_NOTICE, "downloading thumbnail %s", filename); if (unlikely_log((fd=psync_file_open(tmpfilepath, P_O_WRONLY, P_O_CREAT|P_O_TRUNC))==INVALID_HANDLE_VALUE)) goto err1; sock=psync_http_connect_multihost(psync_find_result(thumb, "hosts", PARAM_ARRAY), &host); if (unlikely_log(!sock)) goto err2; psync_slprintf(cookie, sizeof(cookie), "Cookie: dwltag=%s\015\012", psync_find_result(thumb, "dwltag", PARAM_STR)->str); if (unlikely_log(psync_http_request(sock, host, path, 0, 0, cookie))) goto err3; if (unlikely_log(psync_http_next_request(sock))) goto err3; buff=(char *)psync_malloc(PSYNC_COPY_BUFFER_SIZE); while (1){ rd=psync_http_request_readall(sock, buff, PSYNC_COPY_BUFFER_SIZE); if (rd<=0) break; if (psync_file_write(fd, buff, rd)!=rd) break; } psync_free(buff); err3: psync_http_close(sock); err2: psync_file_close(fd); err1: if (rd==0 && !psync_file_rename_overwrite(tmpfilepath, filepath)) debug(D_NOTICE, "downloaded thumbnail %s", filename); else debug(D_WARNING, "downloading of thumbnail %s failed", filename); psync_free(tmpfilepath); err0: psync_free(filepath); }
static void insert_cache_team(int i, const binresult *team, void *_this) { const char *nameret = 0; nameret = psync_find_result(team, "name", PARAM_STR)->str; uint64_t teamid = 0; psync_sql_res *q; teamid = psync_find_result(team, "id", PARAM_NUM)->num; //debug(D_NOTICE, "Team name %s team id %lld\n", nameret,(long long)teamid); q=psync_sql_prep_statement("REPLACE INTO baccountteam (id, name) VALUES (?, ?)"); psync_sql_bind_uint(q, 1, teamid); psync_sql_bind_lstring(q, 2, nameret, strlen(nameret)); psync_sql_run_free(q); }
static void copy_team(int i, const binresult *user, void *_this) { const char *emailret = ""; team_visitor_params *params = (team_visitor_params *) _this; emailret = psync_find_result(user, "name", PARAM_STR)->str; *(params->length) = strlen(emailret); *(params->name) = psync_strndup(emailret, *(params->length)); }
void cache_ba_my_teams() { psync_socket *sock; binresult *bres; int i; const binresult *users; const binresult *user; const binresult *teams; psync_sql_res *q; psync_sql_lock(); q=psync_sql_prep_statement("DELETE FROM myteams "); psync_sql_run_free(q); binparam params[] = { P_STR("auth", psync_my_auth), P_STR("timeformat", "timestamp"), P_STR("userids", "me"), P_STR("showteams", "1"), P_STR("showeveryone", "1") }; sock = psync_apipool_get(); bres = send_command(sock, "account_users", params); if (likely(bres)) psync_apipool_release(sock); else { psync_apipool_release_bad(sock); debug(D_WARNING, "Send command returned invalid result.\n"); return; } users = psync_find_result(bres, "users", PARAM_ARRAY); if (!users->length) { psync_free(bres); debug(D_WARNING, "Account_users returned empty result!\n"); return; } else { user = users->array[0]; teams = psync_find_result(user, "teams", PARAM_ARRAY); if (teams->length) { for (i = 0; i < teams->length; ++i) cache_my_team(teams->array[i]); } } psync_free(bres); psync_sql_unlock(); }
static void cache_my_team(const binresult *team1) { const char *nameret = 0; const binresult *team; uint64_t teamid = 0; psync_sql_res *q; team = psync_find_result(team1, "team", PARAM_HASH); nameret = psync_find_result(team, "name", PARAM_STR)->str; teamid = psync_find_result(team, "id", PARAM_NUM)->num; //debug(D_NOTICE, "My Team name %s team id %lld\n", nameret,(long long)teamid); q=psync_sql_prep_statement("INSERT INTO myteams (id, name) VALUES (?, ?)"); psync_sql_bind_uint(q, 1, teamid); psync_sql_bind_lstring(q, 2, nameret, strlen(nameret)); psync_sql_run_free(q); }
static int handle_result(const binresult *bres, uint64_t result, char **err) { const char *errorret = 0; errorret = psync_find_result(bres, "error", PARAM_STR)->str; if(strlen(errorret) == 0) errorret = psync_find_result(bres, "message", PARAM_STR)->str; *err = psync_strndup(errorret, strlen(errorret)); debug(D_WARNING, "command gettreepublink returned error code %u message %s", (unsigned)result, errorret); psync_process_api_error(result); if (psync_handle_api_result(result)==PSYNC_NET_TEMPFAIL) return -result; else { *err = psync_strndup("Connection error.", 17); return -1; } }
static void fill_actionid(const binresult *ntf, psync_notification_t *pntf, psync_list_builder_t *builder){ const char *action; action=psync_find_result(ntf, "action", PARAM_STR)->str; if (!strcmp(action, "gotofolder")){ pntf->actionid=PNOTIFICATION_ACTION_GO_TO_FOLDER; pntf->actiondata.folderid=psync_find_result(ntf, "folderid", PARAM_NUM)->num; } else if (!strcmp(action, "opensharerequest")){ pntf->actionid=PNOTIFICATION_ACTION_SHARE_REQUEST; pntf->actiondata.sharerequestid=psync_find_result(ntf, "sharerequestid", PARAM_NUM)->num; } else if (!strcmp(action, "openurl")){ pntf->actionid=PNOTIFICATION_ACTION_GO_TO_URL; pntf->actiondata.url=psync_find_result(ntf, "url", PARAM_STR)->str; psync_list_add_string_offset(builder, offsetof(psync_notification_t, actiondata.url)); } else pntf->actionid=PNOTIFICATION_ACTION_NONE; }
static int check_token(char *token, uint32_t tlen, unsigned char *key, uint32_t keylen, unsigned char *hashhex){ binparam params[]={P_LSTR(PSYNC_CHECKSUM, hashhex, PSYNC_HASH_DIGEST_HEXLEN), P_LSTR("keydata", key, keylen), P_LSTR("token", token, tlen)}; psync_socket *api; binresult *res; uint64_t result; api=psync_apipool_get(); if (unlikely_log(!api)) return 0; res=send_command(api, "checkfileownershiptoken", params); if (unlikely_log(!res)){ psync_apipool_release_bad(api); return 0; } psync_apipool_release(api); result=psync_find_result(res, "result", PARAM_NUM)->num; psync_free(res); return result?0:1; }
static void insert_cache_email(int i, const binresult *user, void *_this) { const char *char_field = 0; char_field = psync_find_result(user, "email", PARAM_STR)->str; uint64_t id = 0; psync_sql_res *q; int active = 0; int frozen = 0; active = psync_find_result(user, "active", PARAM_BOOL)->num; frozen = psync_find_result(user, "frozen", PARAM_BOOL)->num; id = psync_find_result(user, "id", PARAM_NUM)->num; if (id && (active || frozen)) { q=psync_sql_prep_statement("REPLACE INTO baccountemail (id, mail, firstname, lastname) VALUES (?, ?, ?, ?)"); psync_sql_bind_uint(q, 1, id); psync_sql_bind_lstring(q, 2, char_field, strlen(char_field)); char_field = psync_find_result(user, "firstname", PARAM_STR)->str; psync_sql_bind_lstring(q, 3, char_field, strlen(char_field)); char_field = psync_find_result(user, "lastname", PARAM_STR)->str; psync_sql_bind_lstring(q, 4, char_field, strlen(char_field)); psync_sql_run_free(q); } }
int do_psync_account_stopshare(psync_shareid_t usershareids[], int nusershareid, psync_shareid_t teamshareids[], int nteamshareid, char **err) { psync_socket *api; binresult *bres; uint64_t result,userresult,teamresult; char *ids1 = NULL; char *ids2 = NULL; char *idsp = 0; int i,pind = 1, numparam = 1,k; binparam *t; const binresult *userres, *teamres, *statres; *err = 0; numparam += !!nusershareid + !!nteamshareid; if (unlikely(numparam == 1)) return -3; t = (binparam *) psync_malloc(numparam*sizeof(binparam)); init_param_str(t, "auth", psync_my_auth); if (nusershareid) { ids1 = (char *) psync_malloc(nusershareid*FOLDERID_ENTRY_SIZE); idsp = ids1; for (i = 0; i < nusershareid; ++i) { k = sprintf(idsp, "%lld",(long long) usershareids[i]); if (unlikely(k <= 0 )) break; idsp[k] = ','; idsp = idsp + k + 1; } if (i > 0) *(idsp - 1) = '\0'; //debug(D_NOTICE, "usershareids %s",ids1); init_param_str(t + pind++, "usershareids", ids1); } if (nteamshareid) { ids2 = (char *) psync_malloc(nteamshareid*FOLDERID_ENTRY_SIZE); idsp = ids2; for (i = 0; i < nteamshareid; ++i) { k = sprintf(idsp, "%lld", (long long) teamshareids[i]); if (unlikely(k <= 0 )) break; idsp[k] = ','; idsp = idsp + k + 1; } if (i > 0) *(idsp - 1) = '\0'; //debug(D_NOTICE, "teamshareids %s",ids2); init_param_str(t + pind++, "teamshareids", ids2); } api=psync_apipool_get(); if (unlikely(!api)) { debug(D_WARNING, "Can't gat api from the pool. No pool ?\n"); return -2; } bres = do_send_command(api, "account_stopshare", sizeof("account_stopshare") - 1, t, pind, -1, 1); if (likely(bres)) psync_apipool_release(api); else { psync_apipool_release_bad(api); debug(D_WARNING, "Send command returned in valid result.\n"); return -2; } result=psync_find_result(bres, "result", PARAM_NUM)->num; if (unlikely(result)) return handle_result(bres, result, err); statres = psync_find_result(bres, "status", PARAM_HASH); teamres = psync_find_result(statres, "team", PARAM_ARRAY)->array[0]; teamresult = psync_find_result(teamres, "result", PARAM_NUM)->num; userres = psync_find_result(statres, "user", PARAM_ARRAY)->array[0]; userresult = psync_find_result(userres, "result", PARAM_NUM)->num; if (!userresult || !teamresult) result = 0; else { if(userresult == INVALID_SHAREDID_RESULT && teamresult == INVALID_SHAREDID_RESULT) result = handle_result(userres, userresult, err); else if (userresult) result = handle_result(userres, userresult, err); else result = handle_result(teamres, teamresult, err); } if (ids1) psync_free(ids1); if (ids2) psync_free(ids2); psync_free(bres); psync_free(t); return result; }
int do_psync_account_teams(psync_userid_t teamids[], int nids, result_visitor vis, void *param) { psync_socket *sock; binresult *bres; char *ids = NULL; char *idsp = 0; int k,i; const binresult *users; if (nids) { ids = (char *) psync_malloc(nids*FOLDERID_ENTRY_SIZE); idsp = ids; for (i = 0; i < nids; ++i) { k = sprintf(idsp, "%lld", (long long) teamids[i]); if (unlikely(k <= 0 )) break; idsp[k] = ','; idsp = idsp + k + 1; } if (i > 0) *(idsp - 1) = '\0'; //debug(D_NOTICE, "Account_teams numids %d\n", nids); binparam params[] = {P_STR("auth", psync_my_auth), P_STR("timeformat", "timestamp"), P_STR("teamids", ids), P_STR("showeveryone", "1")}; sock = psync_apipool_get(); bres = send_command(sock, "account_teams", params); } else { if (psync_my_auth[0]) { binparam params[] = {P_STR("auth", psync_my_auth), P_STR("timeformat", "timestamp"), P_STR("showeveryone", "1")}; sock = psync_apipool_get(); bres = send_command(sock, "account_teams", params); } else if (psync_my_user && psync_my_pass) { binparam params[] = {P_STR("username", psync_my_user), P_STR("password", psync_my_pass), P_STR("timeformat", "timestamp"), P_STR("showeveryone", "1")}; sock = psync_apipool_get(); bres = send_command(sock, "account_teams", params); } else return -1; } if (likely(bres)) psync_apipool_release(sock); else { psync_apipool_release_bad(sock); debug(D_WARNING, "Send command returned in valid result.\n"); return -1; } users = psync_find_result(bres, "teams", PARAM_ARRAY); //debug(D_NOTICE, "Result contains %d teams\n", users->length); if (!users->length){ psync_free(bres); psync_free(ids); debug(D_WARNING, "Account_teams returned empty result!\n"); return -2; } else { for (i = 0; i < users->length; ++i) vis(i, users->array[i], param); } psync_free(bres); psync_free(ids); return 0; }
psync_notification_list_t *psync_notifications_get(){ psync_list_builder_t *builder; psync_notification_list_t *res; const binresult *ntf_res, *notifications, *ntf, *br; const char *filename; char *thumbpath, *filepath; psync_notification_t *pntf; psync_tree *thumbs, *nx; psync_stat_t st; uint32_t cntnew, cnttotal, i; cntnew=0; thumbpath=psync_get_private_dir(PSYNC_DEFAULT_NTF_THUMB_DIR); thumbs=PSYNC_TREE_EMPTY; if (likely(thumbpath)) psync_list_dir_fast(thumbpath, psync_notifications_thumb_dir_list, &thumbs); builder=psync_list_builder_create(sizeof(psync_notification_t), offsetof(psync_notification_list_t, notifications)); pthread_mutex_lock(&ntf_mutex); if (ntf_processed_result) ntf_res=ntf_processed_result; else if (ntf_result){ ntf_res=ntf_result; debug(D_NOTICE, "using not processed result for now"); } else ntf_res=NULL; if (ntf_res){ notifications=psync_find_result(ntf_res, "notifications", PARAM_ARRAY); cnttotal=notifications->length; for (i=0; i<cnttotal; i++){ ntf=notifications->array[i]; pntf=(psync_notification_t *)psync_list_bulder_add_element(builder); br=psync_find_result(ntf, "notification", PARAM_STR); pntf->text=br->str; psync_list_add_lstring_offset(builder, offsetof(psync_notification_t, text), br->length); pntf->thumb=NULL; br=psync_check_result(ntf, "thumb", PARAM_HASH); if (br && thumbpath){ filename=strrchr(psync_find_result(br, "path", PARAM_STR)->str, '/'); if (filename++){ psync_notification_remove_from_list(&thumbs, filename); filepath=psync_strcat(thumbpath, PSYNC_DIRECTORY_SEPARATOR, filename, NULL); if (!psync_stat(filepath, &st)){ pntf->thumb=filepath; psync_list_add_string_offset(builder, offsetof(psync_notification_t, thumb)); } else debug(D_WARNING, "could not stat thumb %s which is supposed to be downloaded", filename); psync_free(filepath); } } pntf->mtime=psync_find_result(ntf, "mtime", PARAM_NUM)->num; pntf->notificationid=psync_find_result(ntf, "notificationid", PARAM_NUM)->num; pntf->isnew=psync_find_result(ntf, "isnew", PARAM_BOOL)->num; if (pntf->isnew) cntnew++; pntf->iconid=psync_find_result(ntf, "iconid", PARAM_NUM)->num; fill_actionid(ntf, pntf, builder); } } pthread_mutex_unlock(&ntf_mutex); thumbs=psync_tree_get_first_safe(thumbs); while (thumbs){ nx=psync_tree_get_next_safe(thumbs); debug(D_NOTICE, "deleting unused thumb %s", psync_tree_element(thumbs, psync_thumb_list_t, tree)->name); filepath=psync_strcat(thumbpath, PSYNC_DIRECTORY_SEPARATOR, psync_tree_element(thumbs, psync_thumb_list_t, tree)->name, NULL); psync_file_delete(filepath); psync_free(filepath); psync_free(psync_tree_element(thumbs, psync_thumb_list_t, tree)); thumbs=nx; } psync_free(thumbpath); res=(psync_notification_list_t *)psync_list_builder_finalize(builder); res->newnotificationcnt=cntnew; return res; }