void state_sync_modes( char* channel, char* ts, char* modes, char* key, char* lim ) { long its = strtol( ts, NULL, 10 ); char* echannel = db_encode( channel ); db_res res = db_query( "SELECT ts FROM cur_chanlist WHERE lchannel='%s'", echannel ); db_row row = db_get_row( res ); long ots = strtol( row[ 0 ], NULL, 10 ); db_free_result( res ); printf( "Channel %s has ts %li, joined with %li\n", channel, ots, its ); if ( ots == -1 || its > ots ) { char* lim_pos = strstr( modes, "l" ); char* key_pos = strstr( modes, "k" ); mode_buffer[ 0 ] = '\0'; if ( lim_pos == NULL && key_pos == NULL ) { strcpy( mode_buffer, modes ); } else if ( lim_pos == NULL ) { snprintf( mode_buffer, 512, "%s %s", modes, key ); } else if ( key_pos == NULL ) { snprintf( mode_buffer, 512, "%s %s", modes, lim ); } else if ( key_pos > lim_pos ) { snprintf( mode_buffer, 512, "%s %s %s", modes, lim, key ); } else { snprintf( mode_buffer, 512, "%s %s %s", modes, key, lim ); } res = db_query( "UPDATE cur_chanlist SET ts='%li', modes='%s' WHERE lchannel='%s'", its, mode_buffer, echannel ); db_free_result( res ); } free( echannel ); }