void sflphone_fill_account_list(void) { account_list_init(); gchar **array = dbus_account_list(); for (gchar **accountID = array; accountID && *accountID; ++accountID) { account_t *acc = create_account_with_ID(*accountID); if (acc->properties == NULL) { g_warning("SFLphone: Error: Could not fetch details for account %s", *accountID); break; } account_list_add(acc); /* Fill the actual array of credentials */ dbus_get_credentials(acc); gchar * status = account_lookup(acc, CONFIG_ACCOUNT_REGISTRATION_STATUS); if (g_strcmp0(status, "REGISTERED") == 0) acc->state = ACCOUNT_STATE_REGISTERED; else if (g_strcmp0(status, "UNREGISTERED") == 0) acc->state = ACCOUNT_STATE_UNREGISTERED; else if (g_strcmp0(status, "TRYING") == 0) acc->state = ACCOUNT_STATE_TRYING; else if (g_strcmp0(status, "ERROR_GENERIC") == 0) acc->state = ACCOUNT_STATE_ERROR; else if (g_strcmp0(status, "ERROR_AUTH") == 0) acc->state = ACCOUNT_STATE_ERROR_AUTH; else if (g_strcmp0(status, "ERROR_NETWORK") == 0) acc->state = ACCOUNT_STATE_ERROR_NETWORK; else if (g_strcmp0(status, "ERROR_HOST") == 0) acc->state = ACCOUNT_STATE_ERROR_HOST; else if (g_strcmp0(status, "ERROR_NOT_ACCEPTABLE") == 0) acc->state = ACCOUNT_STATE_ERROR_NOT_ACCEPTABLE; else if (g_strcmp0(status, "ERROR_EXIST_STUN") == 0) acc->state = ACCOUNT_STATE_ERROR_EXIST_STUN; else if (g_strcmp0(status, "READY") == 0) acc->state = ACCOUNT_STATE_IP2IP_READY; else { g_warning("Unexpected status %s", status); acc->state = ACCOUNT_STATE_INVALID; } gchar * code = account_lookup(acc, CONFIG_ACCOUNT_REGISTRATION_STATE_CODE); if (code != NULL) acc->protocol_state_code = atoi(code); acc->protocol_state_description = account_lookup(acc, CONFIG_ACCOUNT_REGISTRATION_STATE_DESC); } g_strfreev(array); // Set the current account message number current_account_set_message_number(current_account_get_message_number()); }
struct account_desc* account_list_find(struct list_head* list, const char* username, const char* realm) { struct list_head* get = NULL; struct list_head* n = NULL; list_iterate_safe(get, n, list) { struct account_desc* tmp = list_get(get, struct account_desc, list); if(!strncmp(tmp->username, username, sizeof(tmp->username) - 1)) { /* if realm is specified, try a match otherwise the peer is found */ if(!realm || !strncmp(tmp->realm, realm, sizeof(tmp->realm) - 1)) { //~ syslog(LOG_NOTICE, "Peer found in list."); return tmp; } } } // vvv ---- using database ---------------------------------------------------- #ifdef USE_DATABASE syslog(LOG_NOTICE, "Peer not found in list."); char* user_password = turn_get_password(username); if(user_password != NULL) { account_list_add ( list, account_desc_new(username, user_password, realm, AUTHORIZED) ); free(user_password); syslog(LOG_NOTICE, "Peer found in database and added to the list."); return account_list_find(list, username, realm); } else { free(user_password); syslog(LOG_WARNING, "Peer not found in database."); return NULL; } #endif // ^^^ ---- using database ---------------------------------------------------- /* not found */ return NULL; }
static void add_account_cb(SFLPhoneClient *client) { account_t *new_account = create_default_account(); account_list_add(new_account); run_account_dialog(new_account->accountID, client, TRUE); }
int account_parse_file(struct list_head* list, const char* file) { char line[512]; char* save_ptr = NULL; const char* delim = ":"; char* token = NULL; char* login = NULL; char* password = NULL; char* realm = NULL; FILE* account_file = NULL; struct account_desc* desc = NULL; account_file = fopen(file, "r"); if(!account_file) { return -1; } while(!feof(account_file)) { enum account_state state = AUTHORIZED; if(!fgets(line, sizeof(line) - 1, account_file)) { continue; } /* replace end of line by NULL character */ save_ptr = strchr(line, '\n'); if(save_ptr) { *save_ptr = 0x00; } save_ptr = NULL; token = strtok_r(line, delim, &save_ptr); if(!token) { continue; } login = strdup(token); token = strtok_r(NULL, delim, &save_ptr); if(!token) { free(login); continue; } password = strdup(token); token = strtok_r(NULL, delim, &save_ptr); if(!token) { free(login); free(password); continue; } realm = strdup(token); token = strtok_r(NULL, delim, &save_ptr); if(token) { if(!strcmp(token, "authorized")) { state = AUTHORIZED; } else if(!strcmp(token, "restricted")) { state = RESTRICTED; } else if(!strcmp(token, "refused")) { state = REFUSED; } } /* add it to the list (only for non-refused account) */ if(state != REFUSED) { desc = account_desc_new(login, password, realm, state); if(desc) { account_list_add(list, desc); } } /* cleanup */ desc = NULL; free(login); free(password); free(realm); } fclose(account_file); return 0; }