-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonitor.go
More file actions
95 lines (84 loc) · 2.55 KB
/
monitor.go
File metadata and controls
95 lines (84 loc) · 2.55 KB
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
package main
import (
"log"
"strings"
"github.com/bcgraham/tsumtsum/external/line"
"github.com/bcgraham/tsumtsum/external/thrift"
)
type MonitorCommand struct{}
var monitorCommand MonitorCommand
func (x *MonitorCommand) Execute(args []string) error {
session := MustNewSession(common.User, common.Device, common.ReportingServer)
session.DeleteAfterInviting()
return nil
}
func init() {
if _, err := parser.AddCommand("monitor",
"Monitor invites",
"Monitors invites as they are sent and removes those friends from your contact list (presumably they are no longer needed).",
&monitorCommand); err != nil {
log.Fatal(err)
}
}
func (s *Session) DeleteAfterInviting() {
// TODO: figure out why this doesn't work
// localRev, err := client.GetLastOpRevision()
var localRev int64
var invited, deleted int
for {
client, err := NewStandardClient("P4")
if err != nil {
log.Fatalf("Could not make standard client for delete after invite process: %v", err)
}
copyAuthToken(client, s.client)
operations, err := client.FetchOperations(localRev, 10)
if err != nil {
s.logger.Printf("Couldn't get messages: %v\n", err)
}
for _, op := range operations {
if isInviteFrom(op, s.profile.GetMid()) {
invited++
// send to central service
err := s.SendReport(Report{
Submitter: s.username,
MID: op.GetMessage().GetTo(),
Type: invite,
})
if err != nil {
s.logger.Printf("C: Error sending invite record: %v\n", err)
}
// delete contact
err = s.DeleteContactByMid(op.GetMessage().GetTo())
if err != nil {
s.logger.Printf("D: Error deleting contact: %v\n", err)
} else {
deleted++
}
monitorProgress(deleted, invited)
}
if op.GetRevision() > localRev {
localRev = op.GetRevision()
}
}
}
}
func monitorProgress(deleted, invited int) {
printProgress(prog{
str: "Witnessed %d invites; deleted %d invites (%.2f%%).",
args: []interface{}{
invited, deleted, 100 * float64(deleted) / float64(invited),
},
})
}
func isInviteFrom(op *line.Operation, userMID string) bool {
if op.GetTypeA1() == line.OpType_SEND_MESSAGE {
msg := op.GetMessage()
from := msg.GetFrom()
return strings.Contains(msg.GetText(), "LINE: Disney Tsum Tsum") && strings.Contains(msg.GetText(), "has invited you to play") && from == userMID
}
return false
}
func copyAuthToken(dst *line.TalkServiceClient, src *line.TalkServiceClient) {
authToken := src.Transport.(*thrift.THttpClient).GetHeader("X-Line-Access")
dst.Transport.(*thrift.THttpClient).SetHeader("X-Line-Access", authToken)
}