📄 mod_roster.c
字号:
/* -------------------------------------------------------------------------- * * License * * The contents of this file are subject to the Jabber Open Source License * Version 1.0 (the "JOSL"). You may not copy or use this file, in either * source code or executable form, except in compliance with the JOSL. You * may obtain a copy of the JOSL at http://www.jabber.org/ or at * http://www.opensource.org/. * * Software distributed under the JOSL is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL * for the specific language governing rights and limitations under the * JOSL. * * Copyrights * * Portions created by or assigned to Jabber.com, Inc. are * Copyright (c) 1999-2002 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. * * Alternatively, the contents of this file may be used under the terms of the * GNU General Public License Version 2 or later (the "GPL"), in which case * the provisions of the GPL are applicable instead of those above. If you * wish to allow use of your version of this file only under the terms of the * GPL and not to allow others to use your version of this file under the JOSL, * indicate your decision by deleting the provisions above and replace them * with the notice and other provisions required by the GPL. If you do not * delete the provisions above, a recipient may use your version of this file * under either the JOSL or the GPL. * * * --------------------------------------------------------------------------*/#include "jsm.h"xmlnode mod_roster_get(udata u){ xmlnode ret; log_debug("mod_roster","getting %s's roster",u->user); /* get the existing roster */ ret = xdb_get(u->si->xc, u->id, NS_ROSTER); if(ret == NULL) { /* there isn't one, sucky, create a container node and let xdb manage it */ log_debug("mod_roster","creating"); ret = xmlnode_new_tag("query"); xmlnode_put_attrib(ret,"xmlns",NS_ROSTER); } return ret;}xmlnode mod_roster_get_item(xmlnode roster, jid id, char *name, int *newflag){ xmlnode ret; log_debug("mod_roster","getting item %s",jid_full(id)); ret = jid_nodescan(id,roster); if(ret == NULL) { /* there isn't one, brew one up */ log_debug("mod_roster","creating"); ret = xmlnode_insert_tag(roster,"item"); xmlnode_put_attrib(ret,"jid",jid_full(id)); if(name != NULL) xmlnode_put_attrib(ret,"name",name); xmlnode_put_attrib(ret,"subscription","none"); *newflag = 1; } return ret;}void mod_roster_push(udata user, xmlnode item){ /* push the item to all session */ session cur; xmlnode packet, query; log_debug("mod_roster","pushing %s",xmlnode2str(item)); if(xmlnode_get_attrib(item,"hidden") != NULL) return; /* create a jpacket roster item push */ packet = xmlnode_new_tag("iq"); xmlnode_put_attrib(packet, "type", "set"); query = xmlnode_insert_tag(packet, "query"); xmlnode_put_attrib(query,"xmlns",NS_ROSTER); xmlnode_insert_tag_node(query,item); xmlnode_hide_attrib(xmlnode_get_firstchild(query),"subscribe"); /* hide the server tirds */ /* send a copy to all session that have a roster */ for(cur = user->sessions; cur != NULL; cur = cur->next) if(cur->roster) js_session_to(cur, jpacket_new(xmlnode_dup(packet))); xmlnode_free(packet);}#define S10N_ADD_FROM 1#define S10N_ADD_TO 2#define S10N_REM_FROM 3#define S10N_REM_TO 4void mod_roster_set_s10n(int set, xmlnode item){ switch(set) { /* LAZY ALERT, yeah, redundant code, gak! */ case S10N_ADD_FROM: if(j_strcmp(xmlnode_get_attrib(item,"subscription"),"to") == 0 || j_strcmp(xmlnode_get_attrib(item,"subscription"),"both") == 0) xmlnode_put_attrib(item,"subscription","both"); else xmlnode_put_attrib(item,"subscription","from"); break; case S10N_ADD_TO: if(j_strcmp(xmlnode_get_attrib(item,"subscription"),"from") == 0 || j_strcmp(xmlnode_get_attrib(item,"subscription"),"both") == 0) xmlnode_put_attrib(item,"subscription","both"); else xmlnode_put_attrib(item,"subscription","to"); break; case S10N_REM_FROM: if(j_strcmp(xmlnode_get_attrib(item,"subscription"),"both") == 0 || j_strcmp(xmlnode_get_attrib(item,"subscription"),"to") == 0) xmlnode_put_attrib(item,"subscription","to"); else xmlnode_put_attrib(item,"subscription","none"); break; case S10N_REM_TO: if(j_strcmp(xmlnode_get_attrib(item,"subscription"),"both") == 0 || j_strcmp(xmlnode_get_attrib(item,"subscription"),"from") == 0) xmlnode_put_attrib(item,"subscription","from"); else xmlnode_put_attrib(item,"subscription","none"); break; }}/* force presence updates through all sessions, thanks to 'smarter' presence logic */void mod_roster_pforce(udata u, jid to, int uflag){ session s; xmlnode x; log_debug(ZONE,"brute forcing presence updates"); /* loop through all the sessions */ for(s = u->sessions; s != NULL; s = s->next) { if(uflag) x = jutil_presnew(JPACKET__UNAVAILABLE,NULL,NULL); else x = xmlnode_dup(s->presence); xmlnode_put_attrib(x,"to",jid_full(to)); js_session_from(s,jpacket_new(x)); }}mreturn mod_roster_out_s10n(mapi m){ xmlnode roster, item; int newflag, to, from; jid curr; if(m->packet->to == NULL) return M_PASS; if(jid_cmpx(jid_user(m->s->id),m->packet->to,JID_USER|JID_SERVER) == 0) return M_PASS; /* vanity complex */ log_debug("mod_roster","handling outgoing s10n"); newflag = to = from = 0; roster = mod_roster_get(m->user); item = mod_roster_get_item(roster,m->packet->to,NULL,&newflag); /* vars */ if(j_strcmp(xmlnode_get_attrib(item,"subscription"),"to") == 0) to = 1; if(j_strcmp(xmlnode_get_attrib(item,"subscription"),"from") == 0) from = 1; if(j_strcmp(xmlnode_get_attrib(item,"subscription"),"both") == 0) to = from = 1; switch(jpacket_subtype(m->packet)) { case JPACKET__SUBSCRIBE: if(!to) { xmlnode_put_attrib(item,"ask","subscribe"); mod_roster_push(m->user, item); } break; case JPACKET__SUBSCRIBED: mod_roster_set_s10n(S10N_ADD_FROM,item); /* update subscription */ jid_append(js_trustees(m->user),m->packet->to); /* make them trusted now */ xmlnode_hide_attrib(item,"subscribe"); /* cancel any pending requests */ xmlnode_hide_attrib(item,"hidden"); /* don't hide it anymore */ mod_roster_pforce(m->user, m->packet->to, 0); /* they are now subscribed to us, send them our presence */ mod_roster_push(m->user, item); break; case JPACKET__UNSUBSCRIBE: if(to) { xmlnode_put_attrib(item,"ask","unsubscribe"); mod_roster_push(m->user, item); }else if(newflag){ xmlnode_hide(item); } break; case JPACKET__UNSUBSCRIBED: if(from) { mod_roster_set_s10n(S10N_REM_FROM,item); /* update subscription */ /* remove them from the user trusted list */ for(curr = js_trustees(m->user);curr != NULL && jid_cmp(curr->next,m->packet->to) != 0;curr = curr->next); if(curr != NULL && curr->next != NULL) curr->next = curr->next->next; mod_roster_pforce(m->user, m->packet->to, 1); /* make us offline */ mod_roster_push(m->user, item); }else if(newflag){ xmlnode_hide(item); }else{ if(xmlnode_get_attrib(item,"hidden") != NULL) xmlnode_hide(item); /* remove it for good */ else xmlnode_hide_attrib(item,"subscribe"); /* just cancel any pending requests */ } break; } /* save the roster */ /* XXX what do we do if the set fails? hrmf... */ xdb_set(m->si->xc, m->user->id, NS_ROSTER, roster); /* make sure it's sent from the *user*, not the resource */ xmlnode_put_attrib(m->packet->x,"from",jid_full(jid_user(m->s->id))); jpacket_reset(m->packet); xmlnode_free(roster); return M_PASS;}mreturn mod_roster_out_iq(mapi m){ xmlnode roster, cur, pres, item; int newflag; jid id; if(!NSCHECK(m->packet->iq,NS_ROSTER)) return M_PASS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -