3137
|
1 #!/usr/bin/env python3 |
|
2 |
986
|
3 |
|
4 # jp: a SAT command line tool |
3136
|
5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
986
|
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 |
3028
|
20 from . import base |
986
|
21 from sat.core.i18n import _ |
3040
|
22 from sat_frontends.jp.constants import Const as C |
986
|
23 |
|
24 __commands__ = ["Bookmarks"] |
|
25 |
|
26 STORAGE_LOCATIONS = ('local', 'private', 'pubsub') |
|
27 TYPES = ('muc', 'url') |
|
28 |
|
29 class BookmarksCommon(base.CommandBase): |
|
30 """Class used to group common options of bookmarks subcommands""" |
|
31 |
|
32 def add_parser_options(self, location_default='all'): |
|
33 self.parser.add_argument('-l', '--location', type=str, choices=(location_default,) + STORAGE_LOCATIONS, default=location_default, help=_("storage location (default: %(default)s)")) |
|
34 self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)")) |
|
35 |
|
36 class BookmarksList(BookmarksCommon): |
|
37 |
|
38 def __init__(self, host): |
|
39 super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks')) |
|
40 |
3040
|
41 async def start(self): |
|
42 try: |
|
43 data = await self.host.bridge.bookmarksList( |
|
44 self.args.type, self.args.location, self.host.profile) |
|
45 except Exception as e: |
|
46 self.disp(f"can't get bookmarks list: {e}", error=True) |
|
47 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
|
48 |
986
|
49 mess = [] |
|
50 for location in STORAGE_LOCATIONS: |
|
51 if not data[location]: |
|
52 continue |
|
53 loc_mess = [] |
3040
|
54 loc_mess.append(f"{location}:") |
986
|
55 book_mess = [] |
3028
|
56 for book_link, book_data in list(data[location].items()): |
986
|
57 name = book_data.get('name') |
|
58 autojoin = book_data.get('autojoin', 'false') == 'true' |
|
59 nick = book_data.get('nick') |
3028
|
60 book_mess.append("\t%s[%s%s]%s" % ((name+' ') if name else '', |
986
|
61 book_link, |
3028
|
62 ' (%s)' % nick if nick else '', |
|
63 ' (*)' if autojoin else '')) |
|
64 loc_mess.append('\n'.join(book_mess)) |
|
65 mess.append('\n'.join(loc_mess)) |
986
|
66 |
3028
|
67 print('\n\n'.join(mess)) |
3040
|
68 self.host.quit() |
986
|
69 |
|
70 |
|
71 class BookmarksRemove(BookmarksCommon): |
|
72 |
|
73 def __init__(self, host): |
|
74 super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark')) |
|
75 |
|
76 def add_parser_options(self): |
|
77 super(BookmarksRemove, self).add_parser_options() |
3040
|
78 self.parser.add_argument( |
|
79 'bookmark', help=_('jid (for muc bookmark) or url of to remove')) |
|
80 self.parser.add_argument( |
|
81 "-f", "--force", action="store_true", |
|
82 help=_("delete bookmark without confirmation"),) |
|
83 |
|
84 async def start(self): |
|
85 if not self.args.force: |
|
86 await self.host.confirmOrQuit(_("Are you sure to delete this bookmark?")) |
986
|
87 |
3040
|
88 try: |
|
89 await self.host.bridge.bookmarksRemove( |
|
90 self.args.type, self.args.bookmark, self.args.location, self.host.profile) |
|
91 except Exception as e: |
|
92 self.disp(_(f"can't delete bookmark: {e}"), error=True) |
|
93 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
|
94 else: |
|
95 self.disp(_('bookmark deleted')) |
|
96 self.host.quit() |
986
|
97 |
|
98 |
|
99 class BookmarksAdd(BookmarksCommon): |
|
100 |
|
101 def __init__(self, host): |
|
102 super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark')) |
|
103 |
|
104 def add_parser_options(self): |
|
105 super(BookmarksAdd, self).add_parser_options(location_default='auto') |
3040
|
106 self.parser.add_argument( |
|
107 'bookmark', help=_('jid (for muc bookmark) or url of to remove')) |
3028
|
108 self.parser.add_argument('-n', '--name', help=_("bookmark name")) |
986
|
109 muc_group = self.parser.add_argument_group(_('MUC specific options')) |
3028
|
110 muc_group.add_argument('-N', '--nick', help=_('nickname')) |
3040
|
111 muc_group.add_argument( |
|
112 '-a', '--autojoin', action='store_true', |
|
113 help=_('join room on profile connection')) |
986
|
114 |
3040
|
115 async def start(self): |
986
|
116 if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None): |
3040
|
117 self.parser.error(_("You can't use --autojoin or --nick with --type url")) |
986
|
118 data = {} |
|
119 if self.args.autojoin: |
|
120 data['autojoin'] = 'true' |
|
121 if self.args.nick is not None: |
|
122 data['nick'] = self.args.nick |
|
123 if self.args.name is not None: |
|
124 data['name'] = self.args.name |
3040
|
125 try: |
|
126 await self.host.bridge.bookmarksAdd( |
|
127 self.args.type, self.args.bookmark, data, self.args.location, |
|
128 self.host.profile) |
|
129 except Exception as e: |
|
130 self.disp(f"can't add bookmark: {e}", error=True) |
|
131 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
|
132 else: |
|
133 self.disp(_('bookmark successfully added')) |
|
134 self.host.quit() |
986
|
135 |
|
136 |
|
137 class Bookmarks(base.CommandBase): |
|
138 subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd) |
|
139 |
|
140 def __init__(self, host): |
|
141 super(Bookmarks, self).__init__(host, 'bookmarks', use_profile=False, help=_('manage bookmarks')) |