Add more operator option, also for export/import.

Fix var transfer from one form to another.
Improve handling with in-chat command arguments.
This commit is contained in:
Schimon Jehudah 2024-03-28 17:21:45 +00:00
parent 01c4959610
commit e0bc0bddf7
4 changed files with 142 additions and 58 deletions

View file

@ -1,2 +1,2 @@
__version__ = '0.1.51' __version__ = '0.1.52'
__version_info__ = (0, 1, 51) __version_info__ = (0, 1, 52)

View file

@ -286,7 +286,6 @@ class Slixfeed(slixmpp.ClientXMPP):
bookmarks = await XmppBookmark.get(self) bookmarks = await XmppBookmark.get(self)
await action.xmpp_muc_autojoin(self, bookmarks) await action.xmpp_muc_autojoin(self, bookmarks)
jids = await XmppPubsub.get_pubsub_services(self) jids = await XmppPubsub.get_pubsub_services(self)
print(jids)
for jid_bare in jids: for jid_bare in jids:
if jid_bare not in self.settings: if jid_bare not in self.settings:
jid_file = jid_bare jid_file = jid_bare
@ -897,7 +896,7 @@ class Slixfeed(slixmpp.ClientXMPP):
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
values = payload['values'] values = payload['values']
jid = values['jid'] jid = values['jid'] if 'jid' in values else None
jid_bare = session['from'].bare jid_bare = session['from'].bare
if jid != jid_bare and not is_operator(self, jid_bare): if jid != jid_bare and not is_operator(self, jid_bare):
text_warn = ('Posting to {} is restricted to operators only.' text_warn = ('Posting to {} is restricted to operators only.'
@ -1020,7 +1019,17 @@ class Slixfeed(slixmpp.ClientXMPP):
# It is not possible to assign non-str to transfer. # It is not possible to assign non-str to transfer.
# feed = values['feed'] # feed = values['feed']
node = values['node'][0] node = values['node'][0]
jid = values['jid'][0] jid = values['jid'] if 'jid' in values else None
jid_bare = session['from'].bare
if jid != jid_bare and not is_operator(self, jid_bare):
text_warn = 'You are not suppose to be here.'
session['allow_prev'] = False
session['has_next'] = False
session['next'] = None
session['notes'] = [['warn', text_warn]]
session['prev'] = None
session['payload'] = None
return session
url = values['url'][0] url = values['url'][0]
# xep = values['xep'][0] # xep = values['xep'][0]
xep = None xep = None
@ -1525,9 +1534,9 @@ class Slixfeed(slixmpp.ClientXMPP):
values = payload['values'] values = payload['values']
node = values['node'] if 'node' in values else None node = values['node'] if 'node' in values else None
url = values['subscription'] url = values['subscription']
jid = values['jid'] if 'jid' in values else None
jid_bare = session['from'].bare jid_bare = session['from'].bare
if is_operator(self, jid_bare) and jid: if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid[0] if isinstance(jid, list) else jid jid_file = jid[0] if isinstance(jid, list) else jid
form.add_field(var='jid', form.add_field(var='jid',
ftype='hidden', ftype='hidden',
@ -1957,10 +1966,10 @@ class Slixfeed(slixmpp.ClientXMPP):
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
values = payload['values'] values = payload['values']
jid = values['jid']
jid_bare = session['from'].bare jid_bare = session['from'].bare
form = self['xep_0004'].make_form('form', 'Subscriptions') form = self['xep_0004'].make_form('form', 'Subscriptions')
if is_operator(self, jid_bare) and jid: if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid jid_file = jid
form.add_field(ftype='hidden', form.add_field(ftype='hidden',
value=jid, value=jid,
@ -2301,6 +2310,7 @@ class Slixfeed(slixmpp.ClientXMPP):
function_name = sys._getframe().f_code.co_name function_name = sys._getframe().f_code.co_name
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
jid_bare = session['from'].bare
match payload['values']['option']: match payload['values']['option']:
case 'activity': case 'activity':
# TODO dialog for JID and special dialog for operator # TODO dialog for JID and special dialog for operator
@ -2310,7 +2320,6 @@ class Slixfeed(slixmpp.ClientXMPP):
case 'admin': case 'admin':
# NOTE Even though this check is already conducted on previous # NOTE Even though this check is already conducted on previous
# form, this check is being done just in case. # form, this check is being done just in case.
jid_bare = session['from'].bare
if is_operator(self, jid_bare): if is_operator(self, jid_bare):
if self.is_component: if self.is_component:
# NOTE This will be changed with XEP-0222 XEP-0223 # NOTE This will be changed with XEP-0222 XEP-0223
@ -2370,6 +2379,15 @@ class Slixfeed(slixmpp.ClientXMPP):
desc='Enter URL to an OPML file.', desc='Enter URL to an OPML file.',
required=True) required=True)
url['validate']['datatype'] = 'xs:anyURI' url['validate']['datatype'] = 'xs:anyURI'
if is_operator(self, jid_bare):
form.add_field(ftype='fixed',
label='Subscriber')
form.add_field(desc=('Enter a Jabber ID to import '
'subscriptions to (The default '
'Jabber ID is your own).'),
ftype='text-single',
label='Jabber ID',
var='jid')
session['allow_complete'] = True session['allow_complete'] = True
session['has_next'] = False session['has_next'] = False
session['next'] = self._handle_import_complete session['next'] = self._handle_import_complete
@ -2377,8 +2395,8 @@ class Slixfeed(slixmpp.ClientXMPP):
case 'export': case 'export':
form = self['xep_0004'].make_form('form', 'Export') form = self['xep_0004'].make_form('form', 'Export')
form['instructions'] = ('To easily import subscriptions from ' form['instructions'] = ('To easily import subscriptions from '
'one News Reader to another, then it ' 'one News Reader to another, it is '
'is always recommended to export ' 'always recommended to export '
'subscriptions into OPML file. See ' 'subscriptions into OPML file. See '
'About -> Software for a list of ' 'About -> Software for a list of '
'News Readers offered for desktop and ' 'News Readers offered for desktop and '
@ -2393,6 +2411,15 @@ class Slixfeed(slixmpp.ClientXMPP):
options.addOption('OPML', 'opml') options.addOption('OPML', 'opml')
# options.addOption('HTML', 'html') # options.addOption('HTML', 'html')
# options.addOption('XBEL', 'xbel') # options.addOption('XBEL', 'xbel')
if is_operator(self, jid_bare):
form.add_field(ftype='fixed',
label='Subscriber')
form.add_field(desc=('Enter a Jabber ID to export '
'subscriptions from (The default '
'Jabber ID is your own).'),
ftype='text-single',
label='Jabber ID',
var='jid')
session['allow_complete'] = True session['allow_complete'] = True
session['has_next'] = False session['has_next'] = False
session['next'] = self._handle_export_complete session['next'] = self._handle_export_complete
@ -2547,10 +2574,15 @@ class Slixfeed(slixmpp.ClientXMPP):
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
form = payload form = payload
url = payload['values']['url'] values = payload['values']
url = values['url']
if url.startswith('http') and url.endswith('.opml'): if url.startswith('http') and url.endswith('.opml'):
jid_bare = session['from'].bare jid_bare = session['from'].bare
jid_file = jid_bare.replace('/', '_') if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid[0] if isinstance(jid, list) else jid
else:
jid_file = jid_bare
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
result = await fetch.http(url) result = await fetch.http(url)
count = await action.import_opml(db_file, result) count = await action.import_opml(db_file, result)
@ -2584,14 +2616,21 @@ class Slixfeed(slixmpp.ClientXMPP):
function_name = sys._getframe().f_code.co_name function_name = sys._getframe().f_code.co_name
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
form = payload form = self['xep_0004'].make_form('result', 'Done')
form['instructions'] = 'Export has been completed successfully!'
# form['type'] = 'result'
values = payload['values']
jid_bare = session['from'].bare jid_bare = session['from'].bare
jid_file = jid_bare.replace('/', '_') if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid[0] if isinstance(jid, list) else jid
else:
jid_file = jid_bare
# form = self['xep_0004'].make_form('result', 'Done') # form = self['xep_0004'].make_form('result', 'Done')
# form['instructions'] = ('✅️ Feeds have been exported') # form['instructions'] = ('✅️ Feeds have been exported')
exts = payload['values']['filetype'] exts = values['filetype']
for ext in exts: for ext in exts:
filename = action.export_feeds(self, jid_bare, jid_file, ext) filename = action.export_feeds(self, jid_file, jid_file, ext)
url = await XmppUpload.start(self, jid_bare, filename) url = await XmppUpload.start(self, jid_bare, filename)
chat_type = await get_chat_type(self, jid_bare) chat_type = await get_chat_type(self, jid_bare)
XmppMessage.send_oob(self, jid_bare, url, chat_type) XmppMessage.send_oob(self, jid_bare, url, chat_type)
@ -2600,9 +2639,6 @@ class Slixfeed(slixmpp.ClientXMPP):
label=ext, label=ext,
value=url) value=url)
url_field['validate']['datatype'] = 'xs:anyURI' url_field['validate']['datatype'] = 'xs:anyURI'
form['type'] = 'result'
form['title'] = 'Done'
form['instructions'] = ('Completed successfully!')
session["has_next"] = False session["has_next"] = False
session['next'] = None session['next'] = None
session['payload'] = form session['payload'] = form

View file

@ -246,9 +246,8 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
profile.set_identity(self, 'service') profile.set_identity(self, 'service')
await self['xep_0115'].update_caps() await self['xep_0115'].update_caps()
# self.send_presence() # self.send_presence()
# bookmarks = await self.plugin['xep_0048'].get_bookmarks() # bookmarks = await XmppBookmark.get(self)
# XmppGroupchat.autojoin(self, bookmarks) # await action.xmpp_muc_autojoin(self, bookmarks)
# await XmppGroupchat.autojoin(self, bookmarks)
jids = await XmppPubsub.get_pubsub_services(self) jids = await XmppPubsub.get_pubsub_services(self)
for jid_bare in jids: for jid_bare in jids:
if jid_bare not in self.settings: if jid_bare not in self.settings:
@ -273,8 +272,8 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
# self.send_presence() # self.send_presence()
profile.set_identity(self, 'service') profile.set_identity(self, 'service')
self['xep_0115'].update_caps() self['xep_0115'].update_caps()
# bookmarks = await self.plugin['xep_0048'].get_bookmarks() # bookmarks = await XmppBookmark.get(self)
# await XmppGroupchat.autojoin(self, bookmarks) # await action.xmpp_muc_autojoin(self, bookmarks)
time_end = time.time() time_end = time.time()
difference = time_end - time_begin difference = time_end - time_begin
if difference > 1: logger.warning('{} (time: {})'.format(function_name, if difference > 1: logger.warning('{} (time: {})'.format(function_name,
@ -859,7 +858,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
values = payload['values'] values = payload['values']
jid = values['jid'] jid = values['jid'] if 'jid' in values else None
jid_bare = session['from'].bare jid_bare = session['from'].bare
if jid != jid_bare and not is_operator(self, jid_bare): if jid != jid_bare and not is_operator(self, jid_bare):
text_warn = ('Posting to {} is restricted to operators only.' text_warn = ('Posting to {} is restricted to operators only.'
@ -982,7 +981,17 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
# It is not possible to assign non-str to transfer. # It is not possible to assign non-str to transfer.
# feed = values['feed'] # feed = values['feed']
node = values['node'][0] node = values['node'][0]
jid = values['jid'][0] jid = values['jid'] if 'jid' in values else None
jid_bare = session['from'].bare
if jid != jid_bare and not is_operator(self, jid_bare):
text_warn = 'You are not suppose to be here.'
session['allow_prev'] = False
session['has_next'] = False
session['next'] = None
session['notes'] = [['warn', text_warn]]
session['prev'] = None
session['payload'] = None
return session
url = values['url'][0] url = values['url'][0]
# xep = values['xep'][0] # xep = values['xep'][0]
xep = None xep = None
@ -1487,9 +1496,9 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
values = payload['values'] values = payload['values']
node = values['node'] if 'node' in values else None node = values['node'] if 'node' in values else None
url = values['subscription'] url = values['subscription']
jid = values['jid'] if 'jid' in values else None
jid_bare = session['from'].bare jid_bare = session['from'].bare
if is_operator(self, jid_bare) and jid: if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid[0] if isinstance(jid, list) else jid jid_file = jid[0] if isinstance(jid, list) else jid
form.add_field(var='jid', form.add_field(var='jid',
ftype='hidden', ftype='hidden',
@ -1919,10 +1928,10 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
values = payload['values'] values = payload['values']
jid = values['jid']
jid_bare = session['from'].bare jid_bare = session['from'].bare
form = self['xep_0004'].make_form('form', 'Subscriptions') form = self['xep_0004'].make_form('form', 'Subscriptions')
if is_operator(self, jid_bare) and jid: if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid jid_file = jid
form.add_field(ftype='hidden', form.add_field(ftype='hidden',
value=jid, value=jid,
@ -2263,6 +2272,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
function_name = sys._getframe().f_code.co_name function_name = sys._getframe().f_code.co_name
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
jid_bare = session['from'].bare
match payload['values']['option']: match payload['values']['option']:
case 'activity': case 'activity':
# TODO dialog for JID and special dialog for operator # TODO dialog for JID and special dialog for operator
@ -2272,7 +2282,6 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
case 'admin': case 'admin':
# NOTE Even though this check is already conducted on previous # NOTE Even though this check is already conducted on previous
# form, this check is being done just in case. # form, this check is being done just in case.
jid_bare = session['from'].bare
if is_operator(self, jid_bare): if is_operator(self, jid_bare):
if self.is_component: if self.is_component:
# NOTE This will be changed with XEP-0222 XEP-0223 # NOTE This will be changed with XEP-0222 XEP-0223
@ -2332,6 +2341,15 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
desc='Enter URL to an OPML file.', desc='Enter URL to an OPML file.',
required=True) required=True)
url['validate']['datatype'] = 'xs:anyURI' url['validate']['datatype'] = 'xs:anyURI'
if is_operator(self, jid_bare):
form.add_field(ftype='fixed',
label='Subscriber')
form.add_field(desc=('Enter a Jabber ID to import '
'subscriptions to (The default '
'Jabber ID is your own).'),
ftype='text-single',
label='Jabber ID',
var='jid')
session['allow_complete'] = True session['allow_complete'] = True
session['has_next'] = False session['has_next'] = False
session['next'] = self._handle_import_complete session['next'] = self._handle_import_complete
@ -2339,8 +2357,8 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
case 'export': case 'export':
form = self['xep_0004'].make_form('form', 'Export') form = self['xep_0004'].make_form('form', 'Export')
form['instructions'] = ('To easily import subscriptions from ' form['instructions'] = ('To easily import subscriptions from '
'one News Reader to another, then it ' 'one News Reader to another, it is '
'is always recommended to export ' 'always recommended to export '
'subscriptions into OPML file. See ' 'subscriptions into OPML file. See '
'About -> Software for a list of ' 'About -> Software for a list of '
'News Readers offered for desktop and ' 'News Readers offered for desktop and '
@ -2355,6 +2373,15 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
options.addOption('OPML', 'opml') options.addOption('OPML', 'opml')
# options.addOption('HTML', 'html') # options.addOption('HTML', 'html')
# options.addOption('XBEL', 'xbel') # options.addOption('XBEL', 'xbel')
if is_operator(self, jid_bare):
form.add_field(ftype='fixed',
label='Subscriber')
form.add_field(desc=('Enter a Jabber ID to export '
'subscriptions from (The default '
'Jabber ID is your own).'),
ftype='text-single',
label='Jabber ID',
var='jid')
session['allow_complete'] = True session['allow_complete'] = True
session['has_next'] = False session['has_next'] = False
session['next'] = self._handle_export_complete session['next'] = self._handle_export_complete
@ -2509,10 +2536,15 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
form = payload form = payload
url = payload['values']['url'] values = payload['values']
url = values['url']
if url.startswith('http') and url.endswith('.opml'): if url.startswith('http') and url.endswith('.opml'):
jid_bare = session['from'].bare jid_bare = session['from'].bare
jid_file = jid_bare.replace('/', '_') if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid[0] if isinstance(jid, list) else jid
else:
jid_file = jid_bare
db_file = config.get_pathname_to_database(jid_file) db_file = config.get_pathname_to_database(jid_file)
result = await fetch.http(url) result = await fetch.http(url)
count = await action.import_opml(db_file, result) count = await action.import_opml(db_file, result)
@ -2546,14 +2578,21 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
function_name = sys._getframe().f_code.co_name function_name = sys._getframe().f_code.co_name
logger.debug('{}: jid_full: {}' logger.debug('{}: jid_full: {}'
.format(function_name, jid_full)) .format(function_name, jid_full))
form = payload form = self['xep_0004'].make_form('result', 'Done')
form['instructions'] = 'Export has been completed successfully!'
# form['type'] = 'result'
values = payload['values']
jid_bare = session['from'].bare jid_bare = session['from'].bare
jid_file = jid_bare.replace('/', '_') if is_operator(self, jid_bare) and 'jid' in values:
jid = values['jid']
jid_file = jid[0] if isinstance(jid, list) else jid
else:
jid_file = jid_bare
# form = self['xep_0004'].make_form('result', 'Done') # form = self['xep_0004'].make_form('result', 'Done')
# form['instructions'] = ('✅️ Feeds have been exported') # form['instructions'] = ('✅️ Feeds have been exported')
exts = payload['values']['filetype'] exts = values['filetype']
for ext in exts: for ext in exts:
filename = action.export_feeds(self, jid_bare, jid_file, ext) filename = action.export_feeds(self, jid_file, jid_file, ext)
url = await XmppUpload.start(self, jid_bare, filename) url = await XmppUpload.start(self, jid_bare, filename)
chat_type = await get_chat_type(self, jid_bare) chat_type = await get_chat_type(self, jid_bare)
XmppMessage.send_oob(self, jid_bare, url, chat_type) XmppMessage.send_oob(self, jid_bare, url, chat_type)
@ -2562,9 +2601,6 @@ class SlixfeedComponent(slixmpp.ComponentXMPP):
label=ext, label=ext,
value=url) value=url)
url_field['validate']['datatype'] = 'xs:anyURI' url_field['validate']['datatype'] = 'xs:anyURI'
form['type'] = 'result'
form['title'] = 'Done'
form['instructions'] = ('Completed successfully!')
session["has_next"] = False session["has_next"] = False
session['next'] = None session['next'] = None
session['payload'] = form session['payload'] = form

View file

@ -985,6 +985,7 @@ async def message(self, message):
if is_operator(self, jid_bare): if is_operator(self, jid_bare):
info = message_text[14:] info = message_text[14:]
info = info.split(' ') info = info.split(' ')
if len(info) > 2:
jid = info[0] jid = info[0]
nid = info[1] nid = info[1]
if jid: if jid:
@ -993,6 +994,11 @@ async def message(self, message):
response = 'Deleted node: ' + nid response = 'Deleted node: ' + nid
else: else:
response = 'PubSub JID is missing. Enter PubSub JID.' response = 'PubSub JID is missing. Enter PubSub JID.'
else:
response = ('No action has been taken.'
'\n'
'Missing argument. '
'Enter JID and Node name.')
else: else:
response = ('This action is restricted. ' response = ('This action is restricted. '
'Type: sending news to PubSub.') 'Type: sending news to PubSub.')
@ -1001,6 +1007,7 @@ async def message(self, message):
if is_operator(self, jid_bare): if is_operator(self, jid_bare):
info = message_text[13:] info = message_text[13:]
info = info.split(' ') info = info.split(' ')
if len(info) > 1:
jid = info[0] jid = info[0]
nid = info[1] nid = info[1]
if jid: if jid:
@ -1009,6 +1016,11 @@ async def message(self, message):
response = 'Purged node: ' + nid response = 'Purged node: ' + nid
else: else:
response = 'PubSub JID is missing. Enter PubSub JID.' response = 'PubSub JID is missing. Enter PubSub JID.'
else:
response = ('No action has been taken.'
'\n'
'Missing argument. '
'Enter JID and Node name.')
else: else:
response = ('This action is restricted. ' response = ('This action is restricted. '
'Type: sending news to PubSub.') 'Type: sending news to PubSub.')