[BACK]Return to mumble_client.py CVS log [TXT][DIR] Up to [contributed] / MumbleDicebot

File: [contributed] / MumbleDicebot / mumble_client.py (download)

Revision (vendor branch), Sat Apr 24 09:22:17 2021 UTC (19 months, 1 week ago) by rubenllorente
Branch: rubenllorente, MAIN
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines

Dicebot, far from useful, gets imported

from twisted.internet import ssl, reactor, protocol

class MumbleClientFactory(protocol.ClientFactory):
    protocol = None

    def __init__(self, protocol, nickname, password):
        self.nickname = nickname
        self.password = password
        self.protocol = protocol

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed"

    def clientConnectionLost(self, connector, reason):
        print "Connection lost"

def load_certificate(cert_file):
    from OpenSSL import crypto, SSL
    p12 = crypto.load_pkcs12(file(cert_file, 'rb').read())

    class CtxFactory(ssl.ClientContextFactory):
        def getContext(self):
            self.method = SSL.SSLv23_METHOD
            ctx = ssl.ClientContextFactory.getContext(self)
            return ctx

    return CtxFactory

def create_client(protocol, user, pw=""):
    return MumbleClientFactory(protocol, user, pw)

def start_client(factory, ip, port, certificate=None):
    # Loads certificate if available
    if certificate:
        ctx = load_certificate(certificate)
        ctx = ssl.ClientContextFactory

    reactor.connectSSL(ip, port, factory, ctx())