terom@39: #include "irc_proto.h" terom@36: terom@36: #include terom@36: terom@75: err_t irc_nm_parse_buf (struct irc_nm *nm, char *prefix) terom@75: { terom@149: const char *token; terom@75: terom@149: // parse the first token to determine if this is a nickmask or server name terom@149: token = strsep(&prefix, "!"); terom@149: terom@149: // did strsep find that "!"? terom@149: if (prefix) { terom@149: // it must be a full nickmask terom@149: nm->nickname = token; terom@149: nm->username = strsep(&prefix, "@"); terom@149: nm->hostname = prefix; terom@149: terom@149: if (!prefix) terom@149: // something silly, didn't have an @token terom@149: return ERR_INVALID_NM; terom@149: terom@149: } else { terom@149: // handle as a server name terom@149: nm->nickname = NULL; terom@149: nm->username = NULL; terom@149: nm->hostname = token; terom@149: terom@149: } terom@75: terom@75: // ok terom@75: return SUCCESS; terom@75: terom@75: } terom@75: terom@45: err_t irc_nm_parse (struct irc_nm *nm, char *buf, const char *prefix) terom@45: { terom@45: // too long? terom@45: if (strlen(prefix) >= IRC_PREFIX_MAX) terom@45: return ERR_INVALID_NM; terom@45: terom@45: // copy to mutable buffer terom@45: strcpy(buf, prefix); terom@45: terom@75: // parse from buf terom@75: return irc_nm_parse_buf(nm, buf); terom@45: } terom@45: terom@36: /** terom@36: * Compare two nicknames terom@36: */ terom@36: int irc_cmp_nick (const char *nick1, const char *nick2) terom@36: { terom@36: // XXX: just use strcasecmp for now terom@36: return strcasecmp(nick1, nick2); terom@36: } terom@36: terom@36: int irc_ncmp_nick (const char *nick1, const char *nick2, size_t n) terom@36: { terom@36: // XXX: just use strncasecmp for now terom@36: return strncasecmp(nick1, nick2, n); terom@36: } terom@36: terom@36: err_t irc_prefix_parse_nick (const char *prefix, char nick[IRC_NICK_MAX]) terom@36: { terom@36: const char *bang; terom@36: terom@36: // find the ! terom@36: if ((bang = strchr(prefix, '!')) == NULL) terom@36: return ERR_INVALID_NM; terom@36: terom@36: // too long? terom@36: if (bang - prefix > IRC_NICK_MAX - 1) terom@36: return ERR_INVALID_NICK_LENGTH; terom@36: terom@36: // copy up to the ! terom@36: memcpy(nick, prefix, bang - prefix); terom@36: terom@36: // terminating NUL terom@36: nick[bang - prefix] = '\0'; terom@36: terom@36: // ok terom@36: return SUCCESS; terom@36: } terom@36: terom@36: int irc_prefix_cmp_nick (const char *prefix, const char *nick) terom@36: { terom@36: const char *bang; terom@36: terom@36: // find the ! terom@36: if ((bang = strchr(prefix, '!')) == NULL) terom@36: return -ERR_INVALID_NM; terom@36: terom@36: // compare up to that terom@36: if (irc_ncmp_nick(prefix, nick, (bang - prefix)) == 0) terom@36: // match terom@36: return 0; terom@36: terom@36: else terom@36: // doesn't match terom@36: return 1; terom@36: } terom@72: terom@72: const char* irc_nick_chanflags (const char *nick, char chanflags[IRC_CHANFLAGS_MAX]) terom@72: { terom@72: char *cf = chanflags; terom@72: terom@72: // consume the chanflags, using strchr to look for the char in the set of chanflags... terom@82: // XXX: error if nickname is empty... terom@82: while (*nick && strchr(IRC_CHANFLAGS, *nick) && (cf < chanflags + IRC_CHANFLAGS_MAX - 1)) terom@72: *cf++ = *nick++; terom@72: terom@72: // NUL-terminate chanflags terom@72: *cf = '\0'; terom@72: terom@72: // then return the pointer to the first nickname char terom@72: return nick; terom@72: } terom@72: