otclient.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 paho.mqtt.client
  17. import struct
  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 +
  27. 1 + 2 + len(self._client_id))
  28. # Username, password, clean session
  29. connect_flags = 0x80 + 0x40 + 0x02
  30. command = paho.mqtt.client.CONNECT
  31. packet = bytearray()
  32. packet.append(command)
  33. self._pack_remaining_length(packet, remaining_length)
  34. packet.extend(struct.pack(f"!H{len(protocol)}sBBH",
  35. len(protocol), protocol, proto_ver, connect_flags, keepalive))
  36. packet.extend(self._client_id)
  37. self._keepalive = keepalive
  38. self._easy_log(
  39. paho.mqtt.client.MQTT_LOG_DEBUG,
  40. "Sending CONNECT",
  41. )
  42. return self._packet_queue(command, packet, 0, 0)