struct trie_node *channel_map = NULL;
const char *hostname = "irc.patchysicp.xyz";
-#define is_registered(cl) ((cl)->nick != 0)
+#define is_registered(cl) ((cl)->regstate == REG_REGISTERED)
void sendbuf(struct client_data *client) {
send_log(client->FD, databuf.buf, databuf.len, 0);
}
// at least one client hangs if it doesn't see a known extension
copy_strf(&databuf, ":%s CAP * LS :multi-prefix\n", hostname);
sendbuf(client);
- if (client->regstate != REG_CAPEND || !client->nick || !client->username)
+ if (!is_registered(client)) {
client->regstate = REG_CAPLS;
-
+ }
} else if (strcmp(args[0], "LIST") == 0) {
copy_strf(&databuf, ":%s CAP * LIST :\n", hostname);
sendbuf(client);
copy_strf(&databuf, ":%s CAP * NAK :%s\n", hostname, args[1]);
sendbuf(client);
} else if (strcmp(args[0], "END") == 0) {
- if (client->regstate == REG_CAPLS && client->nick && client->username)
+ if (!is_registered(client)) {
+ client->regstate = REG_INIT;
newUser(client);
+ }
} else {
goto unknown;
}
// User now registered
if (!args[0]) // FIXME: error message
return 0;
- setNick(client, args[0]);
- if (client->regstate != REG_CAPLS && client->nick && client->username)
+ if (is_registered(client)) {
+ setNick(client, args[0]);
+ } else {
+ free(client->nick_reg);
+ client->nick_reg = mystrdup(args[0]);
+ if (!client->nick_reg)
+ abort();
newUser(client);
+ }
} else if(strcmp(command, "USER") == 0) {
if (argc < 4) // FIXME: error message
return 0;
// set username
- free(client->username); // FIXME: maybe don't allow to change it after registration?
+ if (is_registered(client))
+ return 0; // FIXME: error
+ free(client->username);
client->username = malloc(strlen(args[0])+2);
if (!client->username)
abort();
if (!client->hostname)
abort();
// Send welcome
- if (client->regstate != REG_CAPLS && client->nick && client->username)
- newUser(client);
+ newUser(client);
} else if(strcmp(command, "MOTD") == 0) {
// Send welcome
sendMOTD(client);
}
void newUser(struct client_data *client) {
- client->regstate = REG_CAPEND;
+ if (client->regstate == REG_CAPLS || !client->nick_reg || !client->username)
+ return;
+ setNick(client, client->nick_reg);
+ free(client->nick_reg);
+ client->nick_reg = NULL;
+ if (!client->nick)
+ return;
+ client->regstate = REG_REGISTERED;
rpl_welcome(client), sendbuf(client);
rpl_yourhost(client), sendbuf(client);
rpl_created(client), sendbuf(client);