Esempio n. 1
0
/* destroys Session object */
static int session_destroy(Session *s){
GList *it;

	g_message(L_("Deleting session for '%s'"),s->jid);
	if (s->ping_timeout_func) g_source_remove(s->ping_timeout_func);
	if (s->timeout_func) g_source_remove(s->timeout_func);
	if (s->ping_timer) g_timer_destroy(s->ping_timer);
	session_remove_g_source(s);
	while(s->resources) resource_remove((Resource *)s->resources->data,0);
	if (s->ggs){
		if (s->connected){
			debug("gg_logoff(%p)",s->ggs);
			gg_logoff(s->ggs);
		}
		gg_free_session(s->ggs);
		s->ggs=NULL;
	}
	if (s->connected && s->s && s->jid){
		for(it=g_list_first(s->user->contacts);it;it=g_list_next(it)){
			Contact *c=(Contact *)it->data;

			if (!GG_S_NA(c->status) && c->status!=-1){
				char *ujid;
				ujid=jid_build_full(c->uin);
				presence_send(s->s,ujid,s->user->jid,0,NULL,NULL,0);
				g_free(ujid);
			}
		}
	}
	if (s->query) xmlnode_free(s->query);
	if (s->user) user_unref(s->user);
	g_free(s->gg_status_descr);
	g_free(s);
	return 0;
}
Esempio n. 2
0
int session_event_status(Session *s,int status,uin_t uin,char *desc,
				int more,uint32_t ip,uint16_t port,uint32_t version){
int oldstat;
int available;
char *ujid;
char *show;
Contact *c;

	c=user_get_contact(s->user,uin,FALSE);
	if (c==NULL) {
/*		debug(L_("%s got notification from unknown contact %i. Converting to subscription request."),s->user->jid,uin);
		ujid=jid_build(uin);
		presence_send_subscribe(s->s,ujid,s->user->jid);
		g_free(ujid);
*/		debug(L_("%s got notification from unknown contact %i."),s->user->jid,uin);
	       	return 0;
	}
	if (!c->got_probe && c->subscribe!=SUB_TO && c->subscribe!=SUB_BOTH) {
		debug(L_("%s got notification from contact %i, whose presence was not requested. Ignoring."),s->user->jid,uin);
	       	return 0;
	}

	available=status_gg_to_jabber(status,&show,&desc);
	oldstat=user_get_contact_status(s->user,uin);
	user_set_contact_status(s->user,status,uin,desc,more,ip,port,version);

	if (!available && oldstat==-1)
		ujid=jid_build(uin);
	else
		ujid=jid_build_full(uin);

	presence_send(s->s,ujid,s->user->jid,available,show,desc,0);
	g_free(ujid);
	return 0;
}
Esempio n. 3
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;
}
Esempio n. 4
0
void session_broken(Session *s){

	if (s->req_id){
		jabber_iq_send_error(s->s,s->jid,NULL,s->req_id,502,_("Remote Server Error"));
	}
	else{
		GList *it;
		presence_send(s->s,NULL,s->user->jid,0,NULL,"Connection broken",0);
		for(it=g_list_first(s->user->contacts);it;it=g_list_next(it)){
			Contact *c=(Contact *)it->data;

			if (!GG_S_NA(c->status) && c->status!=-1){
				char *ujid;
				ujid=jid_build_full(c->uin);
				presence_send(s->s,ujid,s->user->jid,0,NULL,"Transport disconnected",0);
				g_free(ujid);
			}
		}
	}
	s->connected=0;
	session_schedule_reconnect(s);
	session_remove(s);
}
Esempio n. 5
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;
}
Esempio n. 6
0
int session_io_handler(Session *s){
struct gg_event *event;
char *jid,*str;
int chat;
GIOCondition condition=s->g_pollfd.revents;
time_t timestamp;
gboolean state;

	user_load_locale(s->user);
	debug(L_("Checking error conditions..."));
	if (condition&(G_IO_ERR|G_IO_NVAL)){
		if (condition&G_IO_ERR) g_warning(N_("Error on connection for %s, GGid: %i"),s->jid,s->ggs->uin);
		if (condition&G_IO_HUP){
			g_warning(N_("Hangup on connection for %s, GGid: %i"),s->jid,s->ggs->uin);
			return session_error(s);
		}
		if (condition&G_IO_NVAL) g_warning(N_("Invalid channel on connection for %s"),s->jid);

		session_broken(s);
		return FALSE;
	}

	debug(L_("watching fd (gg_debug_level=%i)..."),gg_debug_level);
	event=gg_watch_fd(s->ggs);
	if (!event){
		g_warning(N_("Connection broken. Session of %s, GGid: %i"),s->jid,s->ggs->uin);
		return session_error(s);
	}

	switch(event->type){
		case GG_EVENT_DISCONNECT:
			g_warning(N_("Server closed connection of %s, GGid: %i"),s->jid,s->ggs->uin);
			session_error(s);
			gg_event_free(event);
			return FALSE;
		case GG_EVENT_CONN_FAILED:
			g_message(N_("Login failed (%d:%s) for %s, GGid: %i"),
					event->event.failure,
					(event->event.failure>GG_FAILURE_NUM_REASONS||event->event.failure<1)?"-UNKNOWN-":gg_failure_reason[event->event.failure-1],
					s->jid,
					s->ggs->uin);
			if (s->req_id)
				jabber_iq_send_error(s->s,s->jid,NULL,s->req_id,401,_("Unauthorized"));
			else {
				str=g_strdup(from_utf8(gg_failure_reason_txt[event->event.failure-1]));
				presence_send(s->s,NULL,s->user->jid,0,NULL,str,0);
				g_free(str);
			}
			state = FALSE;
			if (!s->req_id)
				switch(event->event.failure){
					case GG_FAILURE_RESOLVING:
					case GG_FAILURE_CONNECTING:
					case GG_FAILURE_INVALID:
					case GG_FAILURE_READING:
					case GG_FAILURE_WRITING:
					case GG_FAILURE_TLS:
						state = session_try_next(s);
					default:
						break;
				}
			if (state) {
				s->connected=0;
				session_schedule_reconnect(s);
			} else
				session_remove(s);
			gg_event_free(event);
			return FALSE;
		case GG_EVENT_CONN_SUCCESS:
			g_message(L_("Login succeed for %s, GGid: %i"),s->jid,s->ggs->uin);
			if (s->req_id)
				jabber_iq_send_result(s->s,s->jid,NULL,s->req_id,NULL);
			if (s->req_id){
				g_free(s->req_id);
				s->req_id=NULL;
			}
			if (s->query){
				xmlnode_free(s->query);
				s->query=NULL;
			}
			if (!s->user->confirmed){
				s->user->confirmed=1;
				user_save(s->user);
			}
			s->connected=1;
			session_send_status(s);
			session_send_notify(s);
			presence_send(s->s,NULL,s->user->jid,s->user->invisible?-1:1,NULL,s->gg_status_descr,0);

			if (s->timeout_func) g_source_remove(s->timeout_func);
			s->timeout_func=NULL;
			if (s->ping_timeout_func) g_source_remove(s->ping_timeout_func);
			s->ping_timeout_func=g_timeout_add(ping_interval*1000,session_ping,s);
			if (s->pubdir_change){
				add_request(RT_CHANGE,s->jid,NULL,s->req_id,
							NULL,s->pubdir_change,s->s);
				gg_pubdir50_free(s->pubdir_change);
				s->pubdir_change=NULL;
			}
			if (s->get_roster){
				gg_userlist_request(s->ggs, GG_USERLIST_GET, NULL);
			}
			break;
		case GG_EVENT_NOTIFY:
			session_event_notify(s,event);
			break;
		case GG_EVENT_NOTIFY_DESCR:
			session_event_notify_descr(s,event);
			break;
		case GG_EVENT_NOTIFY60:
			session_event_notify60(s,event);
			break;
		case GG_EVENT_STATUS:
			session_event_status(s,
					event->event.status.status,
					event->event.status.uin,
					event->event.status.descr,
					0,0,0,0);
			break;
		case GG_EVENT_STATUS60:
			session_event_status(s,
					event->event.status60.status,
					event->event.status60.uin,
					event->event.status60.descr,
					1,
					event->event.status60.remote_ip,
					event->event.status60.remote_port,
					event->event.status60.version);
			break;
		case GG_EVENT_MSG:
			if (event->event.msg.recipients_count>1){
				debug(L_("Dropped conference message: sender: %i class: %i time: %lu"),
							event->event.msg.sender,
							event->event.msg.msgclass,
							(unsigned long)event->event.msg.time);
				break;
			}
			gg_messages_in++;
			debug(L_("Message: sender: %i class: %i time: %lu"),
							event->event.msg.sender,
							event->event.msg.msgclass,
							(unsigned long)event->event.msg.time);
			
			if (event->event.msg.sender==0){
				if (!user_sys_msg_received(s->user,event->event.msg.msgclass)) break;
				if (ignore_system_messages == ISM_IGNORE_ALL) break;
				if (ignore_system_messages == ISM_IGNORE_HTML
					&& strstr((const char *)event->event.msg.message, "<HTML>")) break;
				timestamp=event->event.msg.time;
				str=g_strdup_printf(_("GG System message #%i"),
							event->event.msg.msgclass);
				message_send_subject(s->s,jid, s->user->jid, str,
						string_from_gg((const char *)event->event.msg.message),
												timestamp);
				g_free(str);
				break;
			}
			else{
				Contact *c=user_get_contact(s->user,
						event->event.msg.sender,0);
				if ((!c && s->user->ignore_unknown) 
				    || (c && c->ignored)) {
					debug(L_("Ignoring the message."));
			       		break;
				}
				jid=jid_build_full(event->event.msg.sender);
				if ((event->event.msg.msgclass&GG_CLASS_CHAT)!=0) chat=1;
				else chat=0;
			}
			if ((event->event.msg.msgclass&GG_CLASS_QUEUED)!=0){
				timestamp=event->event.msg.time;
			}
			else timestamp=0;
			if(event->event.msg.formats_length>0)
				message_send_rich(s->s,jid,s->user->jid,chat,
						(char *)event->event.msg.message,timestamp,
						event->event.msg.formats_length,(void *)event->event.msg.formats);
			else
				message_send(s->s,jid,s->user->jid,chat,
						string_from_gg((const char *)event->event.msg.message),timestamp);
			g_free(jid);
			break;
		case GG_EVENT_PONG:
			s->waiting_for_pong=FALSE;
			if (s->ping_timer){
				g_timer_stop(s->ping_timer);
				debug(L_("Pong! ping time: %fs"),
						g_timer_elapsed(s->ping_timer,NULL));
			}
			if (s->timeout_func) g_source_remove(s->timeout_func);
			s->timeout_func=NULL;
			break;
		case GG_EVENT_PUBDIR50_SEARCH_REPLY:
			request_response_search(event);
			break;
		case GG_EVENT_PUBDIR50_WRITE:
			request_response_write(event);
			break;
		case GG_EVENT_ACK:
			debug("GG_EVENT_ACK");
			break;
		case GG_EVENT_NONE:
			debug("GG_EVENT_NONE");
			break;
		case GG_EVENT_USERLIST:
			if(event->event.userlist.type==GG_USERLIST_GET_REPLY)
				get_roster_done(s,event);
			else
				g_warning(N_("Wrong gg userlist type: %i"),event->event.userlist.type);
			break;
		default:
			g_warning(N_("Unknown GG event: %i"),event->type);
			break;
	}

	session_setup_g_source(s);

	gg_event_free(event);
	debug(L_("io handler done..."));

	return FALSE;
}