-
Notifications
You must be signed in to change notification settings - Fork 0
/
dazeus-channels.pl
188 lines (162 loc) · 5.29 KB
/
dazeus-channels.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
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
#!/usr/bin/perl
# Channel plugin for DaZeus
# Copyright (C) 2007 Sjors Gielen
# Copyright (C) 2014 Aaron van Geffen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use DaZeus;
use warnings;
use strict;
my ($socket) = @ARGV;
if(!$socket) {
die "Usage: $0 socket\n";
}
my $dazeus = DaZeus->connect($socket) or die $!;
$dazeus->subscribe_command("join" => \&cmd_join);
$dazeus->subscribe_command("part" => \&cmd_part);
$dazeus->subscribe_command("leave" => \&cmd_part);
$dazeus->subscribe_command("cycle" => \&cmd_cycle);
$dazeus->subscribe_command("autojoin" => \&cmd_autojoin);
$dazeus->subscribe_command("identify" => \&cmd_identify);
$dazeus->subscribe("connect" => sub {
my ($dazeus, $event) = @_;
my $network = $event->{'params'}[0];
on_connect($dazeus, $network);
});
# simulate a re-connection to all networks
my $networks = $dazeus->networks();
foreach(@$networks) {
on_connect($dazeus, $_);
}
while($dazeus->handleEvents()) {}
sub reply {
my ($response, $network, $sender, $channel) = @_;
if ($channel eq $dazeus->getNick($network)) {
$dazeus->message($network, $sender, $response);
} else {
$dazeus->message($network, $channel, $response);
}
}
sub cmd_join {
my ($dazeus, $network, $sender, $channel, $command, $args) = @_;
$args = $args ? $args : $channel;
$dazeus->join($network, $args);
reply("OK, joined $args.", $network, $sender, $channel);
}
sub cmd_part {
my ($dazeus, $network, $sender, $channel, $command, $args) = @_;
$args = $args ? $args : $channel;
$dazeus->part($network, $args);
reply("OK, left $args.", $network, $sender, $channel);
}
sub cmd_cycle {
my ($dazeus, $network, $sender, $channel, $command, $args) = @_;
$args = $args ? $args : $channel;
$dazeus->part($network, $args);
$dazeus->join($network, $args);
reply("OK, cycled $args.", $network, $sender, $channel);
}
sub get_autojoin {
my ($dazeus, $network) = @_;
my $a = $dazeus->getProperty("perl.DazChannel.autojoin", $network);
return $a ? @$a : ();
}
sub set_autojoin {
my ($dazeus, $network, $autojoin) = @_;
$dazeus->setProperty("perl.DazChannel.autojoin", $autojoin, $network);
}
sub uniq {
my (@set) = @_;
my %set;
foreach(@set) {
$set{$_} = 1;
}
return keys %set;
}
sub remove {
my ($list, @vars) = @_;
my %list;
foreach(@$list) {
$list{$_} = 1;
}
foreach(@vars) {
delete $list{$_};
}
return keys %list;
}
sub autojoin_now {
my ($dazeus, $network) = @_;
my @current_channels = get_autojoin($dazeus, $network);
foreach(@current_channels) {
$dazeus->join($network, $_);
}
}
sub cmd_autojoin {
my ($dazeus, $network, $sender, $channel, $command, $args) = @_;
my ($verb, @rest) = split /\s+/, $args;
my $rest = join " ", @rest;
$rest = $rest ? $rest : $channel;
if($verb eq "add") {
my @current_channels = get_autojoin($dazeus, $network);
my @channels = split /\s+/, $rest;
my @new_channels = uniq(@current_channels, @channels);
set_autojoin($dazeus, $network, \@new_channels);
reply("Done.", $network, $sender, $channel);
} elsif($verb eq "rm" || $verb eq "del") {
my @current_channels = get_autojoin($dazeus, $network);
my @channels = split /\s+/, $rest;
my @new_channels = remove(\@current_channels, @channels);
set_autojoin($dazeus, $network, \@new_channels);
reply("Done.", $network, $sender, $channel);
} elsif($verb eq "get") {
my @current_channels = get_autojoin($dazeus, $network);
my $channels = join ", ", @current_channels;
reply("Autojoin channels on $network: $channels", $network, $sender, $channel);
} elsif($verb eq "now") {
autojoin_now($dazeus, $network);
reply("Done.", $network, $sender, $channel);
} else {
reply("Usage: autojoin <add|rm|del|get|now> [...]", $network, $sender, $channel);
}
}
use constant nickservpass_var => "perl.DazLoader.nickservpass";
use constant nickservnick_var => "perl.DazLoader.nickservnick";
sub identify_now {
my ($dazeus, $network) = @_;
my $pass = $dazeus->getProperty(nickservpass_var, $network);
my $nick = $dazeus->getProperty(nickservnick_var, $network) || "NickServ";
$dazeus->message($network, $nick, "IDENTIFY " . $pass);
}
sub cmd_identify {
my ($dazeus, $network, $sender, $channel, $command, $args) = @_;
my ($verb, @rest) = split /\s+/, $args;
my $rest = join " ", @rest;
if($verb eq "password") {
$dazeus->setProperty(nickservpass_var, $rest, $network);
reply("Done.", $network, $sender, $channel);
} elsif($verb eq "to") {
$dazeus->setProperty(nickservnick_var, $rest, $network);
reply("Done.", $network, $sender, $channel);
} elsif($verb eq "now") {
identify_now($dazeus, $network);
reply("Done.", $network, $sender, $channel);
}
}
sub on_connect {
my ($dazeus, $network) = @_;
identify_now($dazeus, $network);
autojoin_now($dazeus, $network);
}
1;