-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyesbot.pl
89 lines (70 loc) · 2.11 KB
/
yesbot.pl
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
:- module(yesbot, [
connect/0,
main/1,
yesbot_vsn/1
]).
:- use_module(library(irc_client)).
:- use_module(library(socket)).
:- use_module(library(func)).
:- use_module(library(lambda)).
:- use_module(config).
:- reexport(config, [goals_to_concurrent/2]).
:- initialization reload_version.
yesbot_vsn('1.5.0').
/** <module> Yesbot IRC bot
Extensible IRC bot written in Prolog.
@author Ebrahim Azarisooreh
@license MIT
@tbd Add support for multiple chat servers
*/
%% main(+Doc) is det.
%
% Start a connection and join the ##prolog channel on freenode. Doc is a port
% number of a SWI prolog documentation server; this argument is of interest
% only if the swi_object_search extension is enabled. The server _must_ be run
% on a separate instance of SWI prolog listener.
main(Doc) :-
ignore((integer(Doc),asserta(swi_object_search:doc_port(Doc)))),
thread_create(connect, _, [detached(true), alias(conn)]).
%% connect is failure.
%
% Intializes the extensions the user has chosen after consulting config.pl.
% Then a connection is spawned on a separate thread and joined on termination.
% cleanup routines are run with respect to that connection. The connection is
% persistent and will attempt to reconnect after 2 minutes.
connect :-
repeat,
init_extensions,
catch(
thread_create(join_channels, _, [alias(irc), at_exit(disconnect(irc))]),
Err,
print_message(error, Err)
),
thread_join(irc, _),
writeln("Connection lost, attempting to reconnect ..."),
sleep(120),
fail.
%% join_channels is semidet.
%
% Calls the specs necessary for establishing a connection (from config.pl). Then
% an attempt is made to join the server and channels (if specified). Defaults
% are available in config.pl.
join_channels :-
host(Host),
port(Port),
pass(Pass),
nick(Nick),
chans(Chans),
bot_hostname(Hn),
bot_servername(Sn),
bot_realname(Rn),
Names = [Hn,Sn,Rn],
connect(Host, Port, Pass, Nick, Names, Chans).
reload_version :-
['extensions/yesbot_version'],
( yesbot_version:yesbot_vsn(_)
-> retractall(yesbot_version:yesbot_vsn(_))
; true
),
yesbot_vsn(Vsn),
asserta(yesbot_version:yesbot_vsn(Vsn)).