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; }
Session *session_get_by_jid(const char *jid,Stream *stream,int delay_login){ Session *s; User *u; char *njid; GList *it; g_assert(sessions_jid!=NULL); debug(L_("Looking up session for '%s'"),jid); njid=jid_normalized(jid,0); if (njid==NULL){ g_message(L_("Bad JID: '%s'"),jid); return NULL; } debug(L_("Using '%s' as key"),njid); s=(Session *)g_hash_table_lookup(sessions_jid,(gpointer)njid); g_free(njid); if (s) return s; debug(L_("Session not found")); if (!stream) return NULL; u=user_get_by_jid(jid); if (!u) return NULL; debug(L_("User loaded, processing his subscriptions.")); for(it=g_list_first(u->contacts);it;it=g_list_next(it)){ Contact *c; char *c_jid; c=(Contact *)it->data; if (c->subscribe == SUB_UNDEFINED) { c_jid=jid_build(c->uin); presence_send_subscribe(stream,c_jid,u->jid); g_free(c_jid); } else if (c->subscribe == SUB_FROM || c->subscribe == SUB_BOTH){ c_jid=jid_build(c->uin); presence_send_probe(stream,c_jid,u->jid); g_free(c_jid); } } debug(L_("Creating new session")); return session_create(u,jid,NULL,NULL,stream,delay_login); }