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_unsubscribe(struct stream_s *stream,const char *from,const char *to){ User *u; Session *s; Contact *c; uin_t uin; u=user_get_by_jid(from); if (!u){ g_warning(N_("Presence subscription from unknown user (%s)"),from); presence_send_unsubscribed(stream,to,from); return -1; } if (jid_is_me(to)){ debug(L_("Presence unsubscribe request sent to me")); if (u->subscribe==SUB_TO || u->subscribe==SUB_UNDEFINED) u->subscribe=SUB_NONE; else if (u->subscribe==SUB_BOTH) u->subscribe=SUB_FROM; user_save(u); return 0; } 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_("Unsubscribing %s from %s..."),from,to); uin=jid_get_uin(to); c=user_get_contact(u,uin,FALSE); if (!c) { presence_send_unsubscribed(stream,to,from); return -1; } if (c->subscribe==SUB_TO) c->subscribe=SUB_NONE; else if (c->subscribe==SUB_BOTH) c->subscribe=SUB_FROM; user_save(u); if (s) session_update_contact(s,c); debug(L_("Unsubscribed.")); presence_send_unsubscribed(stream,to,from); if (!GG_S_NA(c->status) && c->status!=-1){ char *ujid; ujid=jid_build_full(uin); presence_send(stream,ujid,u->jid,0,NULL,"Unsubscribed",0); g_free(ujid); } 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 presence_probe(struct stream_s *stream,const char *from,const char *to){ Session *s; User *u; Contact *c; uin_t uin; int status; int available; char *show,*stat,*jid; GList *it; GTime timestamp; s=session_get_by_jid(from,NULL,0); if (jid_is_me(to)){ if (s){ if (!s->connected){ presence_send(stream,to,from,0,NULL,"Disconnected",0); } else{ Resource *r=session_get_cur_resource(s); if (r) presence_send(stream,NULL,s->user->jid,s->user->invisible?-1:r->available, r->show,r->status,0); } return 0; } else if (user_get_by_jid(from)) { presence_send(stream, to, from, 0, NULL, "Not logged in", 0); } else { presence_send_unsubscribed(stream, NULL, from); } return 0; } if (!jid_is_my(to)){ presence_send_unsubscribed(stream,to,from); return -1; } if (s) u=s->user; else u=user_get_by_jid(from); if (!u){ presence_send_unsubscribed(stream,to,from); return -1; } uin=jid_get_uin(to); /* create the contact: if we got 'prope' the user has it on his * contact list, do not change that */ c = user_get_contact(u, uin, TRUE); if (!c) { return -1; } c->got_probe=TRUE; if (s) session_update_contact(s,c); status=0; stat=NULL; timestamp=0; for(it=u->contacts;it;it=it->next){ Contact *c=(Contact *)it->data; if (c && c->uin==uin){ status=c->status; timestamp=c->last_update; stat=c->status_desc; } } if (!status){ // user not found on userlist? presence_send_unsubscribed(stream,to,from); return -1; } if (status==-1) return 0; /* Not known yet */ available=status_gg_to_jabber(status,&show,&stat); if (available) jid=jid_build_full(uin); else jid=jid_build(uin); presence_send(stream,jid,u->jid,available,show,stat,timestamp); g_free(jid); return 0; }