~kris/9p

disco

6253e6bee83073ea1d5d4ade9e2d4d7076cb8303 — Sean Hinchee 7 years ago f2e6645
run go (vet fmt)
5 files changed, 65 insertions(+), 64 deletions(-)

M commands.go
M config.go
M events.go
M main.go
M menu.go
M commands.go => commands.go +2 -2
@@ 1,10 1,10 @@
package main

import (
	"github.com/bwmarrin/discordgo"
	"regexp"
	"strconv"
	"strings"
	"github.com/bwmarrin/discordgo"
)

//ParseForCommands parses input for Commands, returns message if no command specified, else return is empty


@@ 122,7 122,7 @@ func ParseForCommands(line string) string {
		}
		Msg(TextMsg, "name -> %s\n", newName)
		return ""
	}	
	}
	return line
}


M config.go => config.go +30 -29
@@ 1,34 1,35 @@
package main

import (
	"bitbucket.org/mischief/libauth"
	"bufio"
	"fmt"
	"github.com/mischief/ndb"
	"log"
	"os"
	"os/user"
sc	"strconv"
	sc "strconv"
	"strings"
	"bitbucket.org/mischief/libauth"
	"github.com/mischief/ndb"
)

// Types of auth we can do ­ add handling for new modes to atoam()
type AuthModes int

const (
	Pass		AuthModes = iota
	Pass AuthModes = iota
	Factotum
	Unknown		// Placeholder
	Unknown // Placeholder
)

//Configuration is a struct that contains all configuration fields
type Configuration struct {
	AuthMode		AuthModes
	Username		string
	LoadBacklog		bool
	Messages		int
	PromptChar		string
	TimestampChar	string
	password		string
	AuthMode      AuthModes
	Username      string
	LoadBacklog   bool
	Messages      int
	PromptChar    string
	TimestampChar string
	password      string
}

// Config is the global configuration of discord-cli


@@ 50,15 51,15 @@ func atob(s string) bool {
// Convert a string such as "factotum" into factotum
func atoam(s string) AuthModes {
	s = strings.ToLower(s)
	

	if s == "pass" {
		return Pass
	}
	

	if s == "factotum" {
		return Factotum
	}
	

	return Unknown
}



@@ 87,18 88,18 @@ Start:
	if err != nil {
		log.Fatal("error: Could not ")
	}
	

	Config.Username = ndb.Search("username", "").Search("username")
	Config.AuthMode = atoam(ndb.Search("auth", "").Search("auth"))
	

	if Config.AuthMode == Pass {
		Config.password = ndb.Search("username", "").Search("password")
	}

	Config.LoadBacklog		= atob(ndb.Search("loadbacklog", "").Search("loadbacklog"))
	Config.Messages, _		= sc.Atoi(ndb.Search("messages", "").Search("messages"))
	Config.PromptChar		= ndb.Search("promptchar", "").Search("promptchar")
	Config.TimestampChar	= ndb.Search("timestampchar", "").Search("timestampchar")
	Config.LoadBacklog = atob(ndb.Search("loadbacklog", "").Search("loadbacklog"))
	Config.Messages, _ = sc.Atoi(ndb.Search("messages", "").Search("messages"))
	Config.PromptChar = ndb.Search("promptchar", "").Search("promptchar")
	Config.TimestampChar = ndb.Search("timestampchar", "").Search("timestampchar")

}



@@ 118,14 119,14 @@ func CreateConfig() {
	raw := fmt.Sprintf("auth=pass\nloadbacklog=true\nmessages=10\npromptchar=→\ntimestampchar=>\n\nusername=%s	password=\n", scan.Text())

	// Create File
	os.Mkdir(usr.HomeDir + "/lib", 0775)
	
	os.Mkdir(usr.HomeDir+"/lib", 0775)

	file, err := os.Create(usr.HomeDir + ConfigPath)
	

	if err != nil {
		log.Fatalln(err)
	}
	

	file.Chmod(0600)

	// PrintToFile


@@ 150,9 151,9 @@ func CheckState() {
	if Config.Username == "" {
		log.Fatalln("Error: No Username Specified, please edit " + usr.HomeDir + ConfigPath)
	}
	

	// Check and handle password loading
	switch(Config.AuthMode){
	switch Config.AuthMode {
	case Factotum:
		// Acquire password from factotum
		userPwd, err := libauth.Getuserpasswd("proto=pass service=discord user=%s server=discordapp.com", Config.Username)


@@ 171,8 172,8 @@ func CheckState() {
	default:
		// Password is already loaded in auth=pass mode
	}
	
	if(Config.password == "") {

	if Config.password == "" {
		log.Fatalln("Error: No password loaded, cannot auth. Are you missing a factotum key or password= tuple?")
	}
}

M events.go => events.go +1 -1
@@ 38,7 38,7 @@ func newMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
	}
	DMs, err := Session.DiscordGo.UserChannels()
	if err != nil {
		// No DMs 
		// No DMs
		return
	}
	for _, channel := range DMs {

M main.go => main.go +3 -3
@@ 79,7 79,7 @@ func main() {
		//fmt.Print("> ")
		//line, _ := rl.Readline()
		line, err := reader.ReadString('\n')
		

		if err == io.EOF {
			break
		}


@@ 89,7 89,7 @@ func main() {
			for {
				subline, _ := reader.ReadString('\n')
				line += subline
				if strings.Index(subline,  "```") != -1 {
				if strings.Index(subline, "```") != -1 {
					break
				}
			}


@@ 163,7 163,7 @@ func ParseForMentions(line string) string {
	return lineByte
}

//ReplaceMentions replaces mentions to ID 
//ReplaceMentions replaces mentions to ID
func ReplaceMentions(input string) string {
	// Check for guild members that match
	for _, member := range State.Guild.Members {

M menu.go => menu.go +29 -29
@@ 305,39 305,39 @@ func AddUserChannelMenu() {
	if State.Channel.GuildID == "" {
		Msg(ErrorMsg, "Currently in a user channel, move to a guild with :g\n")
	} else {
	SelectMap := make(map[int]string)
Start:
	SelectID := 0
	for _, Member := range State.Members {
		SelectMap[SelectID] = Member.User.ID
		Msg(TextMsg, "[%d] %s\n", SelectID, Member.User.Username)
		SelectID++
	}
	var response string
	fmt.Scanf("%s\n", &response)
		SelectMap := make(map[int]string)
	Start:
		SelectID := 0
		for _, Member := range State.Members {
			SelectMap[SelectID] = Member.User.ID
			Msg(TextMsg, "[%d] %s\n", SelectID, Member.User.Username)
			SelectID++
		}
		var response string
		fmt.Scanf("%s\n", &response)

	if response == "b" {
		return
	}
		if response == "b" {
			return
		}

	ResponseInteger, err := strconv.Atoi(response)
	if err != nil {
		Msg(ErrorMsg, "(CH) Conversion Error: %s\n", err)
		goto Start
	}
		ResponseInteger, err := strconv.Atoi(response)
		if err != nil {
			Msg(ErrorMsg, "(CH) Conversion Error: %s\n", err)
			goto Start
		}

	if ResponseInteger > SelectID-1 || ResponseInteger < 0 {
		Msg(ErrorMsg, "(CH) Error: ID is out of bound\n")
		goto Start
	}
	Chan, err := Session.DiscordGo.UserChannelCreate(SelectMap[ResponseInteger])
		if ResponseInteger > SelectID-1 || ResponseInteger < 0 {
			Msg(ErrorMsg, "(CH) Error: ID is out of bound\n")
			goto Start
		}
		Chan, err := Session.DiscordGo.UserChannelCreate(SelectMap[ResponseInteger])

	if Chan.LastMessageID == "" {
		var firstMessage string
		fmt.Scanf("%s\n", &firstMessage)
		Session.DiscordGo.ChannelMessageSend(Chan.ID, "Test")
	}
	State.Channel = Chan
		if Chan.LastMessageID == "" {
			var firstMessage string
			fmt.Scanf("%s\n", &firstMessage)
			Session.DiscordGo.ChannelMessageSend(Chan.ID, "Test")
		}
		State.Channel = Chan
	}
}