This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageprocessing.go
245 lines (238 loc) · 8.31 KB
/
messageprocessing.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Copyright 2018 Martin Dosch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package main
import (
"strconv"
"strings"
"fluux.io/xmpp"
"github.com/mdosch/goxtxt/twtxt"
)
// processMessage is executing the twtxt commands according to messages
// received and replies the output.
func processMessage(client *xmpp.Client, packet *xmpp.Message, config *configuration) {
words := strings.Fields(packet.Body)
// First word of message body contains the command.
switch strings.ToLower(words[0]) {
// Show help message.
case "help":
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From,
Type: "chat"}, Body: "\"help\": Show this message.\n" +
"\"ping\": Bot replies if available.\n" +
"\"tl\": Show last " + strconv.Itoa(config.TimelineEntries) +
" timeline entries.\n" +
"\"tv [user]\": Show [user]s timeline.\n" +
"\"tw [tweet]\": Will tweet your input [tweet] and afterwards show your timeline.\n" +
"\"tm [user]\": Will show the last " + strconv.Itoa(config.TimelineEntries) +
" mentions. [user] will fall back to \"" + config.Twtxtnick + "\" if not specified.\n" +
"\"tt [tag]\": Will show the last " + strconv.Itoa(config.TimelineEntries) +
" occurrences of #[tag]\n" +
"\"tf [user] [url]\": Follow [user].\n" +
"\"tu [user]\": Unfollow [user].\n" +
"\"to\": List the accounts you are following.\n" +
"\"source\": Shows a link to the sourcecode."}
client.Send(reply)
// Reply to a ping request.
case "ping":
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Pong!"}
client.Send(reply)
// Show link to source code repository.
case "source":
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "https://github.com/mdosch/goxtxt/"}
client.Send(reply)
// Send a tweet.
case "tw":
// Check that message body contains something to tweet.
if len(words) == 1 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "No Input."}
client.Send(reply)
break
}
// Check that tweet doesn't exceed configured maximum length.
tweetLength := len(packet.Body) - 3
if tweetLength > config.MaxCharacters {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Tweet exceeds maximum of " + strconv.Itoa(config.MaxCharacters) +
" characters by " + strconv.Itoa(tweetLength-config.MaxCharacters) +
" characters."}
client.Send(reply)
break
}
// Send the tweet.
_, err := twtxt.Tweet(words[1:])
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
// Automatically show updated timeline after successful tweeting.
fallthrough
// Show timeline.
case "tl":
out, err := twtxt.Timeline(&config.TimelineEntries)
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
// Show only timeline entries from a certain user.
case "tv":
// Check there is a username specified.
if len(words) == 1 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "No Input."}
client.Send(reply)
break
}
// Check there is not more than one username specified.
if len(words) > 2 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Timeline view supports only one user."}
client.Send(reply)
break
}
out, err := twtxt.ViewUser(&config.TimelineEntries, &words[1])
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
// Show @-mentions of a certain user.
case "tm":
// If no username specified show mentions of own user.
if len(words) == 1 {
out, err := twtxt.Mentions(&config.Twtxtnick, &config.TimelineEntries)
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
}
// Show mentions of the specified user.
if len(words) == 2 {
out, err := twtxt.Mentions(&words[1], &config.TimelineEntries)
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
}
// Check that there is not more than one user specified.
if len(words) > 2 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Too many arguments."}
client.Send(reply)
}
// Show timeline entries containing a certain #-tag.
case "tt":
// Check that a tag is specified.
if len(words) == 1 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Missing Input."}
client.Send(reply)
}
// Show timeline entries for the specified tag.
if len(words) == 2 {
out, err := twtxt.Tags(&words[1], &config.TimelineEntries)
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
}
// Check that there is only one tag specified.
if len(words) > 2 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Too many arguments."}
client.Send(reply)
}
// Add a certain user to follow.
case "tf":
// Check that username and URL are specified.
if len(words) != 3 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Missing Input."}
client.Send(reply)
break
}
// Follow the specified user.
out, err := twtxt.UserManagement(true, words[1:])
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
// Stop following a certain user.
case "tu":
// Check that only one username is specified.
if len(words) != 2 {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Wrong parameter count."}
client.Send(reply)
break
}
// Unfollow the specified user.
out, err := twtxt.UserManagement(false, words[1:])
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
// Retrieve a list of users we are following.
case "to":
out, err := twtxt.ListFollowing()
if err != nil {
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Failed."}
client.Send(reply)
break
}
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: *out}
client.Send(reply)
// Point help command if an unknown command is received.
default:
reply := xmpp.Message{PacketAttrs: xmpp.PacketAttrs{To: packet.From, Type: "chat"},
Body: "Unknown command. Send \"help\"."}
client.Send(reply)
}
}