sasl_mechlist_string_build(const struct sasl_session *const restrict p, const struct myuser *const restrict mu,
                           const char **const restrict avoid)
{
	char buf[sizeof sasl_mechlist_string];
	char *bufptr = buf;
	size_t written = 0;
	mowgli_node_t *n;

	(void) memset(buf, 0x00, sizeof buf);

	MOWGLI_ITER_FOREACH(n, sasl_mechanisms.head)
	{
		const struct sasl_mechanism *const mptr = n->data;
		bool in_avoid_list = false;

		continue_if_fail(mptr != NULL);

		for (size_t i = 0; avoid != NULL && avoid[i] != NULL; i++)
		{
			if (strcmp(mptr->name, avoid[i]) == 0)
			{
				in_avoid_list = true;
				break;
			}
		}

		if (in_avoid_list || (mptr->password_based && mu != NULL && (mu->flags & MU_NOPASSWORD)))
			continue;

		const size_t namelen = strlen(mptr->name);

		if (written + namelen >= sizeof buf)
			break;

		(void) memcpy(bufptr, mptr->name, namelen);

		bufptr += namelen;
		*bufptr++ = ',';
		written += namelen + 1;
	}

	if (written)
		*(--bufptr) = 0x00;

	if (p)
		(void) sasl_sts(p->uid, 'M', buf);
	else
		(void) memcpy(sasl_mechlist_string, buf, sizeof buf);
}
sasl_mechlist_string_build(const struct sasl_session *const restrict p, const struct myuser *const restrict mu,
                           const char **const restrict avoid)
{
	char buf[sizeof sasl_mechlist_string];
	char *bufptr = buf;
	size_t written = 0;
	mowgli_node_t *n;

	(void) memset(buf, 0x00, sizeof buf);

	MOWGLI_ITER_FOREACH(n, sasl_mechanisms.head)
	{
		const struct sasl_mechanism *const mptr = n->data;
		bool in_avoid_list = false;

		continue_if_fail(mptr != NULL);

		for (size_t i = 0; avoid != NULL && avoid[i] != NULL; i++)
		{
			if (strcmp(mptr->name, avoid[i]) == 0)
			{
				in_avoid_list = true;
				break;
			}
		}

		if (in_avoid_list || (mptr->password_based && mu != NULL && (mu->flags & MU_NOPASSWORD)))
			continue;

		const size_t namelen = strlen(mptr->name);

		if (written + namelen >= sizeof buf)
			break;

		(void) memcpy(bufptr, mptr->name, namelen);

		bufptr += namelen;
		*bufptr++ = ',';
		written += namelen + 1;
	}

	if (written)
		*(--bufptr) = 0x00;

	if (p)
		(void) sasl_sts(p->uid, 'M', buf);
	else
		(void) memcpy(sasl_mechlist_string, buf, sizeof buf);
}