<commit_before>#!/usr/bin/env python


import os
import sys
import logging
import requests
import argparse

from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout, XMPPError

from pprint import pprint


class InvalidRecipient(Exception):
    pass


class Uploadr(ClientXMPP):

    filename = None

    def __init__(self, jid, password, filename, shorten_url, notify):
        ClientXMPP.__init__(self, jid, password)
        self.add_event_handler("session_start", self.session_start)
        self.register_plugin('xep_0030') # Service Discovery
        self.register_plugin("xep_0363") # HTTP File Upload
        self.filename = filename
        self.shorten_url = shorten_url
        self.notify = notify

    def good_recipient(self, jid):
        if jid == self.boundjid.bare:
            return True
# FIXME: Eigentlich sollte der Roster schon da sein.
        roster = self.get_roster(block=True, timeout = 10)
        jids = roster['roster']['items']
        if jid in jids:
            if jids[jid]['subscription'] == 'both':
                return True
            else:
                return False
        else:
            return False

    def session_start(self, event):
        self.send_presence()
        self.get_roster()

        # Upload File

        try:
            if self.notify:
                if self.good_recipient(self.notify):
                    get_url = self['xep_0363'].upload_file(self.filename)
                    self.send_message(mto=self.notify, mbody=get_url, mtype="chat")
                else:
                    raise InvalidRecipient
            else:
                get_url = self['xep_0363'].upload_file(self.filename)
            if self.shorten_url:
                print self.short(get_url)
            else:
                print get_url
        except IqError as err:
            logging.error('There was an error getting the roster')
            logging.error(err.iq['error']['condition'])<commit_msg>add support for XEP-0066: OOB<commit_after>#!/usr/bin/env python


import os
import sys
import logging
import requests
import argparse

from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout, XMPPError

from pprint import pprint


class InvalidRecipient(Exception):
    pass


class Uploadr(ClientXMPP):

    filename = None

    def __init__(self, jid, password, filename, shorten_url, notify):
        ClientXMPP.__init__(self, jid, password)
        self.add_event_handler("session_start", self.session_start)
        self.register_plugin('xep_0030') # Service Discovery
        self.register_plugin("xep_0066") # OOB
        self.register_plugin("xep_0363") # HTTP File Upload
        self.filename = filename
        self.shorten_url = shorten_url
        self.notify = notify

    def good_recipient(self, jid):
        if jid == self.boundjid.bare:
            return True
# FIXME: Eigentlich sollte der Roster schon da sein.
        roster = self.get_roster(block=True, timeout = 10)
        jids = roster['roster']['items']
        if jid in jids:
            if jids[jid]['subscription'] == 'both':
                return True
            else:
                return False
        else:
            return False

    def session_start(self, event):
        self.send_presence()
        self.get_roster()

        # Upload File

        try:
            if self.notify:
                if self.good_recipient(self.notify):
                    get_url = self['xep_0363'].upload_file(self.filename)
                    m = self.Message()
                    m['to'] = self.notify
                    m['type'] = 'chat'
                    m['body'] = get_url
                    m['oob']['url'] = get_url
                    m.send()
                else:
                    raise InvalidRecipient
            else:
                get_url = self['xep_0363'].upload_file(self.filename)
            if self.shorten_url:
                print self.short(get_url)
            else:
                print get_url
        except IqError as err:
            logging.error('There was an error getting the roster')
            logging.error(err.iq['error']['condition'])
{'fraction_non_alphanumeric': 0.08954154727793696, 'fraction_numerical': 0.010506208213944603, 'mean_word_length': 1.9458509142053446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1110210', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1179, 'n_words': 287}