1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
|
# -*- coding: utf-8 -*-
# Copyright © 2018, 2019 Damir Jelić <poljar@termina.org.uk>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import unicode_literals
import os
import pprint
import socket
import ssl
import time
import copy
from collections import defaultdict, deque
from atomicwrites import atomic_write
from typing import (
Any,
Deque,
Dict,
Optional,
List,
NamedTuple,
DefaultDict,
Type,
Union,
)
from uuid import UUID
from nio import (
Api,
HttpClient,
ClientConfig,
LocalProtocolError,
LoginResponse,
LoginInfoResponse,
Response,
Rooms,
RoomSendResponse,
RoomSendError,
SyncResponse,
ShareGroupSessionResponse,
ShareGroupSessionError,
KeysQueryResponse,
KeysClaimResponse,
DevicesResponse,
UpdateDeviceResponse,
DeleteDevicesAuthResponse,
DeleteDevicesResponse,
TransportType,
RoomMessagesResponse,
RoomMessagesError,
EncryptionError,
GroupEncryptionError,
OlmTrustError,
ErrorResponse,
SyncError,
LoginError,
JoinedMembersResponse,
JoinedMembersError,
RoomKeyEvent,
KeyVerificationStart,
KeyVerificationCancel,
KeyVerificationKey,
KeyVerificationMac,
KeyVerificationEvent,
ToDeviceMessage,
ToDeviceResponse,
ToDeviceError
)
from . import globals as G
from .buffer import OwnAction, OwnMessage, RoomBuffer
from .config import ConfigSection, Option, ServerBufferType
from .globals import SCRIPT_NAME, SERVERS, W, TYPING_NOTICE_TIMEOUT
from .utf import utf8_decode
from .utils import create_server_buffer, key_from_value, server_buffer_prnt
from .uploads import Upload
from .colors import Formatted, FormattedString, DEFAULT_ATTRIBUTES
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse # type: ignore
try:
FileNotFoundError # type: ignore
except NameError:
FileNotFoundError = IOError
EncryptionQueueItem = NamedTuple(
"EncryptionQueueItem",
[
("message_type", str),
("message", Union[Formatted, Upload]),
],
)
class ServerConfig(ConfigSection):
def __init__(self, server_name, config_ptr):
# type: (str, str) -> None
self._server_name = server_name
self._config_ptr = config_ptr
self._option_ptrs = {} # type: Dict[str, str]
options = [
Option(
"autoconnect",
"boolean",
"",
0,
0,
"off",
(
"automatically connect to the matrix server when weechat "
"is starting"
),
),
Option(
"address",
"string",
"",
0,
0,
"",
(
"Hostname or address of the server (note: content is "
"evaluated, see /help eval)"
)
),
Option(
"port", "integer", "", 0, 65535, "443", "Port for the server"
),
Option(
"proxy",
"string",
"",
0,
0,
"",
("Name of weechat proxy to use (see /help proxy) (note: "
"content is evaluated, see /help eval)"),
),
Option(
"ssl_verify",
"boolean",
"",
0,
0,
"on",
("Check that the SSL connection is fully trusted"),
),
Option(
"username",
"string",
"",
0,
0,
"",
(
"Username to use on the server (note: content is "
"evaluated, see /help eval)"
)
),
Option(
"password",
"string",
"",
0,
0,
"",
(
"Password for the server (note: content is evaluated, see "
"/help eval)"
),
),
Option(
"device_name",
"string",
"",
0,
0,
"Weechat Matrix",
(
"Device name to use when logging in, this "
"is only used on the firt login. Afther that the /devices "
"command can be used to change the device name. (note: "
"content is evaluated, see /help eval)"
)
),
Option(
"autoreconnect_delay",
"integer",
"",
0,
86400,
"10",
("Delay (in seconds) before trying to reconnect to server"),
),
Option(
"sso_helper_listening_port",
"integer",
"",
0,
65535,
"0",
("The port that the SSO helpers web server should listen on"),
),
]
section = W.config_search_section(config_ptr, "server")
self._ptr = section
for option in options:
option_name = "{server}.{option}".format(
server=self._server_name, option=option.name
)
self._option_ptrs[option.name] = W.config_new_option(
config_ptr,
section,
option_name,
option.type,
option.description,
option.string_values,
option.min,
option.max,
option.value,
option.value,
0,
"",
"",
"matrix_config_server_change_cb",
self._server_name,
"",
"",
)
autoconnect = ConfigSection.option_property("autoconnect", "boolean")
address = ConfigSection.option_property("address", "string", evaluate=True)
port = ConfigSection.option_property("port", "integer")
proxy = ConfigSection.option_property("proxy", "string", evaluate=True)
ssl_verify = ConfigSection.option_property("ssl_verify", "boolean")
username = ConfigSection.option_property("username", "string",
evaluate=True)
device_name = ConfigSection.option_property("device_name", "string",
evaluate=True)
reconnect_delay = ConfigSection.option_property("autoreconnect_delay", "integer")
password = ConfigSection.option_property(
"password", "string", evaluate=True
)
sso_helper_listening_port = ConfigSection.option_property(
"sso_helper_listening_port",
"integer"
)
def free(self):
W.config_section_free_options(self._ptr)
class MatrixServer(object):
# pylint: disable=too-many-instance-attributes
def __init__(self, name, config_ptr):
# type: (str, str) -> None
# yapf: disable
self.name = name # type: str
self.user_id = ""
self.device_id = "" # type: str
self.room_buffers = dict() # type: Dict[str, RoomBuffer]
self.buffers = dict() # type: Dict[str, str]
self.server_buffer = None # type: Optional[str]
self.fd_hook = None # type: Optional[str]
self.ssl_hook = None # type: Optional[str]
self.timer_hook = None # type: Optional[str]
self.numeric_address = "" # type: Optional[str]
self._connected = False # type: bool
self.connecting = False # type: bool
self.reconnect_delay = 0 # type: int
self.reconnect_time = None # type: Optional[float]
self.sync_time = None # type: Optional[float]
self.socket = None # type: Optional[ssl.SSLSocket]
self.ssl_context = ssl.create_default_context() # type: ssl.SSLContext
self.transport_type = None # type: Optional[TransportType]
self.sso_hook = None
# Enable http2 negotiation on the ssl context.
self.ssl_context.set_alpn_protocols(["h2", "http/1.1"])
self.address = None
self.homeserver = None
self.client = None # type: Optional[HttpClient]
self.access_token = None # type: Optional[str]
self.next_batch = None # type: Optional[str]
self.transaction_id = 0 # type: int
self.lag = 0 # type: int
self.lag_done = False # type: bool
self.busy = False # type: bool
self.first_sync = True
self.send_fd_hook = None # type: Optional[str]
self.send_buffer = b"" # type: bytes
self.device_check_timestamp = None # type: Optional[int]
self.device_deletion_queue = dict() # type: Dict[str, str]
self.encryption_queue = defaultdict(deque) \
# type: DefaultDict[str, Deque[EncryptionQueueItem]]
self.backlog_queue = dict() # type: Dict[str, str]
self.user_gc_time = time.time() # type: float
self.member_request_list = [] # type: List[str]
self.rooms_with_missing_members = [] # type: List[str]
self.lazy_load_hook = None # type: Optional[str]
# These flags remember if we made some requests so that we don't
# make them again while we wait on a response, the flags need to be
# cleared when we disconnect.
self.keys_queried = False # type: bool
self.keys_claimed = defaultdict(bool) # type: Dict[str, bool]
self.group_session_shared = defaultdict(bool) # type: Dict[str, bool]
self.ignore_while_sharing = defaultdict(bool) # type: Dict[str, bool]
self.to_device_sent = [] # type: List[ToDeviceMessage]
# Try to load the device id, the device id is loaded every time the
# user changes but some login flows don't use a user so try to load the
# device for a main user.
self._load_device_id("main")
self.config = ServerConfig(self.name, config_ptr)
self._create_session_dir()
# yapf: enable
def _create_session_dir(self):
path = os.path.join("matrix", self.name)
if not W.mkdir_home(path, 0o700):
message = (
"{prefix}matrix: Error creating server session " "directory"
).format(prefix=W.prefix("error"))
W.prnt("", message)
@property
def connected(self):
return self._connected
@connected.setter
def connected(self, value):
self._connected = value
W.bar_item_update("buffer_modes")
W.bar_item_update("matrix_modes")
def get_session_path(self):
home_dir = W.info_get("weechat_dir", "")
return os.path.join(home_dir, "matrix", self.name)
def _load_device_id(self, user=None):
user = user or self.config.username
file_name = "{}{}".format(user, ".device_id")
path = os.path.join(self.get_session_path(), file_name)
if not os.path.isfile(path):
return
with open(path, "r") as device_file:
device_id = device_file.readline().rstrip()
if device_id:
self.device_id = device_id
def save_device_id(self):
file_name = "{}{}".format(self.config.username or "main", ".device_id")
path = os.path.join(self.get_session_path(), file_name)
with atomic_write(path, overwrite=True) as device_file:
device_file.write(self.device_id)
@staticmethod
def _parse_url(address, port):
if not address.startswith("http"):
address = "https://{}".format(address)
parsed_url = urlparse(address)
homeserver = parsed_url._replace(
netloc=parsed_url.hostname + ":{}".format(port)
)
return homeserver
def _change_client(self):
homeserver = MatrixServer._parse_url(
self.config.address,
self.config.port
)
self.address = homeserver.hostname
self.homeserver = homeserver
config = ClientConfig(store_sync_tokens=True)
self.client = HttpClient(
homeserver.geturl(),
self.config.username,
self.device_id,
self.get_session_path(),
config=config
)
self.client.add_to_device_callback(
self.key_verification_cb,
KeyVerificationEvent
)
def key_verification_cb(self, event):
if isinstance(event, KeyVerificationStart):
self.info_highlight("{user} via {device} has started a key "
"verification process.\n"
"To accept use /olm verification "
"accept {user} {device}".format(
user=event.sender,
device=event.from_device
))
elif isinstance(event, KeyVerificationKey):
sas = self.client.key_verifications.get(event.transaction_id, None)
if not sas:
return
if sas.canceled:
return
device = sas.other_olm_device
emoji = sas.get_emoji()
emojis = [x[0] for x in emoji]
descriptions = [x[1] for x in emoji]
centered_width = 12
def center_emoji(emoji, width):
# Assume each emoji has width 2
emoji_width = 2
# These are emojis that need VARIATION-SELECTOR-16 (U+FE0F) so
# that they are rendered with coloured glyphs. For these, we
# need to add an extra space after them so that they are
# rendered properly in weechat.
variation_selector_emojis = [
'☁️',
'❤️',
'☂️',
'✏️',
'✂️',
'☎️',
'✈️'
]
# Hack to make weechat behave properly when one of the above is
# printed.
if emoji in variation_selector_emojis:
emoji += " "
# This is a trick to account for the fact that emojis are wider
# than other monospace characters.
placeholder = '.' * emoji_width
return placeholder.center(width).replace(placeholder, emoji)
emoji_str = u"".join(center_emoji(e, centered_width)
for e in emojis)
desc = u"".join(d.center(centered_width) for d in descriptions)
short_string = u"\n".join([emoji_str, desc])
self.info_highlight(u"Short authentication string for "
u"{user} via {device}:\n{string}\n"
u"Confirm that the strings match with "
u"/olm verification confirm {user} "
u"{device}".format(
user=device.user_id,
device=device.id,
string=short_string
))
elif isinstance(event, KeyVerificationMac):
try:
sas = self.client.key_verifications[event.transaction_id]
except KeyError:
return
device = sas.other_olm_device
if sas.verified:
self.info_highlight("Device {} of user {} successfully "
"verified".format(
device.id,
device.user_id
))
elif isinstance(event, KeyVerificationCancel):
self.info_highlight("The interactive device verification with "
"user {} got canceled: {}.".format(
event.sender,
event.reason
))
def update_option(self, option, option_name):
if option_name == "address":
self._change_client()
elif option_name == "port":
self._change_client()
elif option_name == "ssl_verify":
value = W.config_boolean(option)
if value:
self.ssl_context.verify_mode = ssl.CERT_REQUIRED
self.ssl_context.check_hostname = True
else:
self.ssl_context.check_hostname = False
self.ssl_context.verify_mode = ssl.CERT_NONE
elif option_name == "username":
value = W.config_string(option)
self.access_token = ""
self._load_device_id()
if self.client:
self.client.user = value
if self.device_id:
self.client.device_id = self.device_id
else:
pass
def send_or_queue(self, request):
# type: (bytes) -> None
self.send(request)
def try_send(self, message):
# type: (MatrixServer, bytes) -> bool
sock = self.socket
if not sock:
return False
total_sent = 0
message_length = len(message)
while total_sent < message_length:
try:
sent = sock.send(message[total_sent:])
except ssl.SSLWantWriteError:
hook = W.hook_fd(sock.fileno(), 0, 1, 0, "send_cb", self.name)
self.send_fd_hook = hook
self.send_buffer = message[total_sent:]
return True
except socket.error as error:
self._abort_send()
errno = "error" + str(error.errno) + " " if error.errno else ""
strerr = error.strerror if error.strerror else "Unknown reason"
strerr = errno + strerr
error_message = (
"{prefix}Error while writing to " "socket: {error}"
).format(prefix=W.prefix("network"), error=strerr)
server_buffer_prnt(self, error_message)
server_buffer_prnt(
self,
("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network")
),
)
self.disconnect()
return False
if sent == 0:
self._abort_send()
server_buffer_prnt(
self,
"{prefix}matrix: Error while writing to socket".format(
prefix=W.prefix("network")
),
)
server_buffer_prnt(
self,
("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network")
),
)
self.disconnect()
return False
total_sent = total_sent + sent
self._finalize_send()
return True
def _abort_send(self):
self.send_buffer = b""
def _finalize_send(self):
# type: (MatrixServer) -> None
self.send_buffer = b""
def info_highlight(self, message):
buf = ""
if self.server_buffer:
buf = self.server_buffer
msg = "{}{}: {}".format(W.prefix("network"), SCRIPT_NAME, message)
W.prnt_date_tags(buf, 0, "notify_highlight", msg)
def info(self, message):
buf = ""
if self.server_buffer:
buf = self.server_buffer
msg = "{}{}: {}".format(W.prefix("network"), SCRIPT_NAME, message)
W.prnt(buf, msg)
def error(self, message):
buf = ""
if self.server_buffer:
buf = self.server_buffer
msg = "{}{}: {}".format(W.prefix("error"), SCRIPT_NAME, message)
W.prnt(buf, msg)
def send(self, data):
# type: (bytes) -> bool
self.try_send(data)
return True
def reconnect(self):
message = ("{prefix}matrix: reconnecting to server...").format(
prefix=W.prefix("network")
)
server_buffer_prnt(self, message)
self.reconnect_time = None
if not self.connect():
self.schedule_reconnect()
def schedule_reconnect(self):
# type: (MatrixServer) -> None
self.connecting = True
self.reconnect_time = time.time()
if self.reconnect_delay:
self.reconnect_delay = (
self.reconnect_delay
* G.CONFIG.network.autoreconnect_delay_growing
)
else:
self.reconnect_delay = self.config.reconnect_delay
if G.CONFIG.network.autoreconnect_delay_max > 0:
self.reconnect_delay = min(self.reconnect_delay,
G.CONFIG.network.autoreconnect_delay_max)
message = (
"{prefix}matrix: reconnecting to server in {t} " "seconds"
).format(prefix=W.prefix("network"), t=self.reconnect_delay)
server_buffer_prnt(self, message)
def _close_socket(self):
# type: () -> None
if self.socket:
try:
self.socket.shutdown(socket.SHUT_RDWR)
except socket.error:
pass
try:
self.socket.close()
except OSError:
pass
def disconnect(self, reconnect=True):
# type: (bool) -> None
if self.fd_hook:
W.unhook(self.fd_hook)
self._close_socket()
self.fd_hook = None
self.socket = None
self.connected = False
self.access_token = ""
self.send_buffer = b""
self.transport_type = None
self.member_request_list = []
if self.client:
try:
self.client.disconnect()
except LocalProtocolError:
pass
self.lag = 0
W.bar_item_update("lag")
self.reconnect_time = None
# Clear our request flags.
self.keys_queried = False
self.keys_claimed = defaultdict(bool)
self.group_session_shared = defaultdict(bool)
self.ignore_while_sharing = defaultdict(bool)
self.to_device_sent = []
if self.server_buffer:
message = ("{prefix}matrix: disconnected from server").format(
prefix=W.prefix("network")
)
server_buffer_prnt(self, message)
if reconnect:
self.schedule_reconnect()
else:
self.reconnect_delay = 0
def connect(self):
# type: (MatrixServer) -> int
if not self.config.address or not self.config.port:
message = "{prefix}Server address or port not set".format(
prefix=W.prefix("error")
)
W.prnt("", message)
return False
if self.connected:
return True
if not self.server_buffer:
create_server_buffer(self)
if not self.timer_hook:
self.timer_hook = W.hook_timer(
1 * 1000, 0, 0, "matrix_timer_cb", self.name
)
ssl_message = " (SSL)" if self.ssl_context.check_hostname else ""
message = (
"{prefix}matrix: Connecting to " "{server}:{port}{ssl}..."
).format(
prefix=W.prefix("network"),
server=self.address,
port=self.config.port,
ssl=ssl_message,
)
W.prnt(self.server_buffer, message)
W.hook_connect(
self.config.proxy,
self.address,
self.config.port,
1,
0,
"",
"connect_cb",
self.name,
)
return True
def schedule_sync(self):
self.sync_time = time.time()
def sync(self, timeout=None, sync_filter=None):
# type: (Optional[int], Optional[Dict[Any, Any]]) -> None
if not self.client:
return
self.sync_time = None
_, request = self.client.sync(timeout, sync_filter,
full_state=self.first_sync)
self.send_or_queue(request)
def login_info(self):
# type: () -> None
if not self.client:
return
if self.client.logged_in:
self.login()
return
_, request = self.client.login_info()
self.send(request)
"""Start a local HTTP server to listen for SSO tokens."""
def start_login_sso(self):
# type: () -> None
if self.sso_hook:
# If there is a stale SSO process hanging around kill it. We could
# let it stay around but the URL that needs to be opened by the
# user is printed out in the callback.
W.hook_set(self.sso_hook, "signal", "term")
self.sso_hook = None
process_args = {
"buffer_flush": "1",
"arg1": "--port",
"arg2": str(self.config.sso_helper_listening_port)
}
self.sso_hook = W.hook_process_hashtable(
"matrix_sso_helper",
process_args,
0,
"sso_login_cb",
self.name
)
def login(self, token=None):
# type: (...) -> None
assert self.client is not None
if self.client.logged_in:
msg = (
"{prefix}{script_name}: Already logged in, " "syncing..."
).format(prefix=W.prefix("network"), script_name=SCRIPT_NAME)
W.prnt(self.server_buffer, msg)
timeout = 0 if self.transport_type == TransportType.HTTP else 30000
limit = (G.CONFIG.network.max_initial_sync_events if self.first_sync else 500)
sync_filter = {
"room": {
"timeline": {"limit": limit},
"state": {"lazy_load_members": True}
}
}
self.sync(timeout, sync_filter)
return
if (not self.config.username or not self.config.password) and not token:
message = "{prefix}User or password not set".format(
prefix=W.prefix("error")
)
W.prnt("", message)
return self.disconnect()
if token:
_, request = self.client.login(
device_name=self.config.device_name, token=token
)
else:
_, request = self.client.login(
password=self.config.password, device_name=self.config.device_name
)
self.send_or_queue(request)
msg = "{prefix}matrix: Logging in...".format(
prefix=W.prefix("network")
)
W.prnt(self.server_buffer, msg)
def devices(self):
_, request = self.client.devices()
self.send_or_queue(request)
def delete_device(self, device_id, auth=None):
uuid, request = self.client.delete_devices([device_id], auth)
self.device_deletion_queue[uuid] = device_id
self.send_or_queue(request)
return
def rename_device(self, device_id, display_name):
content = {
"display_name": display_name
}
_, request = self.client.update_device(device_id, content)
self.send_or_queue(request)
def room_send_state(self, room_buffer, body, event_type):
_, request = self.client.room_put_state(
room_buffer.room.room_id, event_type, body
)
self.send_or_queue(request)
def room_send_redaction(self, room_buffer, event_id, reason=None):
_, request = self.client.room_redact(
room_buffer.room.room_id, event_id, reason
)
self.send_or_queue(request)
def room_kick(self, room_buffer, user_id, reason=None):
_, request = self.client.room_kick(
room_buffer.room.room_id, user_id, reason
)
self.send_or_queue(request)
def room_invite(self, room_buffer, user_id):
_, request = self.client.room_invite(room_buffer.room.room_id, user_id)
self.send_or_queue(request)
def room_join(self, room_id):
_, request = self.client.join(room_id)
self.send_or_queue(request)
def room_leave(self, room_id):
_, request = self.client.room_leave(room_id)
self.send_or_queue(request)
def room_get_messages(self, room_id):
if not self.connected or not self.client.logged_in:
return False
room_buffer = self.find_room_from_id(room_id)
# We're already fetching old messages
if room_buffer.backlog_pending:
return False
if not room_buffer.prev_batch:
return False
uuid, request = self.client.room_messages(
room_id,
room_buffer.prev_batch,
limit=10)
room_buffer.backlog_pending = True
self.backlog_queue[uuid] = room_id
self.send_or_queue(request)
return True
def room_send_read_marker(self, room_id, event_id):
"""Send read markers for the provided room.
Args:
room_id(str): the room for which the read markers should
be sent.
event_id(str): the event id where to set the marker
"""
if not self.connected or not self.client.logged_in:
return
_, request = self.client.room_read_markers(
room_id,
fully_read_event=event_id,
read_event=event_id)
self.send(request)
def room_send_typing_notice(self, room_buffer):
"""Send a typing notice for the provided room.
Args:
room_buffer(RoomBuffer): the room for which the typing notice needs
to be sent.
"""
if not self.connected or not self.client.logged_in:
return
input = room_buffer.weechat_buffer.input
typing_enabled = bool(int(W.string_eval_expression(
G.CONFIG.network.typing_notice_conditions,
{},
{"typing_enabled": str(int(room_buffer.typing_enabled))},
{"type": "condition"}
)))
if not typing_enabled:
return
# Don't send a typing notice if the user is typing in a weechat command
if input.startswith("/") and not input.startswith("//"):
return
# Don't send a typing notice if we only typed a couple of letters.
elif len(input) < 4 and not room_buffer.typing:
return
# If we were typing already and our input bar now has no letters or
# only a couple of letters stop the typing notice.
elif len(input) < 4:
_, request = self.client.room_typing(
room_buffer.room.room_id,
typing_state=False)
room_buffer.typing = False
self.send(request)
return
# Don't send out a typing notice if we already sent one out and it
# didn't expire yet.
if not room_buffer.typing_notice_expired:
return
_, request = self.client.room_typing(
room_buffer.room.room_id,
typing_state=True,
timeout=TYPING_NOTICE_TIMEOUT)
room_buffer.typing = True
self.send(request)
def room_send_upload(
self,
upload
):
"""Send a room message containing the mxc URI of an upload."""
try:
room_buffer = self.find_room_from_id(upload.room_id)
except (ValueError, KeyError):
return True
assert self.client
if room_buffer.room.encrypted:
assert upload.encrypt
content = upload.content
try:
uuid = self.room_send_event(upload.room_id, content)
except (EncryptionError, GroupEncryptionError):
message = EncryptionQueueItem(upload.msgtype, upload)
self.encryption_queue[upload.room_id].append(message)
return False
attributes = DEFAULT_ATTRIBUTES.copy()
formatted = Formatted([FormattedString(
upload.render,
attributes
)])
own_message = OwnMessage(
self.user_id, 0, "", uuid, upload.room_id, formatted
)
room_buffer.sent_messages_queue[uuid] = own_message
self.print_unconfirmed_message(room_buffer, own_message)
return True
def share_group_session(
self,
room_id,
ignore_missing_sessions=False,
ignore_unverified_devices=False
):
self.ignore_while_sharing[room_id] = ignore_unverified_devices
_, request = self.client.share_group_session(
room_id,
ignore_missing_sessions=ignore_missing_sessions,
ignore_unverified_devices=ignore_unverified_devices
)
self.send(request)
self.group_session_shared[room_id] = True
def room_send_event(
self,
room_id, # type: str
content, # type: Dict[str, str]
event_type="m.room.message", # type: str
ignore_unverified_devices=False, # type: bool
):
# type: (...) -> UUID
assert self.client
try:
uuid, request = self.client.room_send(
room_id, event_type, content
)
self.send(request)
return uuid
except GroupEncryptionError:
try:
if not self.group_session_shared[room_id]:
self.share_group_session(
room_id,
ignore_unverified_devices=ignore_unverified_devices
)
raise
except EncryptionError:
if not self.keys_claimed[room_id]:
_, request = self.client.keys_claim(room_id)
self.keys_claimed[room_id] = True
self.send(request)
raise
def room_send_message(
self,
room_buffer, # type: RoomBuffer
formatted, # type: Formatted
msgtype="m.text", # type: str
ignore_unverified_devices=False, # type: bool
in_reply_to_event_id="", # type: str
):
# type: (...) -> bool
room = room_buffer.room
assert self.client
content = {"msgtype": msgtype, "body": formatted.to_plain()}
if formatted.is_formatted() or in_reply_to_event_id:
content["format"] = "org.matrix.custom.html"
content["formatted_body"] = formatted.to_html()
if in_reply_to_event_id:
content["m.relates_to"] = {
"m.in_reply_to": {"event_id": in_reply_to_event_id}
}
try:
uuid = self.room_send_event(
room.room_id,
content,
ignore_unverified_devices=ignore_unverified_devices
)
except (EncryptionError, GroupEncryptionError):
message = EncryptionQueueItem(msgtype, formatted)
self.encryption_queue[room.room_id].append(message)
return False
if msgtype == "m.emote":
message_class = OwnAction # type: Type
else:
message_class = OwnMessage
own_message = message_class(
self.user_id, 0, "", uuid, room.room_id, formatted
)
room_buffer.sent_messages_queue[uuid] = own_message
self.print_unconfirmed_message(room_buffer, own_message)
return True
def print_unconfirmed_message(self, room_buffer, message):
"""Print an outgoing message before getting a receive confirmation.
The message is printed out greyed out and only printed out if the
client is configured to do so. The message needs to be later modified
to contain proper coloring, this is done in the
replace_printed_line_by_uuid() method of the RoomBuffer class.
Args:
room_buffer(RoomBuffer): the buffer of the room where the message
needs to be printed out
message(OwnMessages): the message that should be printed out
"""
if G.CONFIG.network.print_unconfirmed_messages:
room_buffer.printed_before_ack_queue.append(message.uuid)
plain_message = message.formatted_message.to_weechat()
plain_message = W.string_remove_color(plain_message, "")
attributes = DEFAULT_ATTRIBUTES.copy()
attributes["fgcolor"] = G.CONFIG.color.unconfirmed_message_fg
attributes["bgcolor"] = G.CONFIG.color.unconfirmed_message_bg
new_formatted = Formatted([FormattedString(
plain_message,
attributes
)])
new_message = copy.copy(message)
new_message.formatted_message = new_formatted
if isinstance(new_message, OwnAction):
room_buffer.self_action(new_message)
elif isinstance(new_message, OwnMessage):
room_buffer.self_message(new_message)
def keys_upload(self):
_, request = self.client.keys_upload()
self.send_or_queue(request)
def keys_query(self):
_, request = self.client.keys_query()
self.keys_queried = True
self.send_or_queue(request)
def get_joined_members(self, room_id):
if not self.connected or not self.client.logged_in:
return
if room_id in self.member_request_list:
return
self.member_request_list.append(room_id)
_, request = self.client.joined_members(room_id)
self.send(request)
def _print_message_error(self, message):
server_buffer_prnt(
self,
(
"{prefix}Unhandled {status_code} error, please "
"inform the developers about this."
).format(
prefix=W.prefix("error"), status_code=message.response.status
),
)
server_buffer_prnt(self, pprint.pformat(message.__class__.__name__))
server_buffer_prnt(self, pprint.pformat(message.request.payload))
server_buffer_prnt(self, pprint.pformat(message.response.body))
def handle_own_messages_error(self, response):
room_buffer = self.room_buffers[response.room_id]
if response.uuid not in room_buffer.printed_before_ack_queue:
return
message = room_buffer.sent_messages_queue.pop(response.uuid)
room_buffer.mark_message_as_unsent(response.uuid, message)
room_buffer.printed_before_ack_queue.remove(response.uuid)
def handle_own_messages(self, response):
def send_marker():
if not room_buffer.read_markers_enabled:
return
self.room_send_read_marker(response.room_id, response.event_id)
room_buffer.last_read_event = response.event_id
room_buffer = self.room_buffers[response.room_id]
message = room_buffer.sent_messages_queue.pop(response.uuid, None)
# The message might have been returned in a sync response before we got
# a room send response.
if not message:
return
message.event_id = response.event_id
# We already printed the message, just modify it to contain the proper
# colors and formatting.
if response.uuid in room_buffer.printed_before_ack_queue:
room_buffer.replace_printed_line_by_uuid(response.uuid, message)
room_buffer.printed_before_ack_queue.remove(response.uuid)
send_marker()
return
if isinstance(message, OwnAction):
room_buffer.self_action(message)
send_marker()
return
if isinstance(message, OwnMessage):
room_buffer.self_message(message)
send_marker()
return
raise NotImplementedError(
"Unsupported message of type {}".format(type(message))
)
def handle_backlog_response(self, response):
room_id = self.backlog_queue.pop(response.uuid)
room_buffer = self.find_room_from_id(room_id)
room_buffer.first_view = False
room_buffer.handle_backlog(response)
def handle_devices_response(self, response):
if not response.devices:
m = "{}{}: No devices found for this account".format(
W.prefix("error"),
SCRIPT_NAME)
W.prnt(self.server_buffer, m)
header = (W.prefix("network") + SCRIPT_NAME + ": Devices for "
"server {}{}{}:\n"
" Device ID Device Name "
"Last Seen").format(
W.color("chat_server"),
self.name,
W.color("reset")
)
W.prnt(self.server_buffer, header)
lines = []
for device in response.devices:
last_seen_date = ("?" if not device.last_seen_date else
device.last_seen_date.strftime("%Y/%m/%d %H:%M"))
last_seen = "{ip} @ {date}".format(
ip=device.last_seen_ip or "?",
date=last_seen_date
)
device_color = ("chat_self" if device.id == self.device_id else
W.info_get("nick_color_name", device.id))
bold = W.color("bold") if device.id == self.device_id else ""
line = " {}{}{:<18}{}{:<34}{:<}".format(
bold,
W.color(device_color),
device.id,
W.color("resetcolor"),
device.display_name or "",
last_seen
)
lines.append(line)
W.prnt(self.server_buffer, "\n".join(lines))
"""Handle a login info response and chose one of the available flows
This currently supports only SSO and password logins. If both are available
password takes precedence over SSO if a username and password is provided.
"""
def _handle_login_info(self, response):
if ("m.login.sso" in response.flows
and (not self.config.username or not self.config.password)):
self.start_login_sso()
elif "m.login.password" in response.flows:
self.login()
else:
self.error("No supported login flow found")
self.disconnect()
def _handle_login(self, response):
self.access_token = response.access_token
self.user_id = response.user_id
self.client.access_token = response.access_token
self.device_id = response.device_id
self.save_device_id()
message = "{prefix}matrix: Logged in as {user}".format(
prefix=W.prefix("network"), user=self.user_id
)
W.prnt(self.server_buffer, message)
if not self.client.olm_account_shared:
self.keys_upload()
sync_filter = {
"room": {
"timeline": {
"limit": G.CONFIG.network.max_initial_sync_events
},
"state": {"lazy_load_members": True}
}
}
self.sync(timeout=0, sync_filter=sync_filter)
def _handle_room_info(self, response):
for room_id, info in response.rooms.invite.items():
room = self.client.invited_rooms.get(room_id, None)
if room:
if room.inviter:
inviter_msg = " by {}{}".format(
W.color("chat_nick_other"), room.inviter
)
else:
inviter_msg = ""
self.info_highlight(
"You have been invited to {} {}({}{}{}){}"
"{}".format(
room.display_name,
W.color("chat_delimiters"),
W.color("chat_channel"),
room_id,
W.color("chat_delimiters"),
W.color("reset"),
inviter_msg,
)
)
else:
self.info_highlight("You have been invited to {}.".format(
room_id
))
for room_id, info in response.rooms.leave.items():
if room_id not in self.buffers:
continue
room_buffer = self.find_room_from_id(room_id)
room_buffer.handle_left_room(info)
for room_id, info in response.rooms.join.items():
if room_id not in self.buffers:
self.create_room_buffer(room_id, info.timeline.prev_batch)
room_buffer = self.find_room_from_id(room_id)
room_buffer.handle_joined_room(info)
def add_unhandled_users(self, rooms, n):
# type: (List[RoomBuffer], int) -> bool
total_users = 0
while total_users <= n:
try:
room_buffer = rooms.pop()
except IndexError:
return False
handled_users = 0
users = room_buffer.unhandled_users
for user_id in users:
room_buffer.add_user(user_id, 0, True)
handled_users += 1
total_users += 1
if total_users >= n:
room_buffer.unhandled_users = users[handled_users:]
rooms.append(room_buffer)
return True
room_buffer.unhandled_users = []
return False
def _hook_lazy_user_adding(self):
if not self.lazy_load_hook:
hook = W.hook_timer(1 * 1000, 0, 0,
"matrix_load_users_cb", self.name)
self.lazy_load_hook = hook
def decrypt_printed_messages(self, key_event):
"""Decrypt already printed messages and send them to the buffer"""
try:
room_buffer = self.find_room_from_id(key_event.room_id)
except KeyError:
return
decrypted_events = []
for undecrypted_event in room_buffer.undecrypted_events:
if undecrypted_event.session_id != key_event.session_id:
continue
event = self.client.decrypt_event(undecrypted_event)
if event:
decrypted_events.append((undecrypted_event, event))
for event_pair in decrypted_events:
undecrypted_event, event = event_pair
room_buffer.undecrypted_events.remove(undecrypted_event)
room_buffer.replace_undecrypted_line(event)
def start_verification(self, device):
_, request = self.client.start_key_verification(device)
self.send(request)
self.info("Starting an interactive device verification with "
"{} {}".format(device.user_id, device.id))
def accept_sas(self, sas):
_, request = self.client.accept_key_verification(sas.transaction_id)
self.send(request)
def cancel_sas(self, sas):
_, request = self.client.cancel_key_verification(sas.transaction_id)
self.send(request)
def to_device(self, message):
_, request = self.client.to_device(message)
self.send(request)
def confirm_sas(self, sas):
_, request = self.client.confirm_short_auth_string(sas.transaction_id)
self.send(request)
device = sas.other_olm_device
if sas.verified:
self.info("Device {} of user {} successfully verified".format(
device.id,
device.user_id
))
else:
self.info("Waiting for {} to confirm...".format(device.user_id))
def _handle_sync(self, response):
# we got the same batch again, nothing to do
self.first_sync = False
if self.next_batch == response.next_batch:
self.schedule_sync()
return
self._handle_room_info(response)
for event in response.to_device_events:
if isinstance(event, RoomKeyEvent):
message = {
"sender": event.sender,
"sender_key": event.sender_key,
"room_id": event.room_id,
"session_id": event.session_id,
"algorithm": event.algorithm,
"server": self.name,
}
W.hook_hsignal_send("matrix_room_key_received", message)
# TODO try to decrypt some cached undecrypted messages with the
# new key
# self.decrypt_printed_messages(event)
if self.client.should_upload_keys:
self.keys_upload()
if self.client.should_query_keys and not self.keys_queried:
self.keys_query()
for room_buffer in self.room_buffers.values():
# It's our initial sync, we need to fetch room members, so add
# the room to the missing members queue.
# 3 reasons we fetch room members here:
# * If the lazy load room users setting is off, otherwise we will
# fetch them when we switch to the buffer
# * If the room is encrypted, encryption needs the full member
# list for it to work.
# * If we are the only member, it is unlikely really an empty
# room and since we don't want a bunch of "Empty room?"
# buffers in our buffer list we fetch members here.
if not self.next_batch:
if (not G.CONFIG.network.lazy_load_room_users
or room_buffer.room.encrypted
or room_buffer.room.member_count <= 1):
self.rooms_with_missing_members.append(
room_buffer.room.room_id
)
if room_buffer.unhandled_users:
self._hook_lazy_user_adding()
break
self.next_batch = response.next_batch
self.schedule_sync()
W.bar_item_update("matrix_typing_notice")
if self.rooms_with_missing_members:
self.get_joined_members(self.rooms_with_missing_members.pop())
def handle_delete_device_auth(self, response):
device_id = self.device_deletion_queue.pop(response.uuid, None)
if not device_id:
return
for flow in response.flows:
if "m.login.password" in flow["stages"]:
session = response.session
auth = {
"type": "m.login.password",
"session": session,
"user": self.client.user_id,
"password": self.config.password
}
self.delete_device(device_id, auth)
return
self.error("No supported auth method for device deletion found.")
def handle_error_response(self, response):
self.error("Error: {}".format(str(response)))
if isinstance(response, (SyncError, LoginError)):
self.disconnect()
elif isinstance(response, JoinedMembersError):
self.rooms_with_missing_members.append(response.room_id)
self.get_joined_members(self.rooms_with_missing_members.pop())
elif isinstance(response, RoomSendError):
self.handle_own_messages_error(response)
elif isinstance(response, ShareGroupSessionError):
self.group_session_shared[response.room_id] = False
self.share_group_session(
response.room_id,
False,
self.ignore_while_sharing[response.room_id]
)
elif isinstance(response, ToDeviceError):
try:
self.to_device_sent.remove(response.to_device_message)
except ValueError:
pass
def handle_response(self, response):
# type: (Response) -> None
response_lag = response.elapsed
current_lag = 0
if self.client:
current_lag = self.client.lag
if response_lag >= current_lag:
self.lag = response_lag * 1000
self.lag_done = True
W.bar_item_update("lag")
if isinstance(response, ErrorResponse):
self.handle_error_response(response)
if isinstance(response, RoomMessagesError):
room_buffer = self.room_buffers[response.room_id]
room_buffer.backlog_pending = False
elif isinstance(response, ToDeviceResponse):
try:
self.to_device_sent.remove(response.to_device_message)
except ValueError:
pass
elif isinstance(response, LoginResponse):
self._handle_login(response)
elif isinstance(response, LoginInfoResponse):
self._handle_login_info(response)
elif isinstance(response, SyncResponse):
self._handle_sync(response)
elif isinstance(response, RoomSendResponse):
self.handle_own_messages(response)
elif isinstance(response, RoomMessagesResponse):
self.handle_backlog_response(response)
elif isinstance(response, DevicesResponse):
self.handle_devices_response(response)
elif isinstance(response, UpdateDeviceResponse):
self.info("Device name successfully updated")
elif isinstance(response, DeleteDevicesAuthResponse):
self.handle_delete_device_auth(response)
elif isinstance(response, DeleteDevicesResponse):
self.info("Device successfully deleted")
elif isinstance(response, KeysQueryResponse):
self.keys_queried = False
W.bar_item_update("buffer_modes")
W.bar_item_update("matrix_modes")
for user_id, device_dict in response.changed.items():
for device in device_dict.values():
message = {
"user_id": user_id,
"device_id": device.id,
"ed25519": device.ed25519,
"curve25519": device.curve25519,
"deleted": str(device.deleted)
}
W.hook_hsignal_send("matrix_device_changed", message)
elif isinstance(response, JoinedMembersResponse):
self.member_request_list.remove(response.room_id)
room_buffer = self.room_buffers[response.room_id]
users = [user.user_id for user in response.members]
# Don't add the users directly use the lazy load hook.
room_buffer.unhandled_users += users
self._hook_lazy_user_adding()
room_buffer.members_fetched = True
room_buffer.update_buffer_name()
# Fetch the users for the next room.
if self.rooms_with_missing_members:
self.get_joined_members(self.rooms_with_missing_members.pop())
# We are done adding all the users, do a full key query now since
# the client knows all the encrypted room members.
else:
if self.client.should_query_keys and not self.keys_queried:
self.keys_query()
elif isinstance(response, KeysClaimResponse):
self.keys_claimed[response.room_id] = False
try:
self.share_group_session(
response.room_id,
True,
self.ignore_while_sharing[response.room_id]
)
except OlmTrustError as e:
m = ("Untrusted devices found in room: {}".format(e))
room_buffer = self.find_room_from_id(response.room_id)
room_buffer.error(m)
try:
item = self.encryption_queue[response.room_id][0]
if item.message_type not in ["m.file", "m.video",
"m.audio", "m.image"]:
room_buffer.last_message = item.message
except IndexError:
pass
self.encryption_queue[response.room_id].clear()
return
elif isinstance(response, ShareGroupSessionResponse):
room_id = response.room_id
self.group_session_shared[response.room_id] = False
ignore_unverified = self.ignore_while_sharing[response.room_id]
self.ignore_while_sharing[response.room_id] = False
room_buffer = self.room_buffers[room_id]
while self.encryption_queue[room_id]:
item = self.encryption_queue[room_id].popleft()
try:
if item.message_type in [
"m.file",
"m.video",
"m.audio",
"m.image"
]:
ret = self.room_send_upload(item.message)
else:
assert isinstance(item.message, Formatted)
ret = self.room_send_message(
room_buffer,
item.message,
item.message_type,
ignore_unverified_devices=ignore_unverified
)
if not ret:
self.encryption_queue[room_id].pop()
self.encryption_queue[room_id].appendleft(item)
break
except OlmTrustError:
self.encryption_queue[room_id].clear()
# If the item is a normal user message store it in the
# buffer to enable the send-anyways functionality.
if item.message_type not in ["m.file", "m.video",
"m.audio", "m.image"]:
room_buffer.last_message = item.message
break
def create_room_buffer(self, room_id, prev_batch):
room = self.client.rooms[room_id]
buf = RoomBuffer(room, self.name, self.homeserver, prev_batch)
# We sadly don't get a correct summary on full_state from synapse so we
# can't trust it that the members are fully synced
# if room.members_synced:
# buf.members_fetched = True
self.room_buffers[room_id] = buf
self.buffers[room_id] = buf.weechat_buffer._ptr
def find_room_from_ptr(self, pointer):
try:
room_id = key_from_value(self.buffers, pointer)
room_buffer = self.room_buffers[room_id]
return room_buffer
except (ValueError, KeyError):
return None
def find_room_from_id(self, room_id):
room_buffer = self.room_buffers[room_id]
return room_buffer
def garbage_collect_users(self):
""" Remove inactive users.
This tries to keep the number of users added to the nicklist less than
the configuration option matrix.network.max_nicklist_users. It
removes users that have not been active for a day until there are
less than max_nicklist_users or no users are left for removal.
It never removes users that have a bigger power level than the
default one.
This function is run every hour by the server timer callback"""
now = time.time()
self.user_gc_time = now
def day_passed(t1, t2):
return (t2 - t1) > 86400
for room_buffer in self.room_buffers.values():
to_remove = max(
(len(room_buffer.displayed_nicks) -
G.CONFIG.network.max_nicklist_users),
0
)
if not to_remove:
continue
removed = 0
removed_user_ids = []
for user_id, nick in room_buffer.displayed_nicks.items():
user = room_buffer.weechat_buffer.users[nick]
if (not user.speaking_time or
day_passed(user.speaking_time, now)):
room_buffer.weechat_buffer.part(nick, 0, False)
removed_user_ids.append(user_id)
removed += 1
if removed >= to_remove:
break
for user_id in removed_user_ids:
del room_buffer.displayed_nicks[user_id]
def buffer_merge(self):
if not self.server_buffer:
return
buf = self.server_buffer
if G.CONFIG.look.server_buffer == ServerBufferType.MERGE_CORE:
num = W.buffer_get_integer(W.buffer_search_main(), "number")
W.buffer_unmerge(buf, num + 1)
W.buffer_merge(buf, W.buffer_search_main())
elif G.CONFIG.look.server_buffer == ServerBufferType.MERGE:
if SERVERS:
first = None
for server in SERVERS.values():
if server.server_buffer:
first = server.server_buffer
break
if first:
num = W.buffer_get_integer(
W.buffer_search_main(), "number"
)
W.buffer_unmerge(buf, num + 1)
if buf is not first:
W.buffer_merge(buf, first)
else:
num = W.buffer_get_integer(W.buffer_search_main(), "number")
W.buffer_unmerge(buf, num + 1)
@utf8_decode
def matrix_config_server_read_cb(
data, config_file, section, option_name, value
):
return_code = W.WEECHAT_CONFIG_OPTION_SET_ERROR
if option_name:
server_name, option = option_name.rsplit(".", 1)
server = None
if server_name in SERVERS:
server = SERVERS[server_name]
else:
server = MatrixServer(server_name, config_file)
SERVERS[server.name] = server
# Ignore invalid options
if option in server.config._option_ptrs:
return_code = W.config_option_set(
server.config._option_ptrs[option], value, 1
)
# TODO print out error message in case of erroneous return_code
return return_code
@utf8_decode
def matrix_config_server_write_cb(data, config_file, section_name):
if not W.config_write_line(config_file, section_name, ""):
return W.WEECHAT_CONFIG_WRITE_ERROR
for server in SERVERS.values():
for option in server.config._option_ptrs.values():
if not W.config_write_option(config_file, option):
return W.WEECHAT_CONFIG_WRITE_ERROR
return W.WEECHAT_CONFIG_WRITE_OK
@utf8_decode
def matrix_config_server_change_cb(server_name, option):
# type: (str, str) -> int
server = SERVERS[server_name]
option_name = None
# The function config_option_get_string() is used to get differing
# properties from a config option, sadly it's only available in the plugin
# API of weechat.
option_name = key_from_value(server.config._option_ptrs, option)
server.update_option(option, option_name)
return 1
@utf8_decode
def matrix_load_users_cb(server_name, remaining_calls):
server = SERVERS[server_name]
start = time.time()
rooms = [x for x in server.room_buffers.values() if x.unhandled_users]
while server.add_unhandled_users(rooms, 100):
current = time.time()
if current - start >= 0.1:
return W.WEECHAT_RC_OK
# We are done adding users, we can unhook now.
W.unhook(server.lazy_load_hook)
server.lazy_load_hook = None
return W.WEECHAT_RC_OK
@utf8_decode
def matrix_timer_cb(server_name, remaining_calls):
server = SERVERS[server_name]
current_time = time.time()
if (
(not server.connected)
and server.reconnect_time
and current_time >= (server.reconnect_time + server.reconnect_delay)
):
server.reconnect()
return W.WEECHAT_RC_OK
if not server.connected or not server.client.logged_in:
return W.WEECHAT_RC_OK
# check lag, disconnect if it's too big
server.lag = server.client.lag * 1000
server.lag_done = False
W.bar_item_update("lag")
if server.lag > G.CONFIG.network.lag_reconnect * 1000:
server.disconnect()
return W.WEECHAT_RC_OK
for i, message in enumerate(server.client.outgoing_to_device_messages):
if i >= 5:
break
if message in server.to_device_sent:
continue
server.to_device(message)
server.to_device_sent.append(message)
if server.sync_time and current_time > server.sync_time:
timeout = 0 if server.transport_type == TransportType.HTTP else 30000
sync_filter = {
"room": {
"timeline": {"limit": 500},
"state": {"lazy_load_members": True}
}
}
server.sync(timeout, sync_filter)
if current_time > (server.user_gc_time + 3600):
server.garbage_collect_users()
return W.WEECHAT_RC_OK
def create_default_server(config_file):
server = MatrixServer("matrix_org", config_file._ptr)
SERVERS[server.name] = server
option = W.config_get(SCRIPT_NAME + ".server." + server.name + ".address")
W.config_option_set(option, "matrix.org", 1)
return True
@utf8_decode
def send_cb(server_name, file_descriptor):
# type: (str, int) -> int
server = SERVERS[server_name]
if server.send_fd_hook:
W.unhook(server.send_fd_hook)
server.send_fd_hook = None
if server.send_buffer:
server.try_send(server.send_buffer)
return W.WEECHAT_RC_OK
|