1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
|
/* gdict-client-context.c -
*
* Copyright (C) 2005 Emmanuele Bassi <ebassi@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
*/
/**
* SECTION:gdict-client-context
* @short_description: DICT client transport
*
* #GdictClientContext is an implementation of the #GdictContext interface.
* It implements the Dictionary Protocol as defined by the RFC 2229 in order
* to connect to a dictionary server.
*
* You should rarely instantiate this object directely: use an appropriate
* #GdictSource instead.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#include <glib/gi18n-lib.h>
#include "gdict-context-private.h"
#include "gdict-context.h"
#include "gdict-client-context.h"
#include "gdict-enum-types.h"
#include "gdict-marshal.h"
#include "gdict-debug.h"
#include "gdict-utils.h"
#include "gdict-private.h"
typedef enum {
CMD_CLIENT,
CMD_SHOW_DB,
CMD_SHOW_STRAT,
CMD_SHOW_INFO, /* not implemented */
CMD_SHOW_SERVER, /* not implemented */
CMD_MATCH,
CMD_DEFINE,
CMD_STATUS, /* not implemented */
CMD_OPTION_MIME, /* not implemented */
CMD_AUTH, /* not implemented */
CMD_HELP, /* not implemented */
CMD_QUIT,
CMD_INVALID
} GdictCommandType;
#define IS_VALID_CMD(cmd) (((cmd) >= CMD_CLIENT) || ((cmd) < CMD_INVALID))
/* command strings: keep synced with the enum above! */
static const gchar *dict_command_strings[] = {
"CLIENT",
"SHOW DB",
"SHOW STRAT",
"SHOW INFO",
"SHOW SERVER",
"MATCH",
"DEFINE",
"STATUS",
"OPTION MIME",
"AUTH",
"HELP",
"QUIT",
NULL
};
/* command stata */
enum
{
S_START,
S_STATUS,
S_DATA,
S_FINISH
};
typedef struct
{
GdictCommandType cmd_type;
gchar *cmd_string;
guint state;
/* optional parameters passed to the command */
gchar *database;
gchar *strategy;
gchar *word;
/* buffer used to hold the reply from the server */
GString *buffer;
gpointer data;
GDestroyNotify data_destroy;
} GdictCommand;
/* The default string to be passed to the CLIENT command */
#define GDICT_DEFAULT_CLIENT "MATE Dictionary (" VERSION ")"
/* Default server:port couple */
#define GDICT_DEFAULT_HOSTNAME "dict.org"
#define GDICT_DEFAULT_PORT 2628
/* make the hostname lookup expire every five minutes */
#define HOSTNAME_LOOKUP_EXPIRE 300
/* wait 30 seconds between connection and receiving data on the line */
#define CONNECTION_TIMEOUT 30
enum
{
PROP_0,
PROP_HOSTNAME,
PROP_PORT,
PROP_STATUS,
PROP_CLIENT_NAME
};
enum
{
CONNECTED,
DISCONNECTED,
LAST_SIGNAL
};
static guint gdict_client_context_signals[LAST_SIGNAL] = { 0 };
#define GDICT_CLIENT_CONTEXT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GDICT_TYPE_CLIENT_CONTEXT, GdictClientContextPrivate))
struct _GdictClientContextPrivate
{
#ifdef ENABLE_IPV6
struct sockaddr_storage sockaddr;
struct addrinfo *host6info;
#else
struct sockaddr_in sockaddr;
#endif
struct hostent *hostinfo;
time_t last_lookup;
gchar *hostname;
gint port;
GIOChannel *channel;
guint source_id;
guint timeout_id;
GdictCommand *command;
GQueue *commands_queue;
gchar *client_name;
GdictStatusCode status_code;
guint local_only : 1;
guint is_connecting : 1;
};
static void gdict_client_context_iface_init (GdictContextIface *iface);
G_DEFINE_TYPE_WITH_CODE (GdictClientContext,
gdict_client_context,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GDICT_TYPE_CONTEXT,
gdict_client_context_iface_init));
/* GObject methods */
static void gdict_client_context_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gdict_client_context_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gdict_client_context_finalize (GObject *object);
/* GdictContext methods */
static gboolean gdict_client_context_get_databases (GdictContext *context,
GError **error);
static gboolean gdict_client_context_get_strategies (GdictContext *context,
GError **error);
static gboolean gdict_client_context_define_word (GdictContext *context,
const gchar *database,
const gchar *word,
GError **error);
static gboolean gdict_client_context_match_word (GdictContext *context,
const gchar *database,
const gchar *strategy,
const gchar *word,
GError **error);
static void gdict_client_context_clear_hostinfo (GdictClientContext *context);
static gboolean gdict_client_context_lookup_server (GdictClientContext *context,
GError **error);
static gboolean gdict_client_context_io_watch_cb (GIOChannel *source,
GIOCondition condition,
GdictClientContext *context);
static gboolean gdict_client_context_parse_line (GdictClientContext *context,
const gchar *buffer);
static void gdict_client_context_disconnect (GdictClientContext *context);
static void gdict_client_context_force_disconnect (GdictClientContext *context);
static void gdict_client_context_real_connected (GdictClientContext *context);
static void gdict_client_context_real_disconnected (GdictClientContext *context);
static GdictCommand *gdict_command_new (GdictCommandType cmd_type);
static void gdict_command_free (GdictCommand *cmd);
GQuark
gdict_client_context_error_quark (void)
{
return g_quark_from_static_string ("gdict-client-context-error-quark");
}
static void
gdict_client_context_iface_init (GdictContextIface *iface)
{
iface->get_databases = gdict_client_context_get_databases;
iface->get_strategies = gdict_client_context_get_strategies;
iface->match_word = gdict_client_context_match_word;
iface->define_word = gdict_client_context_define_word;
}
static void
gdict_client_context_class_init (GdictClientContextClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = gdict_client_context_set_property;
gobject_class->get_property = gdict_client_context_get_property;
gobject_class->finalize = gdict_client_context_finalize;
g_object_class_override_property (gobject_class,
GDICT_CONTEXT_PROP_LOCAL_ONLY,
"local-only");
/**
* GdictClientContext:client-name
*
* The name of the client using this context; it will be advertised when
* connecting to the dictionary server.
*
* Since: 1.0
*/
g_object_class_install_property (gobject_class,
PROP_CLIENT_NAME,
g_param_spec_string ("client-name",
_("Client Name"),
_("The name of the client of the context object"),
NULL,
(G_PARAM_READABLE | G_PARAM_WRITABLE)));
/**
* GdictClientContext:hostname
*
* The hostname of the dictionary server to connect to.
*
* Since: 1.0
*/
g_object_class_install_property (gobject_class,
PROP_HOSTNAME,
g_param_spec_string ("hostname",
_("Hostname"),
_("The hostname of the dictionary server to connect to"),
NULL,
(G_PARAM_READABLE | G_PARAM_WRITABLE)));
/**
* GdictClientContext:port
*
* The port of the dictionary server to connect to.
*
* Since: 1.0
*/
g_object_class_install_property (gobject_class,
PROP_PORT,
g_param_spec_uint ("port",
_("Port"),
_("The port of the dictionary server to connect to"),
0,
65535,
GDICT_DEFAULT_PORT,
(G_PARAM_READABLE | G_PARAM_WRITABLE)));
/**
* GdictClientContext:status
*
* The status code as returned by the dictionary server.
*
* Since: 1.0
*/
g_object_class_install_property (gobject_class,
PROP_STATUS,
g_param_spec_enum ("status",
_("Status"),
_("The status code as returned by the dictionary server"),
GDICT_TYPE_STATUS_CODE,
GDICT_STATUS_INVALID,
G_PARAM_READABLE));
/**
* GdictClientContext::connected
* @client: the object which received the signal
*
* Emitted when a #GdictClientContext has successfully established a
* connection with a dictionary server.
*
* Since: 1.0
*/
gdict_client_context_signals[CONNECTED] =
g_signal_new ("connected",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictClientContextClass, connected),
NULL, NULL,
gdict_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* GdictClientContext::disconnected
* @client: the object which received the signal
*
* Emitted when a #GdictClientContext has disconnected from a dictionary
* server.
*
* Since: 1.0
*/
gdict_client_context_signals[DISCONNECTED] =
g_signal_new ("disconnected",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictClientContextClass, disconnected),
NULL, NULL,
gdict_marshal_VOID__VOID,
G_TYPE_NONE, 0);
klass->connected = gdict_client_context_real_connected;
klass->disconnected = gdict_client_context_real_disconnected;
g_type_class_add_private (gobject_class, sizeof (GdictClientContextPrivate));
}
static void
gdict_client_context_init (GdictClientContext *context)
{
GdictClientContextPrivate *priv;
priv = GDICT_CLIENT_CONTEXT_GET_PRIVATE (context);
context->priv = priv;
priv->hostname = NULL;
priv->port = 0;
priv->hostinfo = NULL;
#ifdef ENABLE_IPV6
priv->host6info = NULL;
#endif
priv->last_lookup = (time_t) -1;
priv->is_connecting = FALSE;
priv->local_only = FALSE;
priv->status_code = GDICT_STATUS_INVALID;
priv->client_name = NULL;
priv->command = NULL;
priv->commands_queue = g_queue_new ();
}
static void
gdict_client_context_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GdictClientContextPrivate *priv = GDICT_CLIENT_CONTEXT_GET_PRIVATE (object);
switch (prop_id)
{
case PROP_HOSTNAME:
if (priv->hostname)
g_free (priv->hostname);
priv->hostname = g_strdup (g_value_get_string (value));
gdict_client_context_clear_hostinfo (GDICT_CLIENT_CONTEXT (object));
break;
case PROP_PORT:
priv->port = g_value_get_uint (value);
break;
case PROP_CLIENT_NAME:
if (priv->client_name)
g_free (priv->client_name);
priv->client_name = g_strdup (g_value_get_string (value));
break;
case GDICT_CONTEXT_PROP_LOCAL_ONLY:
priv->local_only = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gdict_client_context_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GdictClientContextPrivate *priv = GDICT_CLIENT_CONTEXT_GET_PRIVATE (object);
switch (prop_id)
{
case PROP_STATUS:
g_value_set_enum (value, priv->status_code);
break;
case PROP_HOSTNAME:
g_value_set_string (value, priv->hostname);
break;
case PROP_PORT:
g_value_set_uint (value, priv->port);
break;
case PROP_CLIENT_NAME:
g_value_set_string (value, priv->client_name);
break;
case GDICT_CONTEXT_PROP_LOCAL_ONLY:
g_value_set_boolean (value, priv->local_only);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gdict_client_context_finalize (GObject *object)
{
GdictClientContext *context = GDICT_CLIENT_CONTEXT (object);
GdictClientContextPrivate *priv = context->priv;
/* force disconnection */
gdict_client_context_force_disconnect (context);
if (priv->command)
gdict_command_free (priv->command);
if (priv->commands_queue)
{
g_queue_foreach (priv->commands_queue,
(GFunc) gdict_command_free,
NULL);
g_queue_free (priv->commands_queue);
priv->commands_queue = NULL;
}
if (priv->client_name)
g_free (priv->client_name);
if (priv->hostname)
g_free (priv->hostname);
#ifdef ENABLE_IPV6
if (priv->host6info)
freeaddrinfo (priv->host6info);
#endif
/* chain up parent's finalize method */
G_OBJECT_CLASS (gdict_client_context_parent_class)->finalize (object);
}
/**
* gdict_client_context_new:
* @hostname: the hostname of a dictionary server, or %NULL for the
* default server
* @port: port to be used when connecting to the dictionary server,
* or -1 for the default port
*
* Creates a new #GdictClientContext object for @hostname. Use this
* object to connect and query the dictionary server using the Dictionary
* Protocol as defined by RFC 2229.
*
* Return value: the newly created #GdictClientContext object. You should
* free it using g_object_unref().
*/
GdictContext *
gdict_client_context_new (const gchar *hostname,
gint port)
{
return g_object_new (GDICT_TYPE_CLIENT_CONTEXT,
"hostname", (hostname != NULL ? hostname : GDICT_DEFAULT_HOSTNAME),
"port", (port != -1 ? port : GDICT_DEFAULT_PORT),
"client-name", GDICT_DEFAULT_CLIENT,
NULL);
}
/**
* gdict_client_context_set_hostname:
* @context: a #GdictClientContext
* @hostname: the hostname of a Dictionary server, or %NULL
*
* Sets @hostname as the hostname of the dictionary server to be used.
* If @hostname is %NULL, the default dictionary server will be used.
*/
void
gdict_client_context_set_hostname (GdictClientContext *context,
const gchar *hostname)
{
g_return_if_fail (GDICT_IS_CLIENT_CONTEXT (context));
g_object_set (G_OBJECT (context),
"hostname", (hostname != NULL ? hostname : GDICT_DEFAULT_HOSTNAME),
NULL);
}
/**
* gdict_client_context_get_hostname:
* @context: a #GdictClientContext
*
* Gets the hostname of the dictionary server used by @context.
*
* Return value: the hostname of a dictionary server. The returned string is
* owned by the #GdictClientContext object and should never be modified or
* freed.
*/
const gchar *
gdict_client_context_get_hostname (GdictClientContext *context)
{
gchar *hostname;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), NULL);
g_object_get (G_OBJECT (context), "hostname", &hostname, NULL);
return hostname;
}
/**
* gdict_client_context_set_port:
* @context: a #GdictClientContext
* @port: port of the dictionary server to be used, or -1
*
* Sets the port of the dictionary server to be used when connecting.
*
* If @port is -1, the default port will be used.
*/
void
gdict_client_context_set_port (GdictClientContext *context,
gint port)
{
g_return_if_fail (GDICT_IS_CLIENT_CONTEXT (context));
g_object_set (G_OBJECT (context),
"port", (port != -1 ? port : GDICT_DEFAULT_PORT),
NULL);
}
/**
* gdict_client_context_get_port:
* @context: a #GdictClientContext
*
* Gets the port of the dictionary server used by @context.
*
* Return value: the number of the port.
*/
guint
gdict_client_context_get_port (GdictClientContext *context)
{
guint port;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), -1);
g_object_get (G_OBJECT (context), "port", &port, NULL);
return port;
}
/**
* gdict_client_context_set_client:
* @context: a #GdictClientContext
* @client: the client name to use, or %NULL
*
* Sets @client as the client name to be used when advertising ourselves when
* a connection the the dictionary server has been established.
* If @client is %NULL, the default client name will be used.
*/
void
gdict_client_context_set_client (GdictClientContext *context,
const gchar *client)
{
g_return_if_fail (GDICT_IS_CLIENT_CONTEXT (context));
g_object_set (G_OBJECT (context),
"client-name", (client != NULL ? client : GDICT_DEFAULT_CLIENT),
NULL);
}
/**
* gdict_client_context_get_client:
* @context: a #GdictClientContext
*
* Gets the client name used by @context. See gdict_client_context_set_client().
*
* Return value: the client name. The returned string is owned by the
* #GdictClientContext object and should never be modified or freed.
*/
const gchar *
gdict_client_context_get_client (GdictClientContext *context)
{
gchar *client_name;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), NULL);
g_object_get (G_OBJECT (context), "client-name", &client_name, NULL);
return client_name;
}
/* creates a new command to be sent to the dictionary server */
static GdictCommand *
gdict_command_new (GdictCommandType cmd_type)
{
GdictCommand *retval;
g_assert (IS_VALID_CMD (cmd_type));
retval = g_slice_new0 (GdictCommand);
retval->cmd_type = cmd_type;
retval->state = S_START;
return retval;
}
static void
gdict_command_free (GdictCommand *cmd)
{
if (!cmd)
return;
g_free (cmd->cmd_string);
switch (cmd->cmd_type)
{
case CMD_CLIENT:
case CMD_QUIT:
break;
case CMD_SHOW_DB:
case CMD_SHOW_STRAT:
break;
case CMD_MATCH:
g_free (cmd->database);
g_free (cmd->strategy);
g_free (cmd->word);
break;
case CMD_DEFINE:
g_free (cmd->database);
g_free (cmd->word);
break;
default:
break;
}
if (cmd->buffer)
g_string_free (cmd->buffer, TRUE);
if (cmd->data_destroy)
cmd->data_destroy (cmd->data);
g_slice_free (GdictCommand, cmd);
}
/* push @command into the head of the commands queue; the command queue is
* a FIFO-like pipe: commands go into the head and are retrieved from the
* tail.
*/
static gboolean
gdict_client_context_push_command (GdictClientContext *context,
GdictCommand *command)
{
GdictClientContextPrivate *priv;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
g_assert (command != NULL);
priv = context->priv;
/* avoid pushing a command twice */
if (g_queue_find (priv->commands_queue, command))
{
g_warning ("gdict_client_context_push_command() called on a command already in queue\n");
return FALSE;
}
GDICT_NOTE (DICT, "Pushing command ('%s') into the queue...",
dict_command_strings[command->cmd_type]);
g_queue_push_head (priv->commands_queue, command);
return TRUE;
}
static GdictCommand *
gdict_client_context_pop_command (GdictClientContext *context)
{
GdictClientContextPrivate *priv;
GdictCommand *retval;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
priv = context->priv;
retval = (GdictCommand *) g_queue_pop_tail (priv->commands_queue);
if (!retval)
return NULL;
GDICT_NOTE (DICT, "Getting command ('%s') from the queue...",
dict_command_strings[retval->cmd_type]);
return retval;
}
/* send @command on the wire */
static gboolean
gdict_client_context_send_command (GdictClientContext *context,
GdictCommand *command,
GError **error)
{
GdictClientContextPrivate *priv;
GError *write_error;
gsize written_bytes;
GIOStatus res;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
g_assert (command != NULL && command->cmd_string != NULL);
priv = context->priv;
if (!priv->channel)
{
GDICT_NOTE (DICT, "No connection established");
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_NO_CONNECTION,
_("No connection to the dictionary server at '%s:%d'"),
priv->hostname,
priv->port);
return FALSE;
}
write_error = NULL;
res = g_io_channel_write_chars (priv->channel,
command->cmd_string,
-1,
&written_bytes,
&write_error);
if (res != G_IO_STATUS_NORMAL)
{
g_propagate_error (error, write_error);
return FALSE;
}
/* force flushing of the write buffer */
g_io_channel_flush (priv->channel, NULL);
GDICT_NOTE (DICT, "Wrote %"G_GSIZE_FORMAT" bytes to the channel", written_bytes);
return TRUE;
}
/* gdict_client_context_run_command: runs @command inside @context; this
* function builds the command string and then passes it to the dictionary
* server.
*/
static gboolean
gdict_client_context_run_command (GdictClientContext *context,
GdictCommand *command,
GError **error)
{
GdictClientContextPrivate *priv;
gchar *payload;
GError *send_error;
gboolean res;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
g_assert (command != NULL);
g_assert (IS_VALID_CMD (command->cmd_type));
GDICT_NOTE (DICT, "GdictCommand command =\n"
"{\n"
" .cmd_type = '%02d' ('%s');\n"
" .database = '%s';\n"
" .strategy = '%s';\n"
" .word = '%s';\n"
"}\n",
command->cmd_type, dict_command_strings[command->cmd_type],
command->database ? command->database : "<none>",
command->strategy ? command->strategy : "<none>",
command->word ? command->word : "<none>");
priv = context->priv;
g_assert (priv->command == NULL);
priv->command = command;
/* build the command string to be sent to the server */
switch (command->cmd_type)
{
case CMD_CLIENT:
payload = g_shell_quote (priv->client_name);
command->cmd_string = g_strdup_printf ("%s %s\r\n",
dict_command_strings[CMD_CLIENT],
payload);
g_free (payload);
break;
case CMD_QUIT:
command->cmd_string = g_strdup_printf ("%s\r\n",
dict_command_strings[CMD_QUIT]);
break;
case CMD_SHOW_DB:
command->cmd_string = g_strdup_printf ("%s\r\n",
dict_command_strings[CMD_SHOW_DB]);
break;
case CMD_SHOW_STRAT:
command->cmd_string = g_strdup_printf ("%s\r\n",
dict_command_strings[CMD_SHOW_STRAT]);
break;
case CMD_MATCH:
g_assert (command->word);
payload = g_shell_quote (command->word);
command->cmd_string = g_strdup_printf ("%s %s %s %s\r\n",
dict_command_strings[CMD_MATCH],
(command->database != NULL ? command->database : "!"),
(command->strategy != NULL ? command->strategy : "*"),
payload);
g_free (payload);
break;
case CMD_DEFINE:
g_assert (command->word);
payload = g_shell_quote (command->word);
command->cmd_string = g_strdup_printf ("%s %s %s\r\n",
dict_command_strings[CMD_DEFINE],
(command->database != NULL ? command->database : "!"),
payload);
g_free (payload);
break;
default:
g_assert_not_reached ();
break;
}
g_assert (command->cmd_string);
GDICT_NOTE (DICT, "Sending command ('%s') to the server",
dict_command_strings[command->cmd_type]);
send_error = NULL;
res = gdict_client_context_send_command (context, command, &send_error);
if (!res)
{
g_propagate_error (error, send_error);
return FALSE;
}
return TRUE;
}
/* we use this signal to advertise ourselves to the dictionary server */
static void
gdict_client_context_real_connected (GdictClientContext *context)
{
GdictCommand *cmd;
cmd = gdict_command_new (CMD_CLIENT);
cmd->state = S_FINISH;
/* the CLIENT command should be the first one in our queue, so we place
* it above all other commands the user might have issued between the
* first and the emission of the "connected" signal, by calling it
* directely.
*/
gdict_client_context_run_command (context, cmd, NULL);
}
static void
clear_command_queue (GdictClientContext *context)
{
GdictClientContextPrivate *priv = context->priv;
if (priv->commands_queue)
{
g_queue_foreach (priv->commands_queue,
(GFunc) gdict_command_free,
NULL);
g_queue_free (priv->commands_queue);
}
/* renew */
priv->commands_queue = g_queue_new ();
}
/* force a disconnection from the server */
static void
gdict_client_context_force_disconnect (GdictClientContext *context)
{
GdictClientContextPrivate *priv = context->priv;
if (priv->timeout_id)
{
g_source_remove (priv->timeout_id);
priv->timeout_id = 0;
}
if (priv->source_id)
{
g_source_remove (priv->source_id);
priv->source_id = 0;
}
if (priv->channel)
{
g_io_channel_shutdown (priv->channel, TRUE, NULL);
g_io_channel_unref (priv->channel);
priv->channel = NULL;
}
if (priv->command)
{
gdict_command_free (priv->command);
priv->command = NULL;
}
clear_command_queue (context);
}
static void
gdict_client_context_real_disconnected (GdictClientContext *context)
{
gdict_client_context_force_disconnect (context);
}
/* clear the lookup data */
static void
gdict_client_context_clear_hostinfo (GdictClientContext *context)
{
GdictClientContextPrivate *priv;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
priv = context->priv;
#ifdef ENABLE_IPV6
if (!priv->host6info)
return;
#endif
if (!priv->hostinfo)
return;
#ifdef ENABLE_IPV6
freeaddrinfo (priv->host6info);
#endif
priv->hostinfo = NULL;
}
/* gdict_client_context_lookup_server: perform an hostname lookup in order to
* connect to the dictionary server
*/
static gboolean
gdict_client_context_lookup_server (GdictClientContext *context,
GError **error)
{
GdictClientContextPrivate *priv;
gboolean is_expired = FALSE;
time_t now;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
priv = context->priv;
/* we need the hostname, at this point */
g_assert (priv->hostname != NULL);
time (&now);
if (now >= (priv->last_lookup + HOSTNAME_LOOKUP_EXPIRE))
is_expired = TRUE;
/* we already have resolved the hostname */
#ifdef ENABLE_IPV6
if (priv->host6info && !is_expired)
return TRUE;
#endif
if (priv->hostinfo && !is_expired)
return TRUE;
/* clear any previously acquired lookup data */
gdict_client_context_clear_hostinfo (context);
GDICT_NOTE (DICT, "Looking up hostname '%s'", priv->hostname);
#ifdef ENABLE_IPV6
if (_gdict_has_ipv6 ())
{
struct addrinfo hints, *res;
GDICT_NOTE (DICT, "Hostname '%s' look-up (using IPv6)", priv->hostname);
memset (&hints, 0, sizeof (hints));
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo (priv->hostname, NULL, &hints, &(priv->host6info)) == 0)
{
for (res = priv->host6info; res; res = res->ai_next)
if (res->ai_family == AF_INET6 || res->ai_family == AF_INET)
break;
if (!res)
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_LOOKUP,
_("Lookup failed for hostname '%s': no suitable resources found"),
priv->hostname);
return FALSE;
}
else
{
if (res->ai_family == AF_INET6)
memcpy (&((struct sockaddr_in6 *) &priv->sockaddr)->sin6_addr,
&((struct sockaddr_in6 *) res->ai_addr)->sin6_addr,
sizeof (struct in6_addr));
if (res->ai_family == AF_INET)
memcpy (&((struct sockaddr_in *) &priv->sockaddr)->sin_addr,
&((struct sockaddr_in *) res->ai_addr)->sin_addr,
sizeof (struct in_addr));
priv->sockaddr.ss_family = res->ai_family;
GDICT_NOTE (DICT, "Hostname '%s' found (using IPv6)",
priv->hostname);
priv->last_lookup = time (NULL);
return TRUE;
}
}
else
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_LOOKUP,
_("Lookup failed for host '%s': %s"),
priv->hostname,
gai_strerror (errno));
return FALSE;
}
}
else
{
#endif /* ENABLE_IPV6 */
/* if we don't support IPv6, fallback to usual IPv4 lookup */
GDICT_NOTE (DICT, "Hostname '%s' look-up (using IPv4)", priv->hostname);
((struct sockaddr_in *) &priv->sockaddr)->sin_family = AF_INET;
priv->hostinfo = gethostbyname (priv->hostname);
if (priv->hostinfo)
{
memcpy (&((struct sockaddr_in *) &(priv->sockaddr))->sin_addr,
priv->hostinfo->h_addr,
priv->hostinfo->h_length);
GDICT_NOTE (DICT, "Hostname '%s' found (using IPv4)",
priv->hostname);
priv->last_lookup = time (NULL);
return TRUE;
}
else
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_LOOKUP,
_("Lookup failed for host '%s': host not found"),
priv->hostname);
return FALSE;
}
#ifdef ENABLE_IPV6
}
#endif
g_assert_not_reached ();
return FALSE;
}
/* gdict_client_context_parse_line: parses a line from the dictionary server
* this is the core of the RFC2229 protocol implementation, here's where
* the magic happens.
*/
static gboolean
gdict_client_context_parse_line (GdictClientContext *context,
const gchar *buffer)
{
GdictClientContextPrivate *priv;
GError *server_error = NULL;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
g_assert (buffer != NULL);
priv = context->priv;
GDICT_NOTE (DICT, "parse buffer: '%s'", buffer);
/* connection is a special case: we don't have a command, so we just
* make sure that the server replied with the correct code. WARNING:
* the server might be shutting down or not available, so we must
* take into account those responses too!
*/
if (!priv->command)
{
if (priv->status_code == GDICT_STATUS_CONNECT)
{
/* the server accepts our connection */
g_signal_emit (context, gdict_client_context_signals[CONNECTED], 0);
return TRUE;
}
else if ((priv->status_code == GDICT_STATUS_SERVER_DOWN) ||
(priv->status_code == GDICT_STATUS_SHUTDOWN))
{
/* the server is shutting down or is not available */
g_set_error (&server_error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SERVER_DOWN,
_("Unable to connect to the dictionary server "
"at '%s:%d'. The server replied with "
"code %d (server down)"),
priv->hostname,
priv->port,
priv->status_code);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
return TRUE;
}
else
{
GError *parse_error = NULL;
g_set_error (&parse_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_PARSE,
_("Unable to parse the dictionary server reply\n: '%s'"),
buffer);
g_signal_emit_by_name (context, "error", parse_error);
g_error_free (parse_error);
return FALSE;
}
}
/* disconnection is another special case: the server replies with code
* 221, and closes the connection; we emit the "disconnected" signal
* and close the connection on our side.
*/
if (priv->status_code == GDICT_STATUS_QUIT)
{
g_signal_emit (context, gdict_client_context_signals[DISCONNECTED], 0);
return TRUE;
}
/* here we catch all the errors codes that the server might give us */
server_error = NULL;
switch (priv->status_code)
{
case GDICT_STATUS_NO_MATCH:
g_set_error (&server_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_NO_MATCH,
_("No definitions found for '%s'"),
priv->command->word);
GDICT_NOTE (DICT, "No match: %s", server_error->message);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
server_error = NULL;
priv->command->state = S_FINISH;
break;
case GDICT_STATUS_BAD_DATABASE:
g_set_error (&server_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_INVALID_DATABASE,
_("Invalid database '%s'"),
priv->command->database);
GDICT_NOTE (DICT, "Bad DB: %s", server_error->message);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
server_error = NULL;
priv->command->state = S_FINISH;
break;
case GDICT_STATUS_BAD_STRATEGY:
g_set_error (&server_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_INVALID_STRATEGY,
_("Invalid strategy '%s'"),
priv->command->strategy);
GDICT_NOTE (DICT, "Bad strategy: %s", server_error->message);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
server_error = NULL;
priv->command->state = S_FINISH;
break;
case GDICT_STATUS_BAD_COMMAND:
g_set_error (&server_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_INVALID_COMMAND,
_("Bad command '%s'"),
dict_command_strings[priv->command->cmd_type]);
GDICT_NOTE (DICT, "Bad command: %s", server_error->message);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
server_error = NULL;
priv->command->state = S_FINISH;
break;
case GDICT_STATUS_BAD_PARAMETERS:
g_set_error (&server_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_INVALID_COMMAND,
_("Bad parameters for command '%s'"),
dict_command_strings[priv->command->cmd_type]);
GDICT_NOTE (DICT, "Bad params: %s", server_error->message);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
server_error = NULL;
priv->command->state = S_FINISH;
break;
case GDICT_STATUS_NO_DATABASES_PRESENT:
g_set_error (&server_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_NO_DATABASES,
_("No databases found on dictionary server at '%s'"),
priv->hostname);
GDICT_NOTE (DICT, "No DB: %s", server_error->message);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
server_error = NULL;
priv->command->state = S_FINISH;
break;
case GDICT_STATUS_NO_STRATEGIES_PRESENT:
g_set_error (&server_error, GDICT_CONTEXT_ERROR,
GDICT_CONTEXT_ERROR_NO_STRATEGIES,
_("No strategies found on dictionary server at '%s'"),
priv->hostname);
GDICT_NOTE (DICT, "No strategies: %s", server_error->message);
g_signal_emit_by_name (context, "error", server_error);
g_error_free (server_error);
server_error = NULL;
priv->command->state = S_FINISH;
break;
default:
GDICT_NOTE (DICT, "non-error code: %d", priv->status_code);
break;
}
/* server replied with 'ok' or the command has reached its FINISH state,
* so now we are clear for destroying the current command and check if
* there are other commands on the queue, and run them.
*/
if ((priv->status_code == GDICT_STATUS_OK) ||
(priv->command->state == S_FINISH))
{
GdictCommand *new_command;
GError *run_error;
GdictCommandType last_cmd;
last_cmd = priv->command->cmd_type;
gdict_command_free (priv->command);
priv->command = NULL;
/* notify the end of a command - ignore CLIENT and QUIT commands, as
* we issue them ourselves
*/
if ((last_cmd != CMD_CLIENT) && (last_cmd != CMD_QUIT))
g_signal_emit_by_name (context, "lookup-end");
/* pop the next command from the queue */
new_command = gdict_client_context_pop_command (context);
if (!new_command)
{
/* if the queue is empty, quit */
gdict_client_context_disconnect (context);
new_command = gdict_client_context_pop_command (context);
}
run_error = NULL;
gdict_client_context_run_command (context, new_command, &run_error);
if (run_error)
{
g_signal_emit_by_name (context, "error", run_error);
g_error_free (run_error);
}
return TRUE;
}
GDICT_NOTE (DICT, "check command %d ('%s')[state:%d]",
priv->command->cmd_type,
dict_command_strings[priv->command->cmd_type],
priv->command->state);
/* check command type */
switch (priv->command->cmd_type)
{
case CMD_CLIENT:
case CMD_QUIT:
break;
case CMD_SHOW_DB:
if (priv->status_code == GDICT_STATUS_N_DATABASES_PRESENT)
{
gchar *p;
priv->command->state = S_DATA;
p = g_utf8_strchr (buffer, -1, ' ');
if (p)
p = g_utf8_next_char (p);
GDICT_NOTE (DICT, "server replied: %d databases found", atoi (p));
g_signal_emit_by_name (context, "lookup-start");
}
else if (0 == strcmp (buffer, "."))
priv->command->state = S_FINISH;
else
{
GdictDatabase *db;
gchar *name, *full, *p;
g_assert (priv->command->state == S_DATA);
/* first token: database name;
* second token: database description;
*/
name = (gchar *) buffer;
if (!name)
break;
p = g_utf8_strchr (name, -1, ' ');
if (p)
*p = '\0';
full = g_utf8_next_char (p);
if (full[0] == '\"')
full = g_utf8_next_char (full);
p = g_utf8_strchr (full, -1, '\"');
if (p)
*p = '\0';
db = _gdict_database_new (name);
db->full_name = g_strdup (full);
g_signal_emit_by_name (context, "database-found", db);
gdict_database_unref (db);
}
break;
case CMD_SHOW_STRAT:
if (priv->status_code == GDICT_STATUS_N_STRATEGIES_PRESENT)
{
gchar *p;
priv->command->state = S_DATA;
p = g_utf8_strchr (buffer, -1, ' ');
if (p)
p = g_utf8_next_char (p);
GDICT_NOTE (DICT, "server replied: %d strategies found", atoi (p));
g_signal_emit_by_name (context, "lookup-start");
}
else if (0 == strcmp (buffer, "."))
priv->command->state = S_FINISH;
else
{
GdictStrategy *strat;
gchar *name, *desc, *p;
g_assert (priv->command->state == S_DATA);
name = (gchar *) buffer;
if (!name)
break;
p = g_utf8_strchr (name, -1, ' ');
if (p)
*p = '\0';
desc = g_utf8_next_char (p);
if (desc[0] == '\"')
desc = g_utf8_next_char (desc);
p = g_utf8_strchr (desc, -1, '\"');
if (p)
*p = '\0';
strat = _gdict_strategy_new (name);
strat->description = g_strdup (desc);
g_signal_emit_by_name (context, "strategy-found", strat);
gdict_strategy_unref (strat);
}
break;
case CMD_DEFINE:
if (priv->status_code == GDICT_STATUS_N_DEFINITIONS_RETRIEVED)
{
GdictDefinition *def;
gchar *p;
priv->command->state = S_STATUS;
p = g_utf8_strchr (buffer, -1, ' ');
if (p)
p = g_utf8_next_char (p);
GDICT_NOTE (DICT, "server replied: %d definitions found", atoi (p));
def = _gdict_definition_new (atoi (p));
priv->command->data = def;
priv->command->data_destroy = (GDestroyNotify) gdict_definition_unref;
g_signal_emit_by_name (context, "lookup-start");
}
else if (priv->status_code == GDICT_STATUS_WORD_DB_NAME)
{
GdictDefinition *def;
gchar *word, *db_name, *db_full, *p;
word = (gchar *) buffer;
/* skip the status code */
word = g_utf8_strchr (word, -1, ' ');
word = g_utf8_next_char (word);
if (word[0] == '\"')
word = g_utf8_next_char (word);
p = g_utf8_strchr (word, -1, '\"');
if (p)
*p = '\0';
p = g_utf8_next_char (p);
/* the database name is not protected by "" */
db_name = g_utf8_next_char (p);
if (!db_name)
break;
p = g_utf8_strchr (db_name, -1, ' ');
if (p)
*p = '\0';
p = g_utf8_next_char (p);
db_full = g_utf8_next_char (p);
if (!db_full)
break;
if (db_full[0] == '\"')
db_full = g_utf8_next_char (db_full);
p = g_utf8_strchr (db_full, -1, '\"');
if (p)
*p = '\0';
def = (GdictDefinition *) priv->command->data;
GDICT_NOTE (DICT, "{ word = '%s', db_name = '%s', db_full = '%s' }",
word,
db_name,
db_full);
def->word = g_strdup (word);
def->database_name = g_strdup (db_name);
def->database_full = g_strdup (db_full);
def->definition = NULL;
priv->command->state = S_DATA;
}
else if (strcmp (buffer, ".") == 0)
{
GdictDefinition *def;
gint num;
g_assert (priv->command->state == S_DATA);
def = (GdictDefinition *) priv->command->data;
if (!def)
break;
def->definition = g_string_free (priv->command->buffer, FALSE);
/* store the numer of definitions */
num = def->total;
g_signal_emit_by_name (context, "definition-found", def);
gdict_definition_unref (def);
priv->command->buffer = NULL;
priv->command->data = _gdict_definition_new (num);
priv->command->state = S_STATUS;
}
else
{
g_assert (priv->command->state == S_DATA);
if (!priv->command->buffer)
priv->command->buffer = g_string_new (NULL);
GDICT_NOTE (DICT, "appending to buffer:\n %s", buffer);
/* TODO - collapse '..' to '.' */
g_string_append_printf (priv->command->buffer, "%s\n", buffer);
}
break;
case CMD_MATCH:
if (priv->status_code == GDICT_STATUS_N_MATCHES_FOUND)
{
gchar *p;
priv->command->state = S_DATA;
p = g_utf8_strchr (buffer, -1, ' ');
if (p)
p = g_utf8_next_char (p);
GDICT_NOTE (DICT, "server replied: %d matches found", atoi (p));
g_signal_emit_by_name (context, "lookup-start");
}
else if (0 == strcmp (buffer, "."))
priv->command->state = S_FINISH;
else
{
GdictMatch *match;
gchar *word, *db_name, *p;
g_assert (priv->command->state == S_DATA);
db_name = (gchar *) buffer;
if (!db_name)
break;
p = g_utf8_strchr (db_name, -1, ' ');
if (p)
*p = '\0';
word = g_utf8_next_char (p);
if (word[0] == '\"')
word = g_utf8_next_char (word);
p = g_utf8_strchr (word, -1, '\"');
if (p)
*p = '\0';
match = _gdict_match_new (word);
match->database = g_strdup (db_name);
g_signal_emit_by_name (context, "match-found", match);
gdict_match_unref (match);
}
break;
default:
g_assert_not_reached ();
break;
}
return TRUE;
}
/* retrieve the status code from the server response line */
static gint
get_status_code (const gchar *line,
gint old_status)
{
gchar *status;
gint possible_status, retval;
if (strlen (line) < 3)
return 0;
if (!g_unichar_isdigit (line[0]) ||
!g_unichar_isdigit (line[1]) ||
!g_unichar_isdigit (line[2]))
return 0;
if (!g_unichar_isspace (line[3]))
return 0;
status = g_strndup (line, 3);
possible_status = atoi (status);
g_free (status);
/* status whitelisting: sometimes, a database *cough* moby-thes *cough*
* might return a number as first word; we do a small check here for
* invalid status codes based on the previously set status; we don't check
* the whole line, as we need only to be sure that the status code is
* consistent with what we expect.
*/
switch (old_status)
{
case GDICT_STATUS_WORD_DB_NAME:
case GDICT_STATUS_N_MATCHES_FOUND:
if (possible_status == GDICT_STATUS_OK)
retval = possible_status;
else
retval = 0;
break;
case GDICT_STATUS_N_DEFINITIONS_RETRIEVED:
if (possible_status == GDICT_STATUS_WORD_DB_NAME)
retval = possible_status;
else
retval = 0;
break;
default:
retval = possible_status;
break;
}
return retval;
}
static gboolean
gdict_client_context_io_watch_cb (GIOChannel *channel,
GIOCondition condition,
GdictClientContext *context)
{
GdictClientContextPrivate *priv;
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
priv = context->priv;
/* since this is an asynchronous channel, we might end up here
* even though the channel has been shut down.
*/
if (!priv->channel)
{
g_warning ("No channel available\n");
return FALSE;
}
if (priv->is_connecting)
{
priv->is_connecting = FALSE;
if (priv->timeout_id)
{
g_source_remove (priv->timeout_id);
priv->timeout_id = 0;
}
}
if (condition & G_IO_ERR)
{
GError *err = NULL;
g_set_error (&err, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SOCKET,
_("Connection failed to the dictionary server at %s:%d"),
priv->hostname,
priv->port);
g_signal_emit_by_name (context, "error", err);
g_error_free (err);
return FALSE;
}
while (1)
{
GIOStatus res;
guint status_code;
GError *read_err;
gsize len, term;
gchar *line;
gboolean parse_res;
/* we might sever the connection while still inside the read loop,
* so we must check the state of the channel before actually doing
* the line reading, otherwise we'll end up with death, destruction
* and chaos on all earth. oh, and an assertion failed inside
* g_io_channel_read_line().
*/
if (!priv->channel)
break;
read_err = NULL;
res = g_io_channel_read_line (priv->channel, &line, &len, &term, &read_err);
if (res == G_IO_STATUS_ERROR)
{
if (read_err)
{
GError *err = NULL;
g_set_error (&err, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SOCKET,
_("Error while reading reply from server:\n%s"),
read_err->message);
g_signal_emit_by_name (context, "error", err);
g_error_free (err);
g_error_free (read_err);
}
gdict_client_context_force_disconnect (context);
return FALSE;
}
if (len == 0)
break;
/* truncate the line terminator before parsing */
line[term] = '\0';
status_code = get_status_code (line, priv->status_code);
if ((status_code == 0) || (GDICT_IS_VALID_STATUS_CODE (status_code)))
{
priv->status_code = status_code;
GDICT_NOTE (DICT, "new status = '%d'", priv->status_code);
}
else
priv->status_code = GDICT_STATUS_INVALID;
/* notify changes only for valid status codes */
if (priv->status_code != GDICT_STATUS_INVALID)
g_object_notify (G_OBJECT (context), "status");
parse_res = gdict_client_context_parse_line (context, line);
if (!parse_res)
{
g_free (line);
g_warning ("Parsing failed");
gdict_client_context_force_disconnect (context);
return FALSE;
}
g_free (line);
}
return TRUE;
}
static gboolean
check_for_connection (gpointer data)
{
GdictClientContext *context = data;
#if 0
g_debug (G_STRLOC ": checking for connection (is connecting:%s)",
context->priv->is_connecting ? "true" : "false");
#endif
if (context == NULL)
return FALSE;
if (context->priv->is_connecting)
{
GError *err = NULL;
GDICT_NOTE (DICT, "Forcing a disconnection due to timeout");
g_set_error (&err, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SOCKET,
_("Connection timeout for the dictionary server at '%s:%d'"),
context->priv->hostname,
context->priv->port);
g_signal_emit_by_name (context, "error", err);
g_error_free (err);
gdict_client_context_force_disconnect (context);
}
/* this is a one-off operation */
return FALSE;
}
static gboolean
gdict_client_context_connect (GdictClientContext *context,
GError **error)
{
GdictClientContextPrivate *priv;
GError *lookup_error, *flags_error;
gboolean res;
gint sock_fd, sock_res;
gsize addrlen;
GIOFlags flags;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), FALSE);
priv = context->priv;
if (!priv->hostname)
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_LOOKUP,
_("No hostname defined for the dictionary server"));
return FALSE;
}
/* forgive the absence of a port */
if (!priv->port)
priv->port = GDICT_DEFAULT_PORT;
priv->is_connecting = TRUE;
lookup_error = NULL;
res = gdict_client_context_lookup_server (context, &lookup_error);
if (!res)
{
g_propagate_error (error, lookup_error);
return FALSE;
}
#ifdef ENABLE_IPV6
if (priv->sockaddr.ss_family == AF_INET6)
((struct sockaddr_in6 *) &priv->sockaddr)->sin6_port = g_htons (priv->port);
else
#endif
((struct sockaddr_in *) &priv->sockaddr)->sin_port = g_htons (priv->port);
#ifdef ENABLE_IPV6
if (priv->sockaddr.ss_family == AF_INET6)
{
sock_fd = socket (AF_INET6, SOCK_STREAM, 0);
if (sock_fd < 0)
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SOCKET,
_("Unable to create socket"));
return FALSE;
}
addrlen = sizeof (struct sockaddr_in6);
}
else
{
#endif /* ENABLE_IPV6 */
sock_fd = socket (AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0)
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SOCKET,
_("Unable to create socket"));
return FALSE;
}
addrlen = sizeof (struct sockaddr_in);
#ifdef ENABLE_IPV6
}
#endif
priv->channel = g_io_channel_unix_new (sock_fd);
/* RFC2229 mandates the usage of UTF-8, so we force this encoding */
g_io_channel_set_encoding (priv->channel, "UTF-8", NULL);
g_io_channel_set_line_term (priv->channel, "\r\n", 2);
/* make sure that the channel is non-blocking */
flags = g_io_channel_get_flags (priv->channel);
flags |= G_IO_FLAG_NONBLOCK;
flags_error = NULL;
g_io_channel_set_flags (priv->channel, flags, &flags_error);
if (flags_error)
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SOCKET,
_("Unable to set the channel as non-blocking: %s"),
flags_error->message);
g_error_free (flags_error);
g_io_channel_unref (priv->channel);
return FALSE;
}
/* let the magic begin */
sock_res = connect (sock_fd, (struct sockaddr *) &priv->sockaddr, addrlen);
if ((sock_res != 0) && (errno != EINPROGRESS))
{
g_set_error (error, GDICT_CLIENT_CONTEXT_ERROR,
GDICT_CLIENT_CONTEXT_ERROR_SOCKET,
_("Unable to connect to the dictionary server at '%s:%d'"),
priv->hostname,
priv->port);
return FALSE;
}
priv->timeout_id = g_timeout_add (CONNECTION_TIMEOUT * 1000,
check_for_connection,
context);
/* XXX - remember that g_io_add_watch() increases the reference count
* of the GIOChannel we are using.
*/
priv->source_id = g_io_add_watch (priv->channel,
(G_IO_IN | G_IO_ERR),
(GIOFunc) gdict_client_context_io_watch_cb,
context);
return TRUE;
}
static void
gdict_client_context_disconnect (GdictClientContext *context)
{
GdictCommand *cmd;
g_return_if_fail (GDICT_IS_CLIENT_CONTEXT (context));
/* instead of just breaking the connection to the server, we push
* a QUIT command on the queue, and wait for every other scheduled
* command to perform; this allows the creation of a batch of
* commands.
*/
cmd = gdict_command_new (CMD_QUIT);
cmd->state = S_FINISH;
gdict_client_context_push_command (context, cmd);
}
static gboolean
gdict_client_context_is_connected (GdictClientContext *context)
{
g_assert (GDICT_IS_CLIENT_CONTEXT (context));
/* we are in the middle of a connection attempt */
if (context->priv->is_connecting)
return TRUE;
return (context->priv->channel != NULL && context->priv->source_id != 0);
}
static gboolean
gdict_client_context_get_databases (GdictContext *context,
GError **error)
{
GdictClientContext *client_ctx;
GdictCommand *cmd;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), FALSE);
client_ctx = GDICT_CLIENT_CONTEXT (context);
if (!gdict_client_context_is_connected (client_ctx))
{
GError *connect_error = NULL;
gdict_client_context_connect (client_ctx, &connect_error);
if (connect_error)
{
g_propagate_error (error, connect_error);
return FALSE;
}
}
cmd = gdict_command_new (CMD_SHOW_DB);
return gdict_client_context_push_command (client_ctx, cmd);
}
static gboolean
gdict_client_context_get_strategies (GdictContext *context,
GError **error)
{
GdictClientContext *client_ctx;
GdictCommand *cmd;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), FALSE);
client_ctx = GDICT_CLIENT_CONTEXT (context);
if (!gdict_client_context_is_connected (client_ctx))
{
GError *connect_error = NULL;
gdict_client_context_connect (client_ctx, &connect_error);
if (connect_error)
{
g_propagate_error (error, connect_error);
return FALSE;
}
}
cmd = gdict_command_new (CMD_SHOW_STRAT);
return gdict_client_context_push_command (client_ctx, cmd);
}
static gboolean
gdict_client_context_define_word (GdictContext *context,
const gchar *database,
const gchar *word,
GError **error)
{
GdictClientContext *client_ctx;
GdictCommand *cmd;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), FALSE);
client_ctx = GDICT_CLIENT_CONTEXT (context);
if (!gdict_client_context_is_connected (client_ctx))
{
GError *connect_error = NULL;
gdict_client_context_connect (client_ctx, &connect_error);
if (connect_error)
{
g_propagate_error (error, connect_error);
return FALSE;
}
}
cmd = gdict_command_new (CMD_DEFINE);
cmd->database = g_strdup ((database != NULL ? database : GDICT_DEFAULT_DATABASE));
cmd->word = g_utf8_normalize (word, -1, G_NORMALIZE_NFC);
return gdict_client_context_push_command (client_ctx, cmd);
}
static gboolean
gdict_client_context_match_word (GdictContext *context,
const gchar *database,
const gchar *strategy,
const gchar *word,
GError **error)
{
GdictClientContext *client_ctx;
GdictCommand *cmd;
g_return_val_if_fail (GDICT_IS_CLIENT_CONTEXT (context), FALSE);
client_ctx = GDICT_CLIENT_CONTEXT (context);
if (!gdict_client_context_is_connected (client_ctx))
{
GError *connect_error = NULL;
gdict_client_context_connect (client_ctx, &connect_error);
if (connect_error)
{
g_propagate_error (error, connect_error);
return FALSE;
}
}
cmd = gdict_command_new (CMD_MATCH);
cmd->database = g_strdup ((database != NULL ? database : GDICT_DEFAULT_DATABASE));
cmd->strategy = g_strdup ((strategy != NULL ? strategy : GDICT_DEFAULT_STRATEGY));
cmd->word = g_utf8_normalize (word, -1, G_NORMALIZE_NFC);
return gdict_client_context_push_command (client_ctx, cmd);
}
|