~kris/9p

disco

ref: 9cb32e016388fde789f6aec4876c744003039f2e disco/helper.go -rw-r--r-- 4.6 KiB
9cb32e01 — Sean Hinchee fix a whoopsie (thanks vik) 7 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main

import (
	"bufio"
	"fmt"
	"github.com/bwmarrin/discordgo"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"time"
)

//HexColor is a struct gives RGB values
type HexColor struct {
	R int
	G int
	B int
}

//Msg is a composition of Color.New printf functions
func Msg(MsgType, format string, a ...interface{}) {
	fmt.Printf(format, a...)
}

//Header simply prints a header containing state/session information
func Header() {
	Msg(InfoMsg, "Welcome, %s!\n\n", State.Session.User.Username)
	switch State.Channel.Type {
	case discordgo.ChannelTypeGuildText:
		Msg(InfoMsg, "Guild: %s, Channel: %s\n", State.Guild.Name, State.Channel.Name)
	case discordgo.ChannelTypeDM:
		Msg(InfoMsg, "Channel: %s\n", State.Channel.Recipients[0].Username)
	case discordgo.ChannelTypeGroupDM:
		var nicklist string
		for _, user := range State.Channel.Recipients {
			nicklist += user.Username
		}
		Msg(InfoMsg, "Channel: %s\n", nicklist)
	}
}

//ReceivingMessageParser parses receiving message for mentions, images and MultiLine and returns string array
func ReceivingMessageParser(m *discordgo.Message) []string {
	Message := m.ContentWithMentionsReplaced()

	//Parse images
	for _, Attachment := range m.Attachments {
		Message = Message + " " + Attachment.URL
	}

	// MultiLine comment parsing
	Messages := strings.Split(Message, "\n")

	return Messages
}

//PrintMessages prints amount of Messages to CLI
func PrintMessages(Amount int) {
	for Key, m := range State.Messages {
		name := m.Author.Username
		if member, ok := State.Members[m.Author.Username]; ok {
			if member.Nick != "" {
				name = member.Nick
			}
		}
		if Key >= len(State.Messages)-Amount {
			Messages := ReceivingMessageParser(m)
			for _, Msg := range Messages {
				MessagePrint(string(m.Timestamp), name, Msg)

			}
		}
	}
}

//Notify uses Notify-Send from libnotify to send a notification when a mention arrives.
func Notify(m *discordgo.Message) {
	if *enableNotify == false {
		return
	}
	var Title string
	channel, err := State.Session.DiscordGo.Channel(m.ChannelID)
	if err != nil {
		Msg(ErrorMsg, "(NOT) PM Error: %s\n", err)
	}
	switch channel.Type {
	case discordgo.ChannelTypeGuildText:
		Channel, err := State.Session.DiscordGo.Channel(m.ChannelID)
		if err != nil {
			Msg(ErrorMsg, "(NOT) Channel Error: %s\n", err)
		}
		Guild, err := State.Session.DiscordGo.Guild(Channel.GuildID)
		if err != nil {
			Msg(ErrorMsg, "(NOT) Guild Error: %s\n", err)
		}
		Title = "@" + m.Author.Username + " : " + Guild.Name + "/" + Channel.Name
	case discordgo.ChannelTypeDM:
		Title = fmt.Sprintf("%s (pm)\n", m.Author.Username)
	}
	switch runtime.GOOS {
	case "plan9":
		pr, pw := io.Pipe()
		cmd := exec.Command("/bin/aux/statusmsg", *notifyFlag, Title)
		cmd.Stdin = pr
		go func() {
			defer pw.Close()
			fmt.Fprintf(pw, "%s\n", m.ContentWithMentionsReplaced())
			cmd.Wait()
		}()
		err := cmd.Start()
		if err != nil {
			Msg(ErrorMsg, "%s\n", err)
		}
		ioutil.WriteFile("/dev/wctl", []byte("current"), 0644)
	default:
		cmd := exec.Command("notify-send", Title, m.ContentWithMentionsReplaced())
		err := cmd.Start()
		if err != nil {
			Msg(ErrorMsg, "(NOT) Check if libnotify is installed, or disable notifications.\n")
		}
	}

}

//MessagePrint prints one correctly formatted Message to stdout
func MessagePrint(Time, Username, Content string) {
	//Clean up emoji
	content := ParseForEmoji(Content)
	//var Color color.Attribute
	log.SetFlags(0)
	if *hideTimeStamp {
		log.Printf("%s %s %s\n", Username, Config.PromptChar, content)
	} else {
		TimeStamp, _ := time.Parse(time.RFC3339, Time)
		LocalTime := TimeStamp.Local().Format("2006/01/02 15:04:05")
		log.Printf("%s %s %s %s %s\n", LocalTime, Config.TimestampChar, Username, Config.PromptChar, content)

	}
	log.SetFlags(log.LstdFlags)
}

func dis(a, b int) float64 {
	return float64((a - b) * (a - b))
}

func Rawon() (*os.File, error) {
	consctl, err := os.OpenFile("/dev/consctl", os.O_WRONLY, 0200)
	if err != nil {
		/* not on Plan 9 */
		fmt.Println("\nNot running on Plan 9")
		return consctl, err
	}

	rawon := []byte("rawon")
	_, err = consctl.Write(rawon)
	if err != nil {
		consctl.Close()
		return consctl, err
	}

	return consctl, nil
}

func RawOff(consctl *os.File) error {
	//consctl, err := os.OpenFile("/dev/consctl", os.O_WRONLY, 0200)
	//if err != nil {
	//	/* not on Plan 9 */
	//	return err
	//}

	rawoff := []byte("rawoff")
	_, err := consctl.Write(rawoff)
	if err != nil {
		consctl.Close()
		return err
	}

	consctl.Close()
	return nil
}

func GetCons() string {
	cons, err := os.OpenFile("/dev/cons", os.O_RDWR, 0600)
	if err != nil {
		fmt.Println("Failed to open /dev/cons")
	}
	consScan := bufio.NewScanner(cons)
	consScan.Scan()
	return consScan.Text()
}