Guest User

Untitled

a guest
Jul 23rd, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. # Copyright (C) 2010 Arthur Furlan <afurlan@afurlan.org>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # On Debian systems, you can find the full text of the license in
  12. # /usr/share/common-licenses/GPL-3
  13.  
  14. JABBER = "jabber"
  15. CHAT = "chat"
  16. DELAY = 0.3
  17.  
  18. CHAT_IN_USE = CHAT + ".used"
  19.  
  20.  
  21. import xmpp
  22. from jabberbot import JabberBot, botcmd
  23. from datetime import datetime
  24. import re
  25. import codecs
  26. import time
  27. import os, os.path
  28.  
  29. class MUCJabberBot(JabberBot):
  30.  
  31.     ''' Add features in JabberBot to allow it to handle specific
  32.    caractheristics of multiple users chatroom (MUC). '''
  33.  
  34.     def __init__(self, *args, **kwargs):
  35.         ''' Initialize variables. '''
  36.  
  37.         # answer only direct messages or not?
  38.         self.only_direct = kwargs.get('only_direct', False)
  39.         try:
  40.             del kwargs['only_direct']
  41.         except KeyError:
  42.             pass
  43.  
  44.         # initialize jabberbot
  45.         super(MUCJabberBot, self).__init__(*args, **kwargs)
  46.  
  47.         # create a regex to check if a message is a direct message
  48.         user, domain = str(self.jid).split('@')
  49.         self.direct_message_re = re.compile('^%s(@%s)?[^\w]? ' \
  50.                 % (user, domain))
  51.  
  52.         self.timer = time.time()
  53.  
  54.     def callback_message(self, conn, mess):
  55.         ''' Changes the behaviour of the JabberBot in order to allow
  56.        it to answer direct messages. This is used often when it is
  57.        connected in MUCs (multiple users chatroom). '''
  58.  
  59.         message = mess.getBody()
  60.         if not message:
  61.             return
  62.  
  63.         if mess.getType() != "groupchat":
  64.             return
  65.         if xmpp.NS_DELAY in mess.getProperties():
  66.             return
  67.         if self.jid.bareMatch(mess.getFrom()):
  68.             return
  69.         if not message:
  70.             return
  71.         user = self.get_sender_username(mess)
  72.         if user == nickname:
  73.             return
  74.         if len(message) > 256:
  75.             self.send_simple_reply(mess, "Message is too long")
  76.             return
  77.  
  78.         # it's time to send message to lua
  79.         f = codecs.open(JABBER, "a", "utf-8")
  80.         f.write("<xmpp://%s> %s\n" % (user, " ".join(message.splitlines())))
  81.         f.close()
  82.  
  83.     def idle_proc(self):
  84.         super(MUCJabberBot, self)
  85.         cur = time.time()
  86.         if cur - self.timer > DELAY:
  87.             # let's grab messages from lua folder!
  88.             self.timer = cur
  89.             if not os.path.exists(CHAT):
  90.                 return
  91.             os.rename(CHAT, CHAT_IN_USE)
  92.             f = codecs.open(CHAT_IN_USE, "r", "utf-8")
  93.             for x in f.readlines():
  94.                 self.send("game@minetest.ru", " ".join(x.splitlines()), message_type='groupchat')
  95.             f.close()
  96.  
  97. if __name__ == '__main__':
  98.  
  99.     username = 'game@users.minetest.ru'
  100.     password = '---'
  101.     nickname = 'game'
  102.     chatroom = 'game@minetest.ru'
  103.  
  104.     mucbot = MUCJabberBot(username, password, only_direct=False)
  105.     mucbot.join_room(chatroom, nickname)
  106.     mucbot.serve_forever()
Add Comment
Please, Sign In to add comment