/************************************************************************** Try to connect to a server (get_server_address() must be called first!): - try to create a TCP socket and connect it to `server_addr' - if successful: - start monitoring the socket for packets from the server - send a "login request" packet to the server and - return 0 - if unable to create the connection, close the socket, put an error message in ERRBUF and return the Unix error code (ie., errno, which will be non-zero). **************************************************************************/ static int try_to_connect(const char *username, char *errbuf, int errbufsize) { close_socket_set_callback(close_socket_callback); /* connection in progress? wait. */ if (client.conn.used) { (void) mystrlcpy(errbuf, _("Connection in progress."), errbufsize); return -1; } if ((client.conn.sock = socket(server_addr.saddr.sa_family, SOCK_STREAM, 0)) == -1) { (void) mystrlcpy(errbuf, fc_strerror(fc_get_errno()), errbufsize); return -1; } if (fc_connect(client.conn.sock, &server_addr.saddr, sockaddr_size(&server_addr)) == -1) { (void) mystrlcpy(errbuf, fc_strerror(fc_get_errno()), errbufsize); fc_closesocket(client.conn.sock); client.conn.sock = -1; #ifdef HAVE_WINSOCK return -1; #else return errno; #endif } make_connection(client.conn.sock, username); return 0; }
void main_refresh_folders(void) { int row = 0; struct folder *f; char text[folders_width + 1]; for (f = folder_first(); f; f = folder_next(f)) { unsigned int level; unsigned int i; level = folder_level(f); if (level > 10) { level = 10; } if (f == main_active_folder) { text[0] = '*'; } else { text[0] = ' '; } for (i = 0; i < level; i++) { text[1+i] = ' '; } mystrlcpy(&text[level + 1], folder_name(f), sizeof(text) - 2); mvwprintw(folders_wnd, row++, 0 , text); } wrefresh(folders_wnd); }
/************************************************************************** return a unique guest name WARNING: do not pass pconn->username to this function: it won't return! **************************************************************************/ void get_unique_guest_name(char *name) { unsigned int i; /* first see if the given name is suitable */ if (is_guest_name(name) && !find_conn_by_user(name)) { return; } /* next try bare guest name */ mystrlcpy(name, GUEST_NAME, MAX_LEN_NAME); if (!find_conn_by_user(name)) { return; } /* bare name is taken, append numbers */ for (i = 1; ; i++) { my_snprintf(name, MAX_LEN_NAME, "%s%u", GUEST_NAME, i); /* attempt to find this name; if we can't we're good to go */ if (!find_conn_by_user(name)) { break; } } }
static char * mystrndup(const char *string, int len) { /* allocate copy of string on the heap */ char *buf; assert(string != NULL); buf = (char *)mymalloc(len + 1); mystrlcpy(buf, string, len + 1); return buf; }
static struct city *get_selected_city(void) { XawListReturnStruct *ret; int len; ret=XawListShowCurrent(goto_list); if(ret->list_index==XAW_LIST_NONE) return 0; len = strlen(ret->string); if(len>3 && strcmp(ret->string+len-3, "(A)")==0) { char name[MAX_LEN_NAME]; mystrlcpy(name, ret->string, MIN(sizeof(name),len-2)); return game_find_city_by_name(name); } return game_find_city_by_name(ret->string); }
/************************************************************************** Get ready to [try to] connect to a server: - translate HOSTNAME and PORT (with defaults of "localhost" and DEFAULT_SOCK_PORT respectively) to a raw IP address and port number, and store them in the `server_addr' variable - return 0 on success or put an error message in ERRBUF and return -1 on failure **************************************************************************/ static int get_server_address(const char *hostname, int port, char *errbuf, int errbufsize) { if (port == 0) port = DEFAULT_SOCK_PORT; /* use name to find TCP/IP address of server */ if (!hostname) hostname = "localhost"; if (!net_lookup_service(hostname, port, &server_addr, FALSE)) { (void) mystrlcpy(errbuf, _("Failed looking up host."), errbufsize); return -1; } return 0; }
char * add3strings(const char *str1, const char *str2, const char *str3) { int size; char *buf; assert(str1!= NULL); assert(str2!= NULL); assert(str3!= NULL); size = strlen(str1) + strlen(str2) + strlen(str3) + 1; /* total length plus 0 byte */ buf = (char *) mymalloc(size); /* DPRINTF3(DEBUG_TERMIO,"size1: %d, size2: %d, size3: %d", (int) strlen(str1), (int) strlen(str2), (int) strlen(str3)); */ mystrlcpy(buf, str1, size); mystrlcat(buf, str2, size); mystrlcat(buf, str3, size); return buf; }
/************************************************************************** Fills in the preset list. **************************************************************************/ static void update_cma_preset_list(void) { static char *preset_lines[256]; static char preset_text[256][256]; int i; if (cmafec_preset_num()) { /* Add all CMA presets to the list. */ for (i = 0; i < cmafec_preset_num(); i++) { mystrlcpy(preset_text[i], cmafec_preset_get_descr(i), sizeof(preset_text[i])); preset_lines[i] = preset_text[i]; } XawListChange(preset_list, preset_lines, cmafec_preset_num(), 0, True); } else { /* Show intro message in the preset list. */ XawListChange(preset_list, initial_preset_list, 8, 0, True); } }
void main_set_folder_mails(struct folder *folder) { void *handle = NULL; struct mail_info *mi; int row = 0; int w, h; int from_width = 0; int subject_width = 0; int date_width = 0; char from_buf[128]; getmaxyx(messagelist_wnd, h, w); /* Determine dimensions */ while ((mi = folder_next_mail(folder, &handle))) { int l; const char *from = mail_info_get_from(mi); const char *subject = mi->subject; const char *date = sm_get_date_str(mi->seconds); if (!from) from = "Unknown"; l = strlen(from); if (l > from_width) { from_width = l; } l = mystrlen(subject); if (l > subject_width) { subject_width = l; } l = strlen(date); if (l > date_width) { date_width = l; } } if (from_width >= sizeof(from_buf)) { from_width = sizeof(from_buf) - 1; } /* Draw */ handle = NULL; wmove(messagelist_wnd, 0, 0); while ((mi = folder_next_mail(folder, &handle))) { const char *from = mail_info_get_from(mi); const char *date = sm_get_date_str(mi->seconds); const char *first = " "; mystrlcpy(from_buf, from?from:"Unknown", sizeof(from_buf)); if (row == messagelist_active) { first = "*"; } mvwprintw(messagelist_wnd, row, 0, first); mvwprintw(messagelist_wnd, row, 1, from); mvwprintw(messagelist_wnd, row, 1 + from_width + 1, mi->subject); mvwprintw(messagelist_wnd, row, 1 + from_width + 1 + subject_width + 1, date); row++; } wclrtobot(messagelist_wnd); wrefresh(messagelist_wnd); }
/************************************************************************** Loads a user from the database. **************************************************************************/ static enum authdb_status auth_db_load(struct connection *pconn) { #ifdef HAVE_AUTH char buffer[512] = ""; const int bufsize = sizeof(buffer); int num_rows = 0; MYSQL *sock, mysql; MYSQL_RES *res; MYSQL_ROW row; char *name_buffer; int str_result; mysql_init(&mysql); /* attempt to connect to the server */ if (!(sock = mysql_real_connect(&mysql, auth_config.host.value, auth_config.user.value, auth_config.password.value, auth_config.database.value, atoi(auth_config.port.value), NULL, 0))) { freelog(LOG_ERROR, "Can't connect to server! (%s)", mysql_error(&mysql)); return AUTH_DB_ERROR; } name_buffer = alloc_escaped_string(&mysql, pconn->username); if (name_buffer != NULL) { /* select the password from the entry */ str_result = my_snprintf(buffer, bufsize, "select password from %s where name = '%s'", auth_config.table.value, name_buffer); if (str_result < 0 || str_result >= bufsize || mysql_query(sock, buffer)) { freelog(LOG_ERROR, "db_load query failed for user: %s (%s)", pconn->username, mysql_error(sock)); free_escaped_string(name_buffer); mysql_close(sock); return AUTH_DB_ERROR; } res = mysql_store_result(sock); num_rows = mysql_num_rows(res); /* if num_rows = 0, then we could find no such user */ if (num_rows < 1) { mysql_free_result(res); free_escaped_string(name_buffer); mysql_close(sock); return AUTH_DB_NOT_FOUND; } /* if there are more than one row that matches this name, it's an error * continue anyway though */ if (num_rows > 1) { freelog(LOG_ERROR, "db_load query found multiple entries (%d) for user: %s", num_rows, pconn->username); } /* if there are rows, then fetch them and use the first one */ row = mysql_fetch_row(res); mystrlcpy(pconn->server.password, row[0], sizeof(pconn->server.password)); mysql_free_result(res); /* update the access time for this user */ memset(buffer, 0, bufsize); str_result = my_snprintf(buffer, bufsize, "update %s set accesstime=unix_timestamp(), " "address='%s', logincount=logincount+1 " "where strcmp(name, '%s') = 0", auth_config.table.value, pconn->server.ipaddr, name_buffer); free_escaped_string(name_buffer); name_buffer = NULL; if (str_result < 0 || str_result >= bufsize || mysql_query(sock, buffer)) { freelog(LOG_ERROR, "db_load update accesstime failed for user: %s (%s)", pconn->username, mysql_error(sock)); } } mysql_close(sock); #endif return AUTH_DB_SUCCESS; }