⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.c

📁 AnyQ服务端源代码(2004/10/28)源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* --------------------------------------------------------------------------
 *
 * License
 *
 * The contents of this file are subject to the Jabber Open Source License
 * Version 1.0 (the "License").  You may not copy or use this file, in either
 * source code or executable form, except in compliance with the License.  You
 * may obtain a copy of the License at http://www.jabber.com/license/ or at
 * http://www.opensource.org/.  
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Copyrights
 * 
 * Portions created by or assigned to Jabber.com, Inc. are 
 * Copyright (c) 1999-2000 Jabber.com, Inc.  All Rights Reserved.  Contact
 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
 *
 * Portions Copyright (c) 1998-1999 Jeremie Miller.
 * 
 * Acknowledgements
 * 
 * Special thanks to the Jabber Open Source Contributors for their
 * suggestions and support of Jabber.
 * 
 * --------------------------------------------------------------------------*/

#include "conference.h"

/* Generate extended presence entry */
xmlnode add_extended_presence(cnu from, cnu to, xmlnode presence, char *status, char *reason, char *actor)
{
    xmlnode tag;
    xmlnode element;
    xmlnode item;
    xmlnode output;

    taffil useraffil;
    trole userrole;
    jid user;
    cnr room;
    
    if(presence == NULL)
    {
	output = xmlnode_dup(from->presence);
    }
    else
    {
	output = xmlnode_dup(presence);
    }

    if(from == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing user variable in add_extended_presence", FZONE);
	return output;
    }

    user = from->realid;
    room = from->room;

    tag = xmlnode_insert_tag(output,"x");
    xmlnode_put_attrib(tag, "xmlns", NS_MUC_USER);

    item = xmlnode_insert_tag(tag, "item");

    if(room->visible == 1 || is_admin(room, to->realid))
    {
        xmlnode_put_attrib(item, "jid", jid_full(jid_fix(user)));
    }
	
    useraffil = affiliation_level(room, user);
    xmlnode_put_attrib(item, "affiliation", useraffil.msg);

    userrole = role_level(room, user);
    xmlnode_put_attrib(item, "role", userrole.msg);

    log_debug(NAME, "[%s] status check: status >%s<", FZONE, status);

    /* If this is a nick change, include the new nick if available */
    if(j_strcmp(status, STATUS_MUC_CREATED) == 0)
    {
	room->locked = 1;
    }

    if(status != NULL)
    {
	log_debug(NAME, "[%s] Adding to epp: status >%s<, reason >%s<", FZONE, status, reason);

	/* If this is a nick change, include the new nick if available */
	if(j_strcmp(status, STATUS_MUC_NICKCHANGE) == 0)
	    if(xmlnode_get_data(from->nick) != NULL)
	        xmlnode_put_attrib(item, "nick", xmlnode_get_data(from->nick));

	/* Add reason if available */
        if(reason != NULL)
	{
            element = xmlnode_insert_tag(item, "reason");
	    xmlnode_insert_cdata(element, reason, -1);
	}

	/* Add actor if available */
        if(actor != NULL)
	{
            element = xmlnode_insert_tag(item, "actor");
	    xmlnode_put_attrib(element, "jid", actor);
	}

	/* Add status code if available */
        element = xmlnode_insert_tag(tag, "status");
	xmlnode_put_attrib(element,"code", status);
    }

    return output;
}

/* Used to help debug contents of xhashes */
void dbg_xhash(xht h, const char *key, void *data, void *arg)
{           
    log_debug(NAME,"[%s] Key Debug: <%s>", FZONE, key);
}

/* Is the user a Service Admin? */
int is_sadmin(cni master, jid user)
{
    if(htb_get(&master->sadmin, jid_full(jid_user(jid_fix(user)))) != NULL )
        return 1;
    else
        return 0;
}

/* Is the user an owner for that room */
int is_owner(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in is_owner", FZONE);
	return 0;
    }

    log_debug(NAME, "[%s] Is Owner? >%s<", FZONE, jid_full(jid_fix(user)));

    /* Server admin can override */
    if(is_sadmin(room->master, user))
	    return 1;
    else if(j_strcmp(jid_full(jid_user(jid_fix(room->creator))), jid_full(jid_user(jid_fix(user)))) == 0)
	    return 1;
    else if(htb_get(&room->owner, jid_full(jid_user(jid_fix(user)))) != NULL )
	    return 1;
    else
	    return 0;

}

/* Is the user in the admin affiliation list for that room */
int is_admin(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in is_admin: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    log_debug(NAME, "[%s] Is Admin? >%s<", FZONE, jid_full(jid_fix(user)));

    if(is_owner(room, user))
	    return 1;

    if(htb_get(&room->admin, jid_full(jid_user(jid_fix(user)))) != NULL )
	    return 1;
    else if(htb_get(&room->admin, user->server) != NULL )
	    return 1;
    else
	    return 0;
}

/* Is the user in the moderator role list for that room */
int is_moderator(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in is_admin: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    if(is_owner(room, user))
	return 2;

    log_debug(NAME, "[%s] Is Moderator? >%s<", FZONE, jid_full(jid_fix(user)));

    if(htb_get(&room->moderator, jid_full(jid_fix(user))) != NULL )
	return 1;
    else
	return 0;
}

/* Is the user in the participant role list for that room */
int is_participant(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in has_voice: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    /* Every non-admin has voice in a non-moderated room */
    if(room->moderated == 0) 	
	return 1;

    /* Moderator has voice intrinsic */
    if(is_moderator(room, user))
	return 1;

    /* If moderated, check the voice list */
    if(htb_get(&room->participant, jid_full(jid_fix(user))) != NULL )
	return 1;
    else
	return 0;
}

/* Is the user in the member affiliation list for that room */
int is_member(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in is_member: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    /* Owner is automatically a member */
    if(is_owner(room, user))
	return 1;

    /* Admin is automatically a member */
    if(is_admin(room, user))
	return 1;

    if(htb_get(&room->member, jid_full(jid_user(jid_fix(user)))) != NULL )
	    return 1;
    else if(htb_get(&room->member, user->server) != NULL )
	    return 1;
    else
	    return 0;
}

/* Is the user in the outcast affiliation list for that room */
int is_outcast(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in is_outcast: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    if(htb_get(&room->outcast, jid_full(jid_user(jid_fix(user)))) != NULL )
	    return 1;
    else if(htb_get(&room->outcast, user->server) != NULL )
	    return 1;
    else
	    return 0;
}

/* Only return 1 if visitor */
int is_visitor(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in is_banne: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    if(is_moderator(room, user))
	    return 0;
    else if(is_participant(room, user))
	    return 0;
    else if(htb_get(&room->remote, jid_full(jid_fix(user))) != NULL )
	    return 1;
    else
	    return 0;
}

/* Is user in the room? */
int in_room(cnr room, jid user)
{
    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in in_room: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    if(htb_get(&room->remote, jid_full(jid_fix(user))) != NULL )
	    return 1;
    else
	    return 0;
}

/* Is this a legacy client? */
int is_legacy(cnu user)
{
    cnr room;

    if(user == NULL)
    {
	log_debug(NAME, "[%s] ERR: Missing variable in is_legacy", FZONE);
	return 0;
    }

    room = user->room;

    if(room->legacy)
	return 1;
    else if(user->legacy)
	return 1;
    else
	return 0;
}

/* Is user leaving the room? */
int is_leaving(cnr room, jid user)
{
    cnu target;

    if(room == NULL || user == NULL)
    {
        log_debug(NAME, "[%s] ERR: Missing variable in is_leaving: room=%s user=%s", FZONE, room, user);
	return 0;
    }

    target = htb_get(&room->remote, jid_full(jid_fix(user)));

    if(target != NULL )
    {
	if(target->leaving == 1)    
        {		
            return 1;
	}
    }

    return 0;
}

/* Check if user is already registered */
int is_registered(cni master, char *user, char *nick)
{
    xmlnode results;

    if(user == NULL || nick == NULL)
        return 0;

    results = get_data_bynick(master, nick);

    if(results != NULL)
    {
        log_debug(NAME, "[%s] Found %s in Registered Nicks - checking [%s/%s]", FZONE, nick, user, xmlnode_get_attrib(results, "jid"));

        if(j_strcmp(user, xmlnode_get_attrib(results, "jid")) != 0)
        {
	    /* User taken by someone else */
            xmlnode_free(results);
            return -1;
        }
	else
	{
	    /* Nick belongs to me */
            xmlnode_free(results);
	    return 1;
	}
    }
    else
    {
	/* Nick is free */
        xmlnode_free(results);
	return 0;
    }
}

/* Generic alert function for user/room */
xmlnode _con_send_alert(cnu user, char *text, char *subject, char *status)
{
    xmlnode msg;
    xmlnode element;
    char body[256];
    char reason[128];
    char *type = NULL;

    cnr room = user->room;
    char *room_id = jid_full(jid_fix(room->id));

    if(is_legacy(user) == 0)
    {
	return NULL;
    }

    if(status == NULL)
    {
	    snprintf(body, 256, "%s", text);
    }
    else 
    {
	if(text == NULL)
		strcpy(reason, "None given");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -