forked from zevlg/telega.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelega-tme.el
199 lines (174 loc) · 7.85 KB
/
telega-tme.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
;;; telega-tme.el --- Handling internal telegram links -*- lexical-binding:t -*-
;; Copyright (C) 2019 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;; Created: Sat Jan 19 16:36:01 2019
;; Keywords:
;; telega is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; telega is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with telega. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'cl-lib)
(require 'rx)
(require 'url-parse)
(require 'url-util)
(require 'telega-sticker)
(declare-function telega-chat-get "telega-chat" (chat-id &optional offline-p))
(declare-function telega-chat--goto-msg "telega-chat" (chat msg-id &optional highlight))
(declare-function telega-chat--pop-to-buffer "telega-chat" (chat))
(declare-function telega--searchPublicChat "telega-chat" (username))
(declare-function telega--checkChatInviteLink "telega-chat" (invite-link))
(declare-function telega--joinChatByInviteLink "telega-chat" (invite-link))
(defun telega-tme-open-privatepost (supergroup post)
"Open POST in private SUPERGROUP."
(when-let ((chat (telega-chat-get
(string-to-number (concat "-100" supergroup))
'offline)))
;; msg-id = post * 1048576
(telega-chat--goto-msg
chat (* (string-to-number post) 1048576) 'highlight)))
(defun telega-tme-open-username (username &rest bot-params)
"Open chat by its USERNAME.
BOT-PARAMS are additional params."
(cond ((string= username "telegrampassport")
;; TODO: passport
(message "telega TODO: handle `telegrampassport'"))
((plist-get bot-params :start)
;; :start :startgroup :game :post
(message "telega TODO: handle bot start"))
(t
;; Ordinary user/channel/group, :post
(let ((chat (telega--searchPublicChat username))
(post (plist-get bot-params :post)))
(unless chat
(error "Unknown public chat: %s" username))
(if post
;; See https://github.com/tdlib/td/issues/16
;; msg-id = post * 1048576
(telega-chat--goto-msg
chat (* (string-to-number post) 1048576) 'highlight)
(telega-chat--pop-to-buffer chat))))
))
(defun telega-tme-open-group (group)
"Join the GROUP."
(let* ((url (concat (or (plist-get telega--options :t_me_url)
"https://t.me/")
"joinchat/" group))
(link-check (telega--checkChatInviteLink url))
(chat-id (plist-get link-check :chat_id))
(chat (when link-check
(if (zerop chat-id)
;; Can only join by link
(when (y-or-n-p (format "Join \"%s\"? "
(plist-get link-check :title)))
(telega--joinChatByInviteLink url))
;; Can preview messages before deciding to join
(telega-chat-get chat-id)))))
(when chat
(telega-chat--pop-to-buffer chat))))
(defun telega-tme-open-proxy (_type _proxy)
"Open the PROXY."
;; TYPE is "socks" or "proxy"
;; :server, :port, :user, :pass, :secret
(message "TODO: `telega-tme-open-proxy'")
)
(defun telega-tme-open-stickerset (setname)
"Open sticker set with SETNAME."
(let ((sset (telega--searchStickerSet setname)))
(unless sset
(user-error "No such sticker set: %s" setname))
(telega-describe-stickerset sset)))
(defun telega-tme-open-theme (_slug)
(user-error "`telega-tme-open-theme' not yet implemented"))
(defun telega-tme-parse-query-string (query-string)
"Parse QUERY-STRING and return it as plist.
Multiple params with same name in QUERY-STRING is disallowed."
(let ((query (url-parse-query-string query-string 'downcase)))
(cl-loop for (name val) in query
nconc (list (intern (concat ":" name)) val))))
(defun telega-tme-open-tg (url)
"Open URL starting with `tg:'.
Return non-nil, meaning URL has been handled."
(when (string-prefix-p "tg://" url)
;; Convert it to `tg:' form
(setq url (concat "tg:" (substring url 5))))
(let* ((path-query (url-path-and-query
(url-generic-parse-url url)))
(path (car path-query))
(query (telega-tme-parse-query-string (cdr path-query))))
(cond ((string= path "resolve")
(let ((username (plist-get query :domain)))
(setq query (telega-plist-del query :domain))
(apply 'telega-tme-open-username username query)))
((string= path "join")
(telega-tme-open-group (plist-get query :invite)))
((string= path "addstickers")
(telega-tme-open-stickerset (plist-get query :set)))
((string= path "addtheme")
(telega-tme-open-theme (plist-get query :slug)))
((string= path "privatepost")
(telega-tme-open-privatepost
(plist-get query :channel) (plist-get query :post)))
((or (string= path "msg") (string= path "share"))
)
((string= path "msg_url")
)
((string= path "confirmphone")
)
((or (string= path "passport") (string= path "secureid"))
)
((or (string= path "socks") (string= path "proxy"))
(telega-tme-open-proxy path query))
((string= path "login")
)
(t
(message "telega: Unsupported tg url: %s" url))))
t)
(defun telega-tme-open (url &optional just-convert)
"Open any URL with https://t.me prefix.
If JUST-CONVERT is non-nil, return converted link value.
JUST-CONVERT used for testing only.
Return non-nil if url has been handled."
;; Convert URL to `tg:' form and call `telega-tme-open-tg'
(let* ((path-query (url-path-and-query (url-generic-parse-url url)))
(path (car path-query))
(query (cdr path-query))
(case-fold-search nil) ;ignore case
(tg (cond ((string-match "^/joinchat/\\([a-zA-Z0-9._-]+\\)$" path)
(concat "tg:join?invite=" (match-string 1 path)))
((string-match "^/addstickers/\\([a-zA-Z0-9._]+\\)$" path)
(concat "tg:addstickers?set=" (match-string 1 path)))
((string-match "^/share/url$" path)
(concat "tg:msg_url?" query))
((string-match "^/\\(socks\\|proxy\\)$" path)
(concat "tg:" (match-string 1 path) "?" query))
((string-match
(rx (and line-start "/c/"
(group (? "-") (1+ digit))
"/"
(group (1+ digit))))
path)
(concat "tg:privatepost?channel=" (match-string 1 path)
"&post=" (match-string 2 path)))
((string-match
(rx (and line-start "/"
(group (1+ (regexp "[a-zA-Z0-9\\.\\_]")))
(? "/" (group (1+ digit)))))
path)
(concat "tg:resolve?domain=" (match-string 1 path)
(when (match-string 2 path)
(concat "&post=" (match-string 2 path)))
(when query (concat "&" query)))))))
(cond (just-convert tg)
(tg (telega-tme-open-tg tg) t)
(t (telega-debug "WARN: Can't open \"%s\" internally") nil))))
(provide 'telega-tme)
;;; telega-tme.el ends here