aboutsummaryrefslogtreecommitdiffstats
path: root/ex_autocmd.js
blob: 190110b83a5209b3c24b0730b9b23ebd513eaa13 (plain)
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
// Vimperator plugin: 'Ex Autocmd'
// Last Change: 21-Oct-2008. Jan 2008
// License: Creative Commons
// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid
//
// extends autocmd for Vimperator
// Ex Events:
//      TabSelect
//      TabLeave
//      CurrentPageLoad

var recentTabURI = null;
function tabSelect(e){
    liberator.modules.autocommands.trigger("TabLeave",recentTabURI || "");
    liberator.modules.autocommands.trigger("TabSelect",gBrowser.selectedTab.linkedBrowser.contentWindow.location.href);
    recentTabURI = gBrowser.selectedTab.linkedBrowser.contentWindow.location.href;
}
gBrowser.tabContainer.addEventListener("TabSelect",tabSelect,false);


function currentPageLoad(e){
    var doc = e.originalTarget;
    if (doc instanceof HTMLDocument && doc == gBrowser.contentDocument){
        liberator.modules.autocommands.trigger("CurrentPageLoad",doc.documentURI);
        recentTabURI = doc.documentURI;
    }
}
gBrowser.addEventListener("load", currentPageLoad, true);

n175' href='#n175'>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 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
/*
 * The MIT License
 *
 * Copyright (c) 2010 teramako
 * Copyright (c) 2010-2013 anekos
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

// INFO {{{
let INFO = xml`
  <plugin name="Twittperator" version="1.19.2"
          href="https://github.com/vimpr/vimperator-plugins/raw/master/twittperator.js"
          summary="Twitter Client using OAuth and Streaming API">
    <author email="teramako@gmail.com" href="http://d.hatena.ne.jp/teramako/">teramako</author>
    <author email="anekos@snca.net" href="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
    <license>MIT License</license>
    <project name="Vimperator" minVersion="2.3"/>
    <p>
      This is the Twitter client plugin with OAuth authentication.
    </p>
    <h2>Command</h2>
      - Use completion for comfort.
    <item>
      <tags><![CDATA[:twittperator :tw]]></tags>
      <spec>:tw<oa>ittperator</oa> -getPIN</spec>
      <description>
        <p>Opens the page to authorize Twittperator and get your PIN from Twitter.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> -setPIN <a>PINCode</a></spec>
      <description>
        <p>Allows Twittperator to access Twitter by signifying your PIN.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa></spec>
      <description>
        <p>Shows recent your timeline. (The timeline will be cashed and expired 90 seconds after Twittperator get from Twitter.)</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!</spec>
      <description>
        <p>Gets recent your timeline from Twitter and shows it.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!@</spec>
      <description>
        <p>Shows mentions to you.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!@user</spec>
      <description>
        <p>Show @user's tweets.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> <a>TweetText</a></spec>
      <description>
        <p>Tweets <a>TweetText</a>.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> @user#id <a>TweetText</a></spec>
      <description>
        <p>Tweets a reply to @user.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> RT @user#id: <a>refTweet</a></spec>
      <description>
        <p>Does official retweet.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> <a>TweetText</a> RT @user#id: <a>refTweet</a></spec>
      <description>
        <p>Does classic retweet.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!+status_id</spec>
      <description>
        <p>Adds the tweet to your favorites.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!-status_id</spec>
      <description>
        <p>Delete the tweet from your favorites.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!?<a>SearchText</a></spec>
      <description>
        <p>Shows the result of searching <a>SearchText</a>.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!/<a>URI</a></spec>
      <description>
        <p>Opens <a>URI</a>.</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!delete <a>StatusID</a></spec>
      <description>
        <p>Deletes the <a>StatusID</a> tweet.</p>
      </description>
    </item>
    <h2>Authentication Setting</h2>
    First of all, you have to get your PIN from Twitter and signify it to Twittperator. Type a following command:
    <code>
        :tw -getPIN
    </code>
    and you will get the page to authorize Twittperator to access Twitter in a new tab.
    If you allow and you will get the PIN (7 digit numbers), then yank it.

    Secondarily, authorize Twittperator with your PIN.
    <code>
        :tw -setPIN yanked_PIN
    </code>
    <h2>FAQ</h2>
    <p>
    - What is this ?
    </p>
    <p>
        The plugin that just tweet with Vimperator.
    </p>
    <p>
    - My timeline is hard to see...?
    </p>
    <p>
        We are making an effort, and welcoming patches.
    </p>
    <p>
    - By the way, is it possible to show timeline automatically?
    </p>
    <p>
        Use chirpstream. Write the below line into rc file.
        let g:twittperator_use_chirp = 1
    </p>
    <p>
    - It's too much of the bother to show my timeline manually!!
    </p>
    <p>
        We think implementing a wider display method and a mean of word wrapping will solve this issue.
        Any ideas?
    </p>
    <p>
    - Is there a plan to work together Growl GNTP?
    </p>
    <p>
        Write the plugin.
    </p>
  </plugin>
  <plugin name="Twittperator" version="1.19.2"
          href="https://github.com/vimpr/vimperator-plugins/raw/master/twittperator.js"
          lang="ja"
          summary="OAuth/StreamingAPI対応Twitterクライアント">
    <author email="teramako@gmail.com" href="http://d.hatena.ne.jp/teramako/">teramako</author>
    <author email="anekos@snca.net" href="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
    <license>MIT License</license>
    <project name="Vimperator" minVersion="2.3"/>
    <p>
      これはOAuth認証を用いたTwitterクライアントプラグインです。
    </p>
    <h2>Command</h2>
      - 適当に補完しましょう。
    <item>
      <tags><![CDATA[:twittperator :tw]]></tags>
      <spec>:tw<oa>ittperator</oa> -getPIN</spec>
      <description>
        <p>PINコード取得ページを開きます。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> -setPIN <a>PINcode</a></spec>
      <description>
        <p>PINcodeを設定します。</p>
      </description>
    </item>

    <item>
      <spec>:tw<oa>ittperator</oa></spec>
      <description>
        <p>前回取得したタイムラインを表示します。 (キャッシュが90秒以上古い場合は再取得。)</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!</spec>
      <description>
        <p>強制的に取得したタイムラインを表示します。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!@</spec>
      <description>
        <p>あなたへの言及(mentions)表示します。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!@user</spec>
      <description>
        <p>@user のタイムラインを表示します。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> <a>TweetText</a></spec>
      <description>
        <p><a>TweetText</a>をポストします。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> @user#id <a>TweetText</a></spec>
      <description>
        <p>@user への返信になります。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> RT @user#id: <a>refTweet</a></spec>
      <description>
        <p>公式RTになるはずです。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa> <a>TweetText</a> RT @user#id: <a>refTweet</a></spec>
      <description>
        <p>非公式RTになるはずです。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!+status_id</spec>
      <description>
        <p>tweetをfavoriteします。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!-status_id</spec>
      <description>
        <p>tweetをunfavoriteします。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!?<a>SearchText</a></spec>
      <description>
        <p><a>SearchText</a>の検索結果を表示します。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!/<a>URI</a></spec>
      <description>
        <p><a>URI</a>を開きます。</p>
      </description>
    </item>
    <item>
      <spec>:tw<oa>ittperator</oa>!delete <a>StatusID</a></spec>
      <description>
        <p><a>StatusID</a>のツイートを削除します。</p>
      </description>
    </item>
    <h2>Authentication Setting</h2>
    最初にPINコードを取得し設定する必要があります。
    <code>
        :tw -getPIN
    </code>
    を実行すると新規タブに本アプリケーションを許可するかを問うページが開かれます。
    許可をすると、PINコード(数値)が表示されるのでコピーしてください。

    <code>
        :tw -setPIN コピーしたPINコード
    </code>
    で初期設定完了です。
    <h2>FAQ</h2>
    <p>
    - なんて読むんだ
    </p>
    <p>
        知らん。トゥイットゥペレータと自分は勝手に読んでいる。
    </p>
    <p>
    - 何のためのクライアント?
    </p>
    <p>
        Vimperatorを使っていて、さくっと呟きたいとき用です(ぉ
    </p>
    <p>
    - TL表示をもっと工夫しろ
    </p>
    <p>
        ごめんなさい。改良してコミットしてくれると嬉しいです。
    </p>
    <p>
    - つーか、TLくらい自動取得しろ
    </p>
    <p>
        はい、がんばりました。
        <code>let g:twittperator_use_chirp = 1</code>
        として、chirpstream を利用してください。
    </p>
    <p>
    - ぶっちゃけTL表示とか面倒だよね?
    </p>
    <p>
        はい、がんばります・・・
        でかい表示領域と行の折り返し方法が確立できれば、もっと頑張れる気がします。
    </p>
    <p>
    - Growl GNTP との連携しないの?
    </p>
    <p>
        プラグイン書きましょう。
    </p>
  </plugin>
`;

// }}}

(function() {

  // TwitterOauth for Greasemonkey {{{

  /*
   * http://efcl.info/2010/0610/res1721/
   *
   * The MIT License
   *
   * Copyright (c) 2010 azu
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   * THE SOFTWARE.
   */

  function TwitterOauth() {
      this.initialize.apply(this, arguments);
  }

  TwitterOauth.prototype = (function() {

    // {{{2 oauth.js
    /*
      OAuth.js
      SHA-1.js
      TwitterOauth for Greasemonkey
    */

    /*
     * Copyright 2008 Netflix, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    /* Here's some JavaScript software for implementing OAuth.

       This isn't as useful as you might hope.  OAuth is based around
       allowing tools and websites to talk to each other.  However,
       JavaScript running in web browsers is hampered by security
       restrictions that prevent code running on one website from
       accessing data stored or served on another.

       Before you start hacking, make sure you understand the limitations
       posed by cross-domain XMLHttpRequest.

       On the bright side, some platforms use JavaScript as their
       language, but enable the programmer to access other web sites.
       Examples include Google Gadgets, and Microsoft Vista Sidebar.
       For those platforms, this library should come in handy.
    */

    // The HMAC-SHA1 signature method calls b64_hmac_sha1, defined by
    // http://pajhome.org.uk/crypt/md5/sha1.js

    /* An OAuth message is represented as an object like this:
       {method: "GET", action: "http://server.com/path", parameters: ...}

       The parameters may be either a map {name: value, name2: value2}
       or an Array of name-value pairs [[name, value], [name2, value2]].
       The latter representation is more powerful: it supports parameters
       in a specific sequence, or several parameters with the same name;
       for example [["a", 1], ["b", 2], ["a", 3]].

       Parameter names and values are NOT percent-encoded in an object.
       They must be encoded before transmission and decoded after reception.
       For example, this message object:
       {method: "GET", action: "http://server/path", parameters: {p: "x y"}}
       ... can be transmitted as an HTTP request that begins:
       GET /path?p=x%20y HTTP/1.0
       (This isn't a valid OAuth request, since it lacks a signature etc.)
       Note that the object "x y" is transmitted as x%20y.  To encode
       parameters, you can call OAuth.addToURL, OAuth.formEncode or
       OAuth.getAuthorization.

       This message object model harmonizes with the browser object model for
       input elements of an form, whose value property isn't percent encoded.
       The browser encodes each value before transmitting it. For example,
       see consumer.setInputs in example/consumer.js.
     */

    /* This script needs to know what time it is. By default, it uses the local
       clock (new Date), which is apt to be inaccurate in browsers. To do
       better, you can load this script from a URL whose query string contains
       an oauth_timestamp parameter, whose value is a current Unix timestamp.
       For example, when generating the enclosing document using PHP:

       <script src="oauth.js?oauth_timestamp=<?=time()?>" ...

       Another option is to call OAuth.correctTimestamp with a Unix timestamp.
     */


    var OAuth; if (OAuth == null) OAuth = {};
    OAuth.setProperties = function setProperties(into, from) {
        if (into != null && from != null) {
            for (var key in from) {
                into[key] = from[key];
            }
        }
        return into;
    };

    OAuth.setProperties(OAuth, // utility functions
    {
        percentEncode: function percentEncode(s) {
            if (s == null) {
                return "";
            }
            if (s instanceof Array) {
                var e = "";
                for (var i = 0; i < s.length; ++s) {
                    if (e != "") e += "&";
                    e += OAuth.percentEncode(s[i]);
                }
                return e;
            }
            s = encodeURIComponent(s);
            // Now replace the values which encodeURIComponent doesn't do
            // encodeURIComponent ignores: - _ . ! ~ * ' ( )
            // OAuth dictates the only ones you can ignore are: - _ . ~
            // Source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent
            s = s.replace(/!/g, "%21");
            s = s.replace(/\*/g, "%2A");
            s = s.replace(/'/g, "%27");
            s = s.replace(/\(/g, "%28");
            s = s.replace(/\)/g, "%29");
            return s;
        },
        decodePercent: function decodePercent(s) {
            if (s != null) {
                // Handle application/x-www-form-urlencoded, which is defined by
                // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
                s = s.replace(/\+/g, " ");
            }
            return decodeURIComponent(s);
        },
        /** Convert the given parameters to an Array of name-value pairs. */
        getParameterList: function getParameterList(parameters) {
            if (parameters == null) {
                return [];
            }
            if (typeof parameters != "object") {
                return OAuth.decodeForm(parameters + "");
            }
            if (parameters instanceof Array) {
                return parameters;
            }
            var list = [];
            for (var p in parameters) {
                list.push([p, parameters[p]]);
            }
            return list;
        },
        /** Convert the given parameters to a map from name to value. */
        getParameterMap: function getParameterMap(parameters) {
            if (parameters == null) {
                return {};
            }
            if (typeof parameters != "object") {
                return OAuth.getParameterMap(OAuth.decodeForm(parameters + ""));
            }
            if (parameters instanceof Array) {
                var map = {};
                for (var p = 0; p < parameters.length; ++p) {
                    var key = parameters[p][0];
                    if (map[key] === undefined) { // first value wins
                        map[key] = parameters[p][1];
                    }
                }
                return map;
            }
            return parameters;
        },
        getParameter: function getParameter(parameters, name) {
            if (!parameters instanceof Array) {
                return OAuth.getParameterMap(parameters)[name];
            }
            for (var p = 0; p < parameters.length; ++p) {
                if (parameters[p][0] == name) {
                    return parameters[p][1]; // first value wins
                }
            }
            return null;
        },
        formEncode: function formEncode(parameters) {
            var form = "";
            var list = OAuth.getParameterList(parameters);
            for (var p = 0; p < list.length; ++p) {
                var value = list[p][1];
                if (value == null) value = "";
                if (form != "") form += "&";
                form += OAuth.percentEncode(list[p][0])
                  +"="+ OAuth.percentEncode(value);
            }
            return form;
        },
        decodeForm: function decodeForm(form) {
            var list = [];
            var nvps = form.split("&");
            for (var n = 0; n < nvps.length; ++n) {
                var nvp = nvps[n];
                if (nvp == "") {
                    continue;
                }
                var equals = nvp.indexOf("=");
                var name;
                var value;
                if (equals < 0) {
                    name = OAuth.decodePercent(nvp);
                    value = null;
                } else {
                    name = OAuth.decodePercent(nvp.substring(0, equals));
                    value = OAuth.decodePercent(nvp.substring(equals + 1));
                }
                list.push([name, value]);
            }
            return list;
        },
        setParameter: function setParameter(message, name, value) {
            var parameters = message.parameters;
            if (parameters instanceof Array) {
                for (var p = 0; p < parameters.length; ++p) {
                    if (parameters[p][0] == name) {
                        if (value === undefined) {
                            parameters.splice(p, 1);
                        } else {
                            parameters[p][1] = value;
                            value = undefined;
                        }
                    }
                }
                if (value !== undefined) {
                    parameters.push([name, value]);
                }
            } else {
                parameters = OAuth.getParameterMap(parameters);
                parameters[name] = value;
                message.parameters = parameters;
            }
        },
        setParameters: function setParameters(message, parameters) {
            var list = OAuth.getParameterList(parameters);
            for (var i = 0; i < list.length; ++i) {
                OAuth.setParameter(message, list[i][0], list[i][1]);
            }
        },
        /** Fill in parameters to help construct a request message.
            This function doesn't fill in every parameter.
            The accessor object should be like:
            { consumerKey: "foo", consumerSecret: "bar", accessorSecret: "nurn",
              token: "krelm", tokenSecret: "blah" }
            The accessorSecret property is optional.
         */
        completeRequest: function completeRequest(message, accessor) {
            if (message.method == null) {
                message.method = "GET";
            }
            var map = OAuth.getParameterMap(message.parameters);
            if (map.oauth_consumer_key == null) {
                OAuth.setParameter(message, "oauth_consumer_key", accessor.consumerKey || "");
            }
            if (map.oauth_token == null && accessor.token != null) {
                OAuth.setParameter(message, "oauth_token", accessor.token);
            }
            if (map.oauth_version == null) {
                OAuth.setParameter(message, "oauth_version", "1.0");
            }
            if (map.oauth_timestamp == null) {
                OAuth.setParameter(message, "oauth_timestamp", OAuth.timestamp());
            }
            if (map.oauth_nonce == null) {
                OAuth.setParameter(message, "oauth_nonce", OAuth.nonce(6));
            }
            OAuth.SignatureMethod.sign(message, accessor);
        },
        setTimestampAndNonce: function setTimestampAndNonce(message) {
            OAuth.setParameter(message, "oauth_timestamp", OAuth.timestamp());
            OAuth.setParameter(message, "oauth_nonce", OAuth.nonce(6));
        },
        addToURL: function addToURL(url, parameters) {
            newURL = url;
            if (parameters != null) {
                var toAdd = OAuth.formEncode(parameters);
                if (toAdd.length > 0) {
                    var q = url.indexOf("?");
                    if (q < 0) newURL += "?";
                    else       newURL += "&";
                    newURL += toAdd;
                }
            }
            return newURL;
        },
        /** Construct the value of the Authorization header for an HTTP request. */
        getAuthorizationHeader: function getAuthorizationHeader(realm, parameters) {
            var header = 'OAuth realm="' + OAuth.percentEncode(realm) + '"';
            var list = OAuth.getParameterList(parameters);
            for (var p = 0; p < list.length; ++p) {
                var parameter = list[p];
                var name = parameter[0];
                if (name.indexOf("oauth_") == 0) {
                    header += "," + OAuth.percentEncode(name) + '="' + OAuth.percentEncode(parameter[1]) + '"';
                }
            }
            return header;
        },
        /** Correct the time using a parameter from the URL from which the last script was loaded. */
        correctTimestampFromSrc: function correctTimestampFromSrc(parameterName) {
            parameterName = parameterName || "oauth_timestamp";
            var scripts = document.getElementsByTagName("script");
            if (scripts == null || !scripts.length) return;
            var src = scripts[scripts.length-1].src;
            if (!src) return;
            var q = src.indexOf("?");
            if (q < 0) return;
            parameters = OAuth.getParameterMap(OAuth.decodeForm(src.substring(q+1)));
            var t = parameters[parameterName];
            if (t == null) return;
            OAuth.correctTimestamp(t);
        },
        /** Generate timestamps starting with the given value. */
        correctTimestamp: function correctTimestamp(timestamp) {
            OAuth.timeCorrectionMsec = (timestamp * 1000) - (new Date()).getTime();
        },
        /** The difference between the correct time and my clock. */
        timeCorrectionMsec: 0,
        timestamp: function timestamp() {
            var t = (new Date()).getTime() + OAuth.timeCorrectionMsec;
            return Math.floor(t / 1000);
        },
        nonce: function nonce(length) {
            var chars = OAuth.nonce.CHARS;
            var result = "";
            for (var i = 0; i < length; ++i) {
                var rnum = Math.floor(Math.random() * chars.length);
                result += chars.substring(rnum, rnum+1);
            }
            return result;
        }
    });

    OAuth.nonce.CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";

    /** Define a constructor function,
        without causing trouble to anyone who was using it as a namespace.
        That is, if parent[name] already existed and had properties,
        copy those properties into the new constructor.
     */
    OAuth.declareClass = function declareClass(parent, name, newConstructor) {
        var previous = parent[name];
        parent[name] = newConstructor;
        if (newConstructor != null && previous != null) {
            for (var key in previous) {
                if (key != "prototype") {
                    newConstructor[key] = previous[key];
                }
            }
        }
        return newConstructor;
    }

    /** An abstract algorithm for signing messages. */
    OAuth.declareClass(OAuth, "SignatureMethod", function OAuthSignatureMethod() {});

    OAuth.setProperties(OAuth.SignatureMethod.prototype, // instance members
    {
        /** Add a signature to the message. */
        sign: function sign(message) {
            var baseString = OAuth.SignatureMethod.getBaseString(message);
            var signature = this.getSignature(baseString);
            OAuth.setParameter(message, "oauth_signature", signature);
            return signature; // just in case someone's interested
        },
        /** Set the key string for signing. */
        initialize: function initialize(name, accessor) {
            var consumerSecret;
            if (accessor.accessorSecret != null
                && name.length > 9
                && name.substring(name.length-9) == "-Accessor")
            {
                consumerSecret = accessor.accessorSecret;
            } else {
                consumerSecret = accessor.consumerSecret;
            }
            this.key = OAuth.percentEncode(consumerSecret)
                 +"&"+ OAuth.percentEncode(accessor.tokenSecret);
        }
    });

    /* SignatureMethod expects an accessor object to be like this:
         { tokenSecret: "lakjsdflkj...", consumerSecret: "QOUEWRI..",
           accessorSecret: "xcmvzc..." }
       The accessorSecret property is optional.
     */
    // Class members:
    OAuth.setProperties(OAuth.SignatureMethod, // class members
    {
        sign: function sign(message, accessor) {
            var name = OAuth.getParameterMap(message.parameters).oauth_signature_method;
            if (name == null || name == "") {
                name = "HMAC-SHA1";
                OAuth.setParameter(message, "oauth_signature_method", name);
            }
            OAuth.SignatureMethod.newMethod(name, accessor).sign(message);
        },
        /** Instantiate a SignatureMethod for the given method name. */
        newMethod: function newMethod(name, accessor) {
            var impl = OAuth.SignatureMethod.REGISTERED[name];
            if (impl != null) {
                var method = new impl();
                method.initialize(name, accessor);
                return method;
            }
            var err = new Error("signature_method_rejected");
            var acceptable = "";
            for (var r in OAuth.SignatureMethod.REGISTERED) {
                if (acceptable != "") acceptable += "&";
                acceptable += OAuth.percentEncode(r);
            }
            err.oauth_acceptable_signature_methods = acceptable;
            throw err;
        },
        /** A map from signature method name to constructor. */
        REGISTERED: {},
        /** Subsequently, the given constructor will be used for the named methods.
            The constructor will be called with no parameters.
            The resulting object should usually implement getSignature(baseString).
            You can easily define such a constructor by calling makeSubclass, below.
         */
        registerMethodClass: function registerMethodClass(names, classConstructor) {
            for (var n = 0; n < names.length; ++n) {
                OAuth.SignatureMethod.REGISTERED[names[n]] = classConstructor;
            }
        },
        /** Create a subclass of OAuth.SignatureMethod, with the given getSignature function. */
        makeSubclass: function makeSubclass(getSignatureFunction) {
            var superClass = OAuth.SignatureMethod;
            var subClass = function() {
                superClass.call(this);
            }
            subClass.prototype = new superClass();
            // Delete instance variables from prototype:
            // delete subclass.prototype... There aren't any.
            subClass.prototype.getSignature = getSignatureFunction;
            subClass.prototype.constructor = subClass;
            return subClass;
        },
        getBaseString: function getBaseString(message) {
            var URL = message.action;
            var q = URL.indexOf("?");
            var parameters;
            if (q < 0) {
                parameters = message.parameters;
            } else {
                // Combine the URL query string with the other parameters:
                parameters = OAuth.decodeForm(URL.substring(q + 1));
                var toAdd = OAuth.getParameterList(message.parameters);
                for (var a = 0; a < toAdd.length; ++a) {
                    parameters.push(toAdd[a]);
                }
            }
            return OAuth.percentEncode(message.method.toUpperCase())
             +"&"+ OAuth.percentEncode(OAuth.SignatureMethod.normalizeUrl(URL))
             +"&"+ OAuth.percentEncode(OAuth.SignatureMethod.normalizeParameters(parameters));
        },
        normalizeUrl: function normalizeUrl(url) {
            var uri = OAuth.SignatureMethod.parseUri(url);
            var scheme = uri.protocol.toLowerCase();
            var authority = uri.authority.toLowerCase();
            var dropPort = (scheme == "http" && uri.port == 80)
                        || (scheme == "https" && uri.port == 443);
            if (dropPort) {
                // find the last ":" in the authority
                var index = authority.lastIndexOf(":");
                if (index >= 0) {
                    authority = authority.substring(0, index);
                }
            }
            var path = uri.path;
            if (!path) {
                path = "/"; // conforms to RFC 2616 section 3.2.2
            }
            // we know that there is no query and no fragment here.
            return scheme + "://" + authority + path;
        },
        parseUri: function parseUri(str) {
            /* This function was adapted from parseUri 1.2.1
               http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
             */
            var o = {
              key: ["source", "protocol", "authority", "userInfo", "user",
                    "password", "host", "port", "relative", "path", "directory",
                    "file", "query", "anchor"],
              parser: { strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/ }
            };
            var m = o.parser.strict.exec(str);
            var uri = {};
            var i = 14;
            while (i--) uri[o.key[i]] = m[i] || "";
            return uri;
        },
        normalizeParameters: function normalizeParameters(parameters) {
            if (parameters == null) {
                return "";
            }
            var list = OAuth.getParameterList(parameters);
            var sortable = [];
            for (var p = 0; p < list.length; ++p) {
                var nvp = list[p];
                if (nvp[0] != "oauth_signature") {
                    sortable.push([ OAuth.percentEncode(nvp[0])
                                  + " " // because it comes before any character that can appear in a percentEncoded string.
                                  + OAuth.percentEncode(nvp[1]),
                                  nvp]);
                }
            }
            sortable.sort(function(a, b) {
                              if (a[0] < b[0]) return -1;
                              if (a[0] > b[0]) return 1;
                              return 0;
                          });
            var sorted = [];
            for (var s = 0; s < sortable.length; ++s) {
                sorted.push(sortable[s][1]);
            }
            return OAuth.formEncode(sorted);
        }
    });

    OAuth.SignatureMethod.registerMethodClass(["PLAINTEXT", "PLAINTEXT-Accessor"],
        OAuth.SignatureMethod.makeSubclass(function getSignature(baseString) this.key));

    OAuth.SignatureMethod.registerMethodClass(["HMAC-SHA1", "HMAC-SHA1-Accessor"],
        OAuth.SignatureMethod.makeSubclass(function getSignature(baseString) {
            b64pad = "=";
            var signature = b64_hmac_sha1(this.key, baseString);
            return signature;
        }));

    // oauth.js 2}}}
    // {{{2 sha1.js
    /*
     * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
     * in FIPS PUB 180-1
     * Version 2.1a Copyright Paul Johnston 2000 - 2002.
     * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
     * Distributed under the BSD License
     * See http://pajhome.org.uk/crypt/md5 for details.
     */

    /*
     * Configurable variables. You may need to tweak these to be compatible with
     * the server-side, but the defaults work in most cases.
     */
    var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
    var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
    var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

    /*
     * These are the functions you'll usually want to call
     * They take string arguments and return either hex or base-64 encoded strings
     */
    function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
    function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
    function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
    function hex_hmac_sha1(key, data){return binb2hex(core_hmac_sha1(key, data));}
    function b64_hmac_sha1(key, data){return binb2b64(core_hmac_sha1(key, data));}
    function str_hmac_sha1(key, data){return binb2str(core_hmac_sha1(key, data));}

    /*
     * Perform a simple self-test to see if the VM is working
     */
    function sha1_vm_test()
    {
      return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
    }

    /*
     * Calculate the SHA-1 of an array of big-endian words, and a bit length
     */
    function core_sha1(x, len)
    {
      /* append padding */
      x[len >> 5] |= 0x80 << (24 - len % 32);
      x[((len + 64 >> 9) << 4) + 15] = len;

      var w = Array(80);
      var a =  1732584193;
      var b = -271733879;
      var c = -1732584194;
      var d =  271733878;
      var e = -1009589776;

      for (var i = 0; i < x.length; i += 16)
      {
        var olda = a;
        var oldb = b;
        var oldc = c;
        var oldd = d;
        var olde = e;

        for (var j = 0; j < 80; j++)
        {
          if (j < 16) w[j] = x[i + j];
          else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
          var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
                           safe_add(safe_add(e, w[j]), sha1_kt(j)));
          e = d;
          d = c;
          c = rol(b, 30);
          b = a;
          a = t;
        }

        a = safe_add(a, olda);
        b = safe_add(b, oldb);
        c = safe_add(c, oldc);
        d = safe_add(d, oldd);
        e = safe_add(e, olde);
      }
      return Array(a, b, c, d, e);

    }

    /*
     * Perform the appropriate triplet combination function for the current
     * iteration
     */
    function sha1_ft(t, b, c, d)
    {
      if (t < 20) return (b & c) | ((~b) & d);
      if (t < 40) return b ^ c ^ d;
      if (t < 60) return (b & c) | (b & d) | (c & d);
      return b ^ c ^ d;
    }

    /*
     * Determine the appropriate additive constant for the current iteration
     */
    function sha1_kt(t)
    {
      return (t < 20) ?  1518500249 : (t < 40) ? 1859775393 :
             (t < 60) ? -1894007588 : -899497514;
    }

    /*
     * Calculate the HMAC-SHA1 of a key and some data
     */
    function core_hmac_sha1(key, data)
    {
      var bkey = str2binb(key);
      if (bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);

      var ipad = Array(16), opad = Array(16);
      for (var i = 0; i < 16; i++)
      {
        ipad[i] = bkey[i] ^ 0x36363636;
        opad[i] = bkey[i] ^ 0x5C5C5C5C;
      }

      var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
      return core_sha1(opad.concat(hash), 512 + 160);
    }

    /*
     * Add integers, wrapping at 2^32. This uses 16-bit operations internally
     * to work around bugs in some JS interpreters.
     */
    function safe_add(x, y)
    {
      var lsw = (x & 0xFFFF) + (y & 0xFFFF);
      var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
      return (msw << 16) | (lsw & 0xFFFF);
    }

    /*
     * Bitwise rotate a 32-bit number to the left.
     */
    function rol(num, cnt)
    {
      return (num << cnt) | (num >>> (32 - cnt));
    }

    /*
     * Convert an 8-bit or 16-bit string to an array of big-endian words
     * In 8-bit function, characters >255 have their hi-byte silently ignored.
     */
    function str2binb(str)
    {
      var bin = Array();
      var mask = (1 << chrsz) - 1;
      for (var i = 0; i < str.length * chrsz; i += chrsz)
        bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
      return bin;
    }

    /*
     * Convert an array of big-endian words to a string
     */
    function binb2str(bin)
    {
      var str = "";
      var mask = (1 << chrsz) - 1;
      for (var i = 0; i < bin.length * 32; i += chrsz)
        str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
      return str;
    }

    /*
     * Convert an array of big-endian words to a hex string.
     */
    function binb2hex(binarray)
    {
      var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
      var str = "";
      for (var i = 0; i < binarray.length * 4; i++)
      {
        str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
               hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
      }
      return str;
    }

    /*
     * Convert an array of big-endian words to a base-64 string
     */
    function binb2b64(binarray)
    {
      var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      var str = "";
      for (var i = 0; i < binarray.length * 4; i += 3)
      {
        var triplet = (((binarray[i   >> 2] >> 8 * (3 -  i   %4)) & 0xFF) << 16)
                    | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
                    |  ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
        for (var j = 0; j < 4; j++)
        {
          if (i * 8 + j * 6 > binarray.length * 32) str += b64pad;
          else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
        }
      }
      return str;
    }
    // sha1.js 2}}}

    let p = {
      initialize: function(accessor) {
        if (accessor)
          this.accessor = accessor;
        if (this.isAuthorize()) {
          setup();
        } else {
          preSetup();
        }
      },
      // temp for request
      request: {
        token :"", // response oauth_token
        tokenSecret: ""// response oauth_token_secret
      },
      // トークンが取得済みかの真偽値を返す
      isAuthorize: function() {
        let accessor = this.getAccessor();
        if (accessor.consumerKey && accessor.consumerSecret &&
            accessor.token && accessor.tokenSecret) {
          return true;
        }
        return false;
      },
      getAccessor: function() {
        return {
          consumerKey: this.accessor.get("consumerKey", ""),
          consumerSecret: this.accessor.get("consumerSecret", ""),
          token: this.accessor.get("token", ""),
          tokenSecret: this.accessor.get("tokenSecret", "")
        };
      },
      deleteAccessor: function() {
        var clientInfo = {
          clientName: this.accessor.get("clientName", ""),
          consumerKey: this.accessor.get("consumerKey", ""),
          consumerSecret: this.accessor.get("consumerSecret", ""),
        };
        this.accessor.clear();
        this.accessor.set("clientName", clientInfo.clientName);
        this.accessor.set("consumerKey", clientInfo.consumerKey);
        this.accessor.set("consumerSecret", clientInfo.consumerSecret);
      },
      // 認証ページのURLを取得
      getRequestToken: function(callback) {
        let message = {
          method: "GET",
          action: "https://twitter.com/oauth/request_token",
          parameters: {
            oauth_signature_method: "HMAC-SHA1",
            oauth_consumer_key: this.accessor.get("consumerKey", "")
          }
        };
        OAuth.setTimestampAndNonce(message);
        OAuth.SignatureMethod.sign(message, this.getAccessor());
        var target = OAuth.addToURL(message.action, message.parameters);
        var self = this;
        var options = {
          method: message.method,
          url: target,
          onload: function(d) {
            if (d.status == 200) {
              var res = d.responseText;
              var parameter = self.getParameter(res);
              self.request.token = parameter["oauth_token"];
              self.request.tokenSecret = parameter["oauth_token_secret"];
              // requestURLを引数にcallback
              if (callback) {
                callback("https://twitter.com/oauth/authorize?oauth_token="+self.request.token);
              }
            }else{
              alert(d.statusText);
            }
          },
        };
        Utils.xmlhttpRequest(options);

      },
      setPin: function(pin) {
        let self = this;
        this.getAccessToken(pin, function() {
          Twittperator.echo("getting access token is success.");
          self.initialize();
        });
      },
      // PINを元にaccess tokenを取得して保存、callbackにはaccessorオブジェクトを渡す
      getAccessToken: function(pin, callback) {
        var message = {
          method: "GET",
          action: "https://twitter.com/oauth/access_token",
          parameters: {
            oauth_signature_method: "HMAC-SHA1",
            oauth_consumer_key: this.accessor.get("consumerKey", ""),
            oauth_token: this.request.token, // Request Token
            oauth_verifier: pin
          }
        };
        OAuth.setTimestampAndNonce(message);
        OAuth.SignatureMethod.sign(message, this.request);
        var target = OAuth.addToURL(message.action, message.parameters);
        var self = this;
        var options = {
          method: message.method,
          url: target,
          onload: function(d) {
            if (d.status == 200) {
              /* 返り値からaccess token/access token secretを取り出す */
              var res = d.responseText;
              var parameter = self.getParameter(res);
              self.accessor.set("token", parameter["oauth_token"]);
              self.accessor.set("tokenSecret", parameter["oauth_token_secret"]);
              // Accessorの保存
              //self.saveAccessor();
              if (callback) {
                callback(self.accessor);
              }
            }else{
              window.alert(d.statusText);
            }
          },
        };

        Utils.xmlhttpRequest(options); // 送信
      },
      // api+?+query にアクセスした結果をcallbackに渡す
      get: function(api, query, callback) {
        var btquery =  query ? "?" + this.buildQuery(query) : "";
        var message = {
          method: "GET",
          action: setting.getAPIURL(api + btquery),
          parameters: {
            oauth_signature_method: "HMAC-SHA1",
            oauth_consumer_key: this.accessor.get("consumerKey", ""), // Queryの構築
            oauth_token: this.accessor.get("token", "") // Access token
          }
        };
        OAuth.setTimestampAndNonce(message);
        OAuth.SignatureMethod.sign(message, this.getAccessor());
        var target = OAuth.addToURL(message.action, message.parameters);
        var options = {
          method: message.method,
          url: target,
          onload: function(d) {
            if (d.status == 200) {
              if (callback) {
                  callback(d.responseText);
              }
            }else{
              throw d.statusText || d.toString();
            }
          },
        };
        Utils.xmlhttpRequest(options); // 送信
      },
      post: function(api, content, callback) {
        var message = {
          method: "POST",
          action: setting.getAPIURL(api),
          parameters: {
            oauth_signature_method: "HMAC-SHA1",
            oauth_consumer_key: this.accessor.get("consumerKey", ""),
            oauth_token: this.accessor.get("token", "") // Access token
          }
        };
        // 送信するデータをパラメータに追加する
        for (var key in content) {
          message.parameters[key] = content[key];
        }
        OAuth.setTimestampAndNonce(message);
        OAuth.SignatureMethod.sign(message, this.getAccessor());
        var target = OAuth.addToURL(message.action, message.parameters);
        var options = {
          method: message.method,
          url: target,
          onload: function(d) {
            if (d.status == 200) {
              if (callback) {
                  callback(d.responseText);
              }
            } else {
              // typeof d == object
              throw d.statusText || d.toString();
            }
          }
        };
        Utils.xmlhttpRequest(options); // 送信
      },
      delete: function(api, query, callback) {
        var btquery =  query ? "?" + this.buildQuery(query) : "";
        var message = {
          method: "DELETE",
          action: setting.getAPIURL(api + btquery),
          parameters: {
            oauth_signature_method: "HMAC-SHA1",
            oauth_consumer_key: this.accessor.get("consumerKey", ""), // Queryの構築
            oauth_token: this.accessor.get("token", "") // Access token
          }
        };
        OAuth.setTimestampAndNonce(message);
        OAuth.SignatureMethod.sign(message, this.getAccessor());
        var target = OAuth.addToURL(message.action, message.parameters);
        var options = {
          method: message.method,
          url: target,
          onload: function(d) {
            if (d.status == 200) {
              if (callback) {
                  callback(d.responseText);
              }
            }else{
              throw d.statusText;
            }
          },
        };
        Utils.xmlhttpRequest(options); // 送信
      },
      getAuthorizationHeader: function(api) {
        var message = {
          method: "GET",
          action: api,
          parameters: {
            oauth_signature_method: "HMAC-SHA1",
            oauth_consumer_key: this.accessor.get("consumerKey", ""), // Queryの構築
            oauth_token: this.accessor.get("token", ""), // Access token
            oauth_version: "1.0"
          }
        };
        OAuth.setTimestampAndNonce(message);
        OAuth.SignatureMethod.sign(message, this.getAccessor());
        return OAuth.getAuthorizationHeader("Twitter", message.parameters);
      },
      // Utility関数
      // http://kevin.vanzonneveld.net
      urlEncode: function(str) {
        str = (str+"").toString();
        return encodeURIComponent(str).replace(/!/g, "%21").replace(/"/g, "%27").replace(/\(/g, "%28")
                                      .replace(/\)/g, "%29").replace(/\*/g, "%2A").replace(/%20/g, "+");
      },
      // オブジェクトからクエリを生成
      buildQuery: function(formdata, numeric_prefix, arg_separator) {
        // * example 1: http_build_query({ foo: "bar", php: "Hypertext Processor", baz: "boom", cow: "milk" }, "", "&amp;");
        // * returns 1: "foo=bar&amp;php=Hypertext+Processor&amp;baz=boom&amp;cow=milk"
        // * example 2: http_build_query({ "PHP": "Hypertext Processor", 0: "foo", 1: "bar", 2: "baz", 3: "boom", "cow": "milk" }, "myvar_");
        // * returns 2: "PHP=Hypertext+Processor&myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk"
        var value, key, tmp = [];
        var self = this;
        var _http_build_query_helper = function(key, val, arg_separator) {
          var k, tmp = [];
          if (val === true) {
            val = "1";
          } else if (val === false) {
            val = "0";
          }
          if (val !== null && typeof val === "object") {
            for (k in val) {
              if (val[k] !== null) {
                tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator));
              }
            }
            return tmp.join(arg_separator);
          }
          if (typeof val !== "function") {
            return self.urlEncode(key) + "=" + self.urlEncode(val);
          }
          throw new Error("There was an error processing for http_build_query().");
        }

        if (!arg_separator) {
          arg_separator = "&";
        }
        for (key in formdata) {
          value = formdata[key];
          if (numeric_prefix && !isNaN(key)) {
            key = String(numeric_prefix) + key;
          }
          tmp.push(_http_build_query_helper(key, value, arg_separator));
        }

        return tmp.join(arg_separator);
      },
      // Query string から 連想配列を返す
      getParameter: function(str) {
        if (typeof str == "undefined") return {};
        var itm;
        if (str.indexOf("?", 0) > -1) str = str.split("?", 2)[1];
        return str.split("&").reduce(function(r, it) {
          var [key, value] = it.split("=", 2);
          if (key)
            r[key] = typeof value == "undefined" || decodeURIComponent(value);
          return r;
        }, {});
      }
    };
    return p;
  })();
  {//jsonGet jsonPost jsonDelete の動的定義
    'get post delete'.split(' ').forEach(function (name) {
      let newName =
        'json' + name.replace(/^(.)(.*)/ , function(_, a, b) (a.toUpperCase() + b));
      TwitterOauth.prototype[newName] =
        function (url, query, callback, onError) {
          return TwitterOauth.prototype[name].call(
            this,
            url + '.json',
            query,
            function (text) {
              let json;
              try {
                json = JSON.parse(text);
              } catch (e) {
                (onError
                 ||
                 function(e) {
                   liberator.echoerr('Twitter API Error: ' + url);
                   liberator.log(text);
                   throw e;
                 })(e);
              }
              return callback(json);
            }
          );
        };
    });
  }
  // }}}

  // Twittperator
  function HTTPConnection(url, options) { // {{{
    const ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);

    this.canceled = false;
    this.events = {};

    this.channel =
      ioService.newChannelFromURI(ioService.newURI(url, null, null)).QueryInterface(Ci.nsIHttpChannel);
    this.channel.notificationCallbacks = this;

    if (options) {
      if ("headers" in options) {
        for (let [n, v] in Iterator(options.headers))
          this.channel.setRequestHeader(n, v, true);
      }

      for (let [n, v] in Iterator(options)) {
        if (/^on[A-Z]/.test(n) && (v instanceof Function))
          this.events[n.toLowerCase()] = v;
      }

      this.onRecv = options.onReceive;
    }

    this.channel.asyncOpen(this, null);
  }
  HTTPConnection.prototype = {
    callEvent: function(name) {
      name = name.toLowerCase();
      if (name in this.events)
        return this.events[name].apply(this, Array.slice(arguments, 1));
    },

    cancel: function() {
      if (this.channel){
        this.canceled = true;
        return this.channel.cancel(Cr.NS_ERROR_NOT_AVAILABLE);
      }
    },

    // 実装しないと例外になるメソッドとか
    onStartRequest: function(request, context) {},
    onProgress : function(request, context, progress, progressMax) {},
    onStatus : function(request, context, status, statusArg) {},
    onRedirect : function(oldChannel, newChannel) {},

    onDataAvailable: function(request, context, stream, sourceOffset, length) {
      let inputStream =
        Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
      inputStream.init(stream);
      let data = inputStream.read(length);
      this.callEvent("onReceive", data);
    },

    onStopRequest: function(request, context, status) {
      if (Components.isSuccessCode(status)) {
        this.callEvent("onComplete");
      } else {
        if (!this.canceled)
          this.callEvent("onError");
      }
      delete this.channel;
    },

    onChannelRedirect: function(oldChannel, newChannel, flags) {
      this.channel = newChannel;
    },

    // nsIInterfaceRequestor
    getInterface: function(IID) {
      try {
        return this.QueryInterface(IID);
      } catch (e) {
        throw Components.results.NS_NOINTERFACE;
      }
    },

    // XPCOM インターフェイスに見せかけているので、QI を実装する必要がある
    QueryInterface : function(IID) {
      if (IID.equals(Ci.nsISupports) ||
          IID.equals(Ci.nsIInterfaceRequestor) ||
          IID.equals(Ci.nsIChannelEventSink) ||
          IID.equals(Ci.nsIProgressEventSink) ||
          IID.equals(Ci.nsIHttpEventSink) ||
          IID.equals(Ci.nsIStreamListener))
        return this;

      throw Components.results.NS_NOINTERFACE;
    }
  }; // }}}
  function Stream({ name, url }) { // {{{
    let connection;
    let restartCount = 0;
    let lastParams;
    let listeners = [];
    let retryTimer;

    function clearRetryTimer() {
      if (retryTimer)
        clearTimeout(retryTimer);
      retryTimer = null;
    }

    function updateRetryTimer(restartFunc) {
      clearRetryTimer();
      retryTimer = setTimeout(restartFunc, 60 * 1000);
    }

    function restart() {
      stop();
      if (restartCount > 13)
        return liberator.echoerr("Twittperator: Gave up to connect to " + name + "...");
      liberator.echoerr("Twittperator: " + name + " will be restarted...");
      // 試行済み回数^2 秒後にリトライ
      setTimeout(function() start(lastParams), Math.pow(2, restartCount) * 1000);
      restartCount++;
    }

    function stop() {
      clearRetryTimer();
      if (!connection)
        return;
      connection.cancel();
      liberator.log("Twittperator: stop " + name);
    }

    function start(params) {
      stop();

      liberator.log("Twittperator: start " + name);

      lastParams = params;

      let useProxy = !!setting.proxyHost;
      let requestUrl = url;

      if (params) {
        // XXX Twitter がなぜか + を許容しない気がする(401 を返す)ので、再変換する
        let query = tw.buildQuery(params).replace(/\+/g, "%20");
        requestUrl += '?' + query;
      }

      let authHeader = tw.getAuthorizationHeader(requestUrl);
      let buf = "";

      let onReceive = function(data) {
        try {
          updateRetryTimer(restart);
          let lines = data.split(/\r\n|[\r\n]/);
          if (lines.length >= 2) {
            restartCount = 0;
            lines[0] = buf + lines[0];
            for (let [, line] in Iterator(lines.slice(0, -1))) {
              try {
                if (/^\s*\{/.test(line))
                  onMsg(Utils.fixStatusObject(JSON.parse(line)), line);
              } catch (e) { liberator.log(e); }
            }
            buf = lines.slice(-1)[0];
          } else {
            buf += data;
          }
        } catch (e) {
          liberator.echoerr("Twittperator: Unknown error on " + name + " connection: " + e.name);
        }
      };

      connection = new HTTPConnection(
        requestUrl,
        {
          headers: {
            Authorization: authHeader,
          },
          onReceive: onReceive,
          onError: restart
        }
      );
    }

    function onMsg(msg, raw) {
      listeners.forEach(function(listener) liberator.trapErrors(function() listener(msg, raw)));

      if (msg.text)
        Twittperator.onMessage(msg);

      if (msg.friends)
        Twittperator.onFriends(msg);
    }

    function clearPluginData() {
      listeners = [];
    }

    return {
      start: start,
      stop: stop,
      resetRestartCount: function () (restartCount = 0),
      addListener: function(func) listeners.push(func),
      removeListener: function(func) (listeners = listeners.filter(function(l) (l != func))),
      clearPluginData: clearPluginData
    };
  }; // }}}
  let Twitter = { // {{{
    destroy: function(id) { // {{{
      tw.jsonPost("statuses/destroy/" + id, null, function(res) {
        res = Utils.fixStatusObject(res);
        Twittperator.echo("delete: " + res.user.name + " " + res.text)
      });
    }, // }}}
    favorite: function(id) { // {{{
      tw.jsonPost("favorites/create", {id: id}, function(res) {
        res = Utils.fixStatusObject(res);
        Twittperator.echo("fav: " + res.user.name + " " + res.text)
      });
    }, // }}}
    getUserTimeline: function(target, onload) { // {{{
      let [api, query] = target ? ["statuses/user_timeline", {screen_name: target, count: setting.count}]
                                : ["statuses/home_timeline", {count: setting.count}];

      tw.jsonGet(
        api,
        query,
        function(res) {
          let result = res.map(Utils.fixStatusObject);
          let lastHistory = history[0];
          if (lastHistory) {
            let lastDate = Date.parse(lastHistory.created_at);
            for (let [,msg] in Iterator(result.reverse())) {
              if (Date.parse(msg.created_at) > lastDate)
                history.unshift(msg);
            }
          } else {
            result.forEach(function(msg) history.push(msg));
          }
          onload(result);
        }
      );
    }, // }}}
    lookupUser: function({screenNames, ids}, callback) { // {{{
      tw.jsonGet("users/lookup", { user_id: String(ids || []), screen_name: String(screenNames || []) }, callback);
    }, // }}}
    say: function(status, inReplyToStatusId) { // {{{
      let sendData = {status: status, source: "Twittperator"};
      if (inReplyToStatusId)
        sendData.in_reply_to_status_id = inReplyToStatusId;
      tw.jsonPost("statuses/update", sendData, function(res) {
        let t = Utils.fixStatusObject(res).text;
        Twittperator.echo("Your post " + '"' + t + '" (' + t.length + " characters) was sent.");
      });
    }, // }}}
    retweet: function(id) { // {{{
      let url = "statuses/retweet/" + id;
      tw.jsonPost(url, null, function(res) {
        res = Utils.fixStatusObject(res);
        Twittperator.echo("Retweet: " + res.retweeted_status.text);
      });
    }, // }}}
    unfavorite: function(id) { // {{{
      tw.jsonDelete("favorites/destroy/" + id, null, function(res) {
        res = Utils.fixStatusObject(res);
        Twittperator.echo("unfav: " + res.user.name + " " + res.text, true);
      });
    }, // }}}
    searchUsers: function(word, callback) { // {{{
      tw.jsonGet("users/search", { q: word }, callback);
    }, // }}}
    searchTweets: function(word, callback) { // {{{
      tw.jsonGet("search/tweets", { q: word }, callback);
    } // }}}
  }; // }}}
  let Utils = { // {{{
    anchorLink: function(str) { // {{{
      let m = str.match(/https?:\/\/\S+|@[a-zA-Z0-9_]+/);
      if (m) {
        let left = str.substr(0, m.index);
        let center = m[0];
        let [head, tail] = [center[0], center.slice(1)];
        let right = str.substring(m.index + m[0].length);
        let content = head === "@" ? xml`<a highlight="URL" href=${setting.showTLURLScheme + "://twitter.com/" + tail}> ${center} </a>`
                                   : xml`<a highlight="URL" href=${center}> ${center} </a>`;
        return xml`${Utils.anchorLink(left)}${content}${Utils.anchorLink(right)}`;
      }
      return str;
    }, // }}}
    fixStatusObject: function(st) { // {{{
      const ENTITIY_PAIRS = {
        lt: "<",
        gt: ">",
        quot: '"',
        hellip: "\u2026"
      };

      function unescapeSpecificEntities(str)
        str.replace(/&([a-z\d]+);/g, function(m, n) ENTITIY_PAIRS[n] || m);

      function unescapeBrakets(str)
        str.replace(/&lt;/g, "<").replace(/&gt;/g, ">");

      function fixId (obj, name) {
        let nameStr = name + "_str";
        if (obj.hasOwnProperty(name) && obj.hasOwnProperty(nameStr) && typeof obj[name] === "number")
          obj.__defineGetter__(name, function () this[nameStr]);
      }

      let result = {};
      for (let [n, v] in Iterator(st)) {
        result[n] = v && typeof v === "object" ? Utils.fixStatusObject(v) :
                    n === "text"               ? unescapeBrakets(v) :
                    v;
      }

      for (let [n, v] in Iterator(st)) {
        if (/(^|_)id$/.test(n))
          fixId(result, n);
      }

      return result;
    }, // }}}
    parseSayArg: function(arg) { // {{{
      let m = arg.match(/^(.*)@(\w{1,15})#(\d+)(.*)$/);
      return m ? {
        isReply: true,
        prefix: m[1],
        replyUser: m[2],
        replyID: m[3],
        postfix: m[4],
        text:  m[1] + '@' + m[2] + m[4]
      } : {
        isReply: false,
        text: arg
      };
    }, // }}}
    xmlhttpRequest: function(options) { // {{{
      let xhr = new XMLHttpRequest();
      xhr.open(options.method, options.url, true);
      if (typeof options.onload == "function") {
        xhr.onload = function() {
          options.onload(xhr);
        }
      }
      xhr.send(null);
    }, // }}}
    getPluginNameFromFile: function(file) { // {{{
      return file.leafName.replace(/\..*/, "");
    }, // }}}
  }; // }}}
  let Twittperator = { // {{{
    activitySummary: function(id) { // {{{
      tw.jsonGet(
        'https://api.twitter.com/i/statuses/' + id + '/activity/summary',
        {},
        function(json) {
          function idToIcon (id) {
            return `<span><a href=${'http://twitter.com/#!/' + id}>${icons[id] ? `<img src=${icons[id]}/>` : "&#128568;"}</a></span>`;
          }

          let icons = {};
          for (let [, t] in Iterator(history)) {
            if (t.user && t.user.id_str && t.user.profile_image_url)
              icons[t.user.id_str] = t.user.profile_image_url;
          }
          liberator.echo(xml`
            <dl>
              <dt>Retweeter ${json.retweeters_count}</dt>
              <dd>${template.map(json.retweeters, idToIcon)}</dd>
              <dt>Favoriters ${json.favoriters_count}</dt>
              <dd>${template.map(json.favoriters, idToIcon)}</dd>
            </dl>
          `);
        }
      );
    }, // }}}
    confirm: function(msg, onYes, onNo, onCancel) { // {{{
      if (!onNo)
        onNo = function () Twittperator.echo('canceled.');

      commandline.input(
        msg + " (input 'yes/no'): ",
        function(s) (s === "yes" ? onYes : onNo)(),
        {
          onCancel: onCancel || onNo
        }
      );
    }, // }}}
    echo: function(msg) { // {{{
      liberator.echo("[Twittperator] " + msg);
    }, // }}}
    isProtected: function({ statusId, userId, screenName }) { // {{{
      if (screenName && isp(function(st) st.user && st.user.screen_name == screenName))
        return true;

      if (statusId && isp(function(st) st.user && st.id == statusId))
        return true;

      if (userId && find(function(st) st.user && st.user.id == userId))
        return true;

      return false;

      function isp(f) {
        let r;
        history.some(function(st) f(st) && (r = st));
        return (r && r.user.protected && r.user.screen_name) ? true : false;
      }
    }, // }}}
    jsonGet: function(api, query, callback) { // {{{
      tw.jsonGet(api, query, callback);
    }, // }}}
    loadPlugins: function() { // {{{
      function isEnabled(file) {
        let name = file.leafName.replace(/\..*/, "").replace(/-/g, "_");
        return liberator.globalVariables["twittperator_plugin_" + name];
      }

      function loadPluginFromDir(checkGV, candidates) {
        return function(dir) {
          dir.readDirectory().forEach(function(file) {
            if (/\.tw$/.test(file.path) && (!checkGV || isEnabled(file))) {
              if (candidates) {
                self._plugins.candidates[Utils.getPluginNameFromFile(file)] = file;
              } else {
                Twittperator.sourceScriptFile(file);
              }
            }
          });
        }
      }

      let self = this;
      this._plugins = {
        candidates: {},
        loaded: {}
      };

      ChirpUserStream.clearPluginData();
      TrackingStream.clearPluginData();

      for (let [, c] in Iterator([true, false])) {
        io.getRuntimeDirectories("plugin/twittperator").forEach(loadPluginFromDir(true, c));
        io.getRuntimeDirectories("twittperator").forEach(loadPluginFromDir(false, c));
      }
    }, // }}}
    lookupUser: function(users) { // {{{
      function showUsersInfo(json) { // {{{
        let body = modules.template.map(json, function(user) {
          return xml`
            <tr>
              <td class="twittperator lookup-user photo">
                <img src=${user.profile_image_url} />
              </td>
              <td class="twittperator lookup-user screen-name">
                <a href=${"https://twitter.com/#!/" + user.screen_name}>
                  ${user.name}
                </a>
              </td>
              <td class="twittperator lookup-user attributes">
                ${user.location} -
                id ${user.id_str} -
                ${user.following ? '' : 'not'} following -
                ${user.friends_count}/${user.followers_count} ee/er -
                ${user.statuses_count} tweets -
                ${user.favourites_count} favs -
                ${user.listed_count} listed -
                from ${new Date(user.created_at).toLocaleString()}
              </td>
            </tr>
            <tr>
              <td class="twittperator lookup-user description" colspan="3">
                ${user.description}
              </td>
            </tr>
          `;
        });
        liberator.echo(xml`
            <style type="text/css"><![CDATA[
              .twittperator.lookup-user.photo { vertical-align: top; width: 28px; }
              .twittperator.lookup-user.photo img { border: 0px; width: 24px; height: 24px; vertical-align: baseline; margin: 1px; }
              .twittperator.lookup-user.attributes { white-space: normal !important; }
              .twittperator.lookup-user.description { white-space: normal !important; }
              .twittperator.lookup-user.description a { text-decoration: none; }
            ]]></style>
            <table>${body}</table>
        `);
      } // }}}

      let ids = [], screenNames = [];
      for (let [, v] in Iterator(users))
        (/^\d+$/.test(v) ? ids : screenNames).push(v);
      Twitter.lookupUser({ids: ids, screenNames: screenNames}, function(json) {
        showUsersInfo(json);
      });
    }, // }}}
    onFriends: function(msg) { // {{{
      __context__.Friends = friends = msg.friends;
    }, // }}}
    onMessage: function(msg) { // {{{
      history.unshift(msg);
      if (history.length > setting.historyLimit)
        history.splice(setting.historyLimit);
    }, // }}}
    openLink: function(text) { // {{{
      let m = text.match(/.*?(https?:\/\/\S+)/g);
      if (!m)
        return;

      let links =
        m.map(function(s) s.match(/^(.*?)(https?:\/\/\S+)/).slice(1)) .
          map(function(ss) (ss.reverse(), ss.map(String.trim)))
      Twittperator.selectAndOpenLink(links);
    }, // }}}
    say: function(stat) { // {{{
      let sendData = {};
      let arg = Utils.parseSayArg(stat);
      if (arg.isReply) {
        if (stat.indexOf("RT @" + arg.replyUser + "#" + arg.replyID) == 0) {
          Twittperator.withProtectedUserConfirmation(
            { screenName: arg.replyUser, statusId: arg.replyID },
            "retweet",
            function() Twitter.retweet(arg.replyID)
          );
          return;
        }
        stat = arg.text;
        if (arg.replyID && !arg.prefix)
          sendData.in_reply_to_status_id = arg.replyID;
      }
      Twittperator.withProtectedUserConfirmation(
        { screenName: arg.replyUser, statusId: arg.replyID },
        "reply",
        function() Twitter.say(stat, arg.replyID)
      );
    }, // }}}
    selectAndOpenLink: function(links) { // {{{
      if (!links || !links.length)
        return;

      if (links.length === 1)
        return liberator.open(links[0][0], liberator.NEW_TAB);

      commandline.input(
        "Select URL: ",
        function(arg) liberator.open(arg, liberator.NEW_TAB),
        {
          completer: function(context) {
            context.completions = links;
          }
        }
      );
    }, // }}}
    showUserTimeline: function(arg) { // {{{
      Twitter.getUserTimeline(arg, function(statuses) {
        Twittperator.showTL(Array.slice(statuses).reverse());
      });
    }, // }}}
    showStatusMenu: function(status) { // {{{
    }, // }}}
    showTL: function(s) { // {{{
      function userURL(screen_name)
        (setting.showTLURLScheme + "://twitter.com/" + screen_name);

      function menuEvent(st)
        ("window.parent.liberator.modules.plugins.twittperator.Twittperator.showStatusMenu(" + parseInt(st.id) + ")");

      let html = `<style type="text/css"><![CDATA[
          .twittperator.timeline.user { vertical-align: top; }
          .twittperator.timeline.entry-content { white-space: normal !important; }
          .twittperator.timeline.entry-content a { text-decoration: none; }
          .twittperator.timeline.entry-content.rt:before { content: "RT "; color: silver; }
          img.twittperator.timeline.photo { border: 0px; width: 24px; height: 24px; vertical-align: baseline; margin: 1px; }
      ]]></style>`
          .replace(/(?:\r\n|[\r\n])[ \t]*/g, " ") +
          '<table>' +
          s.reduce(function(table, status) {
            if ("retweeted_status" in status) {
              let rt = status.retweeted_status;
              return table + xml`<tr>
                <td class="twittperator timeline user">
                  <a href=${userURL(rt.user.screen_name)}>
                    <img src=${rt.user.profile_image_url} alt=${rt.user.screen_name} class="twittperator timeline photo"/>
                    <strong>${rt.user.screen_name}&#x202C;</strong>
                  </a>
                  <a href=${userURL(status.user.screen_name)}>
                    <img src=${status.user.profile_image_url} alt=${status.user.screen_name} class="twittperator timeline photo"/>
                  </a>
                </td>
                <td class="twittperator timeline entry-content rt">
                  ${Utils.anchorLink(rt.text)}
                </td>
                <td class="twittperator timeline menu">
                  <a href="javascript: void 0" onclick=${menuEvent(status)}>&#1758;</a>
                </td>
              </tr>`.toString();
            } else {
              return table + xml`<tr>
                <td class="twittperator timeline user">
                  <a href=${userURL(status.user.screen_name)}>
                    <img src=${status.user.profile_image_url} alt=${status.user.screen_name} class="twittperator timeline photo"/>
                    <strong title=${status.user.name}>${status.user.screen_name}&#x202C;</strong>
                  </a>
                </td>
                <td class="twittperator timeline entry-content">
                  ${Utils.anchorLink(status.text)}
                </td>
                <td class="twittperator timeline menu">
                  <a href="javascript: void 0" onclick=${menuEvent(status)}>&#1758;</a>
                </td>
              </tr>`.toString();
            }
          }, "").replace(/(?:\r\n|[\r\n])[ \t]*/g, " ") +
          '</table>';

      window.Services.console.logStringMessage(html);
      liberator.echo(new template.maybeXML(html), true);
    }, // }}}
    showTwitterMentions: function(arg) { // {{{
      tw.jsonGet("statuses/mentions_timeline", null, function(res) {
        Twittperator.showTL(res.map(Utils.fixStatusObject));
      });
    }, // }}}
    showTwitterSearchResult: function(word) { // {{{
      Twitter.searchTweets(word, function(res){
        if (res.statuses.length > 0) {
          Twittperator.showTL(res.statuses.map(Utils.fixStatusObject));
        } else {
          Twittperator.echo("No results found.")
        }
      });
    }, // }}}
    showUsersSeachResult: function(word) { // {{{
      Twitter.searchUsers(word, function(res){
        // フォーマットが違うので変換
        function konbuArt(obj) {
          return {
            __proto__: obj.status,
            user: {
              __proto__: obj,
            }
          };
        }

        // TODO ユーザのリストが返ってきますが、タイムラインとして表示します。
        if (res.length > 0) {
          Twittperator.showTL(res.map(Utils.fixStatusObject).map(konbuArt));
        } else {
          Twittperator.echo("No results found.")
        }
      });
    }, // }}}
    sourceScriptFile: function(file) { // {{{
      let self = this;

      if (self._plugins.loaded[Utils.getPluginNameFromFile(file)])
        return true;

      let stdScript = liberator.plugins.contexts[file.path];

      try {
        let script = Script(file);

        script.__context__.require = function (names) {
          if (!(names instanceof Array))
            names = [names];
          names.forEach(function (name) {
            if (self._plugins.loaded[name])
              return;
            let lib = self._plugins.candidates[name];
            if (lib)
              self.sourceScriptFile(lib);
            else
              throw "Not found twittperator plugin: " + name;
          });
        };

        let uri = services.get("io").newFileURI(file);
        let suffix = '?' + encodeURIComponent(services.get("UUID").generateUUID().toString());

        liberator.loadScript(uri.spec + suffix, script);
        self._plugins.loaded[Utils.getPluginNameFromFile(file)] = script;

      } finally {
        if (stdScript)
          liberator.plugins[stdScript.NAME] = stdScript;
      }
    }, // }}}
    withProtectedUserConfirmation: function(check, actionName, action) { // {{{
      if (Twittperator.isProtected(check)) {
        Twittperator.confirm(
          check.screenName + " is protected user! Do you really want to " + actionName + '?',
          action
        );
      } else {
        action();
      }
    }, // }}}
  }; // }}}
  let Store = storage.newMap("twittperator", { store: true }); // {{{
  Store.set("clientName", "Twittperator");
  Store.set("consumerKey", "GQWob4E5tCHVQnEVPvmorQ");
  Store.set("consumerSecret", "gVwj45GaW6Sp7gdua6UFyiF910ffIety0sD1dv36Cz8");
  // }}}
  let Predicates = { // {{{
    notMine: function (st) {
      let n = setting.screenName;
      return n ? (!st.user || st.user.screen_name !== n) : st;
    },
    selectMine: function (st)
      (!Predicates.notMine(st))
  }; // }}}
  let Completers = (function() { // {{{
    function rt(st)
      ("retweeted_status" in st ? st.retweeted_status : st);

    function removeNewLine(text)
      text.replace(/\r\n|[\r\n]/g, ' ');

    function setTimelineCompleter(context) { // {{{
      function statusObjectFilter(item) {
        let desc = item.description;
        return this.match(desc.user.screen_name) || this.match(desc.text);
      }

      context.compare = void 0;
      context.createRow = function(item, highlightGroup) {
        if (highlightGroup === "CompTitle") {
          return xml`<div highlight="CompTitle" style="white-space: nowrap">
              <li highlight="CompDesc">${item}&#160;</li>
          </div>`;
        }

        let [value, st] = item.item;
        if (st.user) {
          return xml`<div highlight="CompItem" style="white-space: nowrap">
              <li highlight="CompDesc">
                <img src=${st.user.profile_image_url} style="max-width: 24px; max-height: 24px"/>
                &#160;${st.user.screen_name}: ${st.text}
              </li>
          </div>`;
        } else {
          return xml`<div highlight="CompItem" style="white-space: nowrap">
              <li highlight="CompDesc">
                ${st.text}
              </li>
          </div>`;
        }
      };

      context.filters = [statusObjectFilter];
      context.title = "Entry";
    } // }}}

    function makeTimelineCompleter(completer) { // {{{
      return function (context, args) {
        setTimelineCompleter(context);
        return completer(context, args);
      }
    } // }}}

    function completer(generator, nort) {
      let getHistory = nort ? function() history
                            : function() history.map(rt);
      return function(filter) {
        function completer(context, args) {
          let cs = [];
          for (let [, it] in Iterator(getHistory())) {
            if (filter && !filter(it))
              continue;
            let item = generator(it);
            if (item[0])
              cs.push(item);
          }
          context.completions = cs;
        }
        return makeTimelineCompleter(completer);
      }
    }

    return {
      name:
        completer(function(s) [s.user.screen_name, s]),
      atname:
        completer(function(s) ['@' + s.user.screen_name, s]),
      text:
        completer(function(s) [removeNewLine(s.text), s]),
      id:
        completer(function(s) [s.id, s]),
      rawid:
        completer(function(s) [s.id, s], true),
      name_id:
        completer(function(s) ["@" + s.user.screen_name + "#" + s.id, s]),
      name_id_text:
        completer(function(s) ["@" + s.user.screen_name + "#" + s.id + ": " + removeNewLine(s.text), s]),
      screenName:
        completer(function(s) [s.user.screen_name, s]),
      statusPage:
        completer(function(s) [s.user.screen_name + '/status/' + s.id , s]),
      hashtag:
        function(filter) {
          return makeTimelineCompleter(function(context, args){
            context.completions = [
              [
                ['#' + h.text for ([, h] in Iterator(s.entities.hashtags))].join(' '),
                s
              ]
              for ([, s] in Iterator(history))
              if (s.entities && s.entities.hashtags && s.entities.hashtags[0])
            ]
          });
        }
    };
  })(); // }}}
  let SubCommand = function(init) { // {{{
    if (!(init.completer instanceof Array))
      init.completer = [init.completer];

    return {
      __proto__: init,
      get expr() {
        return RegExp(
          "^" +
          this.command.map(function(c) {
            let r = util.escapeRegex(c);
            return /^\W$/.test(c) ? r : r + "( |$)";
          }).join("|")
        );
      },
      match: function(s) s.match(this.expr),
      action: function(args) init.action(args.literalArg.replace(this.expr, "").trim())
    };
  }; // }}}
  let SubCommands = [ // {{{
    SubCommand({
      command: ["+"],
      description: "Fav a tweet",
      action: function(arg) {
        let m = arg.match(/^\d+/);
        if (m)
          Twitter.favorite(m[0]);
      },
      timelineCompleter: true,
      completer: Completers.id(Predicates.notMine)
    }),
    SubCommand({
      command: ["-"],
      description: "Unfav a tweet",
      action: function(arg) {
        let m = arg.match(/^\d+/);
        if (m)
          Twitter.favorite(m[0]);
      },
      timelineCompleter: true,
      completer: Completers.id(Predicates.notMine)
    }),
    SubCommand({
      command: ["@"],
      description: "Show mentions or follower tweets",
      action: function(arg) {
        if (arg.length > 0) {
          Twittperator.showUserTimeline(arg);
        } else {
          Twittperator.showTwitterMentions();
        }
      },
      timelineCompleter: true,
      completer: Completers.name()
    }),
    SubCommand({
      command: ["?"],
      description: "Twitter search",
      action: function(arg) Twittperator.showTwitterSearchResult(arg),
      completer: [
        function (context, args) {
          let lst = [[buffer.URL, 'Current Tab']];
          let w = buffer.getCurrentWord();
          if (w && w.length)
            lst.push([w, 'Current word']);
          context.completions = lst;
        },
        Completers.text()
      ]
    }),
    SubCommand({
      command: ["/"],
      description: "Open link",
      action: function(arg) Twittperator.openLink(arg),
      timelineCompleter: true,
      completer: Completers.text(function(s) /https?:\/\//.test(s.text))
    }),
    SubCommand({
      command: ["delete"],
      description: "Delete status",
      action: function(arg) {
        let m = arg.match(/^\d+/);
        if (m)
          Twitter.destroy(m[0]);
      },
      timelineCompleter: true,
      completer: Completers.rawid(Predicates.selectMine)
    }),
    SubCommand({
      command: ["activity"],
      description: "Activity Summary",
      action: function(arg) {
        Twittperator.activitySummary(arg);
      },
      timelineCompleter: true,
      completer: Completers.id(function(st) st.id)
    }),
    SubCommand({
      command: ["info"],
      description: "Display status information",
      action: function(arg) {
        function dtdd(obj) {
          let items = "";
          for (let [n, v] in Iterator(obj)) {
            let cont = (v && typeof v === "object") ? dtdd(v) : v;
            items += `<dt>${n}</dt><dd>${cont}</dd>`;
          }

          return `<dl>${items}</dl>`;
        }

        let m = arg.match(/^\d+/);
        if (!m)
          return;
        let id = m[0];
        history.filter(function(st) st.id === id).map(dtdd).forEach(function(v) {
          liberator.echo(new template.maybeXML(v));
        });
      },
      timelineCompleter: true,
      completer: Completers.rawid(function(st) st.id)
    }),
    SubCommand({
      command: ["lookupuser"],
      description: "Lookup users",
      action: function(arg) {
        Twittperator.lookupUser(arg.split(/\s+/));
      },
      timelineCompleter: true,
      completer: Completers.screenName()
    }),
    SubCommand({
      command: ["track"],
      description: "Track the specified words.",
      action: function(arg) {
        if (arg.trim().length > 0) {
          Store.set("trackWords", arg);
          TrackingStream.start({track: arg});
        } else {
          TrackingStream.stop();
        }
      },
      completer: function(context, args) {
        let cs = [];
        if (setting.trackWords)
          cs.push([setting.trackWords, "Global variable"]);
        if (Store.get("trackWords"))
          cs.push([Store.get("trackWords"), "Current tracking words"]);
        context.completions = cs;
      }
    }),
    SubCommand({
      command: ["home"],
      description: "Open user home.",
      action: function(arg) liberator.open("http://twitter.com/" + arg, liberator.NEW_TAB),
      timelineCompleter: true,
      completer: Completers.screenName(Predicates.notMine)
    }),
    SubCommand({
      command: ["status"],
      description: "Open status page.",
      action: function(arg) liberator.open("http://twitter.com/" + arg, liberator.NEW_TAB),
      timelineCompleter: true,
      completer: Completers.statusPage(function (s) s.id)
    }),
    SubCommand({
      command: ["thread"],
      description: "Show tweets thread.",
      action: function(arg) {
        function showThread () {
          Twittperator.showTL(thread);
        }
        function getStatus(id, next) {
          let result;
          if (history.some(function (it) (it.id == id && (result = it)))) {
            return next(result);
          }
          // XXX エラーの時はなにか表示しておくべき?
          tw.jsonGet("statuses/show/" + id, null, function(res) next(res), showThread);
        }
        function trace(st) {
          thread.push(st);
          if (st.in_reply_to_status_id) {
            getStatus(st.in_reply_to_status_id, trace);
          } else {
            showThread();
          }
        }

        Twittperator.echo("Start thread tracing..");
        let thread = [];
        getStatus(parseInt(arg), trace);
      },
      timelineCompleter: true,
      completer: Completers.id(function (it) it.in_reply_to_status_id)
    }),
    SubCommand({
      command: ["resetoauth"],
      description: "Reset OAuth Information",
      action: function(arg) {
        Twittperator.confirm(
          'Do you want to reset OAuth information?',
          function () {
            Store.remove("consumerKey");
            Store.remove("consumerSecret");
            Store.remove("token");
            Store.remove("tokenSecret");
            Store.save();
            Twittperator.echo("OAuth information were reset.");
          }
        );
      },
      timelineCompleter: false,
      completer: Completers.id(function (it) it.in_reply_to_status_id)
    }),
    SubCommand({
      command: ["findpeople"],
      description: "Find people with the words.",
      action: function(arg) Twittperator.showUsersSeachResult(arg),
    }),
    SubCommand({
      command: ["restartstreams"],
      description: "Restart streams",
      action: function(arg) startStreams(),
    }),
  ];

  SubCommands.add = function(subCmd) {
    this.push(subCmd);
    return;
  }; // }}}

  // アクセストークン取得前 {{{
  function preSetup() {
    commands.addUserCommand(["tw[ittperator]"], "Twittperator setup command",
      function(args) {
        if (args["-getPIN"]) {
          tw.getRequestToken(function(url) {
            liberator.open(url, { where: liberator.NEW_TAB });
          });
          Twittperator.echo("Please get PIN code and execute\n :tw -setPIN {PINcode}");
        } else if (args["-setPIN"]) {
          tw.setPin(args["-setPIN"]);
        }
      }, {
        options: [
          [["-getPIN"], commands.OPTION_NOARG],
          [["-setPIN"], commands.OPTION_STRING, null, null]
        ],
      }, true);
  } // }}}
  // アクセストークン取得後 {{{
  function setup() {
    function findSubCommand(s) { // {{{
      for (let [, cmd] in util.Array(SubCommands)) {
        let m = cmd.match(s);
        if (m)
          return [cmd, m];
      }
    } // }}}

    function tailMatch(re, str) { // {{{
      let result, m;
      let head = 0;
      let len = 0;
      while (str && (m = str.match(re))) {
        head += len;
        len = m.index + m[0].length;
        str = str.slice(len);
        result = m;
      }
      if (result)
        result.index += head;
      return result;
    } // }}}

    function subCommandCompleter(context, args) { // {{{
      if (!args.literalArg.match(/^(\W|\S+\s)/)) {
        context.title = ["Sub Command", "Description"];
        context.completions = SubCommands.map(function({ command, description }) [command[0], description]);
        return;
      }

      let [subCmd, m] = findSubCommand(args.literalArg) || [];
      if (!subCmd)
        return;

      context.offset += m[0].length;
      let offset = context.offset;

      subCmd.completer.forEach(function (completer, index) {
        context.fork(
          "TwittperatorSubCommand" + index, 0, context,
          function (context) {
            context.offset = offset;
            return completer(context, args);
          }
        );
      });
    } // }}}

    function commandCompelter(context, args) { // {{{
      let len = 0;

      let arg = args.literalArg.slice(0, context.caret);
      let m;
      if (m = arg.match(/^D\s+/)) {
        context.title = "Entry";
        context.advance(m[0].length);
        Completers.name(Predicates.notMine)(context, args);
        return;
      } else if (m = arg.match(/(RT\s+)@.*$/)) {
        (m.index === 0 ? Completers.name_id
                       : Completers.name_id_text)(m.index === 0 && Predicates.notMine)(context, args);
      } else if (m = tailMatch(/(^|\b|\s)#[^#\s]*$/, arg)) {
        Completers.hashtag()(context, args);
      } else if (m = tailMatch(/(^|\b|\s)@[^@\s]*$/, arg)) {
        (m.index === 0 ? Completers.name_id(Predicates.notMine) : Completers.atname(Predicates.notMine))(context, args);
      }

      if (m)
        len = m.index + m[1].length;

      context.title = "Entry";
      context.offset += len;
      // XXX 本文でも検索できるように、@ はなかったことにする
      context.filter = context.filter.replace(/^[@#]/, "");

      context.incomplete = false;
    } // }}}

    commands.addUserCommand(["tw[ittperator]"], "Twittperator command", // {{{
      function(args) {
        let bang = args.bang;
        let arg = args.literalArg;

        if (!arg)
          return Twittperator.showUserTimeline();

        if (args.bang) {
          let [subCmd] = findSubCommand(arg) || [];
          if (subCmd)
            subCmd.action(args);
        } else {
          if (arg.length === 0)
            Twittperator.showUserTimeline();
          else
            Twittperator.say(arg);
        }
      }, {
        bang: true,
        literal: 0,
        hereDoc: true,
        completer: (function () {
          let getting, lastTime;
          return function(context, args) {
            let now = new Date().getTime();
            let doGet =
              setting.autoStatusUpdate &&
              !getting &&
              (!lastTime || ((lastTime + setting.statusValidDuration * 1000) < now));

            let completer = args.bang ? subCommandCompleter : commandCompelter;
            let matches = args.bang || args.literalArg.match(/(RT\s+|)@|#|^D\s+/);

            if (!matches)
              return;

            context.fork(
              "TwittperatorCommand", 0, context,
              function(context) {
                context.incomplete = doGet;
                context.hasitems = !doGet;

                if (doGet) {
                  getting = true;
                  lastTime = now;
                  Twitter.getUserTimeline(null, function() {
                    getting = false;
                    completer(context, args);
                    context.incomplete = false;
                  });
                  return;
                }
                completer(context, args);
              }
            );
          };
        })()
      }, true); // }}}
  } // }}}

  // Initialization {{{
  let setting =
    (function () {
      let gv = liberator.globalVariables;
      return {
        useChirp: !!gv.twittperator_use_chirp,
        allReplies: !!gv.twittperator_all_replies,
        autoStatusUpdate: !!parseInt(gv.twittperator_auto_status_update || 0),
        statusValidDuration: parseInt(gv.twitperator_status_valid_duration || 90),
        historyLimit: (function () {
          let v = gv.twittperator_history_limit;
          return v === 0 ? 0 : (v || 1000)
        })(),
        showTLURLScheme: (function () {
          let v = gv.twittperator_show_tl_with_https_url;
          return "http" + (v === false ? "" : "s");
        })(),
        proxyHost: gv.twittperator_proxy_host,
        proxyPort: gv.twittperator_proxy_port,
        screenName: gv.twittperator_screen_name,
        apiURLBase: "http" + (!!gv.twittperator_use_ssl_connection_for_api_ep ? "s" : "") +
                    "://api.twitter.com/" + (gv.twittperator_twitter_api_version || "1.1")  + "/",
        trackWords: gv.twittperator_track_words,
        count: (gv.twittperator_count || 20),
        lang: (gv.twittperator_lang || ''),
        getAPIURL: function (path) (/^https?\:\/\//.test(path) ? path : this.apiURLBase + path)
      };
    })();

  let statusRefreshTimer;

  let history = __context__.Tweets;
  if (!history)
    history = __context__.Tweets = Store.get("history", []);

  let friends = __context__.Friends;
  if (!friends)
    friends = __context__.Friends = Store.get("friends", []);

  let tw = new TwitterOauth(Store);

  // ストリーム
  let ChirpUserStream = Stream({ name: 'chirp stream', url: "https://userstream.twitter.com/2/user.json" });
  let TrackingStream = Stream({ name: 'tracking stream', url: "https://stream.twitter.com/1/statuses/filter.json" });

  let startStreams = function () {
    ChirpUserStream.resetRestartCount();
    TrackingStream.resetRestartCount();

    if (setting.useChirp){
      if(setting.allReplies)
        ChirpUserStream.start({"replies":"all"});
      else
        ChirpUserStream.start();
    }

    let trackWords = setting.trackWords || Store.get("trackWords");
    if (trackWords)
      TrackingStream.start({track: trackWords});
  };

  // 公開オブジェクト
  __context__.OAuth = tw;
  __context__.ChirpUserStream = ChirpUserStream;
  __context__.TrackingStream = TrackingStream;
  __context__.Twittperator = Twittperator;
  __context__.Twitter = Twitter;
  __context__.Utils = Utils;
  __context__.Store = Store;
  __context__.SubCommand = SubCommand;
  __context__.SubCommands = SubCommands;
  __context__.Completers = Completers;

  Twittperator.loadPlugins();

  liberator.registerObserver('enter', startStreams);

  __context__.onUnload = function() {
    Store.set("history", history);
    Store.set("friends", friends);
    ChirpUserStream.stop();
    TrackingStream.stop();
  };
  // }}}

})();

// vim: sw=2 ts=2 et fdm=marker: