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

📄 utils.c

📁 AnyQ服务端源代码(2004/10/28)源码
💻 C
📖 第 1 页 / 共 2 页
字号:
	else
		snprintf(reason, 128, "%s", text);

        if(j_strcmp(status, STATUS_MUC_KICKED) == 0)
	{
	    type = "normal";
	    snprintf(body, 256, "You have been kicked from the room %s. \n Reason: %s", room_id, reason);
	}

        if(j_strcmp(status, STATUS_MUC_BANNED) == 0)
	{
	    type = "normal";
	    snprintf(body, 256, "You have been kicked and outcast from the room %s. \n Reason: %s", room_id, reason);
	}

        if(j_strcmp(status, STATUS_MUC_SHOWN_JID) == 0)
	{
	    type = "groupchat";
	    snprintf(body, 256, "This room (%s) is not anonymous", room_id);
	}

        if(j_strcmp(status, STATUS_MUC_HIDDEN_JID) == 0)
	{
	    type = "groupchat";
	    snprintf(body, 256, "This room (%s) is anonymous, except for admins", room_id);
	    status = STATUS_MUC_SHOWN_JID;
	}
    }
        
    msg = jutil_msgnew(type, jid_full(jid_fix(user->realid)) , subject, body);
    xmlnode_put_attrib(msg, "from", room_id);
    
    if(status != NULL)
    {
        element = xmlnode_insert_tag(msg,"x");
        xmlnode_put_attrib(element, "xmlns", NS_MUC_USER);
        xmlnode_put_attrib(xmlnode_insert_tag(element, "status"), "code", status);

    }

    return msg;
}

/* User alaert wrapper */
void con_send_alert(cnu user, char *text, char *subject, char *status)
{
    xmlnode msg = _con_send_alert(user, text, subject, status);

    if(msg)
    {
        deliver(dpacket_new(msg), NULL);
    }
}

/* Room status/alert wrapper */
void _con_send_room_status(const char *key, void *data, void *arg)
{
    char *status = (char*)arg;
    cnu user = (cnu)data;
    xmlnode msg = _con_send_alert(user, NULL, NULL, status);

    if(msg)
    {
        deliver(dpacket_new(msg), NULL);
    }
}

/* Send status change to a room */
void con_send_room_status(cnr room, char *status)
{
    htb_walk(&room->local, _con_send_room_status, (void*)status);
}   

/* Splice \n-delimited line into array */
char *linesplit(char **entry) 
{
    char *line;
    char *point;
    char *str = "\n";

    if(!(*entry)) 
        return NULL;

    line = *entry;

    if((point = strstr(*entry, str)))
    {
        *point = 0;
        *entry = point+strlen(str);
    } 
    else
        *entry = NULL;

    return line;
}

/* Integer to String conversion */
char *itoa(int number, char *result)
{
    sprintf(result, "%d", number);
    return result;
}
		
/* Custom Debug message */
char *funcstr(char *file, char *function, int line)
{           
    static char buff[128];
    int i;
		            
    i = snprintf(buff,127,"%s:%d (%s)",file,line,function);
    buff[i] = '\0';
		            
    return buff;
}       

/* Return current date for logfile system */
int minuteget(time_t tin)
{
    time_t timef;
    char timestr[50];
    size_t timelen = 49;
    int results;

    if(tin)
	timef = tin;
    else 
        timef = time(NULL);

    strftime(timestr, timelen, "%M", localtime(&timef));
    results = j_atoi(timestr, -1);

    return results;
}

/* Return current date for logfile system */
char *timeget(time_t tin)
{
    time_t timef;
    char timestr[50];
    size_t timelen = 49;

    if(tin)
	timef = tin;
    else 
        timef = time(NULL);

    strftime(timestr, timelen, "%H:%M", localtime(&timef));

    return j_strdup(timestr);
}

/* Return current date for logfile system */
char *dateget(time_t tin)
{
    time_t timef;
    char timestr[50];
    size_t timelen = 49;

    if(tin)
	timef = tin;
    else 
        timef = time(NULL);

    strftime(timestr, timelen, "%Y-%m-%d", localtime(&timef));

    return j_strdup(timestr);
}

/* Send presence update for a user to the room */
void update_presence(cnu user)
{
    xmlnode result;
    cnr room;

    if(user == NULL)
        return;

    room = user->room;
		    
    /* Send updated presence packet */
    result = xmlnode_dup(user->presence);
    xmlnode_put_vattrib(result,"cnu",(void*)user);

    htb_walk(&room->local, con_room_sendwalk, (void*)result);
    xmlnode_free(result);
    return;
}

/* Generate custom errors for multi-item handler */
void insert_item_error(xmlnode node, char *code, char *msg)
{
    xmlnode element;

    element = xmlnode_insert_tag(node, "error");
    xmlnode_put_attrib(element, "code", code);
    xmlnode_insert_cdata(element, msg, -1);
}

/* Add user into the room roster hash */
int add_roster(cnr room, jid userid)
{
    xmlnode store;
    xmlnode node;
    char *key;

    if(room == NULL || userid == NULL)
    {
        return -1;
    }

    key = j_strdup(jid_full(jid_user(jid_fix(userid))));
    store = htb_get(&room->roster, key);

    /* User not previously registered. Set up */
    if(store == NULL)
    {
        store = xmlnode_new_tag("users");
    }
    else
    {
        node = xmlnode_get_tag(store, spools(xmlnode_pool(store), "item?jid=", jid_full(jid_fix(userid)), xmlnode_pool(store)));

        /* If already in the node, ignore */
        if(node != NULL)
        {
            free(key);
            return 0;
        }
    }

    if(userid->resource != NULL)
    {
        log_debug(NAME, "[%s] adding entry (%s) for jid (%s)", FZONE, jid_full(jid_fix(userid)), jid_full(jid_user(jid_fix(userid))));
        node = xmlnode_new_tag("item");
        xmlnode_put_attrib(node, "jid", jid_full(jid_fix(userid)));

        xmlnode_insert_node(store, node);
        xmlnode_free(node);
    }

    htb_put(&room->roster, key, store);
    free(key);

    return 1;
}

/* Remove a user from the room roster hash */
int remove_roster(cnr room, jid userid)
{
    xmlnode store;
    xmlnode node;
    char *key;

    if(room == NULL || userid == NULL)
    {
        return -1;
    }

    key = j_strdup(jid_full(jid_user(jid_fix(userid))));
    store = htb_get(&room->roster, key);

    if(store == NULL)
    {
        free(key);
        return 1;
    }

    node = xmlnode_get_tag(store, spools(xmlnode_pool(store), "item?jid=", jid_full(jid_fix(userid)), xmlnode_pool(store)));

    if(node == NULL)
    {
        free(key);
        return 1;
    }

    xmlnode_hide(node);

    node = xmlnode_get_tag(store, "item");

    if(node == NULL)
    {
        log_debug(NAME, "[%s] Removing empty entry for jid (%s)", FZONE, jid_full(jid_user(jid_fix(userid))));
	htb_zap(&room->roster, key);
        xmlnode_free(store);
    }
    else
    {
        log_debug(NAME, "[%s] Removing entry (%s) for jid (%s)", FZONE, jid_full(jid_fix(userid)), jid_full(jid_user(jid_fix(userid))));

        htb_put(&room->roster, key, store);
    }

    free(key);
    return 1;
}

/* Get the entries from the room roster hash */
xmlnode get_roster(cnr room, jid userid)
{
    xmlnode store;
    char *key;

    if(room == NULL || userid == NULL)
    {
        return NULL;
    }

    key = j_strdup(jid_full(jid_user(jid_fix(userid))));
    store = htb_get(&room->roster, key);
    free(key);

    return store;
}

char *extractAction(char *origin, pool p)
{
    int i;
    int end;
    spool sp;
    char *output;
    char in[2];

    if(origin == NULL || p == NULL)
    {
	return NULL;
    }

    sp = spool_new(p);

    end = j_strlen(origin);

    for (i = 3 ; i <= end ; i++)
    {
	in[0] = origin[i];
	in[1] = '\0';

	log_debug(NAME, "[%s] >%s< saved", FZONE, in);
	spooler(sp, in, sp);
    }
    output = spool_print(sp);

    return output;
}

/* Check Primeness for hash functions */
int isPrime(unsigned long n)
{
    int prime = 1;
    unsigned long p1,p2 , s;

    if(n > 3)
    {
        p1 = 3;
        p2 = n - 3;
        s = 9;

        while((prime = p2 % p1 ) && (s <= p2))
	{
            p1 += 2;
            p2 -= 2;
            s <<= 2;
            s++;
        }
    }
    return prime;
}

/* Used to check jids and fix case. */
jid jid_fix(jid id)
{
    unsigned char *str;

    if(id == NULL)
    {
        log_debug(NAME, "[%s] ERR - id NULL", FZONE);
        return NULL;
    }

    if(id->server == NULL || j_strlen(id->server) == 0 || j_strlen(id->server) > 255)
        return NULL;

    /* lowercase the hostname, make sure it's valid characters */
    for(str = id->server; *str != '\0'; str++)
    {
        *str = tolower(*str);
    }

    /* cut off the user */
    //if(id->user != NULL && j_strlen(id->user) > 64)
    //    id->user[64] = '\0';

    /* check for low and invalid ascii characters in the username */
    //if(id->user != NULL)
    //    for(str = id->user; *str != '\0'; str++)
    //	{
    //        *str = tolower(*str);
    //	}

    return id;
}

⌨️ 快捷键说明

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