Browse Source

Allow disabling welcome messages (--welcome)

Add the --welcome option, that takes an on/off parameter and
enables/disables welcome messages when a new user joins the MUC.
The default is enabled.

send_welcome() was refactored from muc_online().

Issue: RoboTux's rant.
master
Matteo Cypriani 3 years ago
parent
commit
f3f9b9baa8
2 changed files with 36 additions and 8 deletions
  1. 2
    1
      README.md
  2. 34
    7
      kaabot.py

+ 2
- 1
README.md View File

@@ -37,7 +37,7 @@ Usage

```
usage: kaabot [-h] [-d] [-b DATABASE] [-j JID] [-p PASSWORD] [-m MUC]
[-n NICK] [-V VOCABULARY_FILE]
[-n NICK] [-V VOCABULARY_FILE] [--welcome WELCOME]

optional arguments:
-h, --help show this help message and exit
@@ -53,6 +53,7 @@ optional arguments:
-n NICK, --nick NICK nickname to use in the chatroom (default: KaaBot)
-V VOCABULARY_FILE, --vocabulary_file VOCABULARY_FILE
path to an alternative vocabulary file
--welcome WELCOME welcome users joining the MUC (on/off, default: on)
```

License

+ 34
- 7
kaabot.py View File

@@ -52,7 +52,8 @@ default_vocabulary = {


class KaaBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, database, muc, nick, vocabulary_file):
def __init__(self, jid, password, database, muc, nick, vocabulary_file,
welcome):
sleekxmpp.ClientXMPP.__init__(self, jid, password)

self.muc = muc
@@ -65,6 +66,8 @@ class KaaBot(sleekxmpp.ClientXMPP):

self.vocabulary = self.init_vocabulary(vocabulary_file)

self.welcome = welcome

self.users = self.db['user']
# Initialize table with correct type.
self.users.create_column('nick', sqlalchemy.String)
@@ -319,6 +322,12 @@ class KaaBot(sleekxmpp.ClientXMPP):
mbody=insult,
mtype='groupchat')

def send_welcome(self, nick, dest, date):
msg = self.pick_sentence('welcome').format(nick=nick, date=date)
self.send_message(mto=dest,
mbody=msg,
mtype='groupchat')

def pick_sentence(self, type):
"""Returns a random sentence picked in the loaded vocabulary.

@@ -351,11 +360,12 @@ class KaaBot(sleekxmpp.ClientXMPP):
offline_timestamp = user['offline_timestamp']
date = datetime.datetime.strftime(offline_timestamp,
format="%c")
msg = self.pick_sentence('welcome').format(nick=nick,
date=date)
self.send_message(mto=presence['from'].bare,
mbody=msg,
mtype='groupchat')
logging.debug('KaaBot : user {} connected, last seen {}'
.format(nick, date))
if self.welcome:
dest = presence['from'].bare
self.send_welcome(nick, dest, date)

except TypeError:
msg = 'KaaBot : No offline timestamp yet for {nick}'
logging.debug(msg.format(nick=nick))
@@ -379,6 +389,19 @@ class KaaBot(sleekxmpp.ClientXMPP):
['nick'])


def str_to_bool(text):
"""Converts a string to a boolean.

Raises an exception if the string does not describe a boolean value.
"""
text = text.lower()
if text in ["on", "true", "1"]:
return True
elif text in ["off", "false", "0"]:
return False
raise TypeError


if __name__ == '__main__':

config_dir = xdg.BaseDirectory.save_config_path("kaabot")
@@ -403,6 +426,9 @@ if __name__ == '__main__':
help="nickname to use in the chatroom (default: KaaBot)")
argp.add_argument("-V", "--vocabulary_file", dest="vocabulary_file",
help="path to an alternative vocabulary file")
argp.add_argument("--welcome", dest="welcome", default="on",
type=str_to_bool,
help="welcome users joining the MUC (on/off, default: on)")

args = argp.parse_args()

@@ -427,7 +453,8 @@ if __name__ == '__main__':
logging.debug('Config file exists.')

bot = KaaBot(args.jid, args.password, args.database,
args.muc, args.nick, args.vocabulary_file)
args.muc, args.nick, args.vocabulary_file,
args.welcome)
bot.register_plugin('xep_0045')
bot.register_plugin('xep_0071')
bot.register_plugin('xep_0172')

Loading…
Cancel
Save