コード例 #1
0
ファイル: irc.go プロジェクト: sztanpet/obscommits
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
}
コード例 #2
0
ファイル: factoids.go プロジェクト: sztanpet/obscommits
func HandleAdmin(c *sirc.IConn, m *irc.Message) (abort bool) {
	matches := adminRE.FindStringSubmatch(m.Trailing)
	if len(matches) == 0 {
		return
	}

	var savestate bool
	abort = true

	command := matches[1]
	factoidkey := strings.ToLower(matches[2])
	newfactoidkey := strings.ToLower(matches[3])
	factoid := matches[3]
	if len(matches[4]) > 0 {
		factoid = matches[3] + matches[4]
	}

	switch command {
	case "add":
		fallthrough
	case "mod":
		state.Lock()
		defer state.Unlock()

		s.Factoids[factoidkey] = factoid
		savestate = true
		c.Notice(m, "Added/Modified successfully")

	case "del":
		state.Lock()
		defer state.Unlock()

	restartdelete:
		if _, ok := s.Factoids[factoidkey]; ok {
			delete(s.Factoids, factoidkey)
			c.Notice(m, "Deleted successfully")
			// clean up the aliases too
			for k, v := range s.Aliases {
				if v == factoidkey {
					delete(s.Aliases, k)
				}
			}
		} else if factoidkey, ok = s.Aliases[factoidkey]; ok {
			c.Notice(m, "Found an alias, deleting the original factoid")
			goto restartdelete
		}

		savestate = true

	case "rename":
		if !alphaRE.MatchString(newfactoidkey) {
			return
		}
		state.Lock()
		defer state.Unlock()

		if _, ok := s.Factoids[newfactoidkey]; ok {
			c.Notice(m, "Renaming would overwrite, please delete first")
			return
		}
		if _, ok := s.Aliases[newfactoidkey]; ok {
			c.Notice(m, "Renaming would overwrite an alias, please delete first")
			return
		}
		if _, ok := s.Factoids[factoidkey]; ok {
			s.Factoids[newfactoidkey] = s.Factoids[factoidkey]
			delete(s.Factoids, factoidkey)
			// rename the aliases too
			for k, v := range s.Aliases {
				if v == factoidkey {
					s.Aliases[k] = newfactoidkey
				}
			}
			savestate = true
			c.Notice(m, "Renamed successfully")
		} else {
			c.Notice(m, "Not present")
		}

	case "addalias":
		fallthrough
	case "modalias":
		if !alphaRE.MatchString(newfactoidkey) {
			return
		}

		state.Lock()
		defer state.Unlock()

		// newfactoidkey is the factoid we are going to add an alias for
		// if itself is an alias, get the original factoid key, that is what
		// getfactoidByKey does
		_, newfactoidkey, ok := getfactoidByKey(newfactoidkey)
		if ok {
			s.Aliases[factoidkey] = newfactoidkey
			savestate = true
			c.Notice(m, "Added/Modified alias for ", newfactoidkey, " successfully")
		} else {
			c.Notice(m, "No factoid with name ", newfactoidkey, " found")
		}

	case "delalias":
		state.Lock()
		defer state.Unlock()

		if _, ok := s.Aliases[factoidkey]; ok {
			c.Notice(m, "Deleted alias successfully")
			delete(s.Aliases, factoidkey)
			savestate = true
		}

	default:
		abort = false
		return
	}

	if savestate {
		state.Save(false)
		tpl.invalidate()
	}

	return
}