-
Notifications
You must be signed in to change notification settings - Fork 5
/
redbot.red
207 lines (176 loc) · 3.97 KB
/
redbot.red
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
Red []
do load %redbot-options.red
do %gitter-api.red
do %github-v3.red
; --- support funcs
strip-newline: function [text] [replace/all text newline space]
; ---
asterisk: #"*"
colon: #":"
open-block: #"["
close-block: #"]"
open-paren: #"("
close-paren: #")"
; --- global vars
bot-name: "@botthebot" ; "@redlangbot_twitter"
user: none
room: none
messages: none
reply: none
help: {
* `/help` - show this help
* `/time` - show current bot's time
* `@botthebot do <code>` - do following code and print last value
}
{
* **/issues** - show five latest issues
* **/commits** - show five latest commits
}
; --- basic commands
;
; TODO: some verbosity switch
;
join-room: func [
room-name
] [
print "=== Login sequence"
user: gitter/user-info
room: gitter/join-room user room-name
]
read-messages: func [] [
print "=== Read messages"
messages: gitter/get-unread user room
]
; TODO: move rules elsewhere
hallo-rule: [
thru ["hi" | "hello"]
(reply: "Hi!") ; TODO: add author's name
]
commits-rule: [
thru ["show" | "list"]
thru "commits"
(reply: list-commits)
]
issues-rule: [
thru ["show" | "list"]
thru "issues"
(reply: list-issues)
]
do-rule: [
thru ["do"]
any space
copy value
to end
(reply: do-code value)
]
do-code: func [
value
/local id reply
][
replace/all value "load" ""
replace/all value "save" ""
replace/all value "read" ""
replace/all value "write" ""
save/header %temp.red compose/deep [
write %out mold do [(load value)]
] []
id: call "red %temp.red"
wait 0.5
call rejoin ["kill " id]
read %out
]
parse-command: func [
message
/local reply
] [
reply: switch message/text [
"/time" [reply: rejoin ["It is " now/time]]
"/help" [help]
"/issues" [list-issues]
"/commits" [list-commits]
]
print ["=== Reply:" mold reply]
if reply [gitter/send-message room reply]
]
parse-message: func [
message
] [
print ["--- Parsing" message/id]
reply: none
parse message/text [
thru bot-name ; TODO: we expect that mention is at the beginning. it could not.
[
hallo-rule
| commits-rule
| issues-rule
| do-rule
| help-rule
]
]
print ["=== Reply:" mold reply]
if reply [gitter/send-message room reply]
]
process-mentions: func [] [
print ["=== Process" length? messages/mention "messages"]
foreach message messages/mention [
print ["--- Processing" message]
; we care only about messages for our bot (may change later)
parse-message gitter/get-message room message
]
]
process-messages: func [] [
print ["=== Process" length? messages/chat "messages"]
foreach message messages/chat [
print ["--- Processing" message]
; we care only about messages for our bot (may change later)
parse-command gitter/get-message room message
]
]
mark-messages: func [] [
print ["=== Mark as read" length? messages/chat "messages"]
gitter/mark-as-read user room messages/chat
]
; --- format responses
; TODO: make one function, it is basically the same
list-commits: function [] [
return "TODO"
; TODO
output: copy {Here are five latest commits for [red](http://github.com/red/red):^/^/}
commits: head remove/part skip to block! github/list-commits 'red/red 5 25
foreach commit commits [
append output rejoin [
asterisk space open-block strip-newline commit/commit/message close-block
open-paren commit/commit/url close-paren
newline newline
]
]
output
]
list-issues: func [] [
return "TODO"
; TODO
output: copy {Here are five latest issues for [red](http://github.com/red/red):^/^/}
issues: head remove/part skip to block! github/get-issues/repo 'red/red 5 25
foreach issue issues [
append output rejoin [
asterisk space open-block issue/number close-block
open-paren issue/html_url close-paren
colon space issue/title
newline newline
]
]
output
]
; --- main
redbot: does [
join-room 'red-gitter/lobby
; TODO: this should be in forever loop
forever [
read-messages
process-mentions ; TODO: use just process messages here?
process-messages
mark-messages
wait 3 ; do not run like mad
]
]
"Run Redbot by typing >> redbot"