1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Module::IRC::Completion; |
---|
5 | |
---|
6 | use BarnOwl::Completion::Util qw(complete_flags); |
---|
7 | |
---|
8 | our %users = (); |
---|
9 | our %servers = (); |
---|
10 | |
---|
11 | sub complete_networks { keys %BarnOwl::Module::IRC::ircnets } |
---|
12 | sub complete_dests { keys %users, complete_channels() } |
---|
13 | sub complete_channels { |
---|
14 | my %channels; |
---|
15 | for my $conn (values %BarnOwl::Module::IRC::ircnets) { |
---|
16 | for my $chan (keys %{$conn->conn->{channel_list}}) { |
---|
17 | $channels{$chan} = 1; |
---|
18 | } |
---|
19 | } |
---|
20 | return keys %channels; |
---|
21 | } |
---|
22 | sub complete_nicks { keys %users } |
---|
23 | sub complete_servers { keys %servers } |
---|
24 | |
---|
25 | sub complete_irc_connect { |
---|
26 | my $ctx = shift; |
---|
27 | return complete_flags($ctx, |
---|
28 | [qw(-s)], |
---|
29 | { |
---|
30 | "-a" => \&complete_networks, |
---|
31 | "-p" => sub {}, |
---|
32 | "-n" => sub {}, |
---|
33 | }, |
---|
34 | \&complete_servers |
---|
35 | ); |
---|
36 | } |
---|
37 | |
---|
38 | sub complete_irc_network { |
---|
39 | my $ctx = shift; |
---|
40 | return complete_flags($ctx, |
---|
41 | [], |
---|
42 | { |
---|
43 | "-a" => \&complete_networks, |
---|
44 | }, |
---|
45 | ); |
---|
46 | } |
---|
47 | |
---|
48 | sub complete_irc_dest { |
---|
49 | my $ctx = shift; |
---|
50 | return complete_flags($ctx, |
---|
51 | [], |
---|
52 | { |
---|
53 | "-a" => \&complete_networks, |
---|
54 | }, |
---|
55 | \&complete_dests |
---|
56 | ); |
---|
57 | } |
---|
58 | |
---|
59 | sub complete_irc_join_part { |
---|
60 | my $ctx = shift; |
---|
61 | return complete_flags($ctx, |
---|
62 | [qw(-t)], |
---|
63 | { |
---|
64 | "-a" => \&complete_networks, |
---|
65 | }, |
---|
66 | \&complete_channels |
---|
67 | ); |
---|
68 | } |
---|
69 | |
---|
70 | sub complete_irc_channel { |
---|
71 | my $ctx = shift; |
---|
72 | return complete_flags($ctx, |
---|
73 | [], |
---|
74 | { |
---|
75 | "-a" => \&complete_networks, |
---|
76 | }, |
---|
77 | \&complete_channels |
---|
78 | ); |
---|
79 | } |
---|
80 | |
---|
81 | sub complete_irc_nick { |
---|
82 | my $ctx = shift; |
---|
83 | return complete_flags($ctx, |
---|
84 | [], |
---|
85 | { |
---|
86 | "-a" => \&complete_networks, |
---|
87 | }, |
---|
88 | \&complete_nicks |
---|
89 | ); |
---|
90 | } |
---|
91 | |
---|
92 | sub on_message { |
---|
93 | my $m = shift; |
---|
94 | return unless $m->type eq 'IRC'; |
---|
95 | if ($m->recipient && $m->recipient !~ m{^#}) { |
---|
96 | $users{$m->recipient} = 1; |
---|
97 | } |
---|
98 | if ($m->sender && $m->sender !~ m{^#}) { |
---|
99 | $users{$m->sender} = 1; |
---|
100 | } |
---|
101 | $servers{$m->server} = 1; |
---|
102 | } |
---|
103 | |
---|
104 | BarnOwl::Completion::register_completer('irc-connect' => \&complete_irc_connect); |
---|
105 | BarnOwl::Completion::register_completer('irc-disconnect' => \&complete_irc_network); |
---|
106 | BarnOwl::Completion::register_completer('irc-msg' => \&complete_irc_dest); |
---|
107 | BarnOwl::Completion::register_completer('irc-mode' => \&complete_irc_dest); |
---|
108 | BarnOwl::Completion::register_completer('irc-join' => \&complete_irc_join_part); |
---|
109 | BarnOwl::Completion::register_completer('irc-part' => \&complete_irc_join_part); |
---|
110 | BarnOwl::Completion::register_completer('irc-names' => \&complete_irc_channel); |
---|
111 | BarnOwl::Completion::register_completer('irc-whois' => \&complete_irc_nick); |
---|
112 | BarnOwl::Completion::register_completer('irc-motd' => \&complete_irc_network); |
---|
113 | BarnOwl::Completion::register_completer('irc-topic' => \&complete_irc_channel); |
---|
114 | BarnOwl::Completion::register_completer('irc-quote' => \&complete_irc_network); |
---|
115 | |
---|
116 | $BarnOwl::Hooks::newMessage->add("BarnOwl::Module::IRC::Completion::on_message"); |
---|
117 | |
---|
118 | 1; |
---|