<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Chat admin bot to open and close the chatroom.

Run via cron.

    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010  <NAME>
    This file is part of SleekXMPP.

    See the file LICENSE for copying permission.
"""

import sys
import logging
import getpass
from optparse import OptionParser
from time import sleep

import sleekxmpp

import chat.utils
from multitenancy import tenant

# Python versions before 3.0 do not use UTF-8 encoding
# by default. To ensure that Unicode is handled properly
# throughout SleekXMPP, we will set the default encoding
# ourselves to UTF-8.
if sys.version_info < (3, 0):
    from sleekxmpp.util.misc_ops import setdefaultencoding
    setdefaultencoding('utf8')
else:
    raw_input = input

# Need to query the domain here as doing it inside SendMsgBot doesnt work
DOMAIN = chat.utils.domain()


class SendMsgBot(sleekxmpp.ClientXMPP):
    def __init__(self, jid, password, actions):
        self.me = jid
        self.actions = actions
        sleekxmpp.ClientXMPP.__init__(self, jid, password)

        # The session_start event will be triggered when
        # the bot establishes its connection with the server
        # and the XML streams are ready for use. We want to
        # listen for this event so that we we can initialize
        # our roster.
        self.add_event_handler("session_start", self.start, threaded=True)

    def start(self, event):
        """
        Process the session_start event.

        Typical actions for the session_start event are
        requesting the roster and broadcasting an initial
        presence stanza.

        Arguments:
            event -- An empty dictionary. The session_start
                     event does not provide any additional
                     data.
        """
        self.send_presence()
        self.get_roster()

        for action in self.actions:
            room = "%s@muc.%s" % (action['room'].slug, DOMAIN)
            self.plugin['xep_0045'].joinMUC(room,
                                            "chatadmin",
                                            # If a room password is needed, use:
                                            # password=<PASSWORD>,
                                            pfrom=self.me,
                                            wait=True)
            if action['action'] == 'open':
                if action['members']:
                    config = self.plugin['xep_0045'].getRoomConfig(room)
                    config.field['muc#roomconfig_membersonly']['value'] = 1
                else:
                    config = None
                self.plugin['xep_0045'].configureRoom(room, ifrom=self.me, form=config)
                for member in action['members']:
                    self.plugin['xep_0045'].setAffiliation(
                        room,<commit_msg>Explicitly set room to public or private on creation<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Chat admin bot to open and close the chatroom.

Run via cron.

    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010  <NAME>
    This file is part of SleekXMPP.

    See the file LICENSE for copying permission.
"""

import sys
import logging
import getpass
from optparse import OptionParser
from time import sleep

import sleekxmpp

import chat.utils
from multitenancy import tenant

# Python versions before 3.0 do not use UTF-8 encoding
# by default. To ensure that Unicode is handled properly
# throughout SleekXMPP, we will set the default encoding
# ourselves to UTF-8.
if sys.version_info < (3, 0):
    from sleekxmpp.util.misc_ops import setdefaultencoding
    setdefaultencoding('utf8')
else:
    raw_input = input

# Need to query the domain here as doing it inside SendMsgBot doesnt work
DOMAIN = chat.utils.domain()


class SendMsgBot(sleekxmpp.ClientXMPP):
    def __init__(self, jid, password, actions):
        self.me = jid
        self.actions = actions
        sleekxmpp.ClientXMPP.__init__(self, jid, password)

        # The session_start event will be triggered when
        # the bot establishes its connection with the server
        # and the XML streams are ready for use. We want to
        # listen for this event so that we we can initialize
        # our roster.
        self.add_event_handler("session_start", self.start, threaded=True)

    def start(self, event):
        """
        Process the session_start event.

        Typical actions for the session_start event are
        requesting the roster and broadcasting an initial
        presence stanza.

        Arguments:
            event -- An empty dictionary. The session_start
                     event does not provide any additional
                     data.
        """
        self.send_presence()
        self.get_roster()

        for action in self.actions:
            room = "%s@muc.%s" % (action['room'].slug, DOMAIN)
            self.plugin['xep_0045'].joinMUC(room,
                                            "chatadmin",
                                            # If a room password is needed, use:
                                            # password=<PASSWORD>,
                                            pfrom=self.me,
                                            wait=True)
            if action['action'] == 'open':
                config = self.plugin['xep_0045'].getRoomConfig(room)
                if action['members']:
                    logging.warn('setting member only %s' % room)
                    config.field['muc#roomconfig_membersonly']['value'] = 1
                else:
                    config.field['muc#roomconfig_membersonly']['value'] = 0
                self.plugin['xep_0045'].configureRoom(room, ifrom=self.me, form=config)
                for member in action['members']:
                    self.plugin['xep_0045'].setAffiliation(
                        room,
{'fraction_non_alphanumeric': 0.07926314010478283, 'fraction_numerical': 0.009971269224269056, 'mean_word_length': 1.860318994683422, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3711043', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1496, 'n_words': 514}