From 646847e7ac2f273d2ca5dbfdd3224b85598c2baf Mon Sep 17 00:00:00 2001 From: nicobo Date: Fri, 22 May 2020 08:37:17 +0200 Subject: [PATCH] - remove ~/.xtalk support (does not look standard) ~ options username, password, group are now backend-specific, they only share convenient default options ~ options names fixed for config file compatibility --- README.md | 12 ++---- nicobot/askbot.py | 65 +++++++++++++++--------------- nicobot/jabber.py | 15 ------- test/askbot-sample-conf/config.yml | 15 +++++-- 4 files changed, 48 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 4b12bbc..2b3a363 100644 --- a/README.md +++ b/README.md @@ -180,8 +180,6 @@ The following options are common to both bots : - **--config-file** and **--config-dir** let you change the default configuration directory and file. All configuration files will be looked up from this directory ; `--config-file` allows overriding the location of `config.yml`. - **--backend** selects the *chatter* system to use : it currently supports "console" and "signal" (see below) -- **--username** selects the account to use to send and read message ; its format depends on the backend -- **--recipient** and **--group** select the recipient (only one of them should be given) ; its format depends on the backend - **--stealth** will make the bot connect and listen to messages but print any answer instead of sending it ; useful to observe the bot's behavior in a real chatroom... @@ -207,8 +205,8 @@ By using `--backend jabber` you can make the bot chat with XMPP (a.k.a. Jabber) ### Jabber-specific options -- `--username` and `--password` are the JabberID (e.g. *myusername@myserver.im*) and password of the bot's account, used to send and read messages. If either parameter is missing, will try to read `jid` and `password` from `~/.xtalk` file. -- `--recipient` is the JabberID of the person to send the message to +- `--jabber-username` and `--jabber-password` are the JabberID (e.g. *myusername@myserver.im*) and password of the bot's account used to send and read messages. If `--jabber-username` missing, `--username` will be used. +- `--jabber-recipient` is the JabberID of the person to send the message to. If missing, `--recipient` will be used. @@ -229,10 +227,8 @@ Please see the [man page](https://github.com/AsamK/signal-cli/blob/master/man/si ### Signal-specific options -With signal, make sure : - -- the `--username` parameter is your phone number in international format (e.g. `+33123456789`). In `config.yml`, make sure to put quotes around it to prevent YAML thinking it's an integer (because of the 'plus' sign) -- specify either `--recipient` as an international phone number or `--group` with a base 64 group ID (e.g. `--group "mABCDNVoEFGz0YeZM1234Q=="`). Once registered with Signal, you can list the IDs of the groups you are in with `signal-cli -U +336123456789 listGroups` +- `--signal-username` selects the account to use to send and read message : it is a phone number in international format (e.g. `+33123456789`). In `config.yml`, make sure to put quotes around it to prevent YAML thinking it's an integer (because of the 'plus' sign). If missing, `--username` will be used. +- `--signal-recipient` and `--signal-group` select the recipient (only one of them should be given). Make sure `--signal-recipient` is in international phone number format and `--signal-group` is a base 64 group ID (e.g. `--signal-group "mABCDNVoEFGz0YeZM1234Q=="`). If `--signal-recipient` is missing, `--recipient` will be used. Once registered with Signal, you can list the IDs of the groups you are in with `signal-cli -U +336123456789 listGroups` Sample command line to run the bot with Signal : diff --git a/nicobot/askbot.py b/nicobot/askbot.py index 785ec90..3f8bb50 100644 --- a/nicobot/askbot.py +++ b/nicobot/askbot.py @@ -34,7 +34,6 @@ class Config: 'backend': "console", 'config_file': "config.yml", 'config_dir': os.getcwd(), - 'group': None, 'input_file': sys.stdin, 'max_count': -1, 'patterns': [], @@ -147,8 +146,6 @@ if __name__ == '__main__': """ A convenient CLI to play with this bot. - Arguments are compatible with https://github.com/xmpppy/xmpppy/blob/master/xmpp/cli.py and `$HOME/.xtalk` - but new ones are added. TODO Put generic arguments in bot.py and inherit from it (should probably provide a parent ArgumentParser) """ @@ -165,14 +162,12 @@ if __name__ == '__main__': parser.add_argument("--config-dir", "-C", dest="config_dir", default=config.config_dir, help="Directory where to find configuration files by default.") parser.add_argument('--verbosity', '-V', dest='verbosity', default=config.verbosity, help="Log level") # Chatter-generic arguments - parser.add_argument("--backend", "-b", dest="backend", choices=['console','jabber','xmpp','signal'], default=config.backend, help="Chat backend to use") + parser.add_argument("--backend", "-b", dest="backend", choices=['console','jabber','signal'], default=config.backend, help="Chat backend to use") parser.add_argument("--input-file", "-i", dest="input_file", default=config.input_file, help="File to read messages from (one per line)") - parser.add_argument('--username', '-U', '--jabberid', dest='username', help="Sender's ID (a phone number for Signal, a Jabber Identifier (JID) aka. username for Jabber/XMPP") - parser.add_argument('--recipient', '-r', '--receiver', dest='recipients', action='append', help="Recipient's ID (e.g. '+12345678901' for Signal / JabberID (Receiver address) to send the message to)") - parser.add_argument('--group', '-g', dest='group', help="Group's ID (for Signal : a base64 string (e.g. 'mPC9JNVoKDGz0YeZMsbL1Q==')") + parser.add_argument('--username', '-U', dest='username', help="Sender's ID (a phone number for Signal, a Jabber Identifier (JID) aka. username for Jabber/XMPP") + parser.add_argument('--recipient', '-r', '--receiver', dest='recipients', default=[], action='append', help="Recipient's ID (e.g. '+12345678901' for Signal / JabberID (Receiver address) to send the message to)") parser.add_argument('--stealth', dest='stealth', action="store_true", default=config.stealth, help="Activate stealth mode on any chosen chatter") # Other core options - parser.add_argument('--password', '-P', dest='password', help="Senders's password") parser.add_argument('--max-count', dest='max_count', type=int, default=config.max_count, help="Read this maximum number of responses before exiting") parser.add_argument('--message', '-m', dest='message', help="Message to send. If missing, will read from --input-file") parser.add_argument('--message-file', '-f', dest='message_file', type=argparse.FileType('r'), default=sys.stdin, help="File with the message to send. If missing, will be read from standard input") @@ -182,9 +177,14 @@ if __name__ == '__main__': parser.add_argument("--debug", "-d", action="store_true", dest='debug', default=False, help="Activate debug logs (overrides --verbosity)") # Signal-specific arguments parser.add_argument('--signal-cli', dest='signal_cli', default=config.signal_cli, help="Path to `signal-cli` if not in PATH") + parser.add_argument('--signal-username', dest='signal_username', help="Username when using the Signal backend (overrides --username)") + parser.add_argument('--signal-group', dest='group', help="Group's ID (for Signal : a base64 string (e.g. 'mPC9JNVoKDGz0YeZMsbL1Q==')") + parser.add_argument('--signal-recipient', dest='signal_recipients', action='append', default=[], help="Recipient when using the Signal backend (overrides --recipient)") parser.add_argument('--signal-stealth', dest='signal_stealth', action="store_true", default=config.signal_stealth, help="Activate Signal chatter's specific stealth mode") # Jabber-specific arguments - # TODO + parser.add_argument('--jabber-username', '--jabberid', '--jid', dest='jabber_username', help="Username when using the Jabber/XMPP backend (overrides --username)") + parser.add_argument('--jabber-recipient', dest='jabber_recipients', action='append', default=[], help="Recipient when using the Jabber/XMPP backend (overrides --recipient)") + parser.add_argument('--jabber-password', dest='jabber_password', help="Senders's password") # # 1st pass only matters for 'bootstrap' options : configuration file and logging @@ -250,39 +250,38 @@ if __name__ == '__main__': # # Creates the chat engine depending on the 'backend' parameter - if config.backend in ['jabber','xmpp']: + if config.backend == 'jabber': logging.debug("Jabber/XMPP backend selected") - # Gets jid and password - if not config.username or not config.password: - logging.debug("Missing username or password : reading from .xtalk") - xtalkConf = read_xtalk_file() - logging.debug("Got from .xtalk : %r",xtalkConf) - config.username = xtalkConf['jid'] - config.password = xtalkConf['password'] - if not config.username: - raise ValueError("Missing --username and no ~.xtalk file") - if not config.password: - raise ValueError("Missing --password and no ~.xtalk file") - if len(config.recipients)==0 and not config.group: - raise ValueError("Missing --recipient") + username = config.jabber_username if config.jabber_username else config.username + if not username: + raise ValueError("Missing --jabber-username") + if not config.jabber_password: + raise ValueError("Missing --jabber-password") + recipients = config.jabber_recipients + config.recipients + if len(recipients)==0: + raise ValueError("Missing --jabber-recipient") + # TODO allow multiple recipients chatter = JabberChatter( - jid=config.username, - password=config.password, - recipient=config.recipients[0] + jid=username, + password=config.jabber_password, + recipient=recipients[0] ) elif config.backend == 'signal': logging.debug("Signal backend selected") if not config.signal_cli: raise ValueError("Could not find the 'signal-cli' command in PATH and no --signal-cli given") - if not config.username: - raise ValueError("Missing a username") - if len(config.recipient)==0 and not config.group: - raise ValueError("Either --recipient or --group must be provided") + username = config.signal_username if config.signal_username else config.username + if not username: + raise ValueError("Missing --signal-username") + recipients = config.signal_recipients + config.recipients + if len(recipients)==0 and not config.signal_group: + raise ValueError("Either --signal-recipient or --signal-group must be provided") + # TODO allow multiple recipients chatter = SignalChatter( - username=config.username, - recipient=config.recipients[0], - group=config.group, + username=username, + recipient=recipients[0], + group=config.signal_group, signal_cli=config.signal_cli, stealth=config.signal_stealth ) diff --git a/nicobot/jabber.py b/nicobot/jabber.py index 7c8cdae..5b98b77 100755 --- a/nicobot/jabber.py +++ b/nicobot/jabber.py @@ -16,21 +16,6 @@ from chatter import Chatter from helpers import * -""" Copy-pasted from https://github.com/xmpppy/xmpppy/blob/master/docs/examples/xtalk.py """ -def read_xtalk_file( filename=os.environ['HOME']+'/.xtalk' ): - - jidparams={} - if os.access(filename,os.R_OK): - for ln in open(filename).readlines(): - if not ln[0] in ('#',';'): - key,val=ln.strip().split('=',1) - jidparams[key.lower()]=val - for mandatory in ['jid','password']: - if mandatory not in jidparams.keys(): - raise ValueError('Please point ~/.xtalk config file to valid JID for sending messages. It should look like:\n\n#Uncomment fields before use and type in correct credentials.\n#JID=romeo@montague.net/resource (/resource is optional)\n#PASSWORD=juliet') - return jidparams - - class SliXmppClient(ClientXMPP): diff --git a/test/askbot-sample-conf/config.yml b/test/askbot-sample-conf/config.yml index ba69bdf..314b2bf 100644 --- a/test/askbot-sample-conf/config.yml +++ b/test/askbot-sample-conf/config.yml @@ -5,10 +5,19 @@ patterns: - [ "cancel", "(?i)\\b(cancel|abort)\\b" ] backend: console +#backend: jabber #backend: signal +# Used when backend = signal # Make sure to put quotes around the username field as it is a phone number for Signal -username: "+33123456789" -recipient: "+33123456789" +signal_username: "+33123456789" +signal_recipients: + - "+33123456789" # Get this group ID with the command `signal-cli -u +33123456789 listGroups` -#group: "mABCDNVoEFGz0YeZM1234Q==" +#signal_group: "mABCDNVoEFGz0YeZM1234Q==" + +# Used when backend = jabber +jabber_username: mybot@conversations.im +jabber_password: TheBestPasswordInTheWorld +jabber_recipients: + - itsme@conversations.im