📄 xjab_jcon.c
字号:
*/int xj_jcon_send_msg(xj_jcon jbc, char *to, int tol, char *msg, int msgl, int type){ char msg_buff[4096], *p; int n; xode x; if(jbc == NULL) return -1; x = xode_new_tag("body"); if(!x) return -1; xode_insert_cdata(x, msg, msgl); x = xode_wrap(x, "message"); strncpy(msg_buff, to, tol); msg_buff[tol] = 0; xode_put_attrib(x, "to", msg_buff); switch(type) { case XJ_JMSG_CHAT: xode_put_attrib(x, "type", "chat"); break; case XJ_JMSG_GROUPCHAT: xode_put_attrib(x, "type", "groupchat"); break; default: xode_put_attrib(x, "type", "normal"); } p = xode_to_str(x); n = strlen(p);#ifdef XJ_EXTRA_DEBUG DBG("XJAB:xj_jcon_send_msg: jabber msg:\n%s\n", p);#endif if(send(jbc->sock, p, n, 0) != n) { DBG("XJAB:xj_jcon_send_msg: error - message not sent\n"); goto error; } xode_free(x); return 0;error: xode_free(x); return -1;}/** * receive a message from a JABBER connection * NOT USED */int xj_jcon_recv_msg(xj_jcon jbc, char *from, char *msg){ //char msg_buff[4096]; //sprintf(msg_buff, JB_MSG_NORMAL, to, msg); //send(jbc->sock, msg_buff, strlen(msg_buff), 0); return 0;}/** * send presence * type - "unavailable", "subscribe", "subscribed" .... * status - "online", "away", "unavailable" ... * priority - "0", "1", ... */int xj_jcon_send_presence(xj_jcon jbc, char *sto, char *type, char *status, char *priority){ char *p; int n; xode x, y; if(jbc == NULL) return -1;#ifdef XJ_EXTRA_DEBUG DBG("XJAB:xj_jcon_send_presence: -----START-----\n");#endif x = xode_new_tag("presence"); if(!x) return -1; if(sto != NULL) xode_put_attrib(x, "to", sto); if(type != NULL) xode_put_attrib(x, "type", type); if(status != NULL) { y = xode_insert_tag(x, "status"); xode_insert_cdata(y, status, strlen(status)); } if(priority != NULL) { y = xode_insert_tag(x, "priority"); xode_insert_cdata(y, priority, strlen(priority)); } p = xode_to_str(x); n = strlen(p); if(send(jbc->sock, p, n, 0) != n) { DBG("XJAB:xj_jcon_send_presence: Error - presence not sent\n"); goto error; } xode_free(x);#ifdef XJ_EXTRA_DEBUG DBG("XJAB:xj_jcon_send_presence: presence status was sent\n");#endif return 0;error: xode_free(x); return -1;}/** * send subscribe for user's presence */int xj_jcon_send_subscribe(xj_jcon jbc, char *to, char *from, char *type){ char *p; int n; xode x; if(!jbc || !to) return -1; x = xode_new_tag("presence"); if(!x) return -1; xode_put_attrib(x, "to", to); if(from != NULL) xode_put_attrib(x, "from", from); if(type != NULL) xode_put_attrib(x, "type", type); p = xode_to_str(x); n = strlen(p); if(send(jbc->sock, p, n, 0) != n) { DBG("XJAB:xj_jcon_send_subscribe: Error - subscribe not sent\n"); goto error; } xode_free(x); return 0;error: xode_free(x); return -1;}/** * free the allocated memory space of a JABBER connection */int xj_jcon_free(xj_jcon jbc){ xj_jconf jcf; if(jbc == NULL) return -1;#ifdef XJ_EXTRA_DEBUG DBG("XJAB:xj_jcon_free: -----START-----\n");#endif //if(jbc->sock != -1) // jb_disconnect(jbc); if(jbc->hostname != NULL) _M_FREE(jbc->hostname); if(jbc->stream_id != NULL) _M_FREE(jbc->stream_id); if(jbc->resource != NULL) _M_FREE(jbc->resource);#ifdef XJ_EXTRA_DEBUG DBG("XJAB:xj_jcon_free: %d conferences\n", jbc->nrjconf);#endif while(jbc->nrjconf > 0) { if((jcf=delpos234(jbc->jconf,0))!=NULL) xj_jconf_free(jcf); jbc->nrjconf--; } xj_pres_list_free(jbc->plist); _M_FREE(jbc);#ifdef XJ_EXTRA_DEBUG DBG("XJAB:xj_jcon_free: -----END-----\n");#endif return 0;}/** * create a open connection to Jabber * - id : id of the connection * - jbc : pointer to Jabber connection * - cache_time : life time of the connection * - delay_time : time needed to became an active connection * #return : pointer to the structure or NULL on error */int xj_jcon_set_attrs(xj_jcon jbc, xj_jkey jkey, int cache_time, int delay_time){ int t; if(jbc==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL) return -1; jbc->jkey = jkey; t = get_ticks(); jbc->expire = t + cache_time; jbc->ready = t + delay_time; return 0;}/** * update the life time of the connection * - ojc : pointer to the open connection * - cache_time : number of seconds to keep the connection open * #return : 0 on success or <0 on error */int xj_jcon_update(xj_jcon jbc, int cache_time){ if(jbc == NULL) return -1;#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_update [%.*s] %d\n", jbc->jkey->id->len, jbc->jkey->id->s, cache_time);#endif jbc->expire = get_ticks() + cache_time; return 0; }int xj_jcon_is_ready(xj_jcon jbc, char *to, int tol, char dl){ char *p; str sto; xj_jconf jcf = NULL; if(!jbc || !to || tol <= 0) return -1; sto.s = to; sto.len = tol; if(!xj_jconf_check_addr(&sto, dl)) {#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_is_ready: destination=conference\n");#endif if((jcf=xj_jcon_get_jconf(jbc, &sto, dl))!=NULL) return (jcf->status & XJ_JCONF_READY)?0:3;#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_is_ready: conference does not exist\n");#endif return -1; } p = to; while(p < to+tol && *p!='@') p++; if(p>=to+tol) return -1; p++; if(!strncasecmp(p, XJ_AIM_NAME, XJ_AIM_LEN)) return (jbc->ready & XJ_NET_AIM)?0:((jbc->allowed & XJ_NET_AIM)?1:2); if(!strncasecmp(p, XJ_ICQ_NAME, XJ_ICQ_LEN)) return (jbc->ready & XJ_NET_ICQ)?0:((jbc->allowed & XJ_NET_ICQ)?1:2); if(!strncasecmp(p, XJ_MSN_NAME, XJ_MSN_LEN)) return (jbc->ready & XJ_NET_MSN)?0:((jbc->allowed & XJ_NET_MSN)?1:2); if(!strncasecmp(p, XJ_YAH_NAME, XJ_YAH_LEN)) return (jbc->ready & XJ_NET_YAH)?0:((jbc->allowed & XJ_NET_YAH)?1:2);#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_is_ready: destination=jabber\n");#endif return 0;}xj_jconf xj_jcon_get_jconf(xj_jcon jbc, str* sid, char dl){ xj_jconf jcf = NULL, p; if(!jbc || !sid || !sid->s || sid->len <= 0) return NULL;#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_get_jconf: looking for conference\n"); #endif if((jcf = xj_jconf_new(sid))==NULL) return NULL; if(xj_jconf_init_sip(jcf, jbc->jkey->id, dl)) goto clean; if(jbc->nrjconf && (p = find234(jbc->jconf, (void*)jcf, NULL)) != NULL) {#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_get_jconf: conference found\n");#endif xj_jconf_free(jcf); return p; } if(jbc->nrjconf >= XJ_MAX_JCONF) goto clean; if(jbc->nrjconf==0) if(jbc->jconf==NULL) if((jbc->jconf = newtree234(xj_jconf_cmp)) == NULL) goto clean; if((p = add234(jbc->jconf, (void*)jcf)) != NULL) {#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_get_jconf: new conference created\n");#endif jbc->nrjconf++; return p; }clean: DBG("XJAB: xj_jcon_get_jconf: error looking for conference\n"); xj_jconf_free(jcf); return NULL;}xj_jconf xj_jcon_check_jconf(xj_jcon jbc, char* id){ str sid; xj_jconf jcf = NULL, p = NULL; if(!jbc || !id || !jbc->nrjconf) return NULL;#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_get_jconf: looking for conference\n"); #endif sid.s = id; sid.len = strlen(id); if((jcf = xj_jconf_new(&sid))==NULL) return NULL; if(xj_jconf_init_jab(jcf)) goto clean; if((p = find234(jbc->jconf, (void*)jcf, NULL)) != NULL) {#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_get_jconf: conference found\n");#endif xj_jconf_free(jcf); return p; }clean:#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_get_jconf: conference not found\n");#endif xj_jconf_free(jcf); return NULL; }int xj_jcon_jconf_presence(xj_jcon jbc, xj_jconf jcf, char* type, char* status){ char buff[256]; strncpy(buff, jcf->room.s, jcf->room.len + jcf->server.len +1); buff[jcf->room.len + jcf->server.len +1] = '/'; buff[jcf->room.len + jcf->server.len +2] = 0; buff[jcf->room.len] = '@'; strncat(buff, jcf->nick.s, jcf->nick.len); return xj_jcon_send_presence(jbc,buff,type,status,NULL);}int xj_jcon_del_jconf(xj_jcon jbc, str *sid, char dl, int flag){ xj_jconf jcf = NULL, p = NULL; if(!jbc || !sid || !sid->s || sid->len <= 0) return -1;#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_del_jconf: deleting conference of <%.*s>\n", sid->len, sid->s);#endif if((jcf = xj_jconf_new(sid))==NULL) return -1; if(xj_jconf_init_sip(jcf, jbc->jkey->id, dl)) { xj_jconf_free(jcf); return -1; } p = del234(jbc->jconf, (void*)jcf); if(p != NULL) { if(flag == XJ_JCMD_UNSUBSCRIBE) xj_jcon_jconf_presence(jbc, jcf, "unavailable", NULL); jbc->nrjconf--; xj_jconf_free(p);#ifdef XJ_EXTRA_DEBUG DBG("XJAB: xj_jcon_del_jconf: conference deleted\n");#endif } xj_jconf_free(jcf); return 0;}/********** *********/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -