Exemple #1
0
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;
}
Exemple #2
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;
}
Exemple #3
0
void message_unignore(struct stream_s *stream,const char *from, const char *to,
				const char *args, xmlnode msg){
Session *session;
User *user;
uin_t uin;
Contact *c;
gchar *m;

	session=session_get_by_jid(from,stream,0);
	if (session!=NULL)
		user=session->user;
	else
		user=user_get_by_jid(from);

	if (args && *args) uin=atoi(args);
	else uin=0;

	if (uin<=0) {
		message_ignore(stream,from,to,NULL,msg);
		return;
	}

	c=user_get_contact(user,uin,FALSE);
	if (c) {
		c->ignored=FALSE;
		user_check_contact(user,c);
		if (session) session_update_contact(session,c);
	}
		
	m=g_strdup_printf(_("\nMessages from GG number %li will NOT be ignored."),(long)uin);
	message_send(stream,to,from,1,m,0);
	g_free(m);
	
	user_save(user);
}
Exemple #4
0
void message_ignore_unknown(struct stream_s *stream,const char *from, const char *to,
				const char *args, xmlnode msg){
Session *session;
User *user;
gboolean on;

	session=session_get_by_jid(from,stream,0);
	if (session!=NULL)
		user=session->user;
	else
		user=user_get_by_jid(from);
	if (args && g_strcasecmp(args,"on")==0) on=TRUE;
	else if (args && g_strcasecmp(args,"off")==0) on=FALSE;
	else on=!user->ignore_unknown;

	if (user->ignore_unknown==on){
		message_send(stream,to,from,1,_("No change."),0);
		return;
	}
	user->ignore_unknown=on;

	if (on)
		message_send(stream,to,from,1,_("ignore unknown: on"),0);
	else
		message_send(stream,to,from,1,_("ignore unknown: off"),0);

	if (session!=NULL) session_send_status(session);

	user_save(user);
}
Exemple #5
0
void message_status(struct stream_s *stream,const char *from, const char *to,
				const char *args, xmlnode msg){
Session *session;
User *user;
char *m;

	session=session_get_by_jid(from,stream,0);
	if (session!=NULL)
		user=session->user;
	else
		user=user_get_by_jid(from);

	g_free(user->status);
	if (args) {
		if (!g_strcasecmp(args,"off")) user->status=NULL;
		else user->status=g_strndup(string_to_gg(args),GG_STATUS_DESCR_MAXSIZE);
	}
	else user->status=NULL;

	m=g_strdup_printf(_("status: %s%s%s"),
			(user->status?"`":""),
			(user->status?string_from_gg(user->status):_("not set")),
			(user->status?"'":""));
	message_send(stream,to,from,1,m,0);
	g_free(m);

	if (session!=NULL) session_send_status(session);
	user_save(user);
}
Exemple #6
0
void message_invisible(struct stream_s *stream,const char *from, const char *to,
				const char *args, xmlnode msg){
Session *session;
User *user;
Resource *r;
gboolean on;

	session=session_get_by_jid(from,stream,0);
	if (session!=NULL)
		user=session->user;
	else
		user=user_get_by_jid(from);
	if (args && g_strcasecmp(args,"on")==0) on=TRUE;
	else if (args && g_strcasecmp(args,"off")==0) on=FALSE;
	else on=!user->invisible;

	if (user->invisible==on){
		message_send(stream,to,from,1,_("No change."),0);
		return;
	}
	user->invisible=on;

	if (on)
		message_send(stream,to,from,1,_("invisible: on"),0);
	else
		message_send(stream,to,from,1,_("invisible: off"),0);

	if (session!=NULL) session_send_status(session);
	r=session_get_cur_resource(session);
	if ( r )
		presence_send(stream,NULL,user->jid,user->invisible?-1:r->available,r->show,session->gg_status_descr,0);

	user_save(user);
}
Exemple #7
0
int user_sys_msg_received(User *u,int nr){

	if (nr<=u->last_sys_msg) return 0;
	u->last_sys_msg=nr;
	user_save(u);
	return 1;
}
Exemple #8
0
int user_check_contact(User *u,Contact *c){

	if (c->ignored || c->got_online || c->blocked
			|| c->got_probe || c->subscribe!=SUB_NONE) return 0;
	u->contacts=g_list_remove(u->contacts,c);
	if (c->status_desc) free(c->status_desc);
	g_free(c);
	user_save(u);
	return 0;
}
Exemple #9
0
int presence_subscribed(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_unsubscribe(stream,to,from);
		return -1;
	}
	if (jid_is_me(to)){
		if (u->subscribe==SUB_NONE) u->subscribe=SUB_FROM;
		else if (u->subscribe==SUB_UNDEFINED || u->subscribe==SUB_TO) u->subscribe=SUB_BOTH;
		user_save(u);
		debug(L_("Presence 'subscribed' sent to me"));
		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_("%s accepted %s's subscription..."),from,to);
	uin=jid_get_uin(to);
	c=user_get_contact(u,uin,TRUE);
	if (!c) {
	       	return -1;
	}
	if (c->subscribe==SUB_UNDEFINED) c->subscribe=SUB_BOTH;
	else if (c->subscribe==SUB_NONE) c->subscribe=SUB_FROM;
	else if (c->subscribe==SUB_TO) c->subscribe=SUB_BOTH;
	user_save(u);

	if (s) session_update_contact(s,c);	
	return 0;
}
static void
cache_user (Daemon *daemon,
            User   *user)
{
        gchar       *filename;
        const char  *user_name;

        /* Always use the canonical user name looked up */
        user_name = user_get_user_name (user);

        filename = g_build_filename (USERDIR, user_name, NULL);
        if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
                user_save (user);
        }

        g_free (filename);
}
Exemple #11
0
void message_ignore(struct stream_s *stream,const char *from, const char *to,
				const char *args, xmlnode msg){
Session *session;
User *user;
uin_t uin;
Contact *c;
gchar *m;

	session=session_get_by_jid(from,stream,0);
	if (session!=NULL)
		user=session->user;
	else
		user=user_get_by_jid(from);

	if (args && *args) uin=atoi(args);
	else uin=0;

	if (uin<=0) {
		GList *it;
		m=g_strdup(_("\nMessages from the following GG numbers will be ignored:"));
		for(it=user->contacts;it;it=it->next){
			c=(Contact *)it->data;
			if (!c->ignored) continue;
			m=g_strdup_printf(_("%s\n  %li"),m,(long)c->uin);
		}
		message_send(stream,to,from,1,m,0);
		g_free(m);
		return;
	}

	c=user_get_contact(user,uin,TRUE);
	c->ignored=TRUE;
	if (session) session_update_contact(session,c);
		
	m=g_strdup_printf(_("\nMessages from GG number %li will be ignored."),(long)uin);
	message_send(stream,to,from,1,m,0);
	g_free(m);

	user_save(user);
}
Exemple #12
0
void message_locale(struct stream_s *stream,const char *from, const char *to,
				const char *args, xmlnode msg){
Session *session;
User *user;
char *m;

	session=session_get_by_jid(from,stream,0);
	if (session!=NULL)
		user=session->user;
	else
		user=user_get_by_jid(from);

	if (args && args[0]){
		if (user->locale) g_free(user->locale);
		user->locale=g_strdup(args);
	}

	m=g_strdup_printf(_("Locale set to: %s"),user->locale?user->locale:_("_default_"));
	message_send(stream,to,from,1,m,0);
	g_free(m);
	user_save(user);
}
Exemple #13
0
void draw_style()
{
	
	switch(cur_tool)
	{
		case 1:
			user_new();
			break;
		case 2:user_save();
				break;
		case 3:user_load();
			break;
			
		case 10:
		case 17:
			cur_panel=1;
			top_panel_background();
			top_panel_buttons(buttons_data[cur_tool-1],0,1);
			style_icon();
			break;
	}
}
Exemple #14
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;
}