 .export _permute_nick
 .importzp ptr1

;permutes last character (doesn't add one), so for "Bob" you get:
;Bo_, Bo1 through Bo9, BoA through BoZ
;Gives a total of 36 replacement nicks to try.
;Eventually we run out and start repeating, but by then the IRC
;server will have disconnected us.

;; static void permute_nick(void) {
;; 	static char *last;
;; 
;; 	last = conf->nick + strlen(conf->nick) - 1;
;; 
;; 	if((*last >= '1' && *last < '9') || (*last >= 'A' && *last < 'Z')) {
;; 		(*last)++;
;; 	} else {
;; 		switch(*last) {
;; 			case '_': *last = '1'; break;
;; 			case '9': *last = 'A'; break;
;; 			default:  *last = '_'; break;
;; 		}
;; 	}
;; }

;; WARNING: the address of conf->nick is hardcoded here as $0480.
;; So is its length (25).

 nick = $0480

_permute_nick:
 ldy #0
@l1:
 lda nick,y
 beq perm
 iny
 cpy #$18 ; stop after 24 chars
 bne @l1

perm:
 dey ; last non-null character.
 lda nick,y

 tax

 cpx #'1'
 bcc nonnum
 cpx #'8'+1
 bcc inc_and_ret

nonnum:
 cpx #'A'
 bcc nonaplha
 cpx #'Z'
 bcc inc_and_ret

nonaplha:
 cpx #'_'
 bne non_uscore
 ldx #'1'
 bne ret

non_uscore:
 cpx #'9'
 bne non_9
 ldx #'A'
 bne ret

non_9:
 ldx #'_'
 bne ret

inc_and_ret:
 inx
ret:
 txa
 sta nick,y
 rts
