-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
140 lines (120 loc) · 3.89 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/gologme/log"
"github.com/writeas/activityserve"
)
var err error
func main() {
fmt.Println()
fmt.Println("======================= PHeRePHoNe ==========================")
// introduce ourselves
fmt.Println()
fmt.Println("Pherephone follows some accounts and boosts")
fmt.Println("whatever they post to our followers. See config.ini ")
fmt.Println("for more information and how to set up. ")
debugFlag := flag.Bool("debug", false, "set to true to get debugging information in the console")
flag.Parse()
if *debugFlag == true {
}
// create a logger with levels but without prefixes for easier to read
// debug output
printer := log.New(os.Stdout, "", 0)
printer.EnableLevel("error")
printer.EnableLevel("info")
log.EnableLevel("warn")
if *debugFlag == true {
printer.Info()
printer.Info("debug mode on")
log.EnableLevel("info")
log.EnableLevel("error")
log.EnableLevel("info")
}
configurationFile := activityserve.Setup("config.ini", *debugFlag)
announceReplies, _ := configurationFile.Section("general").Key("announce_replies").Bool()
// get the list of local actors to host and the list
// of remote actors any of them relays (boosts, announces)
jsonFile, err := os.Open("actors.json")
if err != nil {
printer.Error("something is wrong with the json file containing the actors")
printer.Error("")
log.Error(err)
return
}
// Unmarshall it into a map of string arrays
whoFollowsWho := make(map[string]map[string]interface{})
byteValue, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &whoFollowsWho)
if err != nil {
printer.Error("There's an error in your actors.json. Please check!")
printer.Error("")
log.Error(err)
return
}
actors := make(map[string]activityserve.Actor)
// create all local actors if they don't exist yet
for follower, data := range whoFollowsWho {
followees := data["follow"].([]interface{})
printer.Info()
log.Info("Local Actor: " + follower)
if strings.ContainsAny(follower, " \\/:*?\"<>|") {
printer.Warn("local actors can't have spaces or any of these characters in their name: \\/:*?\"<>|")
printer.Warn("Actor " + follower + " will be ignored")
continue
}
localActor, err := activityserve.GetActor(follower, data["summary"].(string), "Service")
if err != nil {
log.Info("error creating local actor")
return
}
// Now follow each one of it's users
log.Info("Users to relay:")
for _, followee := range followees {
log.Info(followee)
go localActor.Follow(followee.(string))
}
// Iterate over the current following users and if anybody doesn't exist
// in the users to follow list unfollow them
for following := range localActor.Following() {
exists := false
for _, followee := range followees {
if followee.(string) == following {
exists = true
break
}
}
if exists == false {
go localActor.Unfollow(following)
}
}
// boost everything that comes in
localActor.OnReceiveContent = func(activity map[string]interface{}) {
// check if we are following the person that sent us the
// message otherwise we're open in spraying spam from whoever
// messages us to our followers
if _, ok := localActor.Following()[activity["actor"].(string)]; ok {
object := activity["object"].(map[string]interface{})
inReplyTo, ok := object["inReplyTo"]
isReply := false
// if the field exists and is not null and is not empty
// then it's a reply
if ok && inReplyTo != nil && inReplyTo != "" {
isReply = true
}
// if it's a reply and announce_replies config option
// is set to false then bail out
if announceReplies == true || isReply == false {
content := activity["object"].(map[string]interface{})
go localActor.Announce(content["id"].(string))
}
}
}
actors[localActor.Name] = localActor
}
activityserve.Serve(actors)
}