-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
157 lines (129 loc) · 3.4 KB
/
commands.go
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
package main
import (
"fmt"
"os"
"strings"
"github.com/alexcoder04/friendly/v2"
"github.com/alexcoder04/iserv2go/iserv/types"
)
func CommandFilesLs(s []string) {
data, err := Client.Files.ReadDir(s[0])
if err != nil {
friendly.Die("Cannot ls: %s", err.Error())
}
for _, f := range data {
fmt.Println(f.Name())
}
}
func CommandFilesCat(s []string) {
data, err := Client.Files.ReadFile(s[0])
if err != nil {
friendly.Die("Cannot cat: %s", err.Error())
}
fmt.Println(data)
}
func CommandFilesDownload(s []string) {
err := Client.Files.DownloadFile(s[0], s[1])
if err != nil {
friendly.Die("Cannot download: %s", err.Error())
}
}
func CommandFilesUpload(s []string) {
err := Client.Files.UploadFile(s[0], s[1])
if err != nil {
friendly.Die("Cannot upload: %s", err.Error())
}
}
func CommandWebGetBadges(s []string) {
badges, err := Client.Web.GetBadges()
if err != nil {
friendly.Die("Cannot load badges: %s", err.Error())
}
for key, value := range badges {
fmt.Printf("%s: %d\n", key, value)
}
}
func CommandWebGetNotifications(s []string) {
notifications, err := Client.Web.GetNotifications()
if err != nil {
friendly.Die("Cannot load notifications: %s", err.Error())
}
if notifications.Status != "success" {
friendly.Die("IServ didn't return success: %s", notifications.Status)
}
for _, n := range notifications.Data.Notifications {
fmt.Printf("%v: %s: %s\n", n.Date, n.Title, n.Message)
}
}
func CommandWebGetUpcomingEvents(s []string) {
events, err := Client.Web.GetUpcomingEvents(30)
if err != nil {
friendly.Die("Cannot load events: %s", err.Error())
}
if len(events.Errors) > 0 {
friendly.Die("IServ returned error: %s", strings.Join(events.Errors, ", "))
}
for _, e := range events.Events {
fmt.Printf("%v - %v: %s\n", e.Start, e.End, e.Title)
}
}
func CommandWebGetCurrentExercises(s []string) {
exercises, err := Client.Web.GetCurrentExercises()
if err != nil {
friendly.Die("Cannot load exercises: %s", err.Error())
}
for _, e := range exercises {
fmt.Printf("%s: %s by %s\n", e.DueDate, e.Title, e.Teacher)
}
}
func CommandWebGetPastExercises(s []string) {
exercises, err := Client.Web.GetPastExercises()
if err != nil {
friendly.Die("Cannot load exercises: %s", err.Error())
}
for _, e := range exercises {
fmt.Printf("%d: %s: '%s' by %s\n", e.Id, e.DueDate, e.Title, e.Teacher)
}
}
func CommandEmailListMailboxes(s []string) {
mailboxes, err := Client.Email.ListMailboxes()
if err != nil {
friendly.Die("Cannot load email mailboxes: %s", err.Error())
}
for _, m := range mailboxes {
fmt.Println(m.Name)
}
}
func CommandEmailReadMailbox(s []string) {
var mb string
if len(s) < 1 {
mb = "INBOX"
} else {
mb = s[0]
}
messages, err := Client.Email.ReadMailbox(mb, 50)
if err != nil {
friendly.Die("Cannot read messages: %s", err.Error())
}
for _, m := range messages {
fmt.Printf("'%s' from %s\n", m.Envelope.Subject, m.Envelope.Sender[0].Address())
}
}
func CommandEmailSendMail(s []string) {
if len(s) > 3 {
friendly.Die("Not enough arguments")
}
myMail := fmt.Sprintf("%s@%s", os.Getenv("ISERV_USERNAME"), os.Getenv("ISERV_HOST"))
m := types.EMail{
Subject: s[1],
From: myMail,
To: s[0],
ToDispName: friendly.GetFullNameFromMailAddress(s[0]),
CCs: []string{},
Body: s[2],
}
err := Client.Email.SendMail(m)
if err != nil {
friendly.Die("Error sending mail: %s", err.Error())
}
}