aboutsummaryrefslogtreecommitdiff
path: root/zyklonb.c
blob: f8d931a7de9f432244a5b6de1ce22d2d6c876d7d (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
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
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
/*
 * zyklonb.c: the experimental IRC bot
 *
 * Copyright (c) 2014, Přemysl Janouch <p.janouch@gmail.com>
 * All rights reserved.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#define PROGRAM_NAME "ZyklonB"
#define PROGRAM_VERSION "alpha"

#include "common.c"
#include <arpa/inet.h>

// --- Configuration (application-specific) ------------------------------------

static struct config_item g_config_table[] =
{
	{ "nickname",        "ZyklonB",         "IRC nickname"                   },
	{ "username",        "bot",             "IRC user name"                  },
	{ "realname",        "ZyklonB IRC bot", "IRC real name/e-mail"           },

	{ "irc_host",        NULL,              "Address of the IRC server"      },
	{ "irc_port",        "6667",            "Port of the IRC server"         },
	{ "ssl_use",         "off",             "Whether to use SSL"             },
	{ "ssl_cert",        NULL,              "Client SSL certificate (PEM)"   },
	{ "autojoin",        NULL,              "Channels to join on start"      },
	{ "reconnect",       "on",              "Whether to reconnect on error"  },
	{ "reconnect_delay", "5",               "Time between reconnecting"      },

	{ "socks_host",      NULL,              "Address of a SOCKS 4a/5 proxy"  },
	{ "socks_port",      "1080",            "SOCKS port number"              },
	{ "socks_username",  NULL,              "SOCKS auth. username"           },
	{ "socks_password",  NULL,              "SOCKS auth. password"           },

	{ "prefix",          ":",               "The prefix for bot commands"    },
	{ "admin",           NULL,              "Host mask for administrators"   },
	{ "plugins",         NULL,              "The plugins to load on startup" },
	{ "plugin_dir",      NULL,              "Where to search for plugins"    },
	{ "recover",         "on",              "Whether to re-launch on crash"  },

	{ NULL,              NULL,              NULL                             }
};

// --- Application data --------------------------------------------------------

struct plugin_data
{
	LIST_HEADER (plugin_data)
	struct bot_context *ctx;            ///< Parent context

	char *name;                         ///< Plugin identifier
	pid_t pid;                          ///< PID of the plugin process

	bool is_zombie;                     ///< Whether the child is a zombie
	bool initialized;                   ///< Ready to exchange IRC messages
	struct str queued_output;           ///< Output queued up until initialized

	// Since we're doing non-blocking I/O, we need to queue up data so that
	// we don't stall on plugins unnecessarily.

	int read_fd;                        ///< The read end of the comm. pipe
	int write_fd;                       ///< The write end of the comm. pipe

	struct str read_buffer;             ///< Unprocessed input
	struct str write_buffer;            ///< Output yet to be sent out
};

static void
plugin_data_init (struct plugin_data *self)
{
	memset (self, 0, sizeof *self);

	self->pid = -1;
	str_init (&self->queued_output);

	self->read_fd = -1;
	str_init (&self->read_buffer);
	self->write_fd = -1;
	str_init (&self->write_buffer);
}

static void
plugin_data_free (struct plugin_data *self)
{
	soft_assert (self->pid == -1);
	free (self->name);

	str_free (&self->read_buffer);
	if (!soft_assert (self->read_fd == -1))
		xclose (self->read_fd);

	str_free (&self->write_buffer);
	if (!soft_assert (self->write_fd == -1))
		xclose (self->write_fd);

	if (!self->initialized)
		str_free (&self->queued_output);
}

struct bot_context
{
	struct str_map config;              ///< User configuration
	regex_t *admin_re;                  ///< Regex to match our administrator
	bool reconnect;                     ///< Whether to reconnect on conn. fail.
	unsigned long reconnect_delay;      ///< Reconnect delay in seconds

	int irc_fd;                         ///< Socket FD of the server
	struct str read_buffer;             ///< Input yet to be processed
	bool irc_ready;                     ///< Whether we may send messages now

	SSL_CTX *ssl_ctx;                   ///< SSL context
	SSL *ssl;                           ///< SSL connection

	struct plugin_data *plugins;        ///< Linked list of plugins
	struct str_map plugins_by_name;     ///< Indexes @em plugins by their name

	struct poller poller;               ///< Manages polled descriptors
	bool quitting;                      ///< User requested quitting
	bool polling;                       ///< The event loop is running
};

static void
bot_context_init (struct bot_context *self)
{
	str_map_init (&self->config);
	self->config.free = free;
	load_config_defaults (&self->config, g_config_table);
	self->admin_re = NULL;

	self->irc_fd = -1;
	str_init (&self->read_buffer);
	self->irc_ready = false;

	self->ssl = NULL;
	self->ssl_ctx = NULL;

	self->plugins = NULL;
	str_map_init (&self->plugins_by_name);

	poller_init (&self->poller);
	self->quitting = false;
	self->polling = false;
}

static void
bot_context_free (struct bot_context *self)
{
	str_map_free (&self->config);
	if (self->admin_re)
		regex_free (self->admin_re);
	str_free (&self->read_buffer);

	// TODO: terminate the plugins properly before this is called
	struct plugin_data *link, *tmp;
	for (link = self->plugins; link; link = tmp)
	{
		tmp = link->next;
		plugin_data_free (link);
		free (link);
	}

	if (self->irc_fd != -1)
		xclose (self->irc_fd);
	if (self->ssl)
		SSL_free (self->ssl);
	if (self->ssl_ctx)
		SSL_CTX_free (self->ssl_ctx);

	str_map_free (&self->plugins_by_name);
	poller_free (&self->poller);
}

static void
irc_shutdown (struct bot_context *ctx)
{
	// TODO: set a timer after which we cut the connection?
	// Generally non-critical
	if (ctx->ssl)
		soft_assert (SSL_shutdown (ctx->ssl) != -1);
	else
		soft_assert (shutdown (ctx->irc_fd, SHUT_WR) == 0);
}

static void
try_finish_quit (struct bot_context *ctx)
{
	if (ctx->quitting && ctx->irc_fd == -1 && !ctx->plugins)
		ctx->polling = false;
}

static bool plugin_zombify (struct plugin_data *);

static void
initiate_quit (struct bot_context *ctx)
{
	// Initiate bringing down of the two things that block our shutdown:
	// a/ the IRC socket, b/ our child processes:

	for (struct plugin_data *plugin = ctx->plugins;
		plugin; plugin = plugin->next)
		plugin_zombify (plugin);
	if (ctx->irc_fd != -1)
		irc_shutdown (ctx);

	ctx->quitting = true;
	try_finish_quit (ctx);
}

static bool irc_send (struct bot_context *ctx,
	const char *format, ...) ATTRIBUTE_PRINTF (2, 3);

static bool
irc_send (struct bot_context *ctx, const char *format, ...)
{
	va_list ap;

	if (g_debug_mode)
	{
		fputs ("[IRC] <== \"", stderr);
		va_start (ap, format);
		vfprintf (stderr, format, ap);
		va_end (ap);
		fputs ("\"\n", stderr);
	}

	if (!soft_assert (ctx->irc_fd != -1))
		return false;

	va_start (ap, format);
	struct str str;
	str_init (&str);
	str_append_vprintf (&str, format, ap);
	str_append (&str, "\r\n");
	va_end (ap);

	bool result = true;
	if (ctx->ssl)
	{
		// TODO: call SSL_get_error() to detect if a clean shutdown has occured
		if (SSL_write (ctx->ssl, str.str, str.len) != (int) str.len)
		{
			print_debug ("%s: %s: %s", __func__, "SSL_write",
				ERR_error_string (ERR_get_error (), NULL));
			result = false;
		}
	}
	else if (write (ctx->irc_fd, str.str, str.len) != (ssize_t) str.len)
	{
		print_debug ("%s: %s: %s", __func__, "write", strerror (errno));
		result = false;
	}

	str_free (&str);
	return result;
}

static bool
irc_initialize_ssl (struct bot_context *ctx, struct error **e)
{
	ctx->ssl_ctx = SSL_CTX_new (SSLv23_client_method ());
	if (!ctx->ssl_ctx)
		goto error_ssl_1;
	// We don't care; some encryption is always better than no encryption
	SSL_CTX_set_verify (ctx->ssl_ctx, SSL_VERIFY_NONE, NULL);
	// XXX: maybe we should call SSL_CTX_set_options() for some workarounds

	ctx->ssl = SSL_new (ctx->ssl_ctx);
	if (!ctx->ssl)
		goto error_ssl_2;

	const char *ssl_cert = str_map_find (&ctx->config, "ssl_cert");
	if (ssl_cert)
	{
		char *path = resolve_config_filename (ssl_cert);
		if (!path)
			print_error ("%s: %s", "cannot open file", ssl_cert);
		// XXX: perhaps we should read the file ourselves for better messages
		else if (!SSL_use_certificate_file (ctx->ssl, path, SSL_FILETYPE_PEM)
			|| !SSL_use_PrivateKey_file (ctx->ssl, path, SSL_FILETYPE_PEM))
			print_error ("%s: %s", "setting the SSL client certificate failed",
				ERR_error_string (ERR_get_error (), NULL));
		free (path);
	}

	SSL_set_connect_state (ctx->ssl);
	if (!SSL_set_fd (ctx->ssl, ctx->irc_fd))
		goto error_ssl_3;
	// Avoid SSL_write() returning SSL_ERROR_WANT_READ
	SSL_set_mode (ctx->ssl, SSL_MODE_AUTO_RETRY);
	if (SSL_connect (ctx->ssl) > 0)
		return true;

error_ssl_3:
	SSL_free (ctx->ssl);
	ctx->ssl = NULL;
error_ssl_2:
	SSL_CTX_free (ctx->ssl_ctx);
	ctx->ssl_ctx = NULL;
error_ssl_1:
	// XXX: these error strings are really nasty; also there could be
	//   multiple errors on the OpenSSL stack.
	error_set (e, "%s: %s", "could not initialize SSL",
		ERR_error_string (ERR_get_error (), NULL));
	return false;
}

static bool
irc_establish_connection (struct bot_context *ctx,
	const char *host, const char *port, struct error **e)
{
	struct addrinfo gai_hints, *gai_result, *gai_iter;
	memset (&gai_hints, 0, sizeof gai_hints);

	// We definitely want TCP.
	gai_hints.ai_socktype = SOCK_STREAM;

	int err = getaddrinfo (host, port, &gai_hints, &gai_result);
	if (err)
	{
		error_set (e, "%s: %s: %s",
			"connection failed", "getaddrinfo", gai_strerror (err));
		return false;
	}

	int sockfd;
	for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next)
	{
		sockfd = socket (gai_iter->ai_family,
			gai_iter->ai_socktype, gai_iter->ai_protocol);
		if (sockfd == -1)
			continue;
		set_cloexec (sockfd);

		int yes = 1;
		soft_assert (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE,
			&yes, sizeof yes) != -1);

		const char *real_host = host;

		// Let's try to resolve the address back into a real hostname;
		// we don't really need this, so we can let it quietly fail
		char buf[NI_MAXHOST];
		err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen,
			buf, sizeof buf, NULL, 0, 0);
		if (err)
			print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
		else
			real_host = buf;

		// XXX: we shouldn't mix these statuses with `struct error'; choose 1!
		char *address = format_host_port_pair (real_host, port);
		print_status ("connecting to %s...", address);
		free (address);

		if (!connect (sockfd, gai_iter->ai_addr, gai_iter->ai_addrlen))
			break;

		xclose (sockfd);
	}

	freeaddrinfo (gai_result);

	if (!gai_iter)
	{
		error_set (e, "connection failed");
		return false;
	}

	ctx->irc_fd = sockfd;
	return true;
}

// --- Signals -----------------------------------------------------------------

static int g_signal_pipe[2];            ///< A pipe used to signal... signals

static struct str_vector
	g_original_argv,                    ///< Original program arguments
	g_recovery_env;                     ///< Environment for re-exec recovery

/// Program termination has been requested by a signal
static volatile sig_atomic_t g_termination_requested;

/// Points to startup reason location within `g_recovery_environment'
static char **g_startup_reason_location;
/// The environment variable used to pass the startup reason when re-executing
static const char g_startup_reason_str[] = "STARTUP_REASON";

static void
sigchld_handler (int signum)
{
	(void) signum;

	int original_errno = errno;
	// Just so that the read end of the pipe wakes up the poller.
	// NOTE: Linux has signalfd() and eventfd(), and the BSD's have kqueue.
	//   All of them are better than this approach, although platform-specific.
	if (write (g_signal_pipe[1], "c", 1) == -1)
		soft_assert (errno == EAGAIN);
	errno = original_errno;
}

static void
sigterm_handler (int signum)
{
	(void) signum;

	g_termination_requested = true;

	int original_errno = errno;
	if (write (g_signal_pipe[1], "t", 1) == -1)
		soft_assert (errno == EAGAIN);
	errno = original_errno;
}

static void
setup_signal_handlers (void)
{
	if (pipe (g_signal_pipe) == -1)
		exit_fatal ("%s: %s", "pipe", strerror (errno));

	set_cloexec (g_signal_pipe[0]);
	set_cloexec (g_signal_pipe[1]);

	// So that the pipe cannot overflow; it would make write() block within
	// the signal handler, which is something we really don't want to happen.
	// The same holds true for read().
	set_blocking (g_signal_pipe[0], false);
	set_blocking (g_signal_pipe[1], false);

	struct sigaction sa;
	sa.sa_flags = SA_RESTART;
	sa.sa_handler = sigchld_handler;
	sigemptyset (&sa.sa_mask);

	if (sigaction (SIGCHLD, &sa, NULL) == -1)
		exit_fatal ("sigaction: %s", strerror (errno));

	signal (SIGPIPE, SIG_IGN);

	sa.sa_handler = sigterm_handler;
	if (sigaction (SIGINT, &sa, NULL) == -1
	 || sigaction (SIGTERM, &sa, NULL) == -1)
		exit_fatal ("sigaction: %s", strerror (errno));
}

static void
translate_signal_info (int no, const char **name, int code, const char **reason)
{
	if (code == SI_USER)         *reason = "signal sent by kill()";
	if (code == SI_QUEUE)        *reason = "signal sent by sigqueue()";

	switch (no)
	{
	case SIGILL:
		*name = "SIGILL";
		if (code == ILL_ILLOPC)  *reason = "illegal opcode";
		if (code == ILL_ILLOPN)  *reason = "illegal operand";
		if (code == ILL_ILLADR)  *reason = "illegal addressing mode";
		if (code == ILL_ILLTRP)  *reason = "illegal trap";
		if (code == ILL_PRVOPC)  *reason = "privileged opcode";
		if (code == ILL_PRVREG)  *reason = "privileged register";
		if (code == ILL_COPROC)  *reason = "coprocessor error";
		if (code == ILL_BADSTK)  *reason = "internal stack error";
		break;
	case SIGFPE:
		*name = "SIGFPE";
		if (code == FPE_INTDIV)  *reason = "integer divide by zero";
		if (code == FPE_INTOVF)  *reason = "integer overflow";
		if (code == FPE_FLTDIV)  *reason = "floating-point divide by zero";
		if (code == FPE_FLTOVF)  *reason = "floating-point overflow";
		if (code == FPE_FLTUND)  *reason = "floating-point underflow";
		if (code == FPE_FLTRES)  *reason = "floating-point inexact result";
		if (code == FPE_FLTINV)  *reason = "invalid floating-point operation";
		if (code == FPE_FLTSUB)  *reason = "subscript out of range";
		break;
	case SIGSEGV:
		*name = "SIGSEGV";
		if (code == SEGV_MAPERR)
			*reason = "address not mapped to object";
		if (code == SEGV_ACCERR)
			*reason = "invalid permissions for mapped object";
		break;
	case SIGBUS:
		*name = "SIGBUS";
		if (code == BUS_ADRALN)  *reason = "invalid address alignment";
		if (code == BUS_ADRERR)  *reason = "nonexistent physical address";
		if (code == BUS_OBJERR)  *reason = "object-specific hardware error";
		break;
	default:
		*name = NULL;
	}
}

static void
recovery_handler (int signum, siginfo_t *info, void *context)
{
	(void) context;

	// TODO: maybe try to force a core dump like this: if (fork() == 0) return;
	// TODO: maybe we could even send "\r\nQUIT :reason\r\n" to the server. >_>
	//   As long as we're not connected via TLS, that is.

	const char *signal_name = NULL, *reason = NULL;
	translate_signal_info (signum, &signal_name, info->si_code, &reason);

	char buf[128], numbuf[8];
	if (!signal_name)
	{
		snprintf (numbuf, sizeof numbuf, "%d", signum);
		signal_name = numbuf;
	}

	if (reason)
		snprintf (buf, sizeof buf, "%s=%s: %s: %s", g_startup_reason_str,
			"signal received", signal_name, reason);
	else
		snprintf (buf, sizeof buf, "%s=%s: %s", g_startup_reason_str,
			"signal received", signal_name);
	*g_startup_reason_location = buf;

	// TODO: maybe pregenerate the path, see the following for some other ways
	//   that would be illegal to do from within a signal handler:
	//     http://stackoverflow.com/a/1024937
	//     http://stackoverflow.com/q/799679
	//   Especially if we change the current working directory in the program.
	//
	//   Note that I can just overwrite g_orig_argv[0].

	// NOTE: our children will read EOF on the read ends of their pipes as a
	//   a result of O_CLOEXEC.  That should be enough to make them terminate.

	char **argv = g_original_argv.vector, **argp = g_recovery_env.vector;
	execve ("/proc/self/exe",        argv, argp);  // Linux
	execve ("/proc/curproc/file",    argv, argp);  // BSD
	execve ("/proc/curproc/exe",     argv, argp);  // BSD
	execve ("/proc/self/path/a.out", argv, argp);  // Solaris
	execve (argv[0],                 argv, argp);  // unreliable fallback

	// Let's just crash
	perror ("execve");
	signal (signum, SIG_DFL);
	raise (signum);
}

static void
prepare_recovery_environment (void)
{
	str_vector_init (&g_recovery_env);
	str_vector_add_vector (&g_recovery_env, environ);

	// Prepare a location within the environment where we will put the startup
	// (or maybe rather restart) reason in case of an irrecoverable error.
	char **iter;
	for (iter = g_recovery_env.vector; *iter; iter++)
	{
		const size_t len = sizeof g_startup_reason_str - 1;
		if (!strncmp (*iter, g_startup_reason_str, len) && (*iter)[len] == '=')
			break;
	}

	if (iter)
		g_startup_reason_location = iter;
	else
	{
		str_vector_add (&g_recovery_env, "");
		g_startup_reason_location =
			g_recovery_env.vector + g_recovery_env.len - 1;
	}
}

static void
setup_recovery_handler (struct bot_context *ctx)
{
	const char *recover_str = str_map_find (&ctx->config, "recover");
	hard_assert (recover_str != NULL);  // We have a default value for this

	bool recover;
	if (!set_boolean_if_valid (&recover, recover_str))
	{
		print_error ("invalid configuration value for `%s'", "recover");
		exit (EXIT_FAILURE);
	}
	if (!recover)
		return;

	// Make sure these signals aren't blocked, otherwise we would be unable
	// to handle them, making the critical conditions fatal.
	sigset_t mask;
	sigemptyset (&mask);
	sigaddset (&mask, SIGSEGV);
	sigaddset (&mask, SIGBUS);
	sigaddset (&mask, SIGFPE);
	sigaddset (&mask, SIGILL);
	sigprocmask (SIG_UNBLOCK, &mask, NULL);

	struct sigaction sa;
	sa.sa_flags = SA_SIGINFO;
	sa.sa_sigaction = recovery_handler;
	sigemptyset (&sa.sa_mask);

	prepare_recovery_environment ();

	// TODO: also handle SIGABRT... or avoid doing abort() in the first place?
	if (sigaction (SIGSEGV, &sa, NULL) == -1
	 || sigaction (SIGBUS,  &sa, NULL) == -1
	 || sigaction (SIGFPE,  &sa, NULL) == -1
	 || sigaction (SIGILL,  &sa, NULL) == -1)
		print_error ("sigaction: %s", strerror (errno));
}

// --- SOCKS 5/4a (blocking implementation) ------------------------------------

// These are awkward protocols.  Note that the `username' is used differently
// in SOCKS 4a and 5.  In the former version, it is the username that you can
// get ident'ed against.  In the latter version, it forms a pair with the
// password field and doesn't need to be an actual user on your machine.

// TODO: make a non-blocking poller-based version of this; we need c-ares

struct socks_addr
{
	enum socks_addr_type
	{
		SOCKS_IPV4 = 1,                 ///< IPv4 address
		SOCKS_DOMAIN = 3,               ///< Domain name to be resolved
		SOCKS_IPV6 = 4                  ///< IPv6 address
	}
	type;                               ///< The type of this address
	union
	{
		uint8_t ipv4[4];                ///< IPv4 address, network octet order
		const char *domain;             ///< Domain name
		uint8_t ipv6[16];               ///< IPv6 address, network octet order
	}
	data;                               ///< The address itself
};

struct socks_data
{
	struct socks_addr address;          ///< Target address
	uint16_t port;                      ///< Target port
	const char *username;               ///< Authentication username
	const char *password;               ///< Authentication password

	struct socks_addr bound_address;    ///< Bound address at the server
	uint16_t bound_port;                ///< Bound port at the server
};

static bool
socks_get_socket (struct addrinfo *addresses, int *fd, struct error **e)
{
	int sockfd;
	for (; addresses; addresses = addresses->ai_next)
	{
		sockfd = socket (addresses->ai_family,
			addresses->ai_socktype, addresses->ai_protocol);
		if (sockfd == -1)
			continue;
		set_cloexec (sockfd);

		int yes = 1;
		soft_assert (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE,
			&yes, sizeof yes) != -1);

		if (!connect (sockfd, addresses->ai_addr, addresses->ai_addrlen))
			break;
		xclose (sockfd);
	}
	if (!addresses)
	{
		error_set (e, "couldn't connect to the SOCKS server");
		return false;
	}
	*fd = sockfd;
	return true;
}

#define SOCKS_FAIL(...)                                                        \
	BLOCK_START                                                                \
		error_set (e, __VA_ARGS__);                                            \
		goto fail;                                                             \
	BLOCK_END
#define SOCKS_RECV(buf, len)                                                   \
	BLOCK_START                                                                \
		if ((n = recv (sockfd, (buf), (len), 0)) == -1)                        \
			SOCKS_FAIL ("%s: %s", "recv", strerror (errno));                   \
		if (n != (len))                                                        \
			SOCKS_FAIL ("%s: %s", "protocol error", "unexpected EOF");         \
	BLOCK_END

static bool
socks_4a_connect (struct addrinfo *addresses, struct socks_data *data,
	int *fd, struct error **e)
{
	int sockfd;
	if (!socks_get_socket (addresses, &sockfd, e))
		return false;

	const void *dest_ipv4 = "\x00\x00\x00\x01";
	const char *dest_domain = NULL;

	char buf[INET6_ADDRSTRLEN];
	switch (data->address.type)
	{
	case SOCKS_IPV4:
		dest_ipv4 = data->address.data.ipv4;
		break;
	case SOCKS_IPV6:
		// About the best thing we can do, not sure if it works anywhere at all
		if (!inet_ntop (AF_INET6, &data->address.data.ipv6, buf, sizeof buf))
			SOCKS_FAIL ("%s: %s", "inet_ntop", strerror (errno));
		dest_domain = buf;
		break;
	case SOCKS_DOMAIN:
		dest_domain = data->address.data.domain;
	}

	struct str req;
	str_init (&req);
	str_append_c (&req, 4);                // version
	str_append_c (&req, 1);                // connect

	str_append_c (&req, data->port >> 8);  // higher bits of port
	str_append_c (&req, data->port);       // lower bits of port
	str_append_data (&req, dest_ipv4, 4);  // destination address

	if (data->username)
		str_append (&req, data->username);
	str_append_c (&req, '\0');

	if (dest_domain)
	{
		str_append (&req, dest_domain);
		str_append_c (&req, '\0');
	}

	ssize_t n = send (sockfd, req.str, req.len, 0);
	str_free (&req);
	if (n == -1)
		SOCKS_FAIL ("%s: %s", "send", strerror (errno));

	uint8_t resp[8];
	SOCKS_RECV (resp, sizeof resp);
	if (resp[0] != 0)
		SOCKS_FAIL ("protocol error");

	switch (resp[1])
	{
	case 90:
		break;
	case 91:
		SOCKS_FAIL ("request rejected or failed");
	case 92:
		SOCKS_FAIL ("%s: %s", "request rejected",
			"SOCKS server cannot connect to identd on the client");
	case 93:
		SOCKS_FAIL ("%s: %s", "request rejected",
			"identd reports different user-id");
	default:
		SOCKS_FAIL ("protocol error");
	}

	*fd = sockfd;
	return true;

fail:
	xclose (sockfd);
	return false;
}

#undef SOCKS_FAIL
#define SOCKS_FAIL(...)                                                        \
	BLOCK_START                                                                \
		error_set (e, __VA_ARGS__);                                            \
		return false;                                                          \
	BLOCK_END

static bool
socks_5_userpass_auth (int sockfd, struct socks_data *data, struct error **e)
{
	size_t ulen = strlen (data->username);
	if (ulen > 255)
		ulen = 255;

	size_t plen = strlen (data->password);
	if (plen > 255)
		plen = 255;

	uint8_t req[3 + ulen + plen], *p = req;
	*p++ = 0x01;  // version
	*p++ = ulen;  // username length
	memcpy (p, data->username, ulen);
	p += ulen;
	*p++ = plen;  // password length
	memcpy (p, data->password, plen);
	p += plen;

	ssize_t n = send (sockfd, req, p - req, 0);
	if (n == -1)
		SOCKS_FAIL ("%s: %s", "send", strerror (errno));

	uint8_t resp[2];
	SOCKS_RECV (resp, sizeof resp);
	if (resp[0] != 0x01)
		SOCKS_FAIL ("protocol error");
	if (resp[1] != 0x00)
		SOCKS_FAIL ("authentication failure");
	return true;
}

static bool
socks_5_auth (int sockfd, struct socks_data *data, struct error **e)
{
	bool can_auth = data->username && data->password;

	uint8_t hello[4];
	hello[0] = 0x05;          // version
	hello[1] = 1 + can_auth;  // number of authentication methods
	hello[2] = 0x00;          // no authentication required
	hello[3] = 0x02;          // username/password

	ssize_t n = send (sockfd, hello, 3 + can_auth, 0);
	if (n == -1)
		SOCKS_FAIL ("%s: %s", "send", strerror (errno));

	uint8_t resp[2];
	SOCKS_RECV (resp, sizeof resp);
	if (resp[0] != 0x05)
		SOCKS_FAIL ("protocol error");

	switch (resp[1])
	{
	case 0x02:
		if (!can_auth)
			SOCKS_FAIL ("protocol error");
		if (!socks_5_userpass_auth (sockfd, data, e))
			return false;
	case 0x00:
		break;
	case 0xFF:
		SOCKS_FAIL ("no acceptable authentication methods");
	default:
		SOCKS_FAIL ("protocol error");
	}
	return true;
}

static bool
socks_5_send_req (int sockfd, struct socks_data *data, struct error **e)
{
	uint8_t req[4 + 256 + 2], *p = req;
	*p++ = 0x05;  // version
	*p++ = 0x01;  // connect
	*p++ = 0x00;  // reserved
	*p++ = data->address.type;

	switch (data->address.type)
	{
	case SOCKS_IPV4:
		memcpy (p, data->address.data.ipv4, sizeof data->address.data.ipv4);
		p += sizeof data->address.data.ipv4;
		break;
	case SOCKS_DOMAIN:
	{
		size_t dlen = strlen (data->address.data.domain);
		if (dlen > 255)
			dlen = 255;

		*p++ = dlen;
		memcpy (p, data->address.data.domain, dlen);
		p += dlen;
		break;
	}
	case SOCKS_IPV6:
		memcpy (p, data->address.data.ipv6, sizeof data->address.data.ipv6);
		p += sizeof data->address.data.ipv6;
		break;
	}
	*p++ = data->port >> 8;
	*p++ = data->port;

	if (send (sockfd, req, p - req, 0) == -1)
		SOCKS_FAIL ("%s: %s", "send", strerror (errno));
	return true;
}

static bool
socks_5_process_resp (int sockfd, struct socks_data *data, struct error **e)
{
	uint8_t resp_header[4];
	ssize_t n;
	SOCKS_RECV (resp_header, sizeof resp_header);
	if (resp_header[0] != 0x05)
		SOCKS_FAIL ("protocol error");

	switch (resp_header[1])
	{
	case 0x00:
		break;
	case 0x01:  SOCKS_FAIL ("general SOCKS server failure");
	case 0x02:  SOCKS_FAIL ("connection not allowed by ruleset");
	case 0x03:  SOCKS_FAIL ("network unreachable");
	case 0x04:  SOCKS_FAIL ("host unreachable");
	case 0x05:  SOCKS_FAIL ("connection refused");
	case 0x06:  SOCKS_FAIL ("TTL expired");
	case 0x07:  SOCKS_FAIL ("command not supported");
	case 0x08:  SOCKS_FAIL ("address type not supported");
	default:    SOCKS_FAIL ("protocol error");
	}

	switch ((data->bound_address.type = resp_header[3]))
	{
	case SOCKS_IPV4:
		SOCKS_RECV (data->bound_address.data.ipv4,
			sizeof data->bound_address.data.ipv4);
		break;
	case SOCKS_IPV6:
		SOCKS_RECV (data->bound_address.data.ipv6,
			sizeof data->bound_address.data.ipv6);
		break;
	case SOCKS_DOMAIN:
	{
		uint8_t len;
		SOCKS_RECV (&len, sizeof len);

		char domain[len + 1];
		SOCKS_RECV (domain, len);
		domain[len] = '\0';

		data->bound_address.data.domain = xstrdup (domain);
	}
	default:
		SOCKS_FAIL ("protocol error");
	}

	uint16_t port;
	SOCKS_RECV (&port, sizeof port);
	data->bound_port = ntohs (port);
	return true;
}

#undef SOCKS_FAIL
#undef SOCKS_RECV

static bool
socks_5_connect (struct addrinfo *addresses, struct socks_data *data,
	int *fd, struct error **e)
{
	int sockfd;
	if (!socks_get_socket (addresses, &sockfd, e))
		return false;

	if (!socks_5_auth (sockfd, data, e)
	 || !socks_5_send_req (sockfd, data, e)
	 || !socks_5_process_resp (sockfd, data, e))
	{
		xclose (sockfd);
		return false;
	}

	*fd = sockfd;
	return true;
}

static int
socks_connect (const char *socks_host, const char *socks_port,
	const char *host, const char *port,
	const char *username, const char *password, struct error **e)
{
	struct addrinfo gai_hints, *gai_result;
	memset (&gai_hints, 0, sizeof gai_hints);
	gai_hints.ai_socktype = SOCK_STREAM;

	int err = getaddrinfo (socks_host, socks_port, &gai_hints, &gai_result);
	if (err)
	{
		error_set (e, "%s: %s", "getaddrinfo", gai_strerror (err));
		return false;
	}

	unsigned long port_no;
	const struct servent *serv;
	if ((serv = getservbyname (port, "tcp")))
		port_no = (uint16_t) ntohs (serv->s_port);
	else if (!xstrtoul (&port_no, port, 10) || !port_no || port_no > UINT16_MAX)
	{
		error_set (e, "invalid port number");
		return false;
	}

	struct socks_data data =
		{ .username = username, .password = password, .port = port_no };

	if      (inet_pton (AF_INET,  host, &data.address.data.ipv4) == 1)
		data.address.type = SOCKS_IPV4;
	else if (inet_pton (AF_INET6, host, &data.address.data.ipv6) == 1)
		data.address.type = SOCKS_IPV6;
	else
	{
		data.address.type = SOCKS_DOMAIN;
		data.address.data.domain = host;
	}

	int fd;
	bool success = socks_5_connect (gai_result, &data, &fd, NULL)
		|| socks_4a_connect (gai_result, &data, &fd, e);

	freeaddrinfo (gai_result);
	if (data.bound_address.type == SOCKS_DOMAIN)
		free ((char *) data.bound_address.data.domain);
	return success ? fd : -1;
}

// --- Plugins -----------------------------------------------------------------

/// The name of the special IRC command for interprocess communication
static const char *plugin_ipc_command = "ZYKLONB";

static struct plugin_data *
plugin_find_by_pid (struct bot_context *ctx, pid_t pid)
{
	struct plugin_data *iter;
	for (iter = ctx->plugins; iter; iter = iter->next)
		if (iter->pid == pid)
			return iter;
	return NULL;
}

static bool
plugin_zombify (struct plugin_data *plugin)
{
	if (plugin->is_zombie)
		return false;

	// FIXME: make sure that we don't remove entries from the poller while we
	//   still may have stuff to read; maybe just check that the read pipe is
	//   empty before closing it... and then on EOF check if `pid == -1' and
	//   only then dispose of it (it'd be best to simulate that both of these
	//   cases may happen).
	ssize_t poller_idx =
		poller_find_by_fd (&plugin->ctx->poller, plugin->write_fd);
	if (poller_idx != -1)
		poller_remove_at_index (&plugin->ctx->poller, poller_idx);

	// TODO: try to flush the write buffer (non-blocking)?

	// The plugin should terminate itself after it receives EOF.
	xclose (plugin->write_fd);
	plugin->write_fd = -1;

	// Make it a pseudo-anonymous zombie.  In this state we process any
	// remaining commands it attempts to send to us before it finally dies.
	str_map_set (&plugin->ctx->plugins_by_name, plugin->name, NULL);
	plugin->is_zombie = true;

	// TODO: wait a few seconds and then send SIGKILL to the plugin
	return true;
}

static void
on_plugin_writable (const struct pollfd *fd, struct plugin_data *plugin)
{
	struct bot_context *ctx = plugin->ctx;
	struct str *buf = &plugin->write_buffer;
	size_t written_total = 0;

	if (fd->revents & ~(POLLOUT | POLLHUP | POLLERR))
		print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);

	while (written_total != buf->len)
	{
		ssize_t n_written = write (fd->fd, buf->str + written_total,
			buf->len - written_total);

		if (n_written < 0)
		{
			if (errno == EAGAIN)
				break;
			if (errno == EINTR)
				continue;

			soft_assert (errno == EPIPE);
			// Zombies shouldn't get dispatched for writability
			hard_assert (!plugin->is_zombie);

			print_debug ("%s: %s", "write", strerror (errno));
			print_error ("failure on writing to plugin `%s',"
				" therefore I'm unloading it", plugin->name);
			plugin_zombify (plugin);
			break;
		}

		// This may be equivalent to EAGAIN on some implementations
		if (n_written == 0)
			break;

		written_total += n_written;
	}

	if (written_total != 0)
		str_remove_slice (buf, 0, written_total);

	if (buf->len == 0)
	{
		// Everything has been written, there's no need to end up in here again
		ssize_t index = poller_find_by_fd (&ctx->poller, fd->fd);
		if (index != -1)
			poller_remove_at_index (&ctx->poller, index);
	}
}

static void
plugin_queue_write (struct plugin_data *plugin)
{
	if (plugin->is_zombie)
		return;

	// Don't let the write buffer grow infinitely.  If there's a ton of data
	// waiting to be processed by the plugin, it usually means there's something
	// wrong with it (such as someone stopping the process).
	if (plugin->write_buffer.len >= (1 << 20))
	{
		print_warning ("plugin `%s' does not seem to process messages fast"
			" enough, I'm unloading it", plugin->name);
		plugin_zombify (plugin);
		return;
	}

	poller_set (&plugin->ctx->poller, plugin->write_fd, POLLOUT,
		(poller_dispatcher_func) on_plugin_writable, plugin);
}

static void
plugin_send (struct plugin_data *plugin, const char *format, ...)
	ATTRIBUTE_PRINTF (2, 3);

static void
plugin_send (struct plugin_data *plugin, const char *format, ...)
{
	va_list ap;

	if (g_debug_mode)
	{
		fprintf (stderr, "[%s] <-- \"", plugin->name);
		va_start (ap, format);
		vfprintf (stderr, format, ap);
		va_end (ap);
		fputs ("\"\n", stderr);
	}

	va_start (ap, format);
	str_append_vprintf (&plugin->write_buffer, format, ap);
	va_end (ap);
	str_append (&plugin->write_buffer, "\r\n");

	plugin_queue_write (plugin);
}

static void
plugin_process_message (const struct irc_message *msg,
	const char *raw, void *user_data)
{
	struct plugin_data *plugin = user_data;
	struct bot_context *ctx = plugin->ctx;

	if (g_debug_mode)
		fprintf (stderr, "[%s] --> \"%s\"\n", plugin->name, raw);

	if (!strcasecmp (msg->command, plugin_ipc_command))
	{
		// Replies are sent in the order in which they came in, so there's
		// no need to attach a special identifier to them.  It might be
		// desirable in some cases, though.

		if (msg->params.len < 1)
			return;

		const char *command = msg->params.vector[0];
		if (!plugin->initialized && !strcasecmp (command, "register"))
		{
			// Register for relaying of IRC traffic
			plugin->initialized = true;

			// Flush any queued up traffic here.  The point of queuing it in
			// the first place is so that we don't have to wait for plugin
			// initialization during startup.
			//
			// Note that if we start filtering data coming to the plugins e.g.
			// based on what it tells us upon registration, we might need to
			// filter `queued_output' as well.
			str_append_str (&plugin->write_buffer, &plugin->queued_output);
			str_free (&plugin->queued_output);

			// NOTE: this may trigger the buffer length check
			plugin_queue_write (plugin);
		}
		else if (!strcasecmp (command, "get_config"))
		{
			if (msg->params.len < 2)
				return;

			const char *value =
				str_map_find (&ctx->config, msg->params.vector[1]);
			// TODO: escape the value (although there's no need to ATM)
			plugin_send (plugin, "%s :%s",
				plugin_ipc_command, value ? value : "");
		}
		else if (!strcasecmp (command, "print"))
		{
			if (msg->params.len < 2)
				return;

			printf ("%s\n", msg->params.vector[1]);
		}
	}
	else if (plugin->initialized && ctx->irc_ready)
	{
		// Pass everything else through to the IRC server
		// XXX: when the server isn't ready yet, these messages get silently
		//   discarded, which shouldn't pose a problem most of the time.
		//   Perhaps we could send a "connected" notification on `register'
		//   if `irc_ready' is true, or after it becomes true later, so that
		//   plugins know when to start sending unprovoked IRC messages.
		// XXX: another case is when the connection gets interrupted and the
		//   plugin tries to send something back while we're reconnecting.
		//   For that we might set up a global buffer that gets flushed out
		//   after `irc_ready' becomes true.  Note that there is always some
		//   chance of messages getting lost without us even noticing it.
		irc_send (ctx, "%s", raw);
	}
}

static void
on_plugin_readable (const struct pollfd *fd, struct plugin_data *plugin)
{
	if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
		print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);

	// TODO: see if I can reuse irc_fill_read_buffer()
	struct str *buf = &plugin->read_buffer;
	while (true)
	{
		str_ensure_space (buf, 512 + 1);
		ssize_t n_read = read (fd->fd, buf->str + buf->len,
			buf->alloc - buf->len - 1);

		if (n_read < 0)
		{
			if (errno == EAGAIN)
				break;
			if (soft_assert (errno == EINTR))
				continue;

			if (!plugin->is_zombie)
			{
				print_error ("failure on reading from plugin `%s',"
					" therefore I'm unloading it", plugin->name);
				plugin_zombify (plugin);
			}
			return;
		}

		// EOF; hopefully it will die soon (maybe it already has)
		if (n_read == 0)
			break;

		buf->str[buf->len += n_read] = '\0';
		if (buf->len >= (1 << 20))
		{
			// XXX: this isn't really the best flood prevention mechanism,
			//   but it wasn't even supposed to be one.
			if (plugin->is_zombie)
			{
				print_error ("a zombie of plugin `%s' is trying to flood us,"
					" therefore I'm killing it", plugin->name);
				kill (plugin->pid, SIGKILL);
			}
			else
			{
				print_error ("plugin `%s' seems to spew out data frantically,"
					" therefore I'm unloading it", plugin->name);
				plugin_zombify (plugin);
			}
			return;
		}
	}

	irc_process_buffer (buf, plugin_process_message, plugin);
}

static bool
is_valid_plugin_name (const char *name)
{
	if (!*name)
		return false;
	for (const char *p = name; *p; p++)
		if (!isgraph (*p) || *p == '/')
			return false;
	return true;
}

static bool
plugin_load (struct bot_context *ctx, const char *name, struct error **e)
{
	const char *plugin_dir = str_map_find (&ctx->config, "plugin_dir");
	if (!plugin_dir)
	{
		error_set (e, "plugin directory not set");
		return false;
	}

	if (!is_valid_plugin_name (name))
	{
		error_set (e, "invalid plugin name");
		return false;
	}

	if (str_map_find (&ctx->plugins_by_name, name))
	{
		error_set (e, "the plugin has already been loaded");
		return false;
	}

	int stdin_pipe[2];
	if (pipe (stdin_pipe) == -1)
	{
		error_set (e, "%s: %s: %s",
			"failed to load the plugin", "pipe", strerror (errno));
		goto fail_1;
	}

	int stdout_pipe[2];
	if (pipe (stdout_pipe) == -1)
	{
		error_set (e, "%s: %s: %s",
			"failed to load the plugin", "pipe", strerror (errno));
		goto fail_2;
	}

	set_cloexec (stdin_pipe[1]);
	set_cloexec (stdout_pipe[0]);

	pid_t pid = fork ();
	if (pid == -1)
	{
		error_set (e, "%s: %s: %s",
			"failed to load the plugin", "fork", strerror (errno));
		goto fail_3;
	}

	if (pid == 0)
	{
		// Redirect the child's stdin and stdout to the pipes
		hard_assert (dup2 (stdin_pipe[0],  STDIN_FILENO)  != -1);
		hard_assert (dup2 (stdout_pipe[1], STDOUT_FILENO) != -1);

		xclose (stdin_pipe[0]);
		xclose (stdout_pipe[1]);

		struct str pathname;
		str_init (&pathname);
		str_append (&pathname, plugin_dir);
		str_append_c (&pathname, '/');
		str_append (&pathname, name);

		// Restore some of the signal handling
		signal (SIGPIPE, SIG_DFL);

		char *const argv[] = { pathname.str, NULL };
		execve (argv[0], argv, environ);

		// We will collect the failure later via SIGCHLD
		print_error ("%s: %s: %s",
			"failed to load the plugin", "exec", strerror (errno));
		_exit (EXIT_FAILURE);
	}

	xclose (stdin_pipe[0]);
	xclose (stdout_pipe[1]);

	set_blocking (stdout_pipe[0], false);
	set_blocking (stdin_pipe[1], false);

	struct plugin_data *plugin = xmalloc (sizeof *plugin);
	plugin_data_init (plugin);
	plugin->ctx = ctx;
	plugin->pid = pid;
	plugin->name = xstrdup (name);
	plugin->read_fd = stdout_pipe[0];
	plugin->write_fd = stdin_pipe[1];

	LIST_PREPEND (ctx->plugins, plugin);
	str_map_set (&ctx->plugins_by_name, name, plugin);

	poller_set (&ctx->poller, stdout_pipe[0], POLLIN,
		(poller_dispatcher_func) on_plugin_readable, plugin);
	return true;

fail_3:
	xclose (stdout_pipe[0]);
	xclose (stdout_pipe[1]);
fail_2:
	xclose (stdin_pipe[0]);
	xclose (stdin_pipe[1]);
fail_1:
	return false;
}

static bool
plugin_unload (struct bot_context *ctx, const char *name, struct error **e)
{
	struct plugin_data *plugin = str_map_find (&ctx->plugins_by_name, name);

	if (!plugin)
	{
		error_set (e, "no such plugin is loaded");
		return false;
	}

	plugin_zombify (plugin);

	// TODO: add a `kill zombies' command to forcefully get rid of processes
	//   that do not understand the request.
	return true;
}

static void
plugin_load_all_from_config (struct bot_context *ctx)
{
	const char *plugin_list = str_map_find (&ctx->config, "plugins");
	if (!plugin_list)
		return;

	struct str_vector plugins;
	str_vector_init (&plugins);

	split_str_ignore_empty (plugin_list, ',', &plugins);
	for (size_t i = 0; i < plugins.len; i++)
	{
		char *name = strip_str_in_place (plugins.vector[i], " ");

		struct error *e = NULL;
		if (!plugin_load (ctx, name, &e))
		{
			print_error ("plugin `%s' failed to load: %s", name, e->message);
			error_free (e);
		}
	}

	str_vector_free (&plugins);
}

// --- Main program ------------------------------------------------------------

static bool
parse_bot_command (const char *s, const char *command, const char **following)
{
	size_t command_len = strlen (command);
	if (strncasecmp (s, command, command_len))
		return false;
	s += command_len;

	// Expect a word boundary, so that we don't respond to invalid things
	if (isalnum (*s))
		return false;

	// Ignore any initial spaces; the rest is the command's argument
	while (isblank (*s))
		s++;
	*following = s;
	return true;
}

static void
split_bot_command_argument_list (const char *arguments, struct str_vector *out)
{
	split_str_ignore_empty (arguments, ',', out);
	for (size_t i = 0; i < out->len; )
	{
		if (!*strip_str_in_place (out->vector[i], " \t"))
			str_vector_remove (out, i);
		else
			i++;
	}
}

static bool
is_private_message (const struct irc_message *msg)
{
	hard_assert (msg->params.len);
	return !strchr ("#&+!", *msg->params.vector[0]);
}

static bool
is_sent_by_admin (struct bot_context *ctx, const struct irc_message *msg)
{
	// No administrator set -> everyone is an administrator
	if (!ctx->admin_re)
		return true;
	return regexec (ctx->admin_re, msg->prefix, 0, NULL, 0) != REG_NOMATCH;
}

static void respond_to_user (struct bot_context *ctx, const struct
	irc_message *msg, const char *format, ...) ATTRIBUTE_PRINTF (3, 4);

static void
respond_to_user (struct bot_context *ctx, const struct irc_message *msg,
	const char *format, ...)
{
	if (!soft_assert (msg->prefix && msg->params.len))
		return;

	char nick[strcspn (msg->prefix, "!") + 1];
	strncpy (nick, msg->prefix, sizeof nick - 1);
	nick[sizeof nick - 1] = '\0';

	struct str text;
	va_list ap;

	str_init (&text);
	va_start (ap, format);
	str_append_vprintf (&text, format, ap);
	va_end (ap);

	if (is_private_message (msg))
		irc_send (ctx, "PRIVMSG %s :%s", nick, text.str);
	else
		irc_send (ctx, "PRIVMSG %s :%s: %s",
			msg->params.vector[0], nick, text.str);

	str_free (&text);
}

static void
process_plugin_load (struct bot_context *ctx,
	const struct irc_message *msg, const char *name)
{
	struct error *e = NULL;
	if (plugin_load (ctx, name, &e))
		respond_to_user (ctx, msg, "plugin `%s' queued for loading", name);
	else
	{
		respond_to_user (ctx, msg, "plugin `%s' could not be loaded: %s",
			name, e->message);
		error_free (e);
	}
}

static void
process_plugin_unload (struct bot_context *ctx,
	const struct irc_message *msg, const char *name)
{
	struct error *e = NULL;
	if (plugin_unload (ctx, name, &e))
		respond_to_user (ctx, msg, "plugin `%s' unloaded", name);
	else
	{
		respond_to_user (ctx, msg, "plugin `%s' could not be unloaded: %s",
			name, e->message);
		error_free (e);
	}
}

static void
process_plugin_reload (struct bot_context *ctx,
	const struct irc_message *msg, const char *name)
{
	// XXX: we might want to wait until the plugin terminates before we try
	//   to reload it (so that it can save its configuration or whatever)

	// So far the only error that can occur is that the plugin hasn't been
	// loaded, which in this case doesn't really matter.
	plugin_unload (ctx, name, NULL);

	process_plugin_load (ctx, msg, name);
}

static void
process_privmsg (struct bot_context *ctx, const struct irc_message *msg)
{
	if (!is_sent_by_admin (ctx, msg))
		return;
	if (msg->params.len < 2)
		return;

	const char *prefix = str_map_find (&ctx->config, "prefix");
	hard_assert (prefix != NULL);  // We have a default value for this

	// For us to recognize the command, it has to start with the prefix,
	// with the exception of PM's sent directly to us.
	const char *text = msg->params.vector[1];
	if (!strncmp (text, prefix, strlen (prefix)))
		text += strlen (prefix);
	else if (!is_private_message (msg))
		return;

	const char *following;
	struct str_vector list;
	str_vector_init (&list);

	if (parse_bot_command (text, "quote", &following))
		// This seems to replace tons of random stupid commands
		irc_send (ctx, "%s", following);
	else if (parse_bot_command (text, "quit", &following))
	{
		// We actually need this command (instead of just `quote') because we
		// could try to reconnect to the server automatically otherwise.
		if (*following)
			irc_send (ctx, "QUIT :%s", following);
		else
			irc_send (ctx, "QUIT");
		initiate_quit (ctx);
	}
	else if (parse_bot_command (text, "status", &following))
	{
		struct str report;
		str_init (&report);

		const char *reason = getenv (g_startup_reason_str);
		if (!reason)
			reason = "launched normally";
		str_append_printf (&report,
			"\x02startup reason:\x0f %s; \x02plugins:\x0f ", reason);
		size_t zombies = 0;
		const char *prepend = "";
		for (struct plugin_data *plugin = ctx->plugins;
			 plugin; plugin = plugin->next)
		{
			if (plugin->is_zombie)
				zombies++;
			else
			{
				str_append_printf (&report, "%s%s", prepend, plugin->name);
				prepend = ", ";
			}
		}
		if (!ctx->plugins)
			str_append (&report, "\x02none\x0f");
		str_append_printf (&report, "; \x02zombies:\x0f %zu", zombies);

		respond_to_user (ctx, msg, "%s", report.str);
		str_free (&report);
	}
	else if (parse_bot_command (text, "load", &following))
	{
		split_bot_command_argument_list (following, &list);
		for (size_t i = 0; i < list.len; i++)
			process_plugin_load (ctx, msg, list.vector[i]);
	}
	else if (parse_bot_command (text, "reload", &following))
	{
		split_bot_command_argument_list (following, &list);
		for (size_t i = 0; i < list.len; i++)
			process_plugin_reload (ctx, msg, list.vector[i]);
	}
	else if (parse_bot_command (text, "unload", &following))
	{
		split_bot_command_argument_list (following, &list);
		for (size_t i = 0; i < list.len; i++)
			process_plugin_unload (ctx, msg, list.vector[i]);
	}

	str_vector_free (&list);
}

static void
irc_forward_message_to_plugins (struct bot_context *ctx, const char *raw)
{
	// For consistency with plugin_process_message()
	if (!ctx->irc_ready)
		return;

	for (struct plugin_data *plugin = ctx->plugins;
		 plugin; plugin = plugin->next)
	{
		if (plugin->is_zombie)
			continue;

		if (plugin->initialized)
			plugin_send (plugin, "%s", raw);
		else
			// TODO: make sure that this buffer doesn't get too large either
			str_append_printf (&plugin->queued_output, "%s\r\n", raw);
	}
}

static void
irc_process_message (const struct irc_message *msg,
	const char *raw, void *user_data)
{
	struct bot_context *ctx = user_data;
	if (g_debug_mode)
		fprintf (stderr, "[%s] ==> \"%s\"\n", "IRC", raw);

	// This should be as minimal as possible, I don't want to have the whole bot
	// written in C, especially when I have this overengineered plugin system.
	// Therefore the very basic functionality only.
	//
	// I should probably even rip out the autojoin...

	irc_forward_message_to_plugins (ctx, raw);

	if (!strcasecmp (msg->command, "PING"))
	{
		if (msg->params.len)
			irc_send (ctx, "PONG :%s", msg->params.vector[0]);
		else
			irc_send (ctx, "PONG");
	}
	else if (!ctx->irc_ready && (!strcasecmp (msg->command, "MODE")
		|| !strcasecmp (msg->command, "376")    // RPL_ENDOFMOTD
		|| !strcasecmp (msg->command, "422")))  // ERR_NOMOTD
	{
		print_status ("successfully connected");
		ctx->irc_ready = true;

		const char *autojoin = str_map_find (&ctx->config, "autojoin");
		if (autojoin)
			irc_send (ctx, "JOIN :%s", autojoin);
	}
	else if (!strcasecmp (msg->command, "PRIVMSG"))
		process_privmsg (ctx, msg);
}

enum irc_read_result
{
	IRC_READ_OK,                        ///< Some data were read successfully
	IRC_READ_EOF,                       ///< The server has closed connection
	IRC_READ_AGAIN,                     ///< No more data at the moment
	IRC_READ_ERROR                      ///< General connection failure
};

static enum irc_read_result
irc_fill_read_buffer_ssl (struct bot_context *ctx, struct str *buf)
{
	int n_read;
start:
	n_read = SSL_read (ctx->ssl, buf->str + buf->len,
		buf->alloc - buf->len - 1 /* null byte */);

	const char *error_info = NULL;
	switch (xssl_get_error (ctx->ssl, n_read, &error_info))
	{
	case SSL_ERROR_NONE:
		buf->str[buf->len += n_read] = '\0';
		return IRC_READ_OK;
	case SSL_ERROR_ZERO_RETURN:
		return IRC_READ_EOF;
	case SSL_ERROR_WANT_READ:
		return IRC_READ_AGAIN;
	case SSL_ERROR_WANT_WRITE:
	{
		// Let it finish the handshake as we don't poll for writability;
		// any errors are to be collected by SSL_read() in the next iteration
		struct pollfd pfd = { .fd = ctx->irc_fd, .events = POLLOUT };
		soft_assert (poll (&pfd, 1, 0) > 0);
		goto start;
	}
	case XSSL_ERROR_TRY_AGAIN:
		goto start;
	default:
		print_debug ("%s: %s: %s", __func__, "SSL_read", error_info);
		return IRC_READ_ERROR;
	}
}

static enum irc_read_result
irc_fill_read_buffer (struct bot_context *ctx, struct str *buf)
{
	ssize_t n_read;
start:
	n_read = recv (ctx->irc_fd, buf->str + buf->len,
		buf->alloc - buf->len - 1 /* null byte */, 0);

	if (n_read > 0)
	{
		buf->str[buf->len += n_read] = '\0';
		return IRC_READ_OK;
	}
	if (n_read == 0)
		return IRC_READ_EOF;

	if (errno == EAGAIN)
		return IRC_READ_AGAIN;
	if (errno == EINTR)
		goto start;

	print_debug ("%s: %s: %s", __func__, "recv", strerror (errno));
	return IRC_READ_ERROR;
}

static bool irc_connect (struct bot_context *, struct error **);
static void irc_queue_reconnect (struct bot_context *);

static void
irc_cancel_timers (struct bot_context *ctx)
{
	ssize_t i;
	struct poller_timers *timers = &ctx->poller.timers;
	while ((i = poller_timers_find_by_data (timers, ctx)) != -1)
		poller_timers_remove_at_index (timers, i);
}

static void
irc_on_reconnect_timeout (void *user_data)
{
	struct bot_context *ctx = user_data;

	struct error *e = NULL;
	if (irc_connect (ctx, &e))
	{
		// TODO: inform plugins about the new connection
		return;
	}

	print_error ("%s", e->message);
	error_free (e);
	irc_queue_reconnect (ctx);
}

static void
irc_queue_reconnect (struct bot_context *ctx)
{
	hard_assert (ctx->irc_fd == -1);
	print_status ("trying to reconnect in %ld seconds...",
		ctx->reconnect_delay);
	poller_timers_add (&ctx->poller.timers,
		irc_on_reconnect_timeout, ctx, ctx->reconnect_delay * 1000);
}

static void
on_irc_disconnected (struct bot_context *ctx)
{
	// Get rid of the dead socket and related things
	if (ctx->ssl)
	{
		SSL_free (ctx->ssl);
		ctx->ssl = NULL;
		SSL_CTX_free (ctx->ssl_ctx);
		ctx->ssl_ctx = NULL;
	}

	ssize_t i = poller_find_by_fd (&ctx->poller, ctx->irc_fd);
	if (i != -1)
		poller_remove_at_index (&ctx->poller, i);

	xclose (ctx->irc_fd);
	ctx->irc_fd = -1;
	ctx->irc_ready = false;

	// TODO: inform plugins about the disconnect event

	// All of our timers have lost their meaning now
	irc_cancel_timers (ctx);

	if (ctx->quitting)
		try_finish_quit (ctx);
	else if (!ctx->reconnect)
		initiate_quit (ctx);
	else
		irc_queue_reconnect (ctx);
}

static void
on_irc_ping_timeout (void *user_data)
{
	struct bot_context *ctx = user_data;
	print_error ("connection timeout");
	on_irc_disconnected (ctx);
}

static void
on_irc_timeout (void *user_data)
{
	// Provoke a response from the server
	struct bot_context *ctx = user_data;
	irc_send (ctx, "PING :%s",
		(char *) str_map_find (&ctx->config, "nickname"));
}

static void
irc_reset_connection_timeouts (struct bot_context *ctx)
{
	irc_cancel_timers (ctx);
	poller_timers_add (&ctx->poller.timers,
		on_irc_timeout, ctx, 3 * 60 * 1000);
	poller_timers_add (&ctx->poller.timers,
		on_irc_ping_timeout, ctx, (3 * 60 + 30) * 1000);
}

static void
on_irc_readable (const struct pollfd *fd, struct bot_context *ctx)
{
	if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
		print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);

	(void) set_blocking (ctx->irc_fd, false);

	struct str *buf = &ctx->read_buffer;
	enum irc_read_result (*fill_buffer)(struct bot_context *, struct str *)
		= ctx->ssl
		? irc_fill_read_buffer_ssl
		: irc_fill_read_buffer;
	bool disconnected = false;
	while (true)
	{
		str_ensure_space (buf, 512);
		switch (fill_buffer (ctx, buf))
		{
		case IRC_READ_AGAIN:
			goto end;
		case IRC_READ_ERROR:
			print_error ("reading from the IRC server failed");
			disconnected = true;
			goto end;
		case IRC_READ_EOF:
			print_status ("the IRC server closed the connection");
			disconnected = true;
			goto end;
		case IRC_READ_OK:
			break;
		}

		if (buf->len >= (1 << 20))
		{
			print_error ("the IRC server seems to spew out data frantically");
			irc_shutdown (ctx);
			goto end;
		}
	}
end:
	(void) set_blocking (ctx->irc_fd, true);
	irc_process_buffer (buf, irc_process_message, ctx);

	if (disconnected)
		on_irc_disconnected (ctx);
	else
		irc_reset_connection_timeouts (ctx);
}

static bool
irc_connect (struct bot_context *ctx, struct error **e)
{
	const char *irc_host = str_map_find (&ctx->config, "irc_host");
	const char *irc_port = str_map_find (&ctx->config, "irc_port");
	const char *ssl_use_str = str_map_find (&ctx->config, "ssl_use");

	const char *socks_host = str_map_find (&ctx->config, "socks_host");
	const char *socks_port = str_map_find (&ctx->config, "socks_port");
	const char *socks_username = str_map_find (&ctx->config, "socks_username");
	const char *socks_password = str_map_find (&ctx->config, "socks_password");

	const char *nickname = str_map_find (&ctx->config, "nickname");
	const char *username = str_map_find (&ctx->config, "username");
	const char *realname = str_map_find (&ctx->config, "realname");

	// We have a default value for these
	hard_assert (irc_port && ssl_use_str && socks_port);
	hard_assert (nickname && username && realname);

	// TODO: again, get rid of `struct error' in here.  The question is: how
	//   do we tell our caller that he should not try to reconnect?
	if (!irc_host)
	{
		error_set (e, "no hostname specified in configuration");
		return false;
	}

	bool use_ssl;
	if (!set_boolean_if_valid (&use_ssl, ssl_use_str))
	{
		error_set (e, "invalid configuration value for `%s'", "use_ssl");
		return false;
	}

	if (socks_host)
	{
		char *address = format_host_port_pair (irc_host, irc_port);
		char *socks_address = format_host_port_pair (socks_host, socks_port);
		print_status ("connecting to %s via %s...", address, socks_address);
		free (socks_address);
		free (address);

		struct error *error = NULL;
		int fd = socks_connect (socks_host, socks_port, irc_host, irc_port,
			socks_username, socks_password, &error);
		if (fd == -1)
		{
			error_set (e, "%s: %s", "SOCKS connection failed", error->message);
			error_free (error);
			return false;
		}
		ctx->irc_fd = fd;
	}
	else if (!irc_establish_connection (ctx, irc_host, irc_port, e))
		return false;

	if (use_ssl && !irc_initialize_ssl (ctx, e))
	{
		xclose (ctx->irc_fd);
		ctx->irc_fd = -1;
		return false;
	}
	print_status ("connection established");

	// TODO: in exec try: 1/ set blocking, 2/ setsockopt() SO_LINGER,
	//   (struct linger) { .l_onoff = true; .l_linger = 1 /* 1s should do */; }
	//   3/ /* O_CLOEXEC */  But only if the QUIT message proves unreliable.
	poller_set (&ctx->poller, ctx->irc_fd, POLLIN,
		(poller_dispatcher_func) on_irc_readable, ctx);
	irc_reset_connection_timeouts (ctx);

	irc_send (ctx, "NICK %s", nickname);
	irc_send (ctx, "USER %s 8 * :%s", username, realname);
	return true;
}

static bool
parse_config (struct bot_context *ctx, struct error **e)
{
	const char *reconnect_str = str_map_find (&ctx->config, "reconnect");
	hard_assert (reconnect_str != NULL);  // We have a default value for this
	if (!set_boolean_if_valid (&ctx->reconnect, reconnect_str))
	{
		error_set (e, "invalid configuration value for `%s'", "reconnect");
		return false;
	}

	const char *delay_str = str_map_find (&ctx->config, "reconnect_delay");
	hard_assert (delay_str != NULL);  // We have a default value for this
	if (!xstrtoul (&ctx->reconnect_delay, delay_str, 10))
	{
		error_set (e, "invalid configuration value for `%s'",
			"reconnect_delay");
		return false;
	}

	hard_assert (!ctx->admin_re);
	const char *admin = str_map_find (&ctx->config, "admin");
	if (!admin)
		return true;

	struct error *error = NULL;
	ctx->admin_re = regex_compile (admin, REG_EXTENDED | REG_NOSUB, &error);
	if (!error)
		return true;

	error_set (e, "invalid configuration value for `%s': %s",
		"admin", error->message);
	error_free (error);
	return false;
}

static void
on_signal_pipe_readable (const struct pollfd *fd, struct bot_context *ctx)
{
	char *dummy;
	(void) read (fd->fd, &dummy, 1);

	if (g_termination_requested && !ctx->quitting)
	{
		// There may be a timer set to reconnect to the server
		irc_cancel_timers (ctx);

		if (ctx->irc_fd != -1)
			irc_send (ctx, "QUIT :Terminated by signal");
		initiate_quit (ctx);
	}

	// Reap all dead children (since the pipe may overflow, we ask waitpid()
	// to return all the zombies it knows about).
	while (true)
	{
		int status;
		pid_t zombie = waitpid (-1, &status, WNOHANG);

		if (zombie == -1)
		{
			// No children to wait on
			if (errno == ECHILD)
				break;

			hard_assert (errno == EINTR);
			continue;
		}

		if (zombie == 0)
			break;

		struct plugin_data *plugin = plugin_find_by_pid (ctx, zombie);
		// Something has died but we don't recognize it (re-exec?)
		if (!soft_assert (plugin != NULL))
			continue;

		// TODO: callbacks on children death, so that we may tell the user
		//       "plugin `name' died like a dirty jewish pig"; use `status'
		if (!plugin->is_zombie && WIFSIGNALED (status))
		{
			const char *notes = "";
#ifdef WCOREDUMP
			if (WCOREDUMP (status))
				notes = " (core dumped)";
#endif
			print_warning ("Plugin `%s' died from signal %d%s",
				plugin->name, WTERMSIG (status), notes);
		}

		// Let's go through the zombie state to simplify things a bit
		// TODO: might not be a completely bad idea to restart the plugin
		plugin_zombify (plugin);

		plugin->pid = -1;

		ssize_t poller_idx = poller_find_by_fd (&ctx->poller, plugin->read_fd);
		if (poller_idx != -1)
			poller_remove_at_index (&ctx->poller, poller_idx);

		xclose (plugin->read_fd);
		plugin->read_fd = -1;

		LIST_UNLINK (ctx->plugins, plugin);
		plugin_data_free (plugin);
		free (plugin);

		// Living child processes block us from quitting
		try_finish_quit (ctx);
	}
}

static void
print_usage (const char *program_name)
{
	fprintf (stderr,
		"Usage: %s [OPTION]...\n"
		"Experimental IRC bot.\n"
		"\n"
		"  -d, --debug     run in debug mode\n"
		"  -h, --help      display this help and exit\n"
		"  -V, --version   output version information and exit\n"
		"  --write-default-cfg [filename]\n"
		"                  write a default configuration file and exit\n",
		program_name);
}

int
main (int argc, char *argv[])
{
	const char *invocation_name = argv[0];
	str_vector_init (&g_original_argv);
	str_vector_add_vector (&g_original_argv, argv);

	static struct option opts[] =
	{
		{ "debug",             no_argument,       NULL, 'd' },
		{ "help",              no_argument,       NULL, 'h' },
		{ "version",           no_argument,       NULL, 'V' },
		{ "write-default-cfg", optional_argument, NULL, 'w' },
		{ NULL,                0,                 NULL,  0  }
	};

	while (1)
	{
		int c, opt_index;

		c = getopt_long (argc, argv, "dhV", opts, &opt_index);
		if (c == -1)
			break;

		switch (c)
		{
		case 'd':
			g_debug_mode = true;
			break;
		case 'h':
			print_usage (invocation_name);
			exit (EXIT_SUCCESS);
		case 'V':
			printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
			exit (EXIT_SUCCESS);
		case 'w':
			call_write_default_config (optarg, g_config_table);
			exit (EXIT_SUCCESS);
		default:
			print_error ("wrong options");
			exit (EXIT_FAILURE);
		}
	}

	print_status (PROGRAM_NAME " " PROGRAM_VERSION " starting");
	setup_signal_handlers ();

	SSL_library_init ();
	atexit (EVP_cleanup);
	SSL_load_error_strings ();
	// XXX: ERR_load_BIO_strings()?  Anything else?
	atexit (ERR_free_strings);

	struct bot_context ctx;
	bot_context_init (&ctx);

	struct error *e = NULL;
	if (!read_config_file (&ctx.config, &e))
	{
		print_error ("error loading configuration: %s", e->message);
		error_free (e);
		exit (EXIT_FAILURE);
	}

	setup_recovery_handler (&ctx);
	poller_set (&ctx.poller, g_signal_pipe[0], POLLIN,
		(poller_dispatcher_func) on_signal_pipe_readable, &ctx);

	plugin_load_all_from_config (&ctx);
	if (!parse_config (&ctx, &e)
	 || !irc_connect (&ctx, &e))
	{
		print_error ("%s", e->message);
		error_free (e);
		exit (EXIT_FAILURE);
	}

	// TODO: clean re-exec support; to save the state I can either use argv,
	//   argp, or I can create a temporary file, unlink it and use the FD
	//   (mkstemp() on a `struct str' constructed from XDG_RUNTIME_DIR, TMPDIR
	//   or /tmp as a last resort + PROGRAM_NAME + ".XXXXXX" -> unlink();
	//   remember to use O_CREAT | O_EXCL).  The state needs to be versioned.
	//   Unfortunately I cannot de/serialize SSL state.

	ctx.polling = true;
	while (ctx.polling)
		poller_run (&ctx.poller);

	bot_context_free (&ctx);
	str_vector_free (&g_original_argv);
	return EXIT_SUCCESS;
}