Esempio n. 1
0
func (s *SessionXmppSuite) Test_WatchStanzas_iq_set_roster_setsExistingRosterItem(c *C) {
	mockIn := &mockConnIOReaderWriter{read: []byte("<client:iq xmlns:client='jabber:client' type='set' to='cde'><query xmlns='jabber:iq:roster'>" +
		"<item jid='*****@*****.**' name='Romeo' subscription='both'>" +
		"<group>Friends</group>" +
		"</item>" +
		"</query></client:iq>")}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	called := 0

	sess := &session{
		config: &config.ApplicationConfig{},
		accountConfig: &config.Account{
			Account: "*****@*****.**",
		},
		r:          roster.New(),
		connStatus: DISCONNECTED,
	}
	sess.conn = conn

	sess.r.AddOrReplace(peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Jill", Group: []string{"Foes"}}, sess.GetConfig()))
	sess.r.AddOrReplace(peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Mo", Group: []string{"Foes"}}, sess.GetConfig()))

	sess.watchStanzas()

	c.Assert(called, Equals, 0)
	c.Assert(sess.r.ToSlice(), DeepEquals, []*roster.Peer{
		peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Jill", Group: []string{"Foes"}}, sess.GetConfig()),
		peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Romeo", Group: []string{"Friends"}}, sess.GetConfig()),
	})
}
Esempio n. 2
0
func (s *SessionXmppSuite) Test_WatchStanzas_presence_unavailable_forNoneKnownUser(c *C) {
	mockIn := &mockConnIOReaderWriter{read: []byte("<client:presence xmlns:client='jabber:client' from='[email protected]/balcony' to='*****@*****.**' type='unavailable'><client:status>going on vacation</client:status></client:presence>")}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	sess := &session{
		r:          roster.New(),
		connStatus: DISCONNECTED,
	}
	sess.conn = conn

	observer := make(chan interface{}, 1)
	sess.Subscribe(observer)

	sess.watchStanzas()

	select {
	case ev := <-observer:
		switch ev.(type) {
		case events.Presence:
			c.Error("Received presence event")
			return
		default:
			// ignore
		}
	case <-time.After(1 * time.Millisecond):
		return
	}
}
Esempio n. 3
0
func (s *SessionXmppSuite) Test_HandleConfirmOrDeny_succeedsOnAllowedAndAskBack(c *C) {
	mockIn := &mockConnIOReaderWriter{}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	called := 0

	sess := &session{
		r:                   roster.New(),
		sessionEventHandler: &mockSessionEventHandler{
		//warn: func(v string) {
		//	called++
		//},
		},
	}
	sess.conn = conn
	sess.r.SubscribeRequest("*****@*****.**", "123", "")

	sess.HandleConfirmOrDeny("*****@*****.**", true)

	c.Assert(called, Equals, 0)
	c.Assert(string(mockIn.write), Matches, "<presence id='123' to='*****@*****.**' type='subscribed'/><presence id='[0-9]+' to='*****@*****.**' type='subscribe'/>")
	_, inMap := sess.r.GetPendingSubscribe("*****@*****.**")
	c.Assert(inMap, Equals, false)
}
Esempio n. 4
0
// Factory creates a new session from the given config
func Factory(c *config.ApplicationConfig, cu *config.Account, df func(tls.Verifier) xi.Dialer) access.Session {
	// Make xmppLogger go to in memory STRING and/or the log file

	inMemoryLog, xmppLogger := createXMPPLogger(c.RawLogFile)

	s := &session{
		config:        c,
		accountConfig: cu,

		r:               roster.New(),
		otrEventHandler: make(map[string]*event.OtrEventHandler),
		lastActionTime:  time.Now(),

		timeouts: make(map[data.Cookie]time.Time),

		autoApproves: make(map[string]bool),

		inMemoryLog:      inMemoryLog,
		xmppLogger:       xmppLogger,
		connectionLogger: logToDebugLog(),
		dialerFactory:    df,
	}

	s.ReloadKeys()
	s.convManager = client.NewConversationManager(s, s)

	go observe(s)
	go checkReconnect(s)

	return s
}
Esempio n. 5
0
func (m *accountManager) addAccount(appConfig *config.ApplicationConfig, account *config.Account, sf access.Factory, df func() interfaces.Dialer) {
	m.Lock()
	defer m.Unlock()

	acc := newAccount(appConfig, account, sf, df)
	acc.session.Subscribe(m.events)
	acc.session.SetCommandManager(m)
	acc.session.SetConnector(acc)

	m.accounts = append(m.accounts, acc)
	m.setContacts(acc, rosters.New())
}
Esempio n. 6
0
func (m *accountManager) addAccount(appConfig *config.ApplicationConfig, account *config.Account) {
	m.Lock()
	defer m.Unlock()

	acc := newAccount(appConfig, account)
	acc.session.Subscribe(m.events)
	acc.session.CommandManager = m
	acc.session.Connector = acc

	m.accounts = append(m.accounts, acc)
	m.setContacts(acc, rosters.New())
}
Esempio n. 7
0
func (s *SessionXmppSuite) Test_WatchStanzas_iq_set_roster_removesRosterItems(c *C) {
	mockIn := &mockConnIOReaderWriter{read: []byte("<client:iq xmlns:client='jabber:client' type='set' to='cde'><query xmlns='jabber:iq:roster'>" +
		"<item jid='*****@*****.**' name='Romeo' subscription='remove'>" +
		"<group>Friends</group>" +
		"</item>" +
		"</query></client:iq>")}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	sess := &session{
		config: &config.ApplicationConfig{},
		accountConfig: &config.Account{
			Account: "*****@*****.**",
		},
		r:          roster.New(),
		connStatus: DISCONNECTED,
	}
	sess.conn = conn

	sess.r.AddOrReplace(peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Mo", Group: []string{"Foes"}}, sess.GetConfig()))
	sess.r.AddOrReplace(peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Jill", Group: []string{"Foes"}}, sess.GetConfig()))
	sess.r.AddOrReplace(peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Mo", Group: []string{"Foes"}}, sess.GetConfig()))

	observer := make(chan interface{}, 1)
	sess.Subscribe(observer)

	sess.watchStanzas()

	c.Assert(sess.r.ToSlice(), DeepEquals, []*roster.Peer{
		peerFrom(data.RosterEntry{Jid: "*****@*****.**", Subscription: "both", Name: "Jill", Group: []string{"Foes"}}, sess.GetConfig()),
	})

	select {
	case ev := <-observer:
		switch ev.(type) {
		case events.Peer:
			c.Error("Received peer event")
			return
		default:
			// ignore
		}
	case <-time.After(1 * time.Millisecond):
		return
	}
}
Esempio n. 8
0
func (m *accountManager) addAccount(appConfig *config.ApplicationConfig, account *config.Account) {
	m.Lock()
	defer m.Unlock()

	acc, err := newAccount(appConfig, account)
	if err != nil {
		//TODO error
		return
	}

	acc.session.Subscribe(m.events)
	acc.session.CommandManager = m

	m.accounts = append(m.accounts, acc)
	m.setContacts(acc, rosters.New())
}
Esempio n. 9
0
func (s *SessionXmppSuite) Test_HandleConfirmOrDeny_failsWhenNoPendingSubscribeIsWaiting(c *C) {
	sess := &session{
		r: roster.New(),
	}

	observer := make(chan interface{}, 1)
	sess.Subscribe(observer)

	sess.HandleConfirmOrDeny("*****@*****.**", true)

	select {
	case ev := <-observer:
		t := ev.(events.Log)
		c.Assert(t.Level, Equals, events.Warn)
	case <-time.After(1 * time.Millisecond):
		c.Errorf("did not receive event")
	}
}
Esempio n. 10
0
func (s *SessionXmppSuite) Test_WatchStanzas_presence_unavailable_forKnownUser(c *C) {
	mockIn := &mockConnIOReaderWriter{read: []byte("<client:presence xmlns:client='jabber:client' from='[email protected]/balcony' to='*****@*****.**' type='unavailable'><client:status>going on vacation</client:status></client:presence>")}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	sess := &session{
		config:        &config.ApplicationConfig{},
		accountConfig: &config.Account{},
		r:             roster.New(),
		connStatus:    DISCONNECTED,
	}
	sess.conn = conn
	sess.r.AddOrReplace(roster.PeerWithState("*****@*****.**", "somewhere", "", ""))

	observer := make(chan interface{}, 1)
	sess.Subscribe(observer)
	sess.watchStanzas()

	p, _ := sess.r.Get("*****@*****.**")
	c.Assert(p.Online, Equals, false)

	for {
		select {
		case ev := <-observer:
			switch t := ev.(type) {
			case events.Presence:
				c.Assert(t.Gone, Equals, true)
				return
			default:
				//ignore
			}
		case <-time.After(1 * time.Millisecond):
			c.Errorf("did not receive event")
			return
		}
	}

}
Esempio n. 11
0
func (s *SessionXmppSuite) Test_WatchStanzas_presence_subscribe(c *C) {
	mockIn := &mockConnIOReaderWriter{read: []byte("<client:presence xmlns:client='jabber:client' from='[email protected]/balcony' to='*****@*****.**' type='subscribe' id='adf12112'/>")}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	sess := &session{
		config:        &config.ApplicationConfig{},
		accountConfig: &config.Account{},
		r:             roster.New(),
		connStatus:    DISCONNECTED,
	}
	sess.conn = conn

	sess.watchStanzas()

	v, _ := sess.r.GetPendingSubscribe("*****@*****.**")
	c.Assert(v, Equals, "adf12112")
}
Esempio n. 12
0
// NewSession creates a new session from the given config
func NewSession(c *config.ApplicationConfig, cu *config.Account) *Session {
	s := &Session{
		Config:        c,
		accountConfig: cu,

		R:               roster.New(),
		OtrEventHandler: make(map[string]*event.OtrEventHandler),
		LastActionTime:  time.Now(),

		timeouts: make(map[xmpp.Cookie]time.Time),

		xmppLogger: openLogFile(c.RawLogFile),
	}

	s.PrivateKeys = parseFromConfig(cu)
	s.ConversationManager = client.NewConversationManager(s, s)

	go observe(s)

	return s
}
Esempio n. 13
0
func (s *SessionXmppSuite) Test_WatchStanzas_presence_regularPresenceIsAdded(c *C) {
	mockIn := &mockConnIOReaderWriter{read: []byte("<client:presence xmlns:client='jabber:client' from='[email protected]/balcony' to='*****@*****.**'><client:show>dnd</client:show></client:presence>")}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	sess := &session{
		config:        &config.ApplicationConfig{},
		accountConfig: &config.Account{},
		r:             roster.New(),
		connStatus:    DISCONNECTED,
	}
	sess.conn = conn

	observer := make(chan interface{}, 1)
	sess.Subscribe(observer)

	sess.watchStanzas()

	st, _, _ := sess.r.StateOf("*****@*****.**")
	c.Assert(st, Equals, "dnd")

	for {
		select {
		case ev := <-observer:
			switch t := ev.(type) {
			case events.Presence:
				c.Assert(t.Gone, Equals, false)
			default:
				//ignore
			}
			return
		case <-time.After(1 * time.Millisecond):
			c.Errorf("did not receive event")
			return
		}
	}
}
Esempio n. 14
0
// Factory creates a new session from the given config
func Factory(c *config.ApplicationConfig, cu *config.Account) access.Session {
	s := &session{
		config:        c,
		accountConfig: cu,

		r:               roster.New(),
		otrEventHandler: make(map[string]*event.OtrEventHandler),
		lastActionTime:  time.Now(),

		timeouts: make(map[xmpp.Cookie]time.Time),

		xmppLogger: openLogFile(c.RawLogFile),
	}

	s.ReloadKeys()
	s.convManager = client.NewConversationManager(s, s)

	go observe(s)
	go checkReconnect(s)

	return s
}
Esempio n. 15
0
func (s *SessionSuite) Test_WatchStanzas_presence_ignoresSameState(c *C) {
	mockIn := &mockConnIOReaderWriter{read: []byte("<client:presence xmlns:client='jabber:client' from='[email protected]/balcony' to='*****@*****.**'><client:show>dnd</client:show></client:presence>")}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		mockIn,
		"[email protected]/foo",
	)

	sess := &session{
		config:        &config.ApplicationConfig{},
		accountConfig: &config.Account{},
		r:             roster.New(),
		connStatus:    DISCONNECTED,
	}
	sess.conn = conn
	sess.r.AddOrReplace(roster.PeerWithState("*****@*****.**", "dnd", "", "", ""))

	observer := make(chan interface{}, 1)
	sess.Subscribe(observer)

	sess.watchStanzas()

	st, _, _ := sess.r.StateOf("*****@*****.**")
	c.Assert(st, Equals, "dnd")

	select {
	case ev := <-observer:
		switch ev.(type) {
		case events.Presence:
			c.Error("Received presence event")
			return
		default:
			// ignore
		}
	case <-time.After(1 * time.Millisecond):
		return
	}
}
Esempio n. 16
0
func (s *SessionXmppSuite) Test_HandleConfirmOrDeny_handlesSendPresenceError(c *C) {
	mockIn := &mockConnIOReaderWriter{}
	conn := xmpp.NewConn(
		xml.NewDecoder(mockIn),
		&mockConnIOReaderWriter{err: errors.New("foo bar")},
		"[email protected]/foo",
	)

	sess := &session{
		r: roster.New(),
	}
	sess.conn = conn
	sess.r.SubscribeRequest("*****@*****.**", "123", "")

	observer := make(chan interface{}, 1)
	sess.Subscribe(observer)

	sess.HandleConfirmOrDeny("*****@*****.**", true)

	for {
		select {
		case ev := <-observer:
			t := ev.(events.Log)
			if t.Level != events.Warn {
				continue
			}

			c.Assert(t.Message, Equals, "Error sending presence stanza: foo bar")
			return

		case <-time.After(1 * time.Millisecond):
			c.Errorf("did not receive event")
			return
		}
	}
}
Esempio n. 17
0
File: ui.go Progetto: PMaynard/coyim
func (u *gtkUI) addInitialAccountsToRoster() {
	for _, account := range u.accounts {
		u.roster.update(account, rosters.New())
	}
}