int presence_subscribe(struct stream_s *stream,const char *from,const char *to){ User *u; Session *s; char *bare; uin_t uin; Contact *c; u=user_get_by_jid(from); if (jid_is_me(to)){ debug(L_("Presence subscribe request sent to me")); if (!u) { presence_send_unsubscribed(stream,to,from); return 0; } presence_send_subscribed(stream,to,from); if (u->subscribe==SUB_UNDEFINED || u->subscribe==SUB_NONE) u->subscribe=SUB_TO; else if (u->subscribe==SUB_FROM) u->subscribe=SUB_BOTH; if (u->subscribe!=SUB_FROM && u->subscribe!=SUB_BOTH){ presence_send_subscribe(stream,to,from); } user_save(u); return 0; } if (!u){ g_warning(N_("Presence subscription from unknown user (%s)"),from); presence_send_unsubscribed(stream,to,from); return -1; } if (!jid_has_uin(to) || !jid_is_my(to)){ g_warning(N_("Bad 'to': %s"),to); return -1; } s=session_get_by_jid(from,stream,0); debug(L_("Subscribing %s to %s..."),from,to); uin=jid_get_uin(to); c=user_get_contact(u,uin,TRUE); if (!c) { presence_send_error(stream,to,from,500,_("Subscription failed")); return -1; } if (c->subscribe==SUB_UNDEFINED || c->subscribe==SUB_NONE) c->subscribe=SUB_TO; else if (c->subscribe==SUB_FROM) c->subscribe=SUB_BOTH; user_save(u); if (s) session_update_contact(s,c); debug(L_("Subscribed.")); presence_send_subscribed(stream,to,from); bare=jid_normalized(from,FALSE); if (c->subscribe!=SUB_FROM && c->subscribe!=SUB_BOTH) { presence_send_subscribe(stream,to,bare); } g_free(bare); return 0; }
int presence(struct stream_s *stream,const char *from,const char *to, int available,const char *show,const char *status,int priority){ Session *s; const char *resource; User *u; s=session_get_by_jid(from,available?stream:NULL,1); if (!s){ debug(L_("presence: No such session: %s"),from); presence_send_error(stream,NULL,from,407,_("Not logged in")); u=user_get_by_jid(from); if (u==NULL) presence_send_unsubscribed(stream,to,from); return -1; } resource=jid_get_resource(from); session_set_status(s,resource,available,show,string_to_gg(status),priority); return 0; }
int jabber_presence(struct stream_s *stream,xmlnode tag){ char *type; char *from; char *to; xmlnode prio_n; xmlnode show_n; xmlnode status_n; char *show,*status; int priority; char *tmp; User *u; type=xmlnode_get_attrib(tag,"type"); from=xmlnode_get_attrib(tag,"from"); to=xmlnode_get_attrib(tag,"to"); if (from) u=user_get_by_jid(from); else u=NULL; user_load_locale(u); if (!acl_is_allowed(from,tag)){ if (type && !strcmp(type,"error")){ debug("Ignoring forbidden presence error"); return -1; } if (!from) return -1; presence_send_error(stream,to,from,405,_("Not allowed")); return -1; } show_n=xmlnode_get_tag(tag,"show"); if (show_n) show=xmlnode_get_data(show_n); else show=NULL; status_n=xmlnode_get_tag(tag,"status"); if (status_n) status=xmlnode_get_data(status_n); else status=NULL; prio_n=xmlnode_get_tag(tag,"priority"); if (prio_n){ tmp=xmlnode_get_data(prio_n); if (tmp) priority=atoi(tmp); else priority=-1; } else priority=-1; if (!type) type="available"; if (!from || !to){ if (strcmp(type,"error")) presence_send_error(stream,to,from,406,_("Not Acceptable")); g_warning(N_("Bad <presence/>: %s"),xmlnode2str(tag)); return -1; } if (!jid_is_my(to)){ if (strcmp(type,"error")) presence_send_error(stream,to,from,406,_("Not Acceptable")); g_warning(N_("Wrong 'to' in %s"),xmlnode2str(tag)); return -1; } if (!strcmp(type,"available")){ if (jid_has_uin(to)) return presence_direct_available(stream,from,to); else return presence(stream,from,to,1,show,status,priority); } else if (!strcmp(type,"unavailable")){ if (jid_has_uin(to)) return presence_direct_unavailable(stream,from,to); else return presence(stream,from,to,0,show,status,priority); } if (!strcmp(type,"invisible")){ if (jid_has_uin(to)) return presence_direct_unavailable(stream,from,to); else return presence(stream,from,to,-1,show,status,priority); } else if (!strcmp(type,"subscribe")) return presence_subscribe(stream,from,to); else if (!strcmp(type,"unsubscribe")) return presence_unsubscribe(stream,from,to); else if (!strcmp(type,"subscribed")) return presence_subscribed(stream,from,to); else if (!strcmp(type,"unsubscribed")) return presence_unsubscribed(stream,from,to); else if (!strcmp(type,"probe")) return presence_probe(stream,from,to); else if (!strcmp(type,"error")){ g_warning(N_("Error presence received: %s"),xmlnode2str(tag)); return 0; } g_warning(N_("Unsupported type in %s"),xmlnode2str(tag)); presence_send_error(stream,to,from,501,_("Not Implemented")); return -1; }