-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpathwar.go
114 lines (101 loc) · 2.45 KB
/
pathwar.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
package main
import (
"fmt"
"time"
"net/http"
"crypto/tls"
"encoding/json"
"io/ioutil"
"log"
"strings"
)
// Configuration of the pathwar module
type PathwarConfig struct {
Targets map[string][]string // List of servers/channels-users to broadcast
EndPoint string // https://user:[email protected]/
}
type PathwarActivity struct {
action string
when string
}
func getActivities(client *http.Client, url string, last *time.Time) (*time.Time, []PathwarActivity) {
resp, err := client.Get(url)
if err != nil {
log.Printf("error: %v", err)
return last, nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("error: %v", err)
return last, nil
}
var data map[string] interface{}
err = json.Unmarshal(body, &data)
if err != nil {
log.Printf("error: %v", err)
return last, nil
}
results := make([]PathwarActivity, 0)
if v, ok := data["_items"]; ok {
switch vv := v.(type) {
case []interface{}:
for _, entry := range(vv) {
switch vvv := entry.(type) {
case map[string] interface{}:
a := PathwarActivity{
action: "",
when: "",
}
if value, ok := vvv["action"]; ok {
a.action = value.(string)
}
if value, ok := vvv["_created"]; ok {
a.when = value.(string)
}
layout := "Mon, 02 Jan 2006 15:04:05 GMT"
created_at, err := time.Parse(layout, a.when)
if (err == nil && (last == nil || created_at.After(*last))) {
results = append(results, a)
last = &created_at
}
}
}
}
}
return last, results
}
// Broadcast listens for private messages and broadcasts them to a list of targets
func Pathwar(chac chan Action, config PathwarConfig) {
a := Action{
Type: A_SAY,
Priority: PRIORITY_LOW,
}
url := fmt.Sprintf("%s/activities?sort=-_updated", config.EndPoint)
var last *time.Time
var data []PathwarActivity
for {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
last, data = getActivities(client, url, last)
for _, entry := range(data) {
what := fmt.Sprintf("NEWS M1CH3L %s (%s)\n", entry.action, entry.when)
for server, targets := range config.Targets {
a.Server = server
a.Channel = ""
a.User = ""
for _, target := range targets {
if strings.Index(target, "#") == 1 {
a.Channel = target
} else {
a.User = target
}
a.Data = what
chac <- a
}
}
}
time.Sleep(1 * 1e9)
}
}