This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmwchat-input.el
261 lines (245 loc) · 8.71 KB
/
tmwchat-input.el
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
(require 'tmwchat-log)
(require 'tmwchat-inventory)
(defun tmwchat-show-beings ()
(dolist (id tmwchat-nearby-player-ids)
(let ((name (gethash id tmwchat-player-names
(format "{ID:%s}" id))))
(tmwchat-log (format "%s (id:%s)" name id)))))
(defun tmwchat-print-inventory ()
(let ((comb-hash (make-hash-table))
(inv-str))
(maphash
(lambda (index cell)
(let ((id (car cell))
(amount (cadr cell)))
(incf (gethash id comb-hash 0) amount)))
tmwchat-player-inventory)
(maphash
(lambda (id amount)
(let ((name (tmwchat-item-name id t)))
(push
(if (> amount 1)
(format "%d %s" amount name)
name)
inv-str)))
comb-hash)
(tmwchat-log "Player inventory: %s"
(mapconcat 'identity inv-str ", "))))
(defun tmwchat--replace-whisper-cmd (nick)
(interactive "sNick:")
(defun insert-formatted (nick-q msg)
(with-current-buffer tmwchat-buffer-name
(delete-region tmwchat--start-point (point-max))
(insert (concat "/w " nick-q " " msg))
(goto-char (point-max))))
(with-current-buffer tmwchat-buffer-name
(let ((line (buffer-substring tmwchat--start-point (point-max)))
(nick-q (if (string-match-p " " nick)
(concat "\"" nick "\"")
nick)))
(condition-case nil
(if (string-prefix-p "/w " line)
(let* ((parsed (tmwchat--parse-msg (substring line 3)))
(old-nick (car parsed))
(msg (cdr parsed)))
(unless (string-equal old-nick nick)
(insert-formatted nick-q msg)))
(insert-formatted nick-q ""))
(error (insert-formatted nick-q ""))))))
(defun tmwchat--find-nick-completion ()
(defun completion-list ()
(union
(tmwchat-get-online-users)
(ring-elements tmwchat-recent-users)))
(defun filter (condp lst)
(delq nil
(mapcar (lambda (x) (and (funcall condp x) x)) lst)))
(let ((onl (tmwchat-get-online-users))
(len 3)
(partial)
(nick)
(memb))
(while (> (length onl) 1)
(setq partial (buffer-substring (- (point) len) (point)))
(setq onl (filter (lambda (nick)
(string-match-p (regexp-quote partial) nick))
onl))
(when (setq memb (member-ignore-case partial onl))
(setq onl (list (car memb))))
(setq len (1+ len)))
(when onl
(setq len (- len 1))
(setq nick (car onl))
(while (string-match-p (regexp-quote partial) nick)
(setq len (1+ len))
(setq partial (buffer-substring (- (point) len) (point))))
(setq len (- len 1))
(when (eq (aref partial 1) 32)
(setq len (- len 1)))
(cons nick len))))
(defun tmwchat--replace-nick-completion (nick partial-len)
(let* ((pt-end (point))
(pt-begin (- pt-end partial-len))
(nick-q (if (string-match-p " " nick)
(concat "\"" nick "\" ")
(concat nick " "))))
(when (equal (char-before pt-begin) 34)
(setq pt-begin (- pt-begin 1)))
(delete-region pt-begin pt-end)
(insert nick-q)))
(defun tmwchat-tab-complete ()
(interactive)
(let ((result (tmwchat--find-nick-completion)))
(when result
(tmwchat--replace-nick-completion (car result) (cdr result)))))
(defun tmwchat-read-print ()
"Top level loop."
(interactive)
(unless (equal tmwchat--start-point (point))
(setq tmwchat-sent (tmwchat-readin))
(newline)
;; (setq tmwchat--start-point (point))
(tmwchat-parse-input)
;; (newline)
))
(defun tmwchat-readin ()
"Read message and return it"
(goto-char (point-max))
(buffer-substring tmwchat--start-point (point)))
(defun tmwchat--parse-msg (msg)
(unless (and (stringp msg) (> (length msg) 0))
(error "tmwchat--parse-msg: msg must be non-empty string"))
(if (string-match "^\"" msg)
(if (string-match "\"" (substring msg 1))
(cons (substring msg 1 (match-end 0))
(substring msg (+ (match-end 0) 2)))
(error "Bad string format"))
(let ((m (string-match " " msg)))
(cons (substring msg 0 m)
(substring msg (+ m 1))))))
(defun chomp (str)
"Chomp leading and tailing whitespace from STR."
(replace-regexp-in-string (rx (or (: bos (* (any " \t\n")))
(: (* (any " \t\n")) eos)))
""
str))
(defun tmwchat-parse-input ()
(cond
((string-equal tmwchat-sent "/help")
(tmwchat-log
(concat
"/help -- show this help\n"
"/room -- show nearby players\n"
"/emote <number> -- show emote\n"
"/emotes -- show emote codes\n"
"/mute -- mute notification sounds\n"
"/unmute -- play notification sounds\n"
"/w NickName Message -- send a PM to NickName\n"
"/w \"NickName With Spaces\" Message -- send PM to NickName\n"
"/party Message -- send message to your party\n"
"/online -- show online players\n"
"/away [optional afk message] -- away from keyboard\n"
"/back -- you are back!\n"
"/sit -- Sit down\n"
"/stand -- Stand up\n"
"/turn left|right|up|down -- turn in given direction\n"
"/goto x y -- go to destination (x, y)\n"
"/where -- print current location\n"
"/inv -- show player inventory\n"
"/zeny -- show player money\n"
"/equip ID -- equip item id\n"
"/block PlayerName -- block player\n"
"/dc -- disconnect\n"
"/debug -- toggle printing debug information\n"
"Any other command sends a message to the public chat"
)))
((string-equal tmwchat-sent "/online")
(tmwchat-log (format "%s" (tmwchat-get-online-users))))
((string-equal tmwchat-sent "/room")
(tmwchat-show-beings))
((string-equal tmwchat-sent "/sit")
(tmwchat-sit))
((string-equal tmwchat-sent "/stand")
(tmwchat-stand))
((string-equal tmwchat-sent "/inv")
(tmwchat-print-inventory))
((string-equal tmwchat-sent "/zeny")
(tmwchat-log "You have %d GP." tmwchat-money))
((string-prefix-p "/turn " tmwchat-sent)
(tmwchat-turn (substring tmwchat-sent 6)))
((string-prefix-p "/emote " tmwchat-sent)
(tmwchat-show-emote (string-to-int (substring tmwchat-sent 7))))
((string-equal "/emotes" tmwchat-sent)
(tmwchat-log (format "%s" tmwchat-emotes)))
((string-equal "/mute" tmwchat-sent)
(tmwchat-log "Sounds are muted")
(setq tmwchat-sound nil))
((string-equal "/unmute" tmwchat-sent)
(tmwchat-log "Sounds are played")
(setq tmwchat-sound t))
((string-prefix-p "/party " tmwchat-sent)
(tmwchat-send-party-message
(tmwchat--make-urls (substring tmwchat-sent 7))))
((string-equal "/back" tmwchat-sent)
(setq tmwchat-away nil))
((string-prefix-p "/away" tmwchat-sent)
(setq tmwchat-away t)
(condition-case nil
(let ((afk-msg (substring tmwchat-sent 6)))
(unless (= (length afk-msg) 0)
(setq tmwchat-away-message (concat "*AFK*: " afk-msg))))
(error nil))
(tmwchat-log tmwchat-away-message))
((string-prefix-p "/equip " tmwchat-sent)
(tmwchat-equip-item (string-to-int (substring tmwchat-sent 7))))
((string-prefix-p "/block " tmwchat-sent)
(let ((nick (chomp (substring tmwchat-sent 7))))
(when (string-prefix-p "\"" nick)
(setq nick (substring nick 1 (- (length nick) 1))))
(when (> (length nick) 0)
(tmwchat-log "Blocking player \"%s\"" nick)
(customize-set-value
'tmwchat-blocked-players
(add-to-list 'tmwchat-blocked-players nick)))))
((string-equal "/dc" tmwchat-sent)
(tmwchat-logoff))
((string-equal "/debug" tmwchat-sent)
(setq tmechat-debug (not tmwchat-debug)))
((string-prefix-p "/w " tmwchat-sent)
(let* ((parsed (tmwchat--parse-msg (substring tmwchat-sent 3)))
(nick (car parsed))
(msg (tmwchat--make-urls (cdr parsed))))
(setq tmwchat--last-whisper-nick nick)
(tmwchat-whisper-message nick msg)))
((string-equal "/where" tmwchat-sent)
(tmwchat-log "map: %s coor: (%d, %d)"
tmwchat-map-name
tmwchat-coor-x
tmwchat-coor-y))
((string-prefix-p "/goto " tmwchat-sent)
(let ((coor (mapcar 'string-to-int
(split-string (substring tmwchat-sent 6)))))
(if (< (length coor) 2)
(tmwchat-log "Usage: /goto x y")
(tmwchat-goto (car coor) (cadr coor)))))
;; ((string-prefix-p "/ " tmwchat-sent)
;; (tmwchat-whisper-message
;; tmwchat--last-whisper-nick
;; (substring tmwchat-sent 2)))
((string-prefix-p "/" tmwchat-sent)
(tmwchat-log "Unknown command. Type /help to get available commands"))
(t
(if tmwchat--whisper-target
(tmwchat-whisper-message tmwchat--whisper-target tmwchat-sent)
(progn
(setq tmwchat--last-whisper-nick nil)
(tmwchat-chat-message (tmwchat--make-urls tmwchat-sent))))))
(delete-region tmwchat--start-point (point-max))
;; (setq tmwchat--start-point (point-max))
(when tmwchat--last-whisper-nick
(let ((nick-q (if (string-match-p " " tmwchat--last-whisper-nick)
(concat "\"" tmwchat--last-whisper-nick "\"")
tmwchat--last-whisper-nick)))
(insert (concat "/w " nick-q " "))
(goto-char (point-max)))))
(provide 'tmwchat-input)