Sfoglia il codice sorgente

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 2 anni fa
parent
commit
f3f9b9baa8
2 ha cambiato i file con 36 aggiunte e 8 eliminazioni
  1. 2
    1
      README.md
  2. 34
    7
      kaabot.py

+ 2
- 1
README.md Vedi File

@@ -37,7 +37,7 @@ Usage
37 37
 
38 38
 ```
39 39
 usage: kaabot [-h] [-d] [-b DATABASE] [-j JID] [-p PASSWORD] [-m MUC]
40
-              [-n NICK] [-V VOCABULARY_FILE]
40
+              [-n NICK] [-V VOCABULARY_FILE] [--welcome WELCOME]
41 41
 
42 42
 optional arguments:
43 43
   -h, --help            show this help message and exit
@@ -53,6 +53,7 @@ optional arguments:
53 53
   -n NICK, --nick NICK  nickname to use in the chatroom (default: KaaBot)
54 54
   -V VOCABULARY_FILE, --vocabulary_file VOCABULARY_FILE
55 55
                         path to an alternative vocabulary file
56
+  --welcome WELCOME     welcome users joining the MUC (on/off, default: on)
56 57
 ```
57 58
 
58 59
 License

+ 34
- 7
kaabot.py Vedi File

@@ -52,7 +52,8 @@ default_vocabulary = {
52 52
 
53 53
 
54 54
 class KaaBot(sleekxmpp.ClientXMPP):
55
-    def __init__(self, jid, password, database, muc, nick, vocabulary_file):
55
+    def __init__(self, jid, password, database, muc, nick, vocabulary_file,
56
+                 welcome):
56 57
         sleekxmpp.ClientXMPP.__init__(self, jid, password)
57 58
 
58 59
         self.muc = muc
@@ -65,6 +66,8 @@ class KaaBot(sleekxmpp.ClientXMPP):
65 66
 
66 67
         self.vocabulary = self.init_vocabulary(vocabulary_file)
67 68
 
69
+        self.welcome = welcome
70
+
68 71
         self.users = self.db['user']
69 72
         # Initialize table with correct type.
70 73
         self.users.create_column('nick', sqlalchemy.String)
@@ -319,6 +322,12 @@ class KaaBot(sleekxmpp.ClientXMPP):
319 322
                           mbody=insult,
320 323
                           mtype='groupchat')
321 324
 
325
+    def send_welcome(self, nick, dest, date):
326
+        msg = self.pick_sentence('welcome').format(nick=nick, date=date)
327
+        self.send_message(mto=dest,
328
+                          mbody=msg,
329
+                          mtype='groupchat')
330
+
322 331
     def pick_sentence(self, type):
323 332
         """Returns a random sentence picked in the loaded vocabulary.
324 333
 
@@ -351,11 +360,12 @@ class KaaBot(sleekxmpp.ClientXMPP):
351 360
                         offline_timestamp = user['offline_timestamp']
352 361
                         date = datetime.datetime.strftime(offline_timestamp,
353 362
                                                           format="%c")
354
-                        msg = self.pick_sentence('welcome').format(nick=nick,
355
-                                                                   date=date)
356
-                        self.send_message(mto=presence['from'].bare,
357
-                                          mbody=msg,
358
-                                          mtype='groupchat')
363
+                        logging.debug('KaaBot : user {} connected, last seen {}'
364
+                                      .format(nick, date))
365
+                        if self.welcome:
366
+                            dest = presence['from'].bare
367
+                            self.send_welcome(nick, dest, date)
368
+
359 369
                     except TypeError:
360 370
                         msg = 'KaaBot : No offline timestamp yet for {nick}'
361 371
                         logging.debug(msg.format(nick=nick))
@@ -379,6 +389,19 @@ class KaaBot(sleekxmpp.ClientXMPP):
379 389
                               ['nick'])
380 390
 
381 391
 
392
+def str_to_bool(text):
393
+    """Converts a string to a boolean.
394
+
395
+    Raises an exception if the string does not describe a boolean value.
396
+    """
397
+    text = text.lower()
398
+    if text in ["on", "true", "1"]:
399
+        return True
400
+    elif text in ["off", "false", "0"]:
401
+        return False
402
+    raise TypeError
403
+
404
+
382 405
 if __name__ == '__main__':
383 406
 
384 407
     config_dir = xdg.BaseDirectory.save_config_path("kaabot")
@@ -403,6 +426,9 @@ if __name__ == '__main__':
403 426
                       help="nickname to use in the chatroom (default: KaaBot)")
404 427
     argp.add_argument("-V", "--vocabulary_file", dest="vocabulary_file",
405 428
                       help="path to an alternative vocabulary file")
429
+    argp.add_argument("--welcome", dest="welcome", default="on",
430
+                      type=str_to_bool,
431
+                      help="welcome users joining the MUC (on/off, default: on)")
406 432
 
407 433
     args = argp.parse_args()
408 434
 
@@ -427,7 +453,8 @@ if __name__ == '__main__':
427 453
         logging.debug('Config file exists.')
428 454
 
429 455
     bot = KaaBot(args.jid, args.password, args.database,
430
-                 args.muc, args.nick, args.vocabulary_file)
456
+                 args.muc, args.nick, args.vocabulary_file,
457
+                 args.welcome)
431 458
     bot.register_plugin('xep_0045')
432 459
     bot.register_plugin('xep_0071')
433 460
     bot.register_plugin('xep_0172')

Loading…
Annulla
Salva