otclient.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. import struct
  17. import paho.mqtt.client
  18. class MQTToTClient(paho.mqtt.client.Client):
  19. # This is equivalent to the original _send_connect, except:
  20. # * the protocol ID is MQTToT.
  21. # * the client ID is sent without a length.
  22. # * all extra stuff like wills, usernames, passwords and MQTTv5 is removed.
  23. def _send_connect(self, keepalive):
  24. proto_ver = self._protocol
  25. protocol = b"MQTToT"
  26. remaining_length = 2 + len(protocol) + 1 + 1 + 2 + len(self._client_id)
  27. # Username, password, clean session
  28. connect_flags = 0x80 + 0x40 + 0x02
  29. command = paho.mqtt.client.CONNECT
  30. packet = bytearray()
  31. packet.append(command)
  32. self._pack_remaining_length(packet, remaining_length)
  33. packet.extend(
  34. struct.pack(
  35. f"!H{len(protocol)}sBBH",
  36. len(protocol),
  37. protocol,
  38. proto_ver,
  39. connect_flags,
  40. keepalive,
  41. )
  42. )
  43. packet.extend(self._client_id)
  44. self._keepalive = keepalive
  45. self._easy_log(
  46. paho.mqtt.client.MQTT_LOG_DEBUG,
  47. "Sending CONNECT",
  48. )
  49. return self._packet_queue(command, packet, 0, 0)