Script: otr.py

WARNING: this script has been disabled (not compatible with Python 3).

Off-The-Record IRC messaging.
Author: Matthew M. Boedicker — Version: 1.9.3 — License: GPL3
For WeeChat ≥ 0.3.0, requires: https://github.com/afflux/pure-python-otr.
Tags: irc, crypto, otr, py2, py3
Added: 2012-05-26 — Updated: 2021-05-11

Download GitHub Repository

   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
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
# -*- coding: utf-8 -*-
"""otr - WeeChat script for Off-the-Record IRC messaging

DISCLAIMER: To the best of my knowledge this script securely provides OTR
messaging in WeeChat, but I offer no guarantee. Please report any security
holes you find.

Copyright (c) 2012-2015 Matthew M. Boedicker <matthewm@boedicker.org>
                        Nils Görs <weechatter@arcor.de>
                        Daniel "koolfy" Faucon <koolfy@koolfy.be>
                        Felix Eckhofer <felix@tribut.de>

Report issues at https://github.com/mmb/weechat-otr

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

# pylint: disable=too-many-lines

from __future__ import unicode_literals

import collections
import glob
import io
import os
import platform
import re
import traceback
import shlex
import shutil
import sys

import potr
import weechat

class PythonVersion2(object):
    """Python 2 version of code that must differ between Python 2 and 3."""

    def __init__(self):
        import cgi
        self.cgi = cgi

        import HTMLParser
        self.html_parser = HTMLParser
        self.html_parser_init_kwargs = {}

        import htmlentitydefs
        self.html_entities = htmlentitydefs

    def html_escape(self, strng):
        """Escape HTML characters in a string."""
        return self.cgi.escape(strng)

    def unicode(self, *args, **kwargs):
        """Return the Unicode version of a string."""
        return unicode(*args, **kwargs)

    def unichr(self, *args, **kwargs):
        """Return the one character string of a Unicode character number."""
        return unichr(*args, **kwargs)

    def to_unicode(self, strng):
        """Convert a utf-8 encoded string to a Unicode."""
        if isinstance(strng, unicode):
            return strng
        return strng.decode('utf-8', 'replace')

    def to_str(self, strng):
        """Convert a Unicode to a utf-8 encoded string."""
        return strng.encode('utf-8', 'replace')

class PythonVersion3(object):
    """Python 3 version of code that must differ between Python 2 and 3."""

    def __init__(self, minor):
        self.minor = minor

        import html
        self.html = html

        import html.parser
        self.html_parser = html.parser
        if self.minor >= 4:
            self.html_parser_init_kwargs = {'convert_charrefs' : True}
        else:
            self.html_parser_init_kwargs = {}

        import html.entities
        self.html_entities = html.entities

    def html_escape(self, strng):
        """Escape HTML characters in a string."""
        return self.html.escape(strng, quote=False)

    def unicode(self, *args, **kwargs):
        """Return the Unicode version of a string."""
        return str(*args, **kwargs)

    def unichr(self, *args, **kwargs):
        """Return the one character string of a Unicode character number."""
        return chr(*args, **kwargs)

    def to_unicode(self, strng):
        """Convert a utf-8 encoded string to unicode."""
        if isinstance(strng, bytes):
            return strng.decode('utf-8', 'replace')
        return strng

    def to_str(self, strng):
        """Convert a Unicode to a utf-8 encoded string."""
        return strng

# We cannot use version_info.major as this is only supported on python >= 2.7
if sys.version_info[0] >= 3:
    PYVER = PythonVersion3(sys.version_info.minor)
else:
    PYVER = PythonVersion2()

SCRIPT_NAME = 'otr'
SCRIPT_DESC = 'Off-the-Record messaging for IRC'
SCRIPT_HELP = """{description}

Quick start:

Add an OTR item to the status bar by adding '[otr]' to the config setting
weechat.bar.status.items. This will show you whether your current conversation
is encrypted, authenticated and logged. /set otr.* for OTR status bar
customization options.

Start a private conversation with a friend who has OTR: /query yourpeer hi

In the private chat buffer: /otr start

If you have not authenticated your peer yet, follow the instructions for
authentication.

You can, at any time, see the current OTR session status and fingerprints with:
/otr status

View OTR policies for your peer: /otr policy

View default OTR policies: /otr policy default

Start/Stop log recording for the current OTR session: /otr log [start|stop]
This will be reverted back to the previous log setting at the end of the session.

To refresh the OTR session: /otr refresh

To end your private conversation: /otr finish

This script supports only OTR protocol version 2.
""".format(description=SCRIPT_DESC)

SCRIPT_AUTHOR = 'Matthew M. Boedicker'
SCRIPT_LICENCE = 'GPL3'
SCRIPT_VERSION = '1.9.3'

OTR_DIR_NAME = 'otr'

OTR_QUERY_RE = re.compile(r'\?OTR(\?|\??v[a-z\d]*\?)')

POLICIES = {
    'allow_v2' : 'allow OTR protocol version 2, effectively enable OTR '
                 'since v2 is the only supported version',
    'require_encryption' : 'refuse to send unencrypted messages when OTR is '
                           'enabled',
    'log' : 'enable logging of OTR conversations',
    'send_tag' : 'advertise your OTR capability using the whitespace tag',
    'html_escape' : 'escape HTML special characters in outbound messages',
    'html_filter' : 'filter HTML in incoming messages',
    }

READ_ONLY_POLICIES = {
    'allow_v1' : False,
    }

ACTION_PREFIX = '/me '
IRC_ACTION_RE = re.compile('^\x01ACTION (?P<text>.*)\x01$')
PLAIN_ACTION_RE = re.compile('^'+ACTION_PREFIX+'(?P<text>.*)$')

IRC_SANITIZE_TABLE = dict((ord(char), None) for char in '\n\r\x00')

# Patch potr.proto.TaggedPlaintext to not end plaintext tags in a space.
#
# When POTR adds OTR tags to plaintext it puts them at the end of the message.
# The tags end in a space which gets stripped off by WeeChat because it
# strips trailing spaces from commands. This causes OTR initiation to fail so
# the following code adds an extra tab at the end of the plaintext tags if
# they end in a space.
#
# The patched version also skips OTR tagging for CTCP messages because it
# breaks the CTCP format.
def patched__bytes__(self):
    """Patched potr.proto.TaggedPlainText.__bytes__."""
    # Do not tag CTCP messages.
    if self.msg.startswith(b'\x01') and \
        self.msg.endswith(b'\x01'):
        return self.msg

    data = self.msg + potr.proto.MESSAGE_TAG_BASE
    for v in self.versions:
        data += potr.proto.MESSAGE_TAGS[v]
    if data.endswith(b' '):
        data += b'\t'
    return data

potr.proto.TaggedPlaintext.__bytes__ = patched__bytes__

def command(buf, command_str):
    """Wrap weechat.command() with utf-8 encode."""
    debug(command_str)
    weechat.command(buf, PYVER.to_str(command_str))

def privmsg(server, nick, message):
    """Send a private message to a nick."""
    for line in message.splitlines():
        command('', '/quote -server {server} PRIVMSG {nick} :{line}'.format(
            server=irc_sanitize(server),
            nick=irc_sanitize(nick),
            line=irc_sanitize(line)))

def build_privmsg_in(fromm, target, msg):
    """Build inbound IRC PRIVMSG command."""
    return ':{user} PRIVMSG {target} :{msg}'.format(
        user=irc_sanitize(fromm),
        target=irc_sanitize(target),
        msg=irc_sanitize(msg))

def build_privmsgs_in(fromm, target, msg, prefix=''):
    """Build an inbound IRC PRIVMSG command for each line in msg.
    If prefix is supplied, prefix each line of msg with it."""
    cmd = []
    for line in msg.splitlines():
        cmd.append(build_privmsg_in(fromm, target, prefix+line))
    return '\r\n'.join(cmd)

def build_privmsg_out(target, msg):
    """Build outbound IRC PRIVMSG command(s)."""
    cmd = []
    for line in msg.splitlines():
        cmd.append('PRIVMSG {target} :{line}'.format(
            target=irc_sanitize(target),
            line=irc_sanitize(line)))
    return '\r\n'.join(cmd)

def irc_sanitize(msg):
    """Remove NUL, CR and LF characters from msg.
    The (utf-8 encoded version of a) string returned from this function
    should be safe to use as an argument in an irc command."""
    return PYVER.unicode(msg).translate(IRC_SANITIZE_TABLE)

def prnt(buf, message):
    """Wrap weechat.prnt() with utf-8 encode."""
    weechat.prnt(buf, PYVER.to_str(message))

def print_buffer(buf, message, level='info'):
    """Print message to buf with prefix,
    using color according to level."""
    prnt(buf, '{prefix}\t{msg}'.format(
        prefix=get_prefix(),
        msg=colorize(message, 'buffer.{0}'.format(level))))

def get_prefix():
    """Returns configured message prefix."""
    return weechat.string_eval_expression(
        config_string('look.prefix'),
        {}, {}, {})

def debug(msg):
    """Send a debug message to the OTR debug buffer."""
    debug_option = config_get_prefixed('general.debug')

    if not weechat.config_boolean(debug_option):
        return

    debug_buffer = weechat.buffer_search('python', 'OTR Debug')
    if not debug_buffer:
        debug_buffer = weechat.buffer_new('OTR Debug', '', '', '', '')
        weechat.buffer_set(debug_buffer, 'title', 'OTR Debug')
        weechat.buffer_set(debug_buffer, 'localvar_set_no_log', '1')

    prnt(debug_buffer, ('{script} debug\t{text}'.format(
        script=SCRIPT_NAME, text=PYVER.unicode(msg))))

def current_user(server_name):
    """Get the nick and server of the current user on a server."""
    return irc_user(info_get('irc_nick', server_name), server_name)

def irc_user(nick, server):
    """Build an IRC user string from a nick and server."""
    return '{nick}@{server}'.format(
        nick=nick.lower(),
        server=server)

def isupport_value(server, feature):
    """Get the value of an IRC server feature."""
    args = '{server},{feature}'.format(server=server, feature=feature)
    return info_get('irc_server_isupport_value', args)

def is_a_channel(channel, server):
    """Return true if a string has an IRC channel prefix."""
    prefixes = \
        tuple(isupport_value(server, 'CHANTYPES')) + \
        tuple(isupport_value(server, 'STATUSMSG'))

    # If the server returns nothing for CHANTYPES and STATUSMSG use
    # default prefixes.
    if not prefixes:
        prefixes = ('#', '&', '+', '!', '@')

    return channel.startswith(prefixes)

class PrivmsgParseException(Exception):
    """Exception class for PRIVMSG parsing exceptions."""
    pass

def parse_irc_privmsg(message, server):
    """Parse an IRC PRIVMSG command and return a dictionary.

    Either the to_channel key or the to_nick key will be set depending on
    whether the message is to a nick or a channel. The other will be None.

    Example input:

    :nick!user@host PRIVMSG #weechat :message here

    Output:

    {'from': 'nick!user@host',
    'from_nick': 'nick',
    'to': '#weechat',
    'to_channel': '#weechat',
    'to_nick': None,
    'text': 'message here'}
    """

    weechat_result = weechat.info_get_hashtable(
        'irc_message_parse', dict(message=message))

    if weechat_result['command'].upper() == 'PRIVMSG':
        target, text = PYVER.to_unicode(
            weechat_result['arguments']).split(' ', 1)
        if text.startswith(':'):
            text = text[1:]

        result = {
            'from': PYVER.to_unicode(weechat_result['host']),
            'to' : target,
            'text': text,
            }

        if weechat_result['host']:
            result['from_nick'] = PYVER.to_unicode(weechat_result['nick'])
        else:
            result['from_nick'] = ''

        if is_a_channel(target, server):
            result['to_channel'] = target
            result['to_nick'] = None
        else:
            result['to_channel'] = None
            result['to_nick'] = target

        return result
    else:
        raise PrivmsgParseException(message)

def has_otr_end(msg):
    """Return True if the message is the end of an OTR message."""
    return msg.endswith('.') or msg.endswith(',')

def first_instance(objs, klass):
    """Return the first object in the list that is an instance of a class."""
    for obj in objs:
        if isinstance(obj, klass):
            return obj

def config_prefix(option):
    """Add the config prefix to an option and return the full option name."""
    return '{script}.{option}'.format(
        script=SCRIPT_NAME,
        option=option)

def config_color(option):
    """Get the color of a color config option."""
    return weechat.color(weechat.config_color(config_get_prefixed(
        'color.{0}'.format(option))))

def config_string(option):
    """Get the string value of a config option with utf-8 decode."""
    return PYVER.to_unicode(weechat.config_string(
        config_get_prefixed(option)))

def config_get(option):
    """Get the value of a WeeChat config option."""
    return weechat.config_get(PYVER.to_str(option))

def config_get_prefixed(option):
    """Get the value of a script prefixed WeeChat config option."""
    return config_get(config_prefix(option))

def buffer_get_string(buf, prop):
    """Wrap weechat.buffer_get_string() with utf-8 encode/decode."""
    if buf is not None:
        encoded_buf = PYVER.to_str(buf)
    else:
        encoded_buf = None

    return PYVER.to_unicode(weechat.buffer_get_string(
        encoded_buf, PYVER.to_str(prop)))

def buffer_is_private(buf):
    """Return True if a buffer is private."""
    return buffer_get_string(buf, 'localvar_type') == 'private'

def info_get(info_name, arguments):
    """Wrap weechat.info_get() with utf-8 encode/decode."""
    return PYVER.to_unicode(weechat.info_get(
        PYVER.to_str(info_name), PYVER.to_str(arguments)))

def msg_irc_from_plain(msg):
    """Transform a plain-text message to irc format.
    This will replace lines that start with /me with the respective
    irc command."""
    return PLAIN_ACTION_RE.sub('\x01ACTION \g<text>\x01', msg)

def msg_plain_from_irc(msg):
    """Transform an irc message to plain-text.
    Any ACTION found will be rewritten as /me <text>."""
    return IRC_ACTION_RE.sub(ACTION_PREFIX + r'\g<text>', msg)

def default_peer_args(args, buf):
    """Get the nick and server of a remote peer from command arguments or
    a buffer.

    args is the [nick, server] slice of arguments from a command.
    If these are present, return them. If args is empty and the buffer buf
    is private, return the remote nick and server of buf."""
    result = None, None

    if len(args) == 2:
        result = tuple(args)
    else:
        if buffer_is_private(buf):
            result = (
                buffer_get_string(buf, 'localvar_channel'),
                buffer_get_string(buf, 'localvar_server'))

    return result

def format_default_policies():
    """Return current default policies formatted as a string for the user."""
    buf = io.StringIO()

    buf.write('Current default OTR policies:\n')

    for policy, desc in sorted(POLICIES.items()):
        buf.write('  {policy} ({desc}) : {value}\n'.format(
            policy=policy,
            desc=desc,
            value=config_string('policy.default.{0}'.format(policy))))

    buf.write('Change default policies with: /otr policy default NAME on|off')

    return buf.getvalue()

def to_bytes(strng):
    """Convert a python str or unicode to bytes."""
    return strng.encode('utf-8', 'replace')

def colorize(msg, color):
    """Colorize each line of msg using color."""
    result = []
    colorstr = config_color(color)

    for line in msg.splitlines():
        result.append('{color}{msg}'.format(
            color=colorstr,
            msg=line))

    return '\r\n'.join(result)

def accounts():
    """Return a list of all IrcOtrAccounts sorted by name."""
    result = []
    for key_path in glob.iglob(os.path.join(OTR_DIR, '*.key3')):
        key_name, _ = os.path.splitext(os.path.basename(key_path))
        result.append(ACCOUNTS[key_name])

    return sorted(result, key=lambda account: account.name)

def show_account_fingerprints():
    """Print all account names and their fingerprints to the core buffer."""
    table_formatter = TableFormatter()
    for account in accounts():
        table_formatter.add_row([
            account.name,
            str(account.getPrivkey())])
    print_buffer('', table_formatter.format())

def show_peer_fingerprints(grep=None):
    """Print peer names and their fingerprints to the core buffer.

    If grep is passed in, show all peer names containing that substring."""
    trust_descs = {
        '' : 'unverified',
        'smp' : 'SMP verified',
        'verified' : 'verified',
        }

    table_formatter = TableFormatter()
    for account in accounts():
        for peer, peer_data in sorted(account.trusts.items()):
            for fingerprint, trust in sorted(peer_data.items()):
                if grep is None or grep in peer:
                    table_formatter.add_row([
                        peer,
                        account.name,
                        potr.human_hash(fingerprint),
                        trust_descs[trust],
                    ])
    print_buffer('', table_formatter.format())

def private_key_file_path(account_name):
    """Return the private key file path for an account."""
    return os.path.join(OTR_DIR, '{0}.key3'.format(account_name))

def read_private_key(key_file_path):
    """Return the private key in a private key file."""
    debug(('read private key', key_file_path))

    with open(key_file_path, 'rb') as key_file:
        return potr.crypt.PK.parsePrivateKey(key_file.read())[0]

def get_context(account_name, context_name):
    """Return a context from an account."""
    return ACCOUNTS[account_name].getContext(context_name)

def get_server_context(server, peer_nick):
    """Return the context for the current server user and peer."""
    return get_context(current_user(server), irc_user(peer_nick, server))

class AccountDict(collections.defaultdict):
    """Dictionary that adds missing keys as IrcOtrAccount instances."""

    def __missing__(self, key):
        debug(('add account', key))
        self[key] = IrcOtrAccount(key)

        return self[key]

class Assembler(object):
    """Reassemble fragmented OTR messages.

    This does not deal with OTR fragmentation, which is handled by potr, but
    fragmentation of received OTR messages that are too large for IRC.
    """
    def __init__(self):
        self.clear()

    def add(self, data):
        """Add data to the buffer."""
        self.value += data

    def clear(self):
        """Empty the buffer."""
        self.value = ''

    def is_done(self):
        """Return True if the buffer is a complete message."""
        return self.is_query() or \
            not to_bytes(self.value).startswith(potr.proto.OTRTAG) or \
            has_otr_end(self.value)

    def get(self):
        """Return the current value of the buffer and empty it."""
        result = self.value
        self.clear()

        return result

    def is_query(self):
        """Return true if the buffer is an OTR query."""
        return OTR_QUERY_RE.search(self.value)

class IrcContext(potr.context.Context):
    """Context class for OTR over IRC."""

    def __init__(self, account, peername):
        super(IrcContext, self).__init__(account, peername)

        self.peer_nick, self.peer_server = peername.split('@', 1)
        self.in_assembler = Assembler()
        self.in_otr_message = False
        self.in_smp = False
        self.smp_question = False

    def policy_config_option(self, policy):
        """Get the option name of a policy option for this context."""
        return config_prefix('.'.join([
            'policy', self.peer_server, self.user.nick, self.peer_nick,
            policy.lower()]))

    def getPolicy(self, key):
        """Get the value of a policy option for this context."""
        key_lower = key.lower()

        if key_lower in READ_ONLY_POLICIES:
            result = READ_ONLY_POLICIES[key_lower]
        elif key_lower == 'send_tag' and self.no_send_tag():
            result = False
        else:
            option = config_get(self.policy_config_option(key))

            if option == '':
                option = config_get(self.user.policy_config_option(key))

            if option == '':
                option = config_get_prefixed('.'.join(
                    ['policy', self.peer_server, key_lower]))

            if option == '':
                option = config_get_prefixed(
                    'policy.default.{0}'.format(key_lower))

            result = bool(weechat.config_boolean(option))

        debug(('getPolicy', key, result))

        return result

    def inject(self, msg, appdata=None):
        """Send a message to the remote peer."""
        if isinstance(msg, potr.proto.OTRMessage):
            msg = PYVER.unicode(msg)
        else:
            msg = PYVER.to_unicode(msg)

        debug(('inject', msg, 'len {0}'.format(len(msg)), appdata))

        privmsg(self.peer_server, self.peer_nick, msg)

    def setState(self, newstate):
        """Handle state transition."""
        debug(('state', self.state, newstate))

        if self.is_encrypted():
            if newstate == potr.context.STATE_ENCRYPTED:
                self.print_buffer(
                    'Private conversation has been refreshed.', 'success')
            elif newstate == potr.context.STATE_FINISHED:
                self.print_buffer(
                    '{peer} has ended the private conversation. You should do '
                    'the same:\n/otr finish'.format(peer=self.peer_nick))
        elif newstate == potr.context.STATE_ENCRYPTED:
            # unencrypted => encrypted
            trust = self.getCurrentTrust()

            # Disable logging before any proof of OTR activity is generated.
            # This is necessary when the session is started automatically, and
            # not by /otr start.
            if not self.getPolicy('log'):
                self.previous_log_level = self.disable_logging()
            else:
                self.previous_log_level = self.get_log_level()
                if self.is_logged():
                    self.hint(
                        'You have enabled the recording to disk of OTR '
                        'conversations. By doing this you are potentially '
                        'putting yourself and your correspondent in danger. '
                        'Please consider disabling this policy with '
                        '"/otr policy default log off". To disable logging '
                        'for this OTR session, use "/otr log stop"')

            if trust is None:
                fpr = str(self.getCurrentKey())
                self.print_buffer('New fingerprint: {0}'.format(fpr), 'warning')
                self.setCurrentTrust('')

            if bool(trust):
                self.print_buffer(
                    'Authenticated secured OTR conversation started.',
                    'success')
            else:
                self.print_buffer(
                    'Unauthenticated secured OTR conversation started.',
                    'warning')
                self.hint(self.verify_instructions())

        if self.state != potr.context.STATE_PLAINTEXT and \
                newstate == potr.context.STATE_PLAINTEXT:
            self.print_buffer('Private conversation ended.')

            # If we altered the logging value, restore it.
            if self.previous_log_level is not None:
                self.restore_logging(self.previous_log_level)

        super(IrcContext, self).setState(newstate)

    def maxMessageSize(self, appdata=None):
        """Return the max message size for this context."""
        # remove 'PRIVMSG <nick> :' from max message size
        result = self.user.maxMessageSize - 10 - len(self.peer_nick)
        debug('max message size {0}'.format(result))

        return result

    def buffer(self):
        """Get the buffer for this context."""
        return info_get(
            'irc_buffer', '{server},{nick}'.format(
                server=self.peer_server,
                nick=self.peer_nick
                ))

    def print_buffer(self, msg, level='info'):
        """Print a message to the buffer for this context.
        level is used to colorize the message."""
        buf = self.buffer()

        # add [nick] prefix if we have only a server buffer for the query
        if self.peer_nick and not buffer_is_private(buf):
            msg = '[{nick}] {msg}'.format(
                nick=self.peer_nick,
                msg=msg)

        print_buffer(buf, msg, level)

    def hint(self, msg):
        """Print a message to the buffer but only when hints are enabled."""
        hints_option = config_get_prefixed('general.hints')

        if weechat.config_boolean(hints_option):
            self.print_buffer(msg, 'hint')

    def smp_finish(self, message=False, level='info'):
        """Reset SMP state and send a message to the user."""
        self.in_smp = False
        self.smp_question = False

        self.user.saveTrusts()
        if message:
            self.print_buffer(message, level)

    def handle_tlvs(self, tlvs):
        """Handle SMP states."""
        if tlvs:
            smp1q = first_instance(tlvs, potr.proto.SMP1QTLV)
            smp3 = first_instance(tlvs, potr.proto.SMP3TLV)
            smp4 = first_instance(tlvs, potr.proto.SMP4TLV)

            if first_instance(tlvs, potr.proto.SMPABORTTLV):
                debug('SMP aborted by peer')
                self.smp_finish('SMP aborted by peer.', 'warning')
            elif self.in_smp and not self.smpIsValid():
                debug('SMP aborted')
                self.smp_finish('SMP aborted.', 'error')
            elif first_instance(tlvs, potr.proto.SMP1TLV):
                debug('SMP1')
                self.in_smp = True

                self.print_buffer(
                    """Peer has requested SMP verification.
Respond with: /otr smp respond <secret>""")
            elif smp1q:
                debug(('SMP1Q', smp1q.msg))
                self.in_smp = True
                self.smp_question = True

                self.print_buffer(
                    """Peer has requested SMP verification: {msg}
Respond with: /otr smp respond <answer>""".format(
    msg=PYVER.to_unicode(smp1q.msg)))
            elif first_instance(tlvs, potr.proto.SMP2TLV):
                if not self.in_smp:
                    debug('Received unexpected SMP2')
                    self.smp_finish()
                else:
                    debug('SMP2')
                    self.print_buffer('SMP progressing.')
            elif smp3 or smp4:
                if smp3:
                    debug('SMP3')
                elif smp4:
                    debug('SMP4')

                if self.smpIsSuccess():

                    if self.smp_question:
                        self.smp_finish(
                            'SMP verification succeeded.', 'success')
                        if not self.is_verified:
                            self.print_buffer(
                                'You may want to authenticate your peer by '
                                'asking your own question:\n'
                                "/otr smp ask <'question'> 'secret'")
                    else:
                        self.smp_finish(
                            'SMP verification succeeded.', 'success')

                else:
                    self.smp_finish('SMP verification failed.', 'error')

    def verify_instructions(self):
        """Generate verification instructions for user."""
        return """You can verify that this contact is who they claim to be in
one of the following ways:

1) Verify each other's fingerprints using a secure channel:
  Your fingerprint : {your_fp}
  {peer}'s fingerprint : {peer_fp}
  then use the command: /otr trust {peer_nick} {peer_server}

2) SMP pre-shared secret that you both know:
  /otr smp ask {peer_nick} {peer_server} 'secret'

3) SMP pre-shared secret that you both know with a question:
  /otr smp ask {peer_nick} {peer_server} <'question'> 'secret'

Note: You can safely omit specifying the peer and server when
      executing these commands from the appropriate conversation
      buffer
""".format(
    your_fp=self.user.getPrivkey(),
    peer=self.peer,
    peer_nick=self.peer_nick,
    peer_server=self.peer_server,
    peer_fp=potr.human_hash(self.crypto.theirPubkey.cfingerprint()))

    def is_encrypted(self):
        """Return True if the conversation with this context's peer is
        currently encrypted."""
        return self.state == potr.context.STATE_ENCRYPTED

    def is_verified(self):
        """Return True if this context's peer is verified."""
        return bool(self.getCurrentTrust())

    def format_policies(self):
        """Return current policies for this context formatted as a string for
        the user."""
        buf = io.StringIO()

        buf.write('Current OTR policies for {peer}:\n'.format(
            peer=self.peer))

        for policy, desc in sorted(POLICIES.items()):
            buf.write('  {policy} ({desc}) : {value}\n'.format(
                policy=policy,
                desc=desc,
                value='on' if self.getPolicy(policy) else 'off'))

        buf.write('Change policies with: /otr policy NAME on|off')

        return buf.getvalue()

    def is_logged(self):
        """Return True if conversations with this context's peer are currently
        being logged to disk."""
        infolist = weechat.infolist_get('logger_buffer', '', '')

        buf = self.buffer()

        result = False

        while weechat.infolist_next(infolist):
            if weechat.infolist_pointer(infolist, 'buffer') == buf:
                result = bool(weechat.infolist_integer(infolist, 'log_enabled'))
                break

        weechat.infolist_free(infolist)

        return result

    def get_log_level(self):
        """Return the current logging level for this context's peer
        or -1 if the buffer uses the default log level of weechat."""
        infolist = weechat.infolist_get('logger_buffer', '', '')

        buf = self.buffer()

        if not config_get(self.get_logger_option_name()):
            result = -1
        else:
            result = 0

            while weechat.infolist_next(infolist):
                if weechat.infolist_pointer(infolist, 'buffer') == buf:
                    result = weechat.infolist_integer(infolist, 'log_level')
                    break

            weechat.infolist_free(infolist)

        return result

    def get_logger_option_name(self):
        """Returns the logger config option for this context's buffer."""
        buf = self.buffer()
        name = buffer_get_string(buf, 'name')
        plugin = buffer_get_string(buf, 'plugin')

        return 'logger.level.{plugin}.{name}'.format(
            plugin=plugin, name=name)

    def disable_logging(self):
        """Return the previous logger level and set the buffer logger level
        to 0. If it was already 0, return None."""
        # If previous_log_level has not been previously set, return the level
        # we detect now.
        if not hasattr(self, 'previous_log_level'):
            previous_log_level = self.get_log_level()

            if self.is_logged():
                weechat.command(self.buffer(), '/mute logger disable')
                self.print_buffer(
                    'Logs have been temporarily disabled for the session. '
                    'They will be restored upon finishing the OTR session.')

            return previous_log_level

        # If previous_log_level was already set, it means we already altered it
        # and that we just detected an already modified logging level.
        # Return the pre-existing value so it doesn't get lost, and we can
        # restore it later.
        else:
            return self.previous_log_level

    def restore_logging(self, previous_log_level):
        """Restore the log level of the buffer."""
        buf = self.buffer()

        if (previous_log_level >= 0) and (previous_log_level < 10):
            self.print_buffer(
                'Restoring buffer logging value to: {0}'.format(
                    previous_log_level), 'warning')
            weechat.command(buf, '/mute logger set {0}'.format(
                previous_log_level))

        if previous_log_level == -1:
            logger_option_name = self.get_logger_option_name()
            self.print_buffer(
                'Restoring buffer logging value to default', 'warning')
            weechat.command(buf, '/mute unset {0}'.format(
                logger_option_name))

        del self.previous_log_level

    def msg_convert_in(self, msg):
        """Transform incoming OTR message to IRC format.
        This includes stripping html, converting plain-text ACTIONs
        and character encoding conversion.
        Only character encoding is changed if context is unencrypted."""
        msg = PYVER.to_unicode(msg)

        if not self.is_encrypted():
            return msg

        if self.getPolicy('html_filter'):
            try:
                msg = IrcHTMLParser.parse(msg)
            except PYVER.html_parser.HTMLParseError:
                pass

        return msg_irc_from_plain(msg)

    def msg_convert_out(self, msg):
        """Convert an outgoing IRC message to be sent over OTR.
        This includes escaping html, converting ACTIONs to plain-text
        and character encoding conversion
        Only character encoding is changed if context is unencrypted."""
        if self.is_encrypted():
            msg = msg_plain_from_irc(msg)

            if self.getPolicy('html_escape'):
                msg = PYVER.html_escape(msg)

        # potr expects bytes to be returned
        return to_bytes(msg)

    def no_send_tag(self):
        """Skip OTR whitespace tagging to bots and services.

        Any nicks matching the otr.general.no_send_tag_regex config setting
        will not be tagged.
        """
        no_send_tag_regex = config_string('general.no_send_tag_regex')
        debug(('no_send_tag', no_send_tag_regex, self.peer_nick))
        if no_send_tag_regex:
            return re.match(no_send_tag_regex, self.peer_nick, re.IGNORECASE)

    def __repr__(self):
        return PYVER.to_str((
            '<{0} {1:x} peer_nick={c.peer_nick} peer_server={c.peer_server}>'
            ).format(self.__class__.__name__, id(self), c=self))

class IrcOtrAccount(potr.context.Account):
    """Account class for OTR over IRC."""

    contextclass = IrcContext

    PROTOCOL = 'irc'
    MAX_MSG_SIZE = 415

    def __init__(self, name):
        super(IrcOtrAccount, self).__init__(
            name, IrcOtrAccount.PROTOCOL, IrcOtrAccount.MAX_MSG_SIZE)

        self.nick, self.server = self.name.split('@', 1)

        # IRC messages cannot have newlines, OTR query and "no plugin" text
        # need to be one message
        self.defaultQuery = self.defaultQuery.replace("\n", ' ')

        self.key_file_path = private_key_file_path(name)
        self.fpr_file_path = os.path.join(OTR_DIR, '{0}.fpr'.format(name))

        self.load_trusts()

    def load_trusts(self):
        """Load trust data from the fingerprint file."""
        if os.path.exists(self.fpr_file_path):
            with open(self.fpr_file_path) as fpr_file:
                for line in fpr_file:
                    debug(('load trust check', line))

                    context, account, protocol, fpr, trust = \
                        PYVER.to_unicode(line[:-1]).split('\t')

                    if account == self.name and \
                            protocol == IrcOtrAccount.PROTOCOL:
                        debug(('set trust', context, fpr, trust))
                        self.setTrust(context, fpr, trust)

    def loadPrivkey(self):
        """Load key file.

        If no key file exists, load the default key. If there is no default
        key, a new key will be generated automatically by potr."""
        debug(('load private key', self.key_file_path))

        if os.path.exists(self.key_file_path):
            return read_private_key(self.key_file_path)
        else:
            default_key = config_string('general.defaultkey')
            if default_key:
                default_key_path = private_key_file_path(default_key)

                if os.path.exists(default_key_path):
                    shutil.copyfile(default_key_path, self.key_file_path)
                    return read_private_key(self.key_file_path)

    def savePrivkey(self):
        """Save key file."""
        debug(('save private key', self.key_file_path))

        with open(self.key_file_path, 'wb') as key_file:
            key_file.write(self.getPrivkey().serializePrivateKey())

    def saveTrusts(self):
        """Save trusts."""
        with open(self.fpr_file_path, 'w') as fpr_file:
            for uid, trusts in self.trusts.items():
                for fpr, trust in trusts.items():
                    debug(('trust write', uid, self.name,
                           IrcOtrAccount.PROTOCOL, fpr, trust))
                    fpr_file.write(PYVER.to_str('\t'.join(
                        (uid, self.name, IrcOtrAccount.PROTOCOL, fpr, trust))))
                    fpr_file.write('\n')

    def end_all_private(self):
        """End all currently encrypted conversations."""
        for context in self.ctxs.values():
            if context.is_encrypted():
                context.disconnect()

    def policy_config_option(self, policy):
        """Get the option name of a policy option for this account."""
        return config_prefix('.'.join([
            'policy', self.server, self.nick, policy.lower()]))

class IrcHTMLParser(PYVER.html_parser.HTMLParser):
    """A simple HTML parser that throws away anything but newlines and links"""

    @staticmethod
    def parse(data):
        """Create a temporary IrcHTMLParser and parse a single string"""
        parser = IrcHTMLParser(**PYVER.html_parser_init_kwargs)
        parser.feed(data)
        parser.close()
        return parser.result

    def reset(self):
        """Forget all state, called from __init__"""
        PYVER.html_parser.HTMLParser.reset(self)
        self.result = ''
        self.linktarget = ''
        self.linkstart = 0

    def handle_starttag(self, tag, attrs):
        """Called when a start tag is encountered"""
        if tag == 'br':
            self.result += '\n'
        elif tag == 'a':
            attrs = dict(attrs)
            if 'href' in attrs:
                self.result += '['
                self.linktarget = attrs['href']
                self.linkstart = len(self.result)

    def handle_endtag(self, tag):
        """Called when an end tag is encountered"""
        if tag == 'a':
            if self.linktarget:
                if self.result[self.linkstart:] == self.linktarget:
                    self.result += ']'
                else:
                    self.result += ']({0})'.format(self.linktarget)
                self.linktarget = ''

    def handle_data(self, data):
        """Called for character data (i.e. text)"""
        self.result += data

    def handle_entityref(self, name):
        """Called for entity references, such as &amp;"""
        try:
            self.result += PYVER.unichr(
                PYVER.html_entities.name2codepoint[name])
        except KeyError:
            self.result += '&{0};'.format(name)

    def handle_charref(self, name):
        """Called for character references, such as &#39;"""
        try:
            if name.startswith('x'):
                self.result += PYVER.unichr(int(name[1:], 16))
            else:
                self.result += PYVER.unichr(int(name))
        except ValueError:
            self.result += '&#{0};'.format(name)

class TableFormatter(object):
    """Format lists of string into aligned tables."""

    def __init__(self):
        self.rows = []
        self.max_widths = None

    def add_row(self, row):
        """Add a row to the table."""
        self.rows.append(row)
        row_widths = [len(s) for s in row]
        if self.max_widths is None:
            self.max_widths = row_widths
        else:
            self.max_widths = list(map(max, self.max_widths, row_widths))

    def format(self):
        """Return the formatted table as a string."""
        return '\n'.join([self.format_row(row) for row in self.rows])

    def format_row(self, row):
        """Format a single row as a string."""
        return ' |'.join(
            [s.ljust(self.max_widths[i]) for i, s in enumerate(row)])

def message_in_cb(data, modifier, modifier_data, string):
    """Incoming message callback"""
    debug(('message_in_cb', data, modifier, modifier_data, string))

    parsed = parse_irc_privmsg(
        PYVER.to_unicode(string), PYVER.to_unicode(modifier_data))
    debug(('parsed message', parsed))

    # skip processing messages to public channels
    if parsed['to_channel']:
        return string

    server = PYVER.to_unicode(modifier_data)

    context = get_server_context(server, parsed['from_nick'])

    context.in_assembler.add(parsed['text'])

    result = ''

    if context.in_assembler.is_done():
        try:
            msg, tlvs = context.receiveMessage(
                # potr expects bytes
                to_bytes(context.in_assembler.get()))

            debug(('receive', msg, tlvs))

            if msg:
                result = PYVER.to_str(build_privmsgs_in(
                    parsed['from'], parsed['to'],
                    context.msg_convert_in(msg)))

            context.handle_tlvs(tlvs)
        except potr.context.ErrorReceived as err:
            context.print_buffer('Received OTR error: {0}'.format(
                PYVER.to_unicode(err.args[0])), 'error')
        except potr.context.NotEncryptedError:
            context.print_buffer(
                'Received encrypted data but no private session established.',
                'warning')
        except potr.context.NotOTRMessage:
            result = string
        except potr.context.UnencryptedMessage as err:
            result = PYVER.to_str(build_privmsgs_in(
                parsed['from'], parsed['to'], PYVER.to_unicode(
                    msg_plain_from_irc(err.args[0])),
                'Unencrypted message received: '))

    weechat.bar_item_update(SCRIPT_NAME)

    return result

def message_out_cb(data, modifier, modifier_data, string):
    """Outgoing message callback."""
    result = ''

    # If any exception is raised in this function, WeeChat will not send the
    # outgoing message, which could be something that the user intended to be
    # encrypted. This paranoid exception handling ensures that the system
    # fails closed and not open.
    try:
        debug(('message_out_cb', data, modifier, modifier_data, string))

        parsed = parse_irc_privmsg(
            PYVER.to_unicode(string), PYVER.to_unicode(modifier_data))
        debug(('parsed message', parsed))

        # skip processing messages to public channels
        if parsed['to_channel']:
            return string

        server = PYVER.to_unicode(modifier_data)

        context = get_server_context(server, parsed['to_nick'])
        is_query = OTR_QUERY_RE.search(parsed['text'])

        parsed_text_bytes = to_bytes(parsed['text'])

        is_otr_message = \
            parsed_text_bytes[:len(potr.proto.OTRTAG)] == potr.proto.OTRTAG

        if is_otr_message and not is_query:
            if not has_otr_end(parsed['text']):
                debug('in OTR message')
                context.in_otr_message = True
            else:
                debug('complete OTR message')
            result = string
        elif context.in_otr_message:
            if has_otr_end(parsed['text']):
                context.in_otr_message = False
                debug('in OTR message end')
            result = string
        else:
            debug(('context send message', parsed['text'], parsed['to_nick'],
                   server))

            if context.policyOtrEnabled() and \
                not context.is_encrypted() and \
                not is_query and \
                context.getPolicy('require_encryption'):
                context.print_buffer(
                    'Your message will not be sent, because policy requires an '
                    'encrypted connection.', 'error')
                context.hint(
                    'Wait for the OTR connection or change the policy to allow '
                    'clear-text messages:\n'
                    '/otr policy require_encryption off')

            try:
                ret = context.sendMessage(
                    potr.context.FRAGMENT_SEND_ALL,
                    context.msg_convert_out(parsed['text']))

                if ret:
                    debug(('sendMessage returned', ret))
                    result = PYVER.to_str(
                        build_privmsg_out(
                            parsed['to_nick'], PYVER.to_unicode(ret)
                            ))

            except potr.context.NotEncryptedError as err:
                if err.args[0] == potr.context.EXC_FINISHED:
                    context.print_buffer(
                        'Your message was not sent. End your private '
                        'conversation:\n/otr finish',
                        'error')
                else:
                    raise

        weechat.bar_item_update(SCRIPT_NAME)
    # pylint: disable=bare-except
    except:
        try:
            print_buffer('', traceback.format_exc(), 'error')
            print_buffer('', 'Versions: {versions}'.format(
                versions=dependency_versions()), 'error')
            context.print_buffer(
                'Failed to send message. See core buffer for traceback.',
                'error')
        # pylint: disable=bare-except
        except:
            pass

    return result

def shutdown():
    """Script unload callback."""
    debug('shutdown')

    weechat.config_write(CONFIG_FILE)

    for account in ACCOUNTS.values():
        account.end_all_private()

    free_all_config()

    weechat.bar_item_remove(OTR_STATUSBAR)

    return weechat.WEECHAT_RC_OK

def command_cb(data, buf, args):
    """Parse and dispatch WeeChat OTR commands."""
    result = weechat.WEECHAT_RC_ERROR

    arg_parts = [PYVER.to_unicode(arg) for arg in shlex.split(args)]

    if len(arg_parts) in (1, 3) and arg_parts[0] in ('start', 'refresh'):
        nick, server = default_peer_args(arg_parts[1:3], buf)

        if nick is not None and server is not None:
            context = get_server_context(server, nick)
            # We need to wall disable_logging() here so that no OTR-related
            # buffer messages get logged at any point. disable_logging() will
            # be called again when effectively switching to encrypted, but
            # the previous_log_level we set here will be preserved for later
            # restoring.
            if not context.getPolicy('log'):
                context.previous_log_level = context.disable_logging()
            else:
                context.previous_log_level = context.get_log_level()

            context.hint(
                'Sending OTR query... Please await confirmation of the OTR '
                'session being started before sending a message.')
            if not context.getPolicy('send_tag'):
                context.hint(
                    'To try OTR on all conversations with {peer}: /otr '
                    'policy send_tag on'.format(peer=context.peer))

            privmsg(server, nick, '?OTR?')

            result = weechat.WEECHAT_RC_OK
    elif len(arg_parts) in (1, 3) and arg_parts[0] in ('finish', 'end'):
        nick, server = default_peer_args(arg_parts[1:3], buf)

        if nick is not None and server is not None:
            context = get_server_context(server, nick)
            context.disconnect()

            result = weechat.WEECHAT_RC_OK

    elif len(arg_parts) in (1, 3) and arg_parts[0] == 'status':
        nick, server = default_peer_args(arg_parts[1:3], buf)

        if nick is not None and server is not None:
            context = get_server_context(server, nick)
            if context.is_encrypted():
                context.print_buffer(
                    'This conversation is encrypted.', 'success')
                context.print_buffer("Your fingerprint is: {0}".format(
                    context.user.getPrivkey()))
                context.print_buffer("Your peer's fingerprint is: {0}".format(
                    potr.human_hash(context.crypto.theirPubkey.cfingerprint())))
                if context.is_verified():
                    context.print_buffer(
                        "The peer's identity has been verified.",
                        'success')
                else:
                    context.print_buffer(
                        "You have not verified the peer's identity yet.",
                        'warning')
            else:
                context.print_buffer(
                    "This current conversation is not encrypted.",
                    'warning')

            result = weechat.WEECHAT_RC_OK

    elif len(arg_parts) in range(2, 7) and arg_parts[0] == 'smp':
        action = arg_parts[1]

        if action == 'respond':
            # Check if nickname and server are specified
            if len(arg_parts) == 3:
                nick, server = default_peer_args([], buf)
                secret = arg_parts[2]
            elif len(arg_parts) == 5:
                nick, server = default_peer_args(arg_parts[2:4], buf)
                secret = arg_parts[4]
            else:
                return weechat.WEECHAT_RC_ERROR

            if secret:
                secret = PYVER.to_str(secret)

            context = get_server_context(server, nick)
            context.smpGotSecret(secret)

            result = weechat.WEECHAT_RC_OK

        elif action == 'ask':
            question = None
            secret = None

            # Nickname and server are not specified
            # Check whether it's a simple challenge or a question/answer request
            if len(arg_parts) == 3:
                nick, server = default_peer_args([], buf)
                secret = arg_parts[2]
            elif len(arg_parts) == 4:
                nick, server = default_peer_args([], buf)
                secret = arg_parts[3]
                question = arg_parts[2]

            # Nickname and server are specified
            # Check whether it's a simple challenge or a question/answer request
            elif len(arg_parts) == 5:
                nick, server = default_peer_args(arg_parts[2:4], buf)
                secret = arg_parts[4]
            elif len(arg_parts) == 6:
                nick, server = default_peer_args(arg_parts[2:4], buf)
                secret = arg_parts[5]
                question = arg_parts[4]
            else:
                return weechat.WEECHAT_RC_ERROR

            context = get_server_context(server, nick)

            if secret:
                secret = PYVER.to_str(secret)
            if question:
                question = PYVER.to_str(question)

            try:
                context.smpInit(secret, question)
            except potr.context.NotEncryptedError:
                context.print_buffer(
                    'There is currently no encrypted session with {0}.'.format(
                        context.peer), 'error')
            else:
                if question:
                    context.print_buffer('SMP challenge sent...')
                else:
                    context.print_buffer('SMP question sent...')
                context.in_smp = True
                result = weechat.WEECHAT_RC_OK

        elif action == 'abort':
            # Nickname and server are not specified
            if len(arg_parts) == 2:
                nick, server = default_peer_args([], buf)
            # Nickname and server are specified
            elif len(arg_parts) == 4:
                nick, server = default_peer_args(arg_parts[2:4], buf)
            else:
                return weechat.WEECHAT_RC_ERROR

            context = get_server_context(server, nick)

            if context.in_smp:
                try:
                    context.smpAbort()
                except potr.context.NotEncryptedError:
                    context.print_buffer(
                        'There is currently no encrypted session with {0}.'
                        .format(context.peer), 'error')
                else:
                    debug('SMP aborted')
                    context.smp_finish('SMP aborted.')
                    result = weechat.WEECHAT_RC_OK

    elif len(arg_parts) in (1, 3) and arg_parts[0] == 'trust':
        nick, server = default_peer_args(arg_parts[1:3], buf)

        if nick is not None and server is not None:
            context = get_server_context(server, nick)

            if context.crypto.theirPubkey is not None:
                context.setCurrentTrust('verified')
                context.print_buffer('{peer} is now authenticated.'.format(
                    peer=context.peer))

                weechat.bar_item_update(SCRIPT_NAME)
            else:
                context.print_buffer(
                    'No fingerprint for {peer}. Start an OTR conversation '
                    'first: /otr start'.format(peer=context.peer),
                    'error')

            result = weechat.WEECHAT_RC_OK
    elif len(arg_parts) in (1, 3) and arg_parts[0] == 'distrust':
        nick, server = default_peer_args(arg_parts[1:3], buf)

        if nick is not None and server is not None:
            context = get_server_context(server, nick)

            if context.crypto.theirPubkey is not None:
                context.setCurrentTrust('')
                context.print_buffer(
                    '{peer} is now de-authenticated.'.format(
                        peer=context.peer))

                weechat.bar_item_update(SCRIPT_NAME)
            else:
                context.print_buffer(
                    'No fingerprint for {peer}. Start an OTR conversation '
                    'first: /otr start'.format(peer=context.peer), 'error')

            result = weechat.WEECHAT_RC_OK

    elif len(arg_parts) in (1, 2) and arg_parts[0] == 'log':
        nick, server = default_peer_args([], buf)
        if len(arg_parts) == 1:
            if nick is not None and server is not None:
                context = get_server_context(server, nick)

                if context.is_encrypted():
                    if context.is_logged():
                        context.print_buffer(
                            'This conversation is currently being logged.',
                            'warning')
                        result = weechat.WEECHAT_RC_OK

                    else:
                        context.print_buffer(
                            'This conversation is currently NOT being logged.')
                        result = weechat.WEECHAT_RC_OK
                else:
                    context.print_buffer(
                        'OTR LOG: Not in an OTR session', 'error')
                    result = weechat.WEECHAT_RC_OK

            else:
                print_buffer('', 'OTR LOG: Not in an OTR session', 'error')
                result = weechat.WEECHAT_RC_OK

        if len(arg_parts) == 2:
            if nick is not None and server is not None:
                context = get_server_context(server, nick)

                if arg_parts[1] == 'start' and \
                        not context.is_logged() and \
                        context.is_encrypted():
                    if context.previous_log_level is None:
                        context.previous_log_level = context.get_log_level()
                    context.print_buffer(
                        'From this point on, this conversation will be '
                        'logged. Please keep in mind that by doing so you '
                        'are potentially putting yourself and your '
                        'interlocutor at risk. You can disable this by doing '
                        '/otr log stop',
                        'warning')
                    weechat.command(buf, '/mute logger set 9')
                    result = weechat.WEECHAT_RC_OK

                elif arg_parts[1] == 'stop' and \
                        context.is_logged() and \
                        context.is_encrypted():
                    if context.previous_log_level is None:
                        context.previous_log_level = context.get_log_level()
                    weechat.command(buf, '/mute logger set 0')
                    context.print_buffer(
                        'From this point on, this conversation will NOT be '
                        'logged ANYMORE.')
                    result = weechat.WEECHAT_RC_OK

                elif not context.is_encrypted():
                    context.print_buffer(
                        'OTR LOG: Not in an OTR session', 'error')
                    result = weechat.WEECHAT_RC_OK

                else:
                    # Don't need to do anything.
                    result = weechat.WEECHAT_RC_OK

            else:
                print_buffer('', 'OTR LOG: Not in an OTR session', 'error')

    elif len(arg_parts) in (1, 2, 3, 4) and arg_parts[0] == 'policy':
        if len(arg_parts) == 1:
            nick, server = default_peer_args([], buf)

            if nick is not None and server is not None:
                context = get_server_context(server, nick)

                context.print_buffer(context.format_policies())
            else:
                prnt('', format_default_policies())

            result = weechat.WEECHAT_RC_OK

        elif len(arg_parts) == 2 and arg_parts[1].lower() == 'default':
            nick, server = default_peer_args([], buf)

            if nick is not None and server is not None:
                context = get_server_context(server, nick)

                context.print_buffer(format_default_policies())
            else:
                prnt('', format_default_policies())

            result = weechat.WEECHAT_RC_OK

        elif len(arg_parts) == 3 and arg_parts[1].lower() in POLICIES:
            nick, server = default_peer_args([], buf)

            if nick is not None and server is not None:
                context = get_server_context(server, nick)

                policy_var = context.policy_config_option(arg_parts[1].lower())

                command('', '/set {policy} {value}'.format(
                    policy=policy_var,
                    value=arg_parts[2]))

                context.print_buffer(context.format_policies())

                result = weechat.WEECHAT_RC_OK

        elif len(arg_parts) == 4 and \
            arg_parts[1].lower() == 'default' and \
            arg_parts[2].lower() in POLICIES:
            nick, server = default_peer_args([], buf)

            policy_var = "otr.policy.default." + arg_parts[2].lower()

            command('', '/set {policy} {value}'.format(
                policy=policy_var,
                value=arg_parts[3]))

            if nick is not None and server is not None:
                context = get_server_context(server, nick)

                context.print_buffer(format_default_policies())
            else:
                prnt('', format_default_policies())

            result = weechat.WEECHAT_RC_OK
    elif len(arg_parts) in (1, 2) and arg_parts[0] == 'fingerprint':
        if len(arg_parts) == 1:
            show_account_fingerprints()
            result = weechat.WEECHAT_RC_OK
        elif len(arg_parts) == 2:
            if arg_parts[1] == 'all':
                show_peer_fingerprints()
            else:
                show_peer_fingerprints(grep=arg_parts[1])
            result = weechat.WEECHAT_RC_OK

    return result

def otr_statusbar_cb(data, item, window):
    """Update the statusbar."""
    if window:
        buf = weechat.window_get_pointer(window, 'buffer')
    else:
        # If the bar item is in a root bar that is not in a window, window
        # will be empty.
        buf = weechat.current_buffer()

    if not buffer_is_private(buf):
        return ''

    local_user = irc_user(
        buffer_get_string(buf, 'localvar_nick'),
        buffer_get_string(buf, 'localvar_server'))

    remote_user = irc_user(
        buffer_get_string(buf, 'localvar_channel'),
        buffer_get_string(buf, 'localvar_server'))

    context = get_context(local_user, remote_user)

    encrypted_str = config_string('look.bar.state.encrypted')
    unencrypted_str = config_string('look.bar.state.unencrypted')
    authenticated_str = config_string('look.bar.state.authenticated')
    unauthenticated_str = config_string('look.bar.state.unauthenticated')
    logged_str = config_string('look.bar.state.logged')
    notlogged_str = config_string('look.bar.state.notlogged')

    bar_parts = []

    if context.is_encrypted():
        if encrypted_str:
            bar_parts.append(''.join([
                config_color('status.encrypted'),
                encrypted_str,
                config_color('status.default')]))

        if context.is_verified():
            if authenticated_str:
                bar_parts.append(''.join([
                    config_color('status.authenticated'),
                    authenticated_str,
                    config_color('status.default')]))
        elif unauthenticated_str:
            bar_parts.append(''.join([
                config_color('status.unauthenticated'),
                unauthenticated_str,
                config_color('status.default')]))

        if context.is_logged():
            if logged_str:
                bar_parts.append(''.join([
                    config_color('status.logged'),
                    logged_str,
                    config_color('status.default')]))
        elif notlogged_str:
            bar_parts.append(''.join([
                config_color('status.notlogged'),
                notlogged_str,
                config_color('status.default')]))

    elif unencrypted_str:
        bar_parts.append(''.join([
            config_color('status.unencrypted'),
            unencrypted_str,
            config_color('status.default')]))

    result = config_string('look.bar.state.separator').join(bar_parts)

    if result:
        result = '{color}{prefix}{result}'.format(
            color=config_color('status.default'),
            prefix=config_string('look.bar.prefix'),
            result=result)

    if context.is_encrypted():
        weechat.buffer_set(buf, 'localvar_set_otr_encrypted', 'true')
    else:
        weechat.buffer_set(buf, 'localvar_set_otr_encrypted', 'false')

    if context.is_verified():
        weechat.buffer_set(buf, 'localvar_set_otr_authenticated', 'true')
    else:
        weechat.buffer_set(buf, 'localvar_set_otr_authenticated', 'false')

    if context.is_logged():
        weechat.buffer_set(buf, 'localvar_set_otr_logged', 'true')
    else:
        weechat.buffer_set(buf, 'localvar_set_otr_logged', 'false')

    return result

def bar_config_update_cb(data, option):
    """Callback for updating the status bar when its config changes."""
    weechat.bar_item_update(SCRIPT_NAME)

    return weechat.WEECHAT_RC_OK

def policy_completion_cb(data, completion_item, buf, completion):
    """Callback for policy tab completion."""
    for policy in POLICIES:
        weechat.hook_completion_list_add(
            completion, policy, 0, weechat.WEECHAT_LIST_POS_SORT)

    return weechat.WEECHAT_RC_OK

def policy_create_option_cb(data, config_file, section, name, value):
    """Callback for creating a new policy option when the user sets one
    that doesn't exist."""
    weechat.config_new_option(
        config_file, section, name, 'boolean', '', '', 0, 0, value, value, 0,
        '', '', '', '', '', '')

    return weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED

def logger_level_update_cb(data, option, value):
    """Callback called when any logger level changes."""
    weechat.bar_item_update(SCRIPT_NAME)

    return weechat.WEECHAT_RC_OK

def buffer_switch_cb(data, signal, signal_data):
    """Callback for buffer switched.

    Used for updating the status bar item when it is in a root bar.
    """
    weechat.bar_item_update(SCRIPT_NAME)

    return weechat.WEECHAT_RC_OK

def buffer_closing_cb(data, signal, signal_data):
    """Callback for buffer closed.

    It closes the OTR session when the buffer is about to be closed.
    """
    result = weechat.WEECHAT_RC_ERROR
    nick, server = default_peer_args([], signal_data)

    if nick is not None and server is not None:
        context = get_server_context(server, nick)
        context.disconnect()

        result = weechat.WEECHAT_RC_OK
    return result

def init_config():
    """Set up configuration options and load config file."""
    global CONFIG_FILE
    CONFIG_FILE = weechat.config_new(SCRIPT_NAME, '', '')

    global CONFIG_SECTIONS
    CONFIG_SECTIONS = {}

    CONFIG_SECTIONS['general'] = weechat.config_new_section(
        CONFIG_FILE, 'general', 0, 0, '', '', '', '', '', '', '', '', '', '')

    for option, typ, desc, default in [
            ('debug',
             'boolean',
             'OTR script debugging',
             'off'),
            ('hints',
             'boolean',
             'Give helpful hints how to use this script and how to stay '
             'secure while using OTR (recommended)',
             'on'),
            ('defaultkey',
             'string',
             'default private key to use for new accounts (nick@server)',
             ''),
            ('no_send_tag_regex',
             'string',
             'do not OTR whitespace tag messages to nicks matching this regex '
             '(case insensitive)',
             '^(alis|chanfix|global|.+serv|\*.+)$'),
        ]:
        weechat.config_new_option(
            CONFIG_FILE, CONFIG_SECTIONS['general'], option, typ, desc, '', 0,
            0, default, default, 0, '', '', '', '', '', '')

    CONFIG_SECTIONS['color'] = weechat.config_new_section(
        CONFIG_FILE, 'color', 0, 0, '', '', '', '', '', '', '', '', '', '')

    for option, desc, default, update_cb in [
            ('status.default',
             'status bar default color',
             'default',
             'bar_config_update_cb'),
            ('status.encrypted',
             'status bar encrypted indicator color',
             'green',
             'bar_config_update_cb'),
            ('status.unencrypted',
             'status bar unencrypted indicator color',
             'lightred',
             'bar_config_update_cb'),
            ('status.authenticated',
             'status bar authenticated indicator color',
             'green',
             'bar_config_update_cb'),
            ('status.unauthenticated',
             'status bar unauthenticated indicator color',
             'lightred',
             'bar_config_update_cb'),
            ('status.logged',
             'status bar logged indicator color',
             'lightred',
             'bar_config_update_cb'),
            ('status.notlogged',
             'status bar not logged indicator color',
             'green',
             'bar_config_update_cb'),
            ('buffer.hint',
             'text color for hints',
             'lightblue',
             ''),
            ('buffer.info',
             'text color for informational messages',
             'default',
             ''),
            ('buffer.success',
             'text color for success messages',
             'lightgreen',
             ''),
            ('buffer.warning',
             'text color for warnings',
             'yellow',
             ''),
            ('buffer.error',
             'text color for errors',
             'lightred',
             ''),
        ]:
        weechat.config_new_option(
            CONFIG_FILE, CONFIG_SECTIONS['color'], option, 'color', desc, '', 0,
            0, default, default, 0, '', '', update_cb, '', '', '')

    CONFIG_SECTIONS['look'] = weechat.config_new_section(
        CONFIG_FILE, 'look', 0, 0, '', '', '', '', '', '', '', '', '', '')

    for option, desc, default, update_cb in [
            ('bar.prefix',
             'prefix for OTR status bar item',
             'OTR:',
             'bar_config_update_cb'),
            ('bar.state.encrypted',
             'shown in status bar when conversation is encrypted',
             'SEC',
             'bar_config_update_cb'),
            ('bar.state.unencrypted',
             'shown in status bar when conversation is not encrypted',
             '!SEC',
             'bar_config_update_cb'),
            ('bar.state.authenticated',
             'shown in status bar when peer is authenticated',
             'AUTH',
             'bar_config_update_cb'),
            ('bar.state.unauthenticated',
             'shown in status bar when peer is not authenticated',
             '!AUTH',
             'bar_config_update_cb'),
            ('bar.state.logged',
             'shown in status bar when peer conversation is being logged to '
             'disk',
             'LOG',
             'bar_config_update_cb'),
            ('bar.state.notlogged',
             'shown in status bar when peer conversation is not being logged '
             'to disk',
             '!LOG',
             'bar_config_update_cb'),
            ('bar.state.separator',
             'separator for states in the status bar',
             ',',
             'bar_config_update_cb'),
            ('prefix',
             'prefix used for messages from otr (note: content is evaluated, '
             'see /help eval)',
             '${color:default}:! ${color:brown}otr${color:default} !:',
             ''),
        ]:
        weechat.config_new_option(
            CONFIG_FILE, CONFIG_SECTIONS['look'], option, 'string', desc, '',
            0, 0, default, default, 0, '', '', update_cb, '', '', '')

    CONFIG_SECTIONS['policy'] = weechat.config_new_section(
        CONFIG_FILE, 'policy', 1, 1, '', '', '', '', '', '',
        'policy_create_option_cb', '', '', '')

    for option, desc, default in [
            ('default.allow_v2',
             'default allow OTR v2 policy',
             'on'),
            ('default.require_encryption',
             'default require encryption policy',
             'off'),
            ('default.log',
             'default enable logging to disk',
             'off'),
            ('default.send_tag',
             'default send tag policy',
             'off'),
            ('default.html_escape',
             'default HTML escape policy',
             'off'),
            ('default.html_filter',
             'default HTML filter policy',
             'on'),
        ]:
        weechat.config_new_option(
            CONFIG_FILE, CONFIG_SECTIONS['policy'], option, 'boolean', desc, '',
            0, 0, default, default, 0, '', '', '', '', '', '')

    weechat.config_read(CONFIG_FILE)

def free_all_config():
    """Free all config options, sections and config file."""
    for section in CONFIG_SECTIONS.values():
        weechat.config_section_free_options(section)
        weechat.config_section_free(section)

    weechat.config_free(CONFIG_FILE)

def create_dir():
    """Create the OTR subdirectory in the WeeChat config directory if it does
    not exist."""
    if not os.path.exists(OTR_DIR):
        weechat.mkdir_parents(OTR_DIR, 0o700)

def git_info():
    """If this script is part of a git repository return the repo state."""
    result = None
    script_dir = os.path.dirname(os.path.realpath(__file__))
    git_dir = os.path.join(script_dir, '.git')
    if os.path.isdir(git_dir):
        import subprocess
        try:
            # We can't use check_output here without breaking compatibility
            # for Python 2.6, but we ignore the return value anyway, so Popen
            # is only slightly more complicated:
            process = subprocess.Popen([
                'git',
                '--git-dir', git_dir,
                '--work-tree', script_dir,
                'describe', '--dirty', '--always',
                ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            output = process.communicate()[0]
            if output:
                result = PYVER.to_unicode(output).lstrip('v').rstrip()
        except OSError:
            pass

    return result

def weechat_version_ok():
    """Check if the WeeChat version is compatible with this script.

    If WeeChat version < 0.4.2 log an error to the core buffer and return
    False. Otherwise return True.
    """
    weechat_version = weechat.info_get('version_number', '') or 0
    if int(weechat_version) < 0x00040200:
        error_message = (
            '{script_name} requires WeeChat version >= 0.4.2. The current '
            'version is {current_version}.').format(
                script_name=SCRIPT_NAME,
                current_version=weechat.info_get('version', ''))
        prnt('', error_message)
        return False
    return True

SCRIPT_VERSION = git_info() or SCRIPT_VERSION

def dependency_versions():
    """Return a string containing the versions of all dependencies."""
    return ('weechat-otr {script_version}, '
            'potr {potr_major}.{potr_minor}.{potr_patch}-{potr_sub}, '
            'Python {python_version}, '
            'WeeChat {weechat_version}'
           ).format(
               script_version=SCRIPT_VERSION,
               potr_major=potr.VERSION[0],
               potr_minor=potr.VERSION[1],
               potr_patch=potr.VERSION[2],
               potr_sub=potr.VERSION[3],
               python_version=platform.python_version(),
               weechat_version=weechat.info_get('version', ''))

def excepthook(typ, value, tracebak):
    """Add dependency versions to tracebacks."""
    sys.stderr.write('Versions: ')
    sys.stderr.write(dependency_versions())
    sys.stderr.write('\n')

    sys.__excepthook__(typ, value, tracebak)

sys.excepthook = excepthook

if weechat.register(
        SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENCE, SCRIPT_DESC,
        'shutdown', ''):
    if weechat_version_ok():
        init_config()

        options = {
            'directory': 'data',
        }
        OTR_DIR = weechat.string_eval_path_home('%%h/%s' % OTR_DIR_NAME,
                                                {}, {}, options)
        create_dir()

        ACCOUNTS = AccountDict()

        weechat.hook_modifier('irc_in_privmsg', 'message_in_cb', '')
        weechat.hook_modifier('irc_out_privmsg', 'message_out_cb', '')

        weechat.hook_command(
            SCRIPT_NAME, SCRIPT_HELP,
            'start [NICK SERVER] || '
            'refresh [NICK SERVER] || '
            'finish [NICK SERVER] || '
            'end [NICK SERVER] || '
            'status [NICK SERVER] || '
            'smp ask [NICK SERVER] [QUESTION] SECRET || '
            'smp respond [NICK SERVER] SECRET || '
            'smp abort [NICK SERVER] || '
            'trust [NICK SERVER] || '
            'distrust [NICK SERVER] || '
            'log [start|stop] || '
            'policy [POLICY on|off] || '
            'fingerprint [SEARCH|all]',
            '',
            'start %(nick) %(irc_servers) %-||'
            'refresh %(nick) %(irc_servers) %-||'
            'finish %(nick) %(irc_servers) %-||'
            'end %(nick) %(irc_servers) %-||'
            'status %(nick) %(irc_servers) %-||'
            'smp ask|respond %(nick) %(irc_servers) %-||'
            'smp abort %(nick) %(irc_servers) %-||'
            'trust %(nick) %(irc_servers) %-||'
            'distrust %(nick) %(irc_servers) %-||'
            'log start|stop %-||'
            'policy %(otr_policy) on|off %-||'
            'fingerprint all %-||',
            'command_cb',
            '')

        weechat.hook_completion(
            'otr_policy', 'OTR policies', 'policy_completion_cb', '')

        weechat.hook_config('logger.level.irc.*', 'logger_level_update_cb', '')

        weechat.hook_signal('buffer_switch', 'buffer_switch_cb', '')
        weechat.hook_signal('buffer_closing', 'buffer_closing_cb', '')

        OTR_STATUSBAR = weechat.bar_item_new(
            SCRIPT_NAME, 'otr_statusbar_cb', '')
        weechat.bar_item_update(SCRIPT_NAME)