Ejemplo n.º 1
0
func TestRequestReceivers(t *testing.T) {
	success := []struct {
		msg *irc.Message
		rec []string
	}{
		{
			msg: irc.ParseMessage(":Angel PRIVMSG user1, user2 :example message"),
			rec: []string{"Angel"},
		},
		{
			msg: irc.ParseMessage(":Angel PRIVMSG #general :example message"),
			rec: []string{"#general"},
		},
		{
			msg: irc.ParseMessage(":Angel PRIVMSG #general , #privatechannel :example message"),
			rec: []string{"#general", "#privatechannel"},
		},
	}

	for _, args := range success {
		req := &relay.Request{Message: args.msg}
		rec := req.Receivers()

		assert.Equal(t, args.rec, rec)
	}
}
Ejemplo n.º 2
0
Archivo: raw.go Proyecto: cosban/muggy
func Raw(s ircx.Sender, m *irc.Message, message string) {
	if !isOwner(s, m.Name) {
		return
	}
	log.Printf("RAW: %s\n%+v", message, irc.ParseMessage(message))
	messages.QueueMessages(s, irc.ParseMessage(message))
}
Ejemplo n.º 3
0
func TestServerQuit(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":services.robustirc.net NICK blorgh 1 1425542735 enforcer services.robustirc.net services.robustirc.net 0 :Services Enforcer"))

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":blorgh QUIT")),
		":blorgh!enforcer@robust/0x13c6cdee3e749faf QUIT :")
}
Ejemplo n.º 4
0
func relevantNick(msg *irc.Message, prev, next logCursor, reset logReset) (bool, error) {
	if len(msg.Params) < 1 {
		return false, nil
	}

	last, err := isLastMessage(prev, next, reset)
	if err != nil {
		return true, err
	}
	if last {
		return false, nil
	}

	// Forever retain the first NICK message so that the connection is logged in.
	earliestNick := true
	for {
		rmsg, err := prev(types.RobustIRCFromClient)
		if err != nil {
			if err == CursorEOF {
				break
			}
			return true, err
		}
		nmsg := irc.ParseMessage(rmsg.Data)
		if strings.ToUpper(nmsg.Command) == irc.NICK {
			earliestNick = false
		}
	}

	if earliestNick {
		return true, err
	}

	reset()

	for {
		rmsg, err := next(types.RobustIRCFromClient)
		if err != nil {
			if err == CursorEOF {
				break
			}
			return true, err
		}
		nmsg := irc.ParseMessage(rmsg.Data)
		// TOPIC relies on the NICK.
		if nmsg.Command == irc.TOPIC {
			return true, nil
		}
		// There is a newer NICK command, so discard this one.
		if nmsg.Command == irc.NICK {
			return false, nil
		}
	}

	return true, nil
}
Ejemplo n.º 5
0
func TestServerInvite(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("JOIN #test"))

	mustMatchIrcmsgs(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ INVITE mero #test")),
		[]*irc.Message{
			irc.ParseMessage(":robustirc.net 341 ChanServ mero #test"),
			irc.ParseMessage(":ChanServ!services@services INVITE mero :#test"),
			irc.ParseMessage(":robustirc.net NOTICE #test :ChanServ invited mero into the channel."),
		})

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ INVITE moro #test")),
		":robustirc.net 401 ChanServ moro :No such nick/channel")

	i.ProcessMessage(types.RobustId{}, ids["mero"], irc.ParseMessage("JOIN #test"))

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ INVITE mero #test")),
		":robustirc.net 443 ChanServ mero #test :is already on channel")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ INVITE mero #toast")),
		":robustirc.net 403 ChanServ #toast :No such channel")
}
Ejemplo n.º 6
0
func stdIRCServerWithServices() (*IRCServer, map[string]types.RobustId) {
	i, ids := stdIRCServer()
	i.Config.IRC.Services = append(i.Config.IRC.Services, config.Service{
		Password: "******",
	})
	ids["services"] = types.RobustId{Id: 0x13c6cdee3e749faf}
	i.CreateSession(ids["services"], "auth-server")
	i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("PASS :services=mypass"))
	i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SERVER services.robustirc.net 1 :Services for IRC Networks"))
	return i, ids
}
Ejemplo n.º 7
0
func TestServerPrivmsg(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("JOIN #test"))

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PRIVMSG secure :ohai")),
		":ChanServ!services@services PRIVMSG secure :ohai")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PRIVMSG socoro :ohai")),
		":robustirc.net 401 ChanServ socoro :No such nick/channel")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PRIVMSG #test :ohai")),
		":ChanServ!services@services PRIVMSG #test :ohai")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PRIVMSG")),
		":robustirc.net 411 ChanServ :No recipient given (PRIVMSG)")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PRIVMSG #test")),
		":robustirc.net 412 ChanServ :No text to send")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PRIVMSG #toast :a")),
		":robustirc.net 403 ChanServ #toast :No such channel")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ NOTICE")),
		":robustirc.net 411 ChanServ :No recipient given (NOTICE)")
}
Ejemplo n.º 8
0
func TestServerSvsmode(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("MODE secure")),
		":sECuRE!blah@robust/0x13b5aa0a2bcfb8ad MODE sECuRE :+")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSMODE secure +r")),
		":services.robustirc.net MODE sECuRE :+r")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSMODE socoro +r")),
		":robustirc.net 401 * socoro :No such nick/channel")

	mustMatchIrcmsgs(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSMODE secure +rq")),
		[]*irc.Message{
			irc.ParseMessage(":robustirc.net 501 * :Unknown MODE flag"),
			irc.ParseMessage(":services.robustirc.net MODE sECuRE :+r"),
		})

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSMODE secure +d-r")),
		":services.robustirc.net MODE sECuRE :+")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSMODE secure d-r")),
		":robustirc.net 501 * :Unknown MODE flag")
}
Ejemplo n.º 9
0
func relevantUser(msg *irc.Message, prev, next logCursor, reset logReset) (bool, error) {
	if len(msg.Params) < 1 {
		return false, nil
	}

	// This is the first USER message. If the next message is a QUIT message,
	// this session cannot have modified any state.
	rmsg, err := next(types.RobustAny)
	if err == nil && isDeleteSession(rmsg) {
		return false, nil
	}
	prev(types.RobustAny)

	for {
		rmsg, err := prev(types.RobustIRCFromClient)
		if err != nil {
			if err == CursorEOF {
				break
			}
			return true, err
		}
		pmsg := irc.ParseMessage(rmsg.Data)
		// There already was a USER message, so discard this one.
		if pmsg.Command == irc.USER {
			return false, nil
		}
	}

	return true, nil
}
Ejemplo n.º 10
0
func relevantJoin(msg *irc.Message, prev, next logCursor, reset logReset) (bool, error) {
	if len(msg.Params) < 1 {
		return false, nil
	}

	lcnames := multipleChannels(msg.Params[0])
	for {
		rmsg, err := next(types.RobustIRCFromClient)
		if err != nil {
			if err == CursorEOF {
				break
			}
			return true, err
		}
		nmsg := irc.ParseMessage(rmsg.Data)
		if nmsg.Command == irc.TOPIC &&
			len(nmsg.Params) > 0 &&
			lcnames[ChanToLower(nmsg.Params[0])] {
			return true, nil
		}
		// TODO: support KICK
		if nmsg.Command == irc.PART && len(nmsg.Params) > 0 {
			for channelname := range multipleChannels(nmsg.Params[0]) {
				delete(lcnames, channelname)
			}
			if len(lcnames) == 0 {
				return false, nil
			}
		}
	}

	return true, nil
}
Ejemplo n.º 11
0
func Handle(input string, DB *neoism.Database) {
	msg := irc.ParseMessage(input)
	if msg == nil {
		fmt.Println("Could not parse message")
	}
	store(msg, DB)
}
Ejemplo n.º 12
0
func handleAdmin(c *sirc.IConn, m *irc.Message) bool {
	matches := adminRE.FindStringSubmatch(m.Trailing)
	if len(matches) == 0 {
		return false
	}
	adminState.Lock()
	// lifo defer order
	defer adminState.Save()
	defer adminState.Unlock()

	host := strings.TrimSpace(matches[2])
	switch matches[1] {
	case "addadmin":
		admins[host] = struct{}{}
		c.Notice(m, "Added host successfully")
	case "deladmin":
		delete(admins, host)
		c.Notice(m, "Removed host successfully")
	case "raw":
		nm := irc.ParseMessage(matches[2])
		if nm == nil {
			c.Notice(m, "Could not parse, are you sure you know the irc protocol?")
		} else {
			go c.Write(nm)
		}
	}

	return true
}
Ejemplo n.º 13
0
func TestServerSjoin(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("SJOIN 1 #test :ChanServ")),
		":robustirc.net 421 sECuRE SJOIN :Unknown command")
}
Ejemplo n.º 14
0
func TestServerWelcome(t *testing.T) {
	events := make(chan Event, 10)
	srv := NewServer(testServerName)
	srv.Subscribe(events)
	defer srv.Close()

	send, receive := make(chan *irc.Message, 10), make(chan *irc.Message, 10)
	u := NewUserMock(send, receive)
	go srv.Connect(u)
	receive <- irc.ParseMessage("NICK foo")
	receive <- irc.ParseMessage("USER root 0 * :Foo Bar")
	if msg := <-send; msg.Command != irc.RPL_WELCOME {
		t.Errorf("got %v; want %v", msg, irc.RPL_WELCOME)
	}
	expectEvent(t, events, ConnectEvent)
}
Ejemplo n.º 15
0
func PrivacyFilterMsg(message *types.RobustMessage) *types.RobustMessage {
	return &types.RobustMessage{
		Id:      message.Id,
		Session: message.Session,
		Type:    message.Type,
		Data:    PrivacyFilterIrcmsg(irc.ParseMessage(message.Data)).String(),
	}
}
Ejemplo n.º 16
0
func TestServerNick(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("JOIN #test"))

	mustMatchIrcmsgs(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("NICK ChanServ 1 1422134861 services robustirc.net services.robustirc.net 0 :Operator Server")),
		[]*irc.Message{})

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("NICK ChanServ 1 1422134861 services robustirc.net services.robustirc.net 0 :Operator Server")),
		":robustirc.net 433 * ChanServ :Nickname is already in use")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("NICK NickServ")),
		":robustirc.net 433 sECuRE NickServ :Nickname is already in use")
}
Ejemplo n.º 17
0
func (cc *ClientConn) SendRaw(message string) (err error) {

	m := irc.ParseMessage(message)
	if m == nil {

		return ErrParseMsg
	}

	return cc.Conn.Encode(m)
}
Ejemplo n.º 18
0
func (user *userImpl) rawMessage(data messages.RawMessage) {
	if len(data.Network) == 0 || len(data.Message) == 0 {
		return
	}

	net := user.GetNetwork(data.Network)
	if net == nil {
		return
	}

	net.Tunnel().Send(msg.ParseMessage(data.Message))
}
Ejemplo n.º 19
0
func isDeleteSession(msg *types.RobustMessage) bool {
	if msg.Type == types.RobustDeleteSession {
		return true
	}
	if msg.Type == types.RobustIRCFromClient {
		parsed := irc.ParseMessage(msg.Data)
		if parsed != nil && parsed.Command == irc.QUIT {
			return true
		}
	}
	return false
}
Ejemplo n.º 20
0
func (m *RobustMessage) PrivacyFilter() string {
	if m.Type != RobustIRCToClient && m.Type != RobustIRCFromClient {
		return m.Data
	}
	if msg := irc.ParseMessage(m.Data); msg != nil {
		command := strings.ToUpper(msg.Command)
		if command == irc.PRIVMSG ||
			command == irc.NOTICE ||
			strings.HasSuffix(command, "serv") {
			msg.Trailing = "<privacy filtered>"
			return string(msg.Bytes())
		}
	}
	return m.Data
}
Ejemplo n.º 21
0
// ParseMessage takes a string and attempts to create a Message struct.
// Returns nil if the Message is invalid.
// TODO: Maybe just use sorbix/irc if we can be without the custom stuff?
func ParseMessage(raw string) (m *Message) {
	m = new(Message)
	m.Message = irc.ParseMessage(raw)
	m.Content = m.Trailing

	if len(m.Params) > 0 {
		m.To = m.Params[0]
	} else if m.Command == "JOIN" {
		m.To = m.Trailing
	}
	if m.Prefix != nil {
		m.From = m.Prefix.Name
	}
	m.TimeStamp = time.Now()

	return m
}
Ejemplo n.º 22
0
Archivo: server.go Proyecto: Ell/bot
func (server *Server) HandleCommands() {
	for {
		msgr, err := server.Pubsub.Receive()
		if err != nil {
			panic(err)
		}

		switch msg := msgr.(type) {
		case *redis.Subscription:
			continue
		case *redis.Message:
			go server.sendMessage(irc.ParseMessage(msg.Payload))
		default:
			panic(fmt.Sprintf("Unknown Redis Message Type: %+v\n", msgr))
		}
	}
}
Ejemplo n.º 23
0
func TestServerJoinPart(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("JOIN #test"))

	mustMatchIrcmsgs(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("NICK ChanServ 1 1422134861 services robustirc.net services.robustirc.net 0 :Operator Server")),
		[]*irc.Message{})

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PART #test")),
		":robustirc.net 442 ChanServ #test :You're not on that channel")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PART #toast")),
		":robustirc.net 403 ChanServ #toast :No such channel")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ JOIN #test")),
		":ChanServ!services@services JOIN :#test")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PART #test")),
		":ChanServ!services@services PART #test")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ JOIN !")),
		":robustirc.net 403 ChanServ ! :No such channel")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ JOIN #new")),
		":ChanServ!services@services JOIN :#new")

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage(":ChanServ PART #new")),
		":ChanServ!services@services PART #new")
}
Ejemplo n.º 24
0
func relevantAway(msg *irc.Message, prev, next logCursor, reset logReset) (bool, error) {
	for {
		rmsg, err := next(types.RobustIRCFromClient)
		if err != nil {
			if err == CursorEOF {
				break
			}
			return true, err
		}
		nmsg := irc.ParseMessage(rmsg.Data)
		// There is a newer AWAY command, discard the old one.
		if nmsg.Command == irc.AWAY {
			return false, nil
		}
	}

	return strings.TrimSpace(msg.Trailing) != "", nil
}
Ejemplo n.º 25
0
func TestServerSvshold(t *testing.T) {
	i, ids := stdIRCServerWithServices()

	now := time.Now()

	serverSession, _ := i.GetSession(ids["services"])
	serverSession.LastActivity = now

	mustMatchIrcmsgs(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSHOLD newnick 5 :held by services")),
		[]*irc.Message{})

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("NICK newnick")),
		":robustirc.net 432 sECuRE newnick :Erroneous Nickname: held by services")

	s, _ := i.GetSession(ids["secure"])
	s.LastActivity = now.Add(10 * time.Second)

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("NICK newnick")),
		":sECuRE!blah@robust/0x13b5aa0a2bcfb8ad NICK :newnick")

	now = time.Now()

	serverSession.LastActivity = now
	s.LastActivity = now

	mustMatchIrcmsgs(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSHOLD anothernick 5 :held by services")),
		[]*irc.Message{})

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("NICK anothernick")),
		":robustirc.net 432 newnick anothernick :Erroneous Nickname: held by services")

	mustMatchIrcmsgs(t,
		i.ProcessMessage(types.RobustId{}, ids["services"], irc.ParseMessage("SVSHOLD anothernick")),
		[]*irc.Message{})

	mustMatchMsg(t,
		i.ProcessMessage(types.RobustId{}, ids["secure"], irc.ParseMessage("NICK anothernick")),
		":newnick!blah@robust/0x13b5aa0a2bcfb8ad NICK :anothernick")
}
Ejemplo n.º 26
0
Archivo: main.go Proyecto: Ell/bot
func (logger *Logger) handleMessages() {
	for msg := range logger.messages {
		coreMsg, err := core.ParseMessage(msg)
		if err != nil {
			panic(err)
		}

		ircMsg := irc.ParseMessage(coreMsg.Message)

		if ircMsg.Command != irc.PING {
			go logger.logRaw(ircMsg, coreMsg.Server)

			if ircMsg.Command == irc.PRIVMSG {
				go logger.logMessage(ircMsg, coreMsg.Server)
			}
		}

		fmt.Printf("%+v\n", ircMsg.String())
	}
}
Ejemplo n.º 27
0
Archivo: main.go Proyecto: Ell/bot
func (handler *Handler) handleMessage(msg string) {
	coreMsg, err := core.ParseMessage(msg)
	if err != nil {
		panic(err)
	}

	ircMsg := irc.ParseMessage(coreMsg.Message)
	server := coreMsg.Server

	callbacks, ok := handler.Callbacks[ircMsg.Command]

	if !ok {
		return
	}

	for _, c := range callbacks {
		go c.Run(handler.Sender, ircMsg, server)
	}

	fmt.Printf("%+v\n", coreMsg.Message)
}
Ejemplo n.º 28
0
func relevantPart(msg *irc.Message, prev, next logCursor, reset logReset) (bool, error) {
	if len(msg.Params) < 1 {
		return false, nil
	}

	lcnames := multipleChannels(msg.Params[0])
	for {
		rmsg, err := prev(types.RobustIRCFromClient)
		if err != nil {
			if err == CursorEOF {
				break
			}
			return true, err
		}
		pmsg := irc.ParseMessage(rmsg.Data)
		if pmsg.Command == irc.JOIN && anyChanInChannels(lcnames, multipleChannels(pmsg.Params[0])) {
			return true, nil
		}
	}

	return false, nil
}
Ejemplo n.º 29
0
func relevantTopic(msg *irc.Message, prev, next logCursor, reset logReset) (bool, error) {
	if len(msg.Params) < 1 {
		return false, nil
	}

	for {
		rmsg, err := next(types.RobustIRCFromClient)
		if err != nil {
			if err == CursorEOF {
				break
			}
			return true, err
		}
		nmsg := irc.ParseMessage(rmsg.Data)
		// There is a newer TOPIC command for this channel, discard the old one.
		if nmsg.Command == irc.TOPIC && nmsg.Params[0] == msg.Params[0] {
			return false, nil
		}
	}

	return true, nil
}
Ejemplo n.º 30
0
func (i *IRCServer) cmdServiceAlias(s *Session, reply *Replyctx, msg *irc.Message) {
	aliases := map[string]string{
		"NICKSERV": "PRIVMSG NickServ :",
		"NS":       "PRIVMSG NickServ :",
		"CHANSERV": "PRIVMSG ChanServ :",
		"CS":       "PRIVMSG ChanServ :",
		"OPERSERV": "PRIVMSG OperServ :",
		"OS":       "PRIVMSG OperServ :",
		"MEMOSERV": "PRIVMSG MemoServ :",
		"MS":       "PRIVMSG MemoServ :",
		"HOSTSERV": "PRIVMSG HostServ :",
		"HS":       "PRIVMSG HostServ :",
		"BOTSERV":  "PRIVMSG BotServ :",
		"BS":       "PRIVMSG BotServ :",
	}
	for alias, expanded := range aliases {
		if strings.ToUpper(msg.Command) != alias {
			continue
		}
		i.cmdPrivmsg(s, reply, irc.ParseMessage(expanded+strings.Join(msg.Params, " ")))
		return
	}
}