forked from gotify/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.go
203 lines (172 loc) · 4.31 KB
/
plugin.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
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
195
196
197
198
199
200
201
202
203
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/gotify/plugin-api"
"io/ioutil"
"log"
"net/http"
"net/url"
"sort"
"sync"
"time"
)
const FeedURL string = "https://security.archlinux.org/advisory/feed.atom"
type Feed struct {
XMLName xml.Name `xml:"feed"`
Entries EntrySlice `xml:"entry"`
}
type Entry struct {
XMLName xml.Name `xml:"entry"`
Title string `xml:"title"`
Link LinkStruct `xml:"link"`
Published time.Time `xml:"published"`
}
type LinkStruct struct {
XMLName xml.Name `xml:"link"`
Location string `xml:"href,attr"`
}
type Storage struct {
LastPublished time.Time `json:"last_published"`
}
type Config struct {
RefreshInterval int `yaml:"refresh_interval"`
}
type EntrySlice []Entry
func (es *EntrySlice) Len() int { return len(*es) }
func (es *EntrySlice) Less(i, j int) bool { return (*es)[i].Published.Before((*es)[j].Published) }
func (es *EntrySlice) Swap(i, j int) { (*es)[i], (*es)[j] = (*es)[j], (*es)[i] }
func GetGotifyPluginInfo() plugin.Info {
return plugin.Info{
ModulePath: "github.com/buckket/gotify-archsec",
Version: "1.0.0",
Author: "buckket",
Website: "https://github.com/buckket/gotify-archsec",
Description: "Poll Arch Linux Security Feed for new advisories",
License: "GPLv3+",
Name: "archsec",
}
}
type ArchSec struct {
msgHandler plugin.MessageHandler
storageHandler plugin.StorageHandler
config *Config
enabled bool
stop chan struct{}
wg *sync.WaitGroup
ticker *time.Ticker
}
func (c *ArchSec) FetchFeed() {
var storage Storage
storageBytes, err := c.storageHandler.Load()
if err != nil {
log.Printf("could not load storage data: %v", err)
}
err = json.Unmarshal(storageBytes, &storage)
if err != nil {
log.Printf("could not parse storage data: %v", err)
}
resp, err := http.Get(FeedURL)
if err != nil {
log.Printf("error while fetching feed: %v", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var feed Feed
err = xml.Unmarshal(body, &feed)
if err != nil {
log.Printf("error while parsing feed: %v", err)
return
}
sort.Sort(&feed.Entries)
for _, entry := range feed.Entries {
if entry.Published.After(storage.LastPublished) {
storage.LastPublished = entry.Published
_ = c.msgHandler.SendMessage(plugin.Message{
Title: entry.Title,
Message: entry.Link.Location,
})
}
}
newStorage, err := json.Marshal(storage)
if err != nil {
log.Printf("could not marshal storage data: %v", err)
return
}
err = c.storageHandler.Save(newStorage)
if err != nil {
log.Printf("could not save storage data: %v", err)
}
}
func (c *ArchSec) Enable() error {
if c.enabled {
return fmt.Errorf("plugin already enabled")
}
c.wg = &sync.WaitGroup{}
c.stop = make(chan struct{})
c.ticker = time.NewTicker(time.Duration(c.config.RefreshInterval) * time.Second)
c.enabled = true
c.wg.Add(1)
go func() {
defer c.wg.Done()
for {
select {
case <-c.stop:
return
case <-c.ticker.C:
c.FetchFeed()
}
}
}()
return nil
}
func (c *ArchSec) Disable() error {
if c.enabled {
c.enabled = false
c.ticker.Stop()
close(c.stop)
c.wg.Wait()
} else {
return fmt.Errorf("plugin already disabled")
}
return nil
}
func (c *ArchSec) GetDisplay(location *url.URL) string {
var storage Storage
storageBytes, err := c.storageHandler.Load()
if err != nil {
return fmt.Sprintf("Could not load storage data: %v", err)
}
err = json.Unmarshal(storageBytes, &storage)
if err != nil {
return fmt.Sprintf("Could not parse storage data: %v (%v)", err, storageBytes)
}
if storage.LastPublished.IsZero() {
return fmt.Sprintf("Feed has not been updated as of yet")
} else {
return fmt.Sprintf("Last entry was published at %s", storage.LastPublished)
}
}
func (c *ArchSec) SetStorageHandler(h plugin.StorageHandler) {
c.storageHandler = h
}
func (c *ArchSec) SetMessageHandler(h plugin.MessageHandler) {
c.msgHandler = h
}
func (c *ArchSec) DefaultConfig() interface{} {
return &Config{
RefreshInterval: 60,
}
}
func (c *ArchSec) ValidateAndSetConfig(config interface{}) error {
c.config = config.(*Config)
return nil
}
func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin {
return &ArchSec{}
}
func main() {
panic("this should be built as go plugin")
}