Exemplo n.º 1
0
func handlePurposeTopic(cmd string, parts []string) {
	// TODO - handle errors
	if len(parts) == 0 {
		switch cmd {
		case "purpose", "c-purpose", "g-purpose":
			if currChannelID[0] == 'C' {
				fmt.Println(findChannel(currChannelID).Purpose.Value)
			} else if currChannelID[0] == 'G' {
				fmt.Println(findGroup(currChannelID).Purpose.Value)
			}
		case "topic", "c-topic", "g-topic":
			if currChannelID[0] == 'C' {
				fmt.Println(findChannel(currChannelID).Topic.Value)
			} else if currChannelID[0] == 'G' {
				fmt.Println(findGroup(currChannelID).Topic.Value)
			}
		}
		return
	}
	id := channelID(parts[0])
	var newThing string
	// If we are changing the current channel or group
	if id == "" {
		newThing = strings.Join(parts, " ")
		id = currChannelID
	} else {
		if len(parts) > 1 {
			newThing = strings.Join(parts[1:], " ")
		} else {
			fmt.Println("Please specify the new value")
			return
		}
	}
	var result string
	var err error
	var resp slack.Response
	switch cmd {
	case "purpose", "c-purpose", "g-purpose":
		var r *slack.PurposeResponse
		r, err = s.SetPurpose(id, newThing)
		resp = r
		if r != nil {
			result = r.Purpose
		}
	case "topic", "c-topic", "g-topic":
		var r *slack.TopicResponse
		r, err = s.SetTopic(currChannelID, strings.Join(parts, " "))
		resp = r
		if r != nil {
			result = r.Topic
		}
	}
	if err != nil {
		fmt.Printf("Error setting new value - %v\n", err)
	} else if !resp.IsOK() {
		fmt.Printf("Error setting new value - %v\n", resp.Error())
	} else {
		fmt.Printf("New value is - %s\n", result)
	}
}
Exemplo n.º 2
0
func handleJoinLeave(cmd string, parts []string) {
	for _, ch := range parts {
		id := channelID(ch)
		if id == "" {
			fmt.Printf("%s not found\n", ch)
			continue
		}
		var r slack.Response
		var err error
		var msg, msgErr string
		switch cmd {
		case "c-join":
			r, err = s.ChannelJoin(ch)
			msg = "Joined channel %s\n"
			msgErr = "Unable to join channel %s - %v\n"
		case "c-leave":
			r, err = s.Leave(id)
			msg = "Left channel %s\n"
			msgErr = "Unable to leave channel %s - %v\n"
		case "g-leave":
			r, err = s.Leave(id)
			msg = "Left group %s\n"
			msgErr = "Unable to leave group %s - %v\n"
		}
		if err != nil {
			fmt.Printf(msgErr, ch, err)
			break
		} else if !r.IsOK() {
			fmt.Printf(msgErr, ch, r.Error())
		} else {
			fmt.Printf(msg, ch)
		}
	}
}
Exemplo n.º 3
0
func handleCreate(cmd string, parts []string) {
	for _, ch := range parts {
		var r slack.Response
		var err error
		if cmd == "c-create" {
			r, err = s.ChannelCreate(ch)
		} else if cmd == "g-create" {
			r, err = s.GroupCreate(ch)
		} else {
			r, err = s.GroupCreateChild(ch)
		}
		if err != nil {
			fmt.Printf("Unable to create %s - %v\n", ch, err)
			break
		} else if !r.IsOK() {
			fmt.Printf("Unable to create %s - %s\n", ch, r.Error())
		} else {
			if cmd == "c-create" {
				fmt.Printf("%s created\n", r.(*slack.ChannelResponse).Channel.Name)
			} else {
				fmt.Printf("%s created\n", r.(*slack.GroupResponse).Group.Name)
			}
		}
	}
}
Exemplo n.º 4
0
func handleArchive(cmd string, parts []string) {
	for _, ch := range parts {
		id := channelID(ch)
		if id == "" {
			fmt.Printf("%s not found\n", ch)
			continue
		}
		var r slack.Response
		var err error
		var action string
		if strings.Contains(cmd, "unarchive") {
			r, err = s.Unarchive(id)
			action = "unarchive"
		} else {
			r, err = s.Archive(id)
			action = "archive"
		}
		if err != nil {
			fmt.Printf("Unable to %s %s - %v\n", action, ch, err)
			break
		} else if !r.IsOK() {
			fmt.Printf("Unable to %s %s - %s\n", action, ch, r.Error())
		} else {
			fmt.Printf("%s %sd\n", action, ch)
		}
	}
}
Exemplo n.º 5
0
func handleInviteKick(cmd string, parts []string) {
	if len(parts) == 0 {
		fmt.Println("Please specify the users")
	} else {
		id := channelID(parts[0])
		users := parts[1:]
		// Use current channel and list of users
		if id == "" {
			id = currChannelID
			users = parts
		}
		for _, u := range users {
			var r slack.Response
			var err error
			var msg, msgErr string
			switch cmd {
			case "c-invite":
				r, err = s.ChannelInvite(id, userID(u))
				msg = "User %s invited to channel %s\n"
				msgErr = "Unable to invite user %s to channel %s - %v\n"
			case "c-kick":
				r, err = s.Kick(id, userID(u))
				msg = "User %s kicked from channel %s\n"
				msgErr = "Unable to kick user %s from channel %s - %v\n"
			case "g-invite":
				r, err = s.GroupInvite(id, userID(u))
				msg = "User %s invited to group %s\n"
				msgErr = "Unable to invite user %s to group %s - %v\n"
			case "g-kick":
				r, err = s.Kick(id, userID(u))
				msg = "User %s kicked from group %s\n"
				msgErr = "Unable to kick user %s from group %s - %v\n"
			}
			if err != nil {
				fmt.Printf(msgErr, u, parts[0], err)
				break
			} else if !r.IsOK() {
				fmt.Printf(msgErr, u, parts[0], r.Error())
			} else {
				fmt.Printf(msg, u, parts[0])
			}
		}
	}
}
Exemplo n.º 6
0
func handleInfo(cmd string, parts []string) {
	if len(parts) == 0 {
		parts = []string{channelName(currChannelID)}
	}
	for _, ch := range parts {
		id := channelID(ch)
		if id == "" {
			fmt.Printf("%s not found\n", ch)
			continue
		}
		var r slack.Response
		var err error
		if id != "" {
			if id[0] == 'C' {
				r, err = s.ChannelInfo(id)
			} else if id[0] == 'G' {
				r, err = s.GroupInfo(id)
			} else {
				fmt.Printf("IM with %s has no info\n", channelName(ch))
				continue
			}
		}
		if err != nil {
			fmt.Printf("Unable to retrieve info for %s - %v\n", ch, err)
		} else if !r.IsOK() {
			fmt.Printf("Unable to retrieve info for %s - %s\n", ch, r.Error())
		} else {
			var b []byte
			if id[0] == 'C' {
				b, err = json.MarshalIndent(r.(*slack.ChannelResponse).Channel, "", "  ")
			} else {
				b, err = json.MarshalIndent(r.(*slack.GroupResponse).Group, "", "  ")
			}
			if err != nil {
				fmt.Printf("Unable to retrieve info for %s - %v\n", ch, err)
			} else {
				fmt.Println(string(b))
			}
		}
	}
}