Ejemplo n.º 1
0
func Uid(hook string, msg *parser.Message, ircd *IRCd) {
	nickname, hopcount, nickTS := msg.Args[0], msg.Args[1], msg.Args[2]
	umode, username, hostname := msg.Args[3], msg.Args[4], msg.Args[5]
	ip, uid, name := msg.Args[6], msg.Args[7], msg.Args[8]
	_ = umode

	err := user.Import(uid, nickname, username, hostname, ip, hopcount, nickTS, name)
	if err != nil {
		// TODO: TS check - Kill remote or local? For now, we kill remote.
		ircd.ToServer <- &parser.Message{
			Prefix:  Config.SID,
			Command: parser.CMD_SQUIT,
			Args: []string{
				uid,
				err.String(),
			},
			DestIDs: []string{msg.SenderID},
		}
	}

	for fwd := range server.Iter() {
		if fwd != msg.SenderID {
			log.Debug.Printf("Forwarding UID from %s to %s", msg.SenderID, fwd)
			fmsg := msg.Dup()
			fmsg.DestIDs = []string{fwd}
		}
	}
}
Ejemplo n.º 2
0
// Local PARTs only
func Part(hook string, msg *parser.Message, ircd *IRCd) {
	reason := "Leaving"
	if len(msg.Args) > 1 {
		reason = msg.Args[1]
	}

	leftchans := []string{}

	for _, channame := range strings.Split(msg.Args[0], ",") {
		channel, err := channel.Get(channame, false)
		if num, ok := err.(*parser.Numeric); ok {
			ircd.ToClient <- num.Message(msg.SenderID)
			continue
		}

		members, err := channel.Part(msg.SenderID)
		if num, ok := err.(*parser.Numeric); ok {
			ircd.ToClient <- num.Message(msg.SenderID)
			continue
		}

		notify := []string{}
		for _, uid := range members {
			if uid[:3] == Config.SID {
				notify = append(notify, uid)
			}
		}

		if len(notify) > 0 {
			ircd.ToClient <- &parser.Message{
				Prefix:  msg.SenderID,
				Command: parser.CMD_PART,
				Args: []string{
					channel.Name(),
				},
				DestIDs: notify,
			}
		}

		leftchans = append(leftchans, channame)
	}

	// Forward to other servers
	if len(leftchans) > 0 {
		leftstr := strings.Join(leftchans, ",")
		for sid := range server.Iter() {
			ircd.ToServer <- &parser.Message{
				Prefix:  msg.SenderID,
				Command: parser.CMD_PART,
				Args: []string{
					leftstr,
					reason,
				},
				DestIDs: []string{sid},
			}
		}
	}
}
Ejemplo n.º 3
0
func SQuit(hook string, msg *parser.Message, ircd *IRCd) {
	split, reason := msg.Args[0], msg.Args[1]

	if split == Config.SID {
		split = msg.SenderID
	}

	// Forward
	for sid := range server.Iter() {
		if sid != msg.SenderID {
			msg := msg.Dup()
			msg.DestIDs = []string{sid}
			ircd.ToServer <- msg
		}
	}
	if server.IsLocal(split) {
		ircd.ToServer <- &parser.Message{
			Command: parser.CMD_ERROR,
			Args: []string{
				"SQUIT: " + reason,
			},
		}
	}

	sids := server.Unlink(split)
	peers := user.Netsplit(sids)
	notify := channel.Netsplit(Config.SID, peers)

	log.Debug.Printf("NET SPLIT: %s", split)
	log.Debug.Printf(" -   SIDs: %v", sids)
	log.Debug.Printf(" -  Peers: %v", peers)
	log.Debug.Printf(" - Notify: %v", notify)

	for uid, peers := range notify {
		if len(peers) > 0 {
			ircd.ToClient <- &parser.Message{
				Prefix:  uid,
				Command: parser.CMD_QUIT,
				Args: []string{
					"*.net *.split",
				},
				DestIDs: peers,
			}
		}
	}
	// Delete all of the peers
	if len(peers) > 0 {
		ircd.ToClient <- &parser.Message{
			Command: parser.INT_DELUSER,
			DestIDs: peers,
		}
	}
}
Ejemplo n.º 4
0
// Local joins only
func Join(hook string, msg *parser.Message, ircd *IRCd) {
	// todo keys
	for _, channame := range strings.Split(msg.Args[0], ",") {
		channel, err := channel.Get(channame, true)
		if num, ok := err.(*parser.Numeric); ok {
			ircd.ToClient <- num.Message(msg.SenderID)
			continue
		}

		members, err := channel.Join(msg.SenderID)
		if num, ok := err.(*parser.Numeric); ok {
			ircd.ToClient <- num.Message(msg.SenderID)
			continue
		}

		notify := []string{}
		for _, uid := range members {
			if uid[:3] == Config.SID {
				notify = append(notify, uid)
			}
		}

		// Forward to other servers
		for sid := range server.Iter() {
			ircd.ToServer <- &parser.Message{
				Prefix:  msg.SenderID,
				Command: parser.CMD_JOIN,
				Args: []string{
					channel.TS(),
					channel.Name(),
					"+",
				},
				DestIDs: []string{sid},
			}
		}

		if len(notify) > 0 {
			ircd.ToClient <- &parser.Message{
				Prefix:  msg.SenderID,
				Command: parser.CMD_JOIN,
				Args: []string{
					channel.Name(),
				},
				DestIDs: notify,
			}
		}

		ircd.ToClient <- channel.NamesMessage(msg.SenderID)
	}
}
Ejemplo n.º 5
0
// Server PART
func SPart(hook string, msg *parser.Message, ircd *IRCd) {
	chanlist, reason := strings.Split(msg.Args[0], ","), msg.Args[1]

	// Forward on to other servers
	for fwd := range server.Iter() {
		if fwd != msg.SenderID {
			log.Debug.Printf("Forwarding PART from %s to %s", msg.SenderID, fwd)
			fmsg := msg.Dup()
			fmsg.DestIDs = []string{fwd}
		}
	}

	for _, channame := range chanlist {
		channel, err := channel.Get(channame, false)
		if num, ok := err.(*parser.Numeric); ok {
			ircd.ToServer <- num.Error(msg.SenderID)
			return
		}

		chanusers, err := channel.Part(msg.Prefix)
		if num, ok := err.(*parser.Numeric); ok {
			ircd.ToServer <- num.Error(msg.SenderID)
			return
		}

		notify := []string{}
		for _, uid := range chanusers {
			if uid[:3] == Config.SID {
				notify = append(notify, uid)
			}
		}

		if len(notify) > 0 {
			ircd.ToClient <- &parser.Message{
				Prefix:  msg.Prefix,
				Command: parser.CMD_PART,
				Args: []string{
					channel.Name(),
					reason,
				},
				DestIDs: notify,
			}
		}
	}
}
Ejemplo n.º 6
0
func Sid(hook string, msg *parser.Message, ircd *IRCd) {
	servname, hopcount, sid, desc := msg.Args[0], msg.Args[1], msg.Args[2], msg.Args[3]

	err := server.Link(msg.Prefix, sid, servname, hopcount, desc)
	if err != nil {
		ircd.ToServer <- &parser.Message{
			Prefix:  Config.SID,
			Command: parser.CMD_SQUIT,
			Args: []string{
				sid,
				err.String(),
			},
			DestIDs: []string{msg.SenderID},
		}
	}

	for fwd := range server.Iter() {
		if fwd != msg.SenderID {
			log.Debug.Printf("Forwarding SID from %s to %s", msg.SenderID, fwd)
			fmsg := msg.Dup()
			fmsg.DestIDs = []string{fwd}
		}
	}
}
Ejemplo n.º 7
0
// Server JOIN and SJOIN
func SJoin(hook string, msg *parser.Message, ircd *IRCd) {
	chanTS, channame, mode := msg.Args[0], msg.Args[1], msg.Args[2]

	uids := []string{msg.Prefix}
	if len(msg.Prefix) == 3 {
		if len(msg.Args) == 3 {
			return
		}
		uids = strings.Split(msg.Args[3], " ")
	}

	_ = chanTS
	_ = mode

	// Forward on to other servers
	for fwd := range server.Iter() {
		if fwd != msg.SenderID {
			log.Debug.Printf("Forwarding SJOIN from %s to %s", msg.SenderID, fwd)
			fmsg := msg.Dup()
			fmsg.DestIDs = []string{fwd}
		}
	}

	for i, uid := range uids {
		uids[i] = uid[len(uid)-9:]
	}

	channel, err := channel.Get(channame, true)
	if num, ok := err.(*parser.Numeric); ok {
		ircd.ToServer <- num.Error(msg.SenderID)
		return
	}

	if len(uids) == 0 && len(msg.Prefix) == 9 {
		uids = []string{msg.Prefix}
	}

	chanusers, err := channel.Join(uids...)
	if num, ok := err.(*parser.Numeric); ok {
		ircd.ToServer <- num.Error(msg.SenderID)
		return
	}

	notify := []string{}
	for _, uid := range chanusers {
		if uid[:3] == Config.SID {
			notify = append(notify, uid)
		}
	}

	if len(notify) > 0 {
		for _, joiner := range uids {
			ircd.ToClient <- &parser.Message{
				Prefix:  joiner,
				Command: parser.CMD_JOIN,
				Args: []string{
					channel.Name(),
				},
				DestIDs: notify,
			}
		}
	}
}
Ejemplo n.º 8
0
func Quit(hook string, msg *parser.Message, ircd *IRCd) {
	quitter := msg.SenderID
	reason := "Client Quit"

	if len(msg.Args) > 0 {
		reason = msg.Args[0]
	}

	if len(msg.SenderID) == 3 {
		quitter = msg.Prefix
	}

	for sid := range server.Iter() {
		log.Debug.Printf("Forwarding QUIT from %s to %s", quitter, sid)
		if sid != msg.SenderID {
			ircd.ToServer <- &parser.Message{
				Prefix:  quitter,
				Command: parser.CMD_QUIT,
				Args: []string{
					reason,
				},
				DestIDs: []string{sid},
			}
		}
	}

	members := channel.PartAll(quitter)
	log.Debug.Printf("QUIT recipients: %#v", members)
	peers := make(map[string]bool)
	for _, users := range members {
		for _, uid := range users {
			if uid[:3] == Config.SID && uid != quitter {
				peers[uid] = true
			}
		}
	}
	if len(peers) > 0 {
		notify := []string{}
		for peer := range peers {
			notify = append(notify, peer)
		}
		ircd.ToClient <- &parser.Message{
			Prefix:  quitter,
			Command: parser.CMD_QUIT,
			Args: []string{
				"Quit: " + reason,
			},
			DestIDs: notify,
		}
	}

	// Will be dropped if it's a remote client
	error := &parser.Message{
		Command: parser.CMD_ERROR,
		Args: []string{
			"Closing Link (" + reason + ")",
		},
		DestIDs: []string{
			quitter,
		},
	}
	ircd.ToClient <- error
}
Ejemplo n.º 9
0
// Handle the NICK, USER, SERVER, and PASS messages
func ConnReg(hook string, msg *parser.Message, ircd *IRCd) {
	var err os.Error
	var u *user.User
	var s *server.Server

	switch len(msg.SenderID) {
	case 3:
		s = server.Get(msg.SenderID, true)
	case 9:
		u = user.Get(msg.SenderID)
	}

	switch msg.Command {
	case parser.CMD_NICK:
		// NICK <nick>
		if u != nil {
			nick := msg.Args[0]
			err = u.SetNick(nick)
		}
	case parser.CMD_USER:
		// USER <user> . . :<real name>
		if u != nil {
			username, realname := msg.Args[0], msg.Args[3]
			err = u.SetUser(username, realname)
		}
	case parser.CMD_PASS:
		if s != nil {
			if len(msg.Args) != 4 {
				return
			}
			// PASS <password> TS <ver> <pfx>
			err = s.SetPass(msg.Args[0], msg.Args[2], msg.Args[3])
		}
	case parser.CMD_CAPAB:
		if s != nil {
			err = s.SetCapab(msg.Args[0])
		}
	case parser.CMD_SERVER:
		if s != nil {
			err = s.SetServer(msg.Args[0], msg.Args[1])
		}
	default:
		log.Warn.Printf("Unknown command %q", msg)
	}

	if u != nil {
		if err != nil {
			switch err := err.(type) {
			case *parser.Numeric:
				msg := err.Message()
				msg.DestIDs = append(msg.DestIDs, u.ID())
				ircd.ToClient <- msg
				return
			default:
				msg := &parser.Message{
					Command: parser.CMD_ERROR,
					Args:    []string{err.String()},
					DestIDs: []string{u.ID()},
				}
				ircd.ToClient <- msg
				return
			}
		}

		nickname, username, realname, _ := u.Info()
		if nickname != "*" && username != "" {
			// Notify servers
			for sid := range server.Iter() {
				ircd.ToServer <- &parser.Message{
					Prefix:  Config.SID,
					Command: parser.CMD_UID,
					Args: []string{
						nickname,
						"1",
						u.TS(),
						"+i",
						username,
						"some.host",
						"127.0.0.1",
						u.ID(),
						realname,
					},
					DestIDs: []string{sid},
				}
			}

			// Process signon
			sendSignon(u, ircd)
			return
		}
	}

	if s != nil {
		if err != nil {
			switch err := err.(type) {
			case *parser.Numeric:
				msg := err.Message()
				msg.DestIDs = append(msg.DestIDs, s.ID())
				ircd.ToServer <- msg
				return
			default:
				msg := &parser.Message{
					Command: parser.CMD_ERROR,
					Args:    []string{err.String()},
					DestIDs: []string{s.ID()},
				}
				ircd.ToServer <- msg
				return
			}
		}

		sid, serv, pass, capab := s.Info()
		if sid != "" && serv != "" && pass != "" && len(capab) > 0 {
			// Notify servers
			for sid := range server.Iter() {
				ircd.ToServer <- &parser.Message{
					Prefix:  Config.SID,
					Command: parser.CMD_SID,
					Args: []string{
						serv,
						"2",
						sid,
						"some server",
					},
					DestIDs: []string{sid},
				}
			}

			sendServerSignon(s, ircd)
			Burst(s, ircd)
		}
	}
}