-
Notifications
You must be signed in to change notification settings - Fork 235
/
hipchat_complete.pl
136 lines (123 loc) · 4.33 KB
/
hipchat_complete.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
# hipchat_complete.pl - (c) 2013 John Morrissey <[email protected]>
# (c) 2014 Brock Wilcox <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# About
# =====
#
# Adds Hipchat tab completion support.
#
# By default, Hipchat's XMPP interface sets user nicks to their full names,
# not their "mention names," so you always have to recall and manually type
# a user's mention name so Hipchat highlights the message, sends them e-mail
# if they're away, etc.
#
# This plugin tab-completes mention names and tab-translates name-based
# nicks to their corresponding "mention names."
#
# For example, if JohnMorrissey has a mention name of @jwm, all of these
# tab complete to @jwm:
#
# John<tab>
# @John<tab>
# @jw<tab>
#
#
# To use
# ======
#
# 1. Install the WebService::HipChat module from CPAN.
#
# 2. /script load hipchat_completion.pl
#
# 3. Get a Hipchat auth v2 token (hipchat.com -> Account settings -> API
# access). In irssi:
#
# /set hipchat_auth_token some-hex-value
#
# 4. If your Hipchat server isn't in the "bitlbee" chatnet (the 'chatnet'
# parameter in your irssi server list for the IRC server you use to
# connect to Hipchat), specify the name of the chatnet:
#
# /set hipchat_chatnet some-chatnet-name
use strict;
use Irssi;
use WebService::HipChat;
our $VERSION = '2.0';
our %IRSSI = (
authors => 'John Morrissey',
name => 'hipchat_complete',
description => 'Translate nicks to HipChat "mention names"',
license => 'BSD',
);
my %NICK_TO_MENTION;
my $LAST_MAP_UPDATED = 0;
sub get_hipchat_people {
my $auth_token = Irssi::settings_get_str('hipchat_auth_token');
if (!$auth_token) {
return;
}
my $hc = WebService::HipChat->new(auth_token => $auth_token);
my $hipchat_users = $hc->get_users->{items};
foreach my $user (@{$hipchat_users}) {
my $name = $user->{name};
$name =~ s/[^A-Za-z]//g;
$NICK_TO_MENTION{$name} = $user->{mention_name};
}
$LAST_MAP_UPDATED = time();
}
sub sig_complete_hipchat_nick {
my ($complist, $window, $word, $linestart, $want_space) = @_;
my $wi = Irssi::active_win()->{active};
return unless ref $wi and $wi->{type} eq 'CHANNEL';
return unless $wi->{server}->{chatnet} eq
Irssi::settings_get_str('hipchat_chatnet');
# Reload the nick -> mention name map periodically,
# so we pick up new users.
if (($LAST_MAP_UPDATED + 4 * 60 * 60) < time()) {
get_hipchat_people();
}
if ($word =~ /^@/) {
$word =~ s/^@//;
}
foreach my $nick ($wi->nicks()) {
if ($nick->{nick} =~ /^\Q$word\E/i) {
push(@$complist, "\@$NICK_TO_MENTION{$nick->{nick}}");
}
}
foreach my $mention (values %NICK_TO_MENTION) {
if ($mention =~ /^\Q$word\E/i) {
push(@$complist, "\@$mention");
}
}
# If there's a mention name completion that begins with $word,
# prefer that over a channel nick/fullname.
@$complist = sort {
return $a =~ /^\@\Q$word\E(.*)$/i ? 0 : 1;
} @$complist;
}
Irssi::settings_add_str('hipchat_complete', 'hipchat_auth_token', '');
Irssi::settings_add_str('hipchat_complete', 'hipchat_chatnet', 'bitlbee');
get_hipchat_people();
Irssi::signal_add('complete word', \&sig_complete_hipchat_nick);