-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.erl
More file actions
166 lines (147 loc) · 5.79 KB
/
bot.erl
File metadata and controls
166 lines (147 loc) · 5.79 KB
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
-module(bot).
-compile(export_all).
-compile({no_auto_import,[load_module/2]}).
-include("definitions.hrl").
get_commands() ->
[
{"update", fun(#{reply:=Reply}) -> bot ! {update, Reply} end, host}
].
init() ->
code:add_path("./mod/bin"),
register(bot, self()),
{SeedA,SeedB,SeedC}=now(),
random:seed(SeedA,SeedB,SeedC),
config:offer_value(config, [permissions], []),
config:offer_value(config, [bot, nick], "Bot32"),
config:offer_value(config, [bot, user], "Bot32"),
config:offer_value(config, [bot, mode], "0"),
config:offer_value(config, [bot, real], "Bot32"),
config:offer_value(config, [bot, prefix], "!"),
config:offer_value(config, [bot, channels], []),
config:offer_value(config, [bot, modules], []),
config:offer_value(config, [bot, on_join], []),
config:offer_value(config, [bot, pass], none),
config:offer_value(config, [bot, names], []),
config:set_value(temp, [bot, commands], []),
util:waitfor(core), % wait for core to startup
case config:require_value(config, [bot, pass]) of
none -> ok;
Pass -> core ! {irc, {pass, Pass}}
end,
core ! {irc, {user, {config:require_value(config, [bot, user]), config:require_value(config, [bot, mode]), config:require_value(config, [bot, real])}}},
core ! {irc, {nick, config:require_value(config, [bot, nick])}},
receive
{irc, {numeric, {{rpl, welcome}, _}}} -> ok
after
10000 -> throw(connection_failed)
end,
timer:sleep(100),
lists:foreach(fun(T) -> core ! {irc, T} end, config:require_value(config, [bot, on_join])),
timer:sleep(100), % wait for server auth
lists:foreach(fun(T) -> core ! {irc, {join, T}} end, config:require_value(config, [bot, channels])),
logging:log(info, ?MODULE, "starting"),
config:set_value(temp, [bot, commands], []),
Modules = config:get_value(config, [bot, modules]),
config:set_value(config, [bot, modules], []),
logging:log(info, ?MODULE, "module load stat: ~s", [modules:load_modules(Modules)]),
loop(),
logging:log(info, ?MODULE, "stopping").
reinit(_) ->
register(bot, self()),
logging:log(info, ?MODULE, "starting"),
loop(),
logging:log(info, ?MODULE, "stopping").
notify_error(msg, {#user{nick=N}, Channel, _}) ->
case config:get_value(config, [bot, nick]) of
Channel -> {irc, {msg, {N, "Error!" }}};
_ -> {irc, {msg, {Channel, [N, ": Error!"]}}}
end;
notify_error(X, Y) -> logging:log(error, ?MODULE, "~p : ~p", [X,Y]).
loop() ->
case receive
{ircfwd, T} -> {irc, T};
{irc, {Type, Params}} ->
logging:log(recv, bot, "{~p, ~p}", [Type, Params]),
case catch handle_irc(Type, Params) of
{'EXIT', {Reason, Stack}} -> logging:log(error, ?MODULE, "handle_irc errored ~p (~p), continuing", [Reason, Stack]), notify_error(Type, Params);
{'EXIT', Term} -> logging:log(error, ?MODULE, "handle_irc exited ~p, continuing", [Term]), notify_error(Type, Params);
T -> T
end;
T when is_atom(T) -> T;
{T, K} when is_atom(T) -> {T, K}
% T -> logging:log(error, ?MODULE, "unknown receive ~p, continuing", [T])
end of
{request_execute, {Pid, Fun}} ->
Pid ! {execute_done, catch Fun()},
bot:loop();
{request_execute, Fun} ->
catch Fun(),
bot:loop();
{multi, List} -> lists:foreach(fun(T) -> core ! T end, List), bot:loop();
{irc, What} -> core ! {irc,What}, bot:loop();
quit -> ok;
error -> error;
ok -> bot:loop();
update ->
spawn(common,purge_call,[bot,reinit,[]]),
ok;
{update,Chan} ->
spawn(common,purge_call_report,[bot,reinit,[],Chan]),
ok;
S -> logging:log(error, ?MODULE, "unknown code ~p, continuing", [S]), bot:loop()
end.
check_utf8(<<>>) -> true;
check_utf8(<<_/utf8, B/binary>>) -> check_utf8(B);
check_utf8(_) -> false.
handle_irc(ctcp, {action, Chan, User=#user{nick=Nick}, Tokens}) ->
case permissions:hasperm(User, Chan, ignore) of
true ->
%logging:log(ignore, ?MODULE, "Ignoring ~s!~s@~s ACTION: ~s.", [Nick, User#user.username, User#user.host, string:join(Tokens, " ")]),
ok;
false ->
distribute_event(ctcp, {action, Chan, User, Tokens})
end;
handle_irc(msg, Params={User=#user{nick=Nick}, Channel, Tokens}) ->
case lists:all(fun(T) -> check_utf8(list_to_binary(T)) end, Tokens) of
false -> logging:log(utf8, ?MODULE, "Ignoring '~s' due to invalid UTF-8", [string:join(Tokens, " ")]);
true ->
case permissions:hasperm(User, ignore) of
true ->
%logging:log(ignore, ?MODULE, "Ignoring ~s!~s@~s: ~s.", [Nick, User#user.username, User#user.host, string:join(Tokens, " ")]),
distribute_event(msg_ignored, {User, Channel, Tokens}),
ok;
false ->
distribute_event(msg, Params),
case config:require_value(config, [bot, nick]) of
Channel ->
case permissions:hasperm(User, admin) of
true -> ok;
_ -> permissions:message_all_rank(["Query from ",Nick], string:join(Tokens, " "), pmlog)
end;
_ -> ok
end
end
end;
handle_irc(nick, {U=#user{nick=OldNick}, NewNick}) ->
case config:get_value(config, [bot, nick]) of
OldNick -> config:set_value(config, [bot, nick], NewNick);
_ ->
distribute_event(nick, {U, NewNick})
end;
handle_irc(notice, {Src, Trg, Msg}) ->
logging:log(info, ?MODULE, "NOTICE received from ~p to ~s: ~s", [Src, Trg, string:join(Msg, " ")]),
ok;
handle_irc(numeric, {{rpl,away},_}) -> ok;
handle_irc(numeric, {{A,B},Params}) ->
logging:log(debug, ?MODULE, "Numeric received: ~p_~p ~s", [A,B,string:join(Params," ")]),
distribute_event(numeric, {{A,B}, Params});
handle_irc(Type, Params) ->
distribute_event(Type, Params).
distribute_event(Type, Params) ->
lists:foreach(fun(Module) ->
case catch util:call_or(Module, handle_event, [Type, Params], null) of
{'EXIT', X} -> logging:log(error, ?MODULE, "~p:handle_event(~p, ~p) exited with ~p", [Module, Type, Params, X]);
{'EXIT', RS, X} -> logging:log(error, ?MODULE, "~p:handle_event(~p, ~p) errored with ~p (~p)", [Module, Type, Params, X, RS]);
_ -> ok
end
end, config:require_value(config, [bot, modules])).