Mercurial > libervia-backend
comparison libervia/cli/bookmarks_legacy.py @ 4327:554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
- Former bookmarks implementation is now labeled as "legacy".
- XEP-0402 is now used for bookmarks when relevant namespaces are found, and it fallbacks
to legacy XEP-0048/XEP-0049 bookmarks otherwise.
- CLI legacy bookmark commands have been moved to `bookmarks legacy`
- CLI bookmarks commands now use the new XEP-0402 (with fallback to legacy one
automatically used if necessary).
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 20 Nov 2024 11:43:27 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4326:5fd6a4dc2122 | 4327:554a87ae17a6 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # Libervia CLI | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from . import base | |
21 from libervia.backend.core.i18n import _ | |
22 from libervia.cli.constants import Const as C | |
23 | |
24 STORAGE_LOCATIONS = ("local", "private", "pubsub") | |
25 TYPES = ("muc", "url") | |
26 | |
27 | |
28 class BookmarksCommon(base.CommandBase): | |
29 """Class used to group common options of bookmarks subcommands""" | |
30 | |
31 def add_parser_options(self, location_default="all"): | |
32 self.parser.add_argument( | |
33 "-l", | |
34 "--location", | |
35 type=str, | |
36 choices=(location_default,) + STORAGE_LOCATIONS, | |
37 default=location_default, | |
38 help=_("storage location (default: %(default)s)"), | |
39 ) | |
40 self.parser.add_argument( | |
41 "-t", | |
42 "--type", | |
43 type=str, | |
44 choices=TYPES, | |
45 default=TYPES[0], | |
46 help=_("bookmarks type (default: %(default)s)"), | |
47 ) | |
48 | |
49 | |
50 class BookmarksList(BookmarksCommon): | |
51 def __init__(self, host): | |
52 super(BookmarksList, self).__init__(host, "list", help=_("list bookmarks")) | |
53 | |
54 async def start(self): | |
55 try: | |
56 data = await self.host.bridge.bookmarks_legacy_list( | |
57 self.args.type, self.args.location, self.host.profile | |
58 ) | |
59 except Exception as e: | |
60 self.disp(f"can't get bookmarks list: {e}", error=True) | |
61 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
62 | |
63 mess = [] | |
64 for location in STORAGE_LOCATIONS: | |
65 if not data[location]: | |
66 continue | |
67 loc_mess = [] | |
68 loc_mess.append(f"{location}:") | |
69 book_mess = [] | |
70 for book_link, book_data in list(data[location].items()): | |
71 name = book_data.get("name") | |
72 autojoin = book_data.get("autojoin", "false") == "true" | |
73 nick = book_data.get("nick") | |
74 book_mess.append( | |
75 "\t%s[%s%s]%s" | |
76 % ( | |
77 (name + " ") if name else "", | |
78 book_link, | |
79 " (%s)" % nick if nick else "", | |
80 " (*)" if autojoin else "", | |
81 ) | |
82 ) | |
83 loc_mess.append("\n".join(book_mess)) | |
84 mess.append("\n".join(loc_mess)) | |
85 | |
86 print("\n\n".join(mess)) | |
87 self.host.quit() | |
88 | |
89 | |
90 class BookmarksRemove(BookmarksCommon): | |
91 def __init__(self, host): | |
92 super(BookmarksRemove, self).__init__(host, "remove", help=_("remove a bookmark")) | |
93 | |
94 def add_parser_options(self): | |
95 super(BookmarksRemove, self).add_parser_options() | |
96 self.parser.add_argument( | |
97 "bookmark", help=_("jid (for muc bookmark) or url of to remove") | |
98 ) | |
99 self.parser.add_argument( | |
100 "-f", | |
101 "--force", | |
102 action="store_true", | |
103 help=_("delete bookmark without confirmation"), | |
104 ) | |
105 | |
106 async def start(self): | |
107 if not self.args.force: | |
108 await self.host.confirm_or_quit(_("Are you sure to delete this bookmark?")) | |
109 | |
110 try: | |
111 await self.host.bridge.bookmarks_legacy_remove( | |
112 self.args.type, self.args.bookmark, self.args.location, self.host.profile | |
113 ) | |
114 except Exception as e: | |
115 self.disp(_("can't delete bookmark: {e}").format(e=e), error=True) | |
116 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
117 else: | |
118 self.disp(_("bookmark deleted")) | |
119 self.host.quit() | |
120 | |
121 | |
122 class BookmarksAdd(BookmarksCommon): | |
123 def __init__(self, host): | |
124 super(BookmarksAdd, self).__init__(host, "add", help=_("add a bookmark")) | |
125 | |
126 def add_parser_options(self): | |
127 super(BookmarksAdd, self).add_parser_options(location_default="auto") | |
128 self.parser.add_argument( | |
129 "bookmark", help=_("jid (for muc bookmark) or url of to remove") | |
130 ) | |
131 self.parser.add_argument("-n", "--name", help=_("bookmark name")) | |
132 muc_group = self.parser.add_argument_group(_("MUC specific options")) | |
133 muc_group.add_argument("-N", "--nick", help=_("nickname")) | |
134 muc_group.add_argument( | |
135 "-a", | |
136 "--autojoin", | |
137 action="store_true", | |
138 help=_("join room on profile connection"), | |
139 ) | |
140 | |
141 async def start(self): | |
142 if self.args.type == "url" and (self.args.autojoin or self.args.nick is not None): | |
143 self.parser.error(_("You can't use --autojoin or --nick with --type url")) | |
144 data = {} | |
145 if self.args.autojoin: | |
146 data["autojoin"] = "true" | |
147 if self.args.nick is not None: | |
148 data["nick"] = self.args.nick | |
149 if self.args.name is not None: | |
150 data["name"] = self.args.name | |
151 try: | |
152 await self.host.bridge.bookmarks_legacy_add( | |
153 self.args.type, | |
154 self.args.bookmark, | |
155 data, | |
156 self.args.location, | |
157 self.host.profile, | |
158 ) | |
159 except Exception as e: | |
160 self.disp(f"can't add bookmark: {e}", error=True) | |
161 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
162 else: | |
163 self.disp(_("bookmark successfully added")) | |
164 self.host.quit() | |
165 | |
166 | |
167 class BookmarksLegacy(base.CommandBase): | |
168 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd) | |
169 | |
170 def __init__(self, host): | |
171 super(BookmarksLegacy, self).__init__( | |
172 host, "legacy", use_profile=False, help=_("manage legacy bookmarks") | |
173 ) |