Exemple #1
0
func reconnect() {
	for _, user := range storage.LoadUsers() {
		session := NewSession()
		session.user = user
		sessions[user.UUID] = session
		go session.write()

		channels := user.GetChannels()

		for _, server := range user.GetServers() {
			i := irc.NewClient(server.Nick, server.Username)
			i.TLS = server.TLS
			i.Password = server.Password
			i.Realname = server.Realname

			i.Connect(server.Address)
			session.setIRC(i.Host, i)
			go newIRCHandler(i, session).run()

			var joining []string
			for _, channel := range channels {
				if channel.Server == server.Address {
					joining = append(joining, channel.Name)
				}
			}
			i.Join(joining...)
		}
	}
}
func TestHandleIRCMotd(t *testing.T) {
	c := irc.NewClient("nick", "user")
	c.Host = "host.com"
	s := NewSession()
	i := newIRCHandler(c, s)

	i.dispatchMessage(&irc.Message{
		Command:  irc.ReplyMotdStart,
		Trailing: "motd title",
	})
	i.dispatchMessage(&irc.Message{
		Command:  irc.ReplyMotd,
		Trailing: "line 1",
	})
	i.dispatchMessage(&irc.Message{
		Command:  irc.ReplyMotd,
		Trailing: "line 2",
	})
	i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfMotd})

	checkResponse(t, "motd", MOTD{
		Server:  "host.com",
		Title:   "motd title",
		Content: []string{"line 1", "line 2"},
	}, <-s.out)
}
func TestHandleIRCNames(t *testing.T) {
	c := irc.NewClient("nick", "user")
	c.Host = "host.com"
	s := NewSession()
	i := newIRCHandler(c, s)

	i.dispatchMessage(&irc.Message{
		Command:  irc.ReplyNamReply,
		Params:   []string{"", "", "#chan"},
		Trailing: "a b c",
	})
	i.dispatchMessage(&irc.Message{
		Command:  irc.ReplyNamReply,
		Params:   []string{"", "", "#chan"},
		Trailing: "d",
	})
	i.dispatchMessage(&irc.Message{
		Command: irc.ReplyEndOfNames,
		Params:  []string{"", "#chan"},
	})

	checkResponse(t, "users", Userlist{
		Server:  "host.com",
		Channel: "#chan",
		Users:   []string{"a", "b", "c", "d"},
	}, <-s.out)
}
func TestHandleIRCWhois(t *testing.T) {
	c := irc.NewClient("nick", "user")
	c.Host = "host.com"
	s := NewSession()
	i := newIRCHandler(c, s)

	i.dispatchMessage(&irc.Message{
		Command: irc.ReplyWhoisUser,
		Params:  []string{"", "nick", "user", "host", "", "realname"},
	})
	i.dispatchMessage(&irc.Message{
		Command: irc.ReplyWhoisServer,
		Params:  []string{"", "", "srv.com"},
	})
	i.dispatchMessage(&irc.Message{
		Command:  irc.ReplyWhoisChannels,
		Trailing: "#chan #chan1",
	})
	i.dispatchMessage(&irc.Message{Command: irc.ReplyEndOfWhois})

	checkResponse(t, "whois", WhoisReply{
		Nick:     "nick",
		Username: "******",
		Host:     "host",
		Realname: "realname",
		Server:   "srv.com",
		Channels: []string{"#chan", "#chan1"},
	}, <-s.out)
}
func (h *wsHandler) connect(b []byte) {
	var data Connect
	json.Unmarshal(b, &data)

	if _, ok := h.session.getIRC(data.Server); !ok {
		log.Println(h.addr, "connecting to", data.Server)

		i := irc.NewClient(data.Nick, data.Username)
		i.TLS = data.TLS
		i.Password = data.Password
		i.Realname = data.Realname

		if idx := strings.Index(data.Server, ":"); idx < 0 {
			h.session.setIRC(data.Server, i)
		} else {
			h.session.setIRC(data.Server[:idx], i)
		}

		i.Connect(data.Server)
		go newIRCHandler(i, h.session).run()

		h.session.user.AddServer(storage.Server{
			Name:     data.Name,
			Address:  i.Host,
			TLS:      data.TLS,
			Password: data.Password,
			Nick:     data.Nick,
			Username: data.Username,
			Realname: data.Realname,
		})
	} else {
		log.Println(h.addr, "already connected to", data.Server)
	}
}
func dispatchMessage(msg *irc.Message) WSResponse {
	c := irc.NewClient("nick", "user")
	c.Host = "host.com"
	s := NewSession()
	s.user = user

	newIRCHandler(c, s).dispatchMessage(msg)

	return <-s.out
}