summaryrefslogtreecommitdiffstats
path: root/scripts/hipchat_complete.pl
diff options
context:
space:
mode:
authorAlexander Færøy2014-05-31 13:10:46 +0200
committerAlexander Færøy2014-05-31 13:10:46 +0200
commit2d0759e6ca5767b48bcc85bf38c2c43d5f0b63b1 (patch)
tree1c5e6d817c88e67b46e216a50e0aef5428bf63df /scripts/hipchat_complete.pl
parent2d080422d79d1fd49d6c5528593ccaaff9bfc583 (diff)
downloadscripts.irssi.org-2d0759e6ca5767b48bcc85bf38c2c43d5f0b63b1.tar.bz2
Import scripts from scripts.irssi.org
Diffstat (limited to 'scripts/hipchat_complete.pl')
-rw-r--r--scripts/hipchat_complete.pl142
1 files changed, 142 insertions, 0 deletions
diff --git a/scripts/hipchat_complete.pl b/scripts/hipchat_complete.pl
new file mode 100644
index 0000000..a8711c6
--- /dev/null
+++ b/scripts/hipchat_complete.pl
@@ -0,0 +1,142 @@
+# hipchat_complete.pl - (c) 2013 John Morrissey <jwm@horde.net>
+#
+# 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 HTTP::Message, JSON, and LWP Perl modules.
+#
+# 2. /script load hipchat_completion.pl
+#
+# 3. Get a Hipchat auth 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 HTTP::Request;
+use Irssi;
+use JSON;
+use LWP::UserAgent;
+
+my $VERSION = '1.0';
+my %IRSSI = (
+ author => 'John Morrissey',
+ contact => 'jwm@horde.net',
+ name => 'hipchat_complete',
+ description => 'Translate nicks to HipChat "mention names"',
+ licence => 'BSD',
+);
+
+my %NICK_TO_MENTION;
+my $LAST_MAP_UPDATED = 0;
+
+sub get_hipchat_people {
+ my $ua = LWP::UserAgent->new;
+ $ua->timeout(5);
+
+ my $auth_token = Irssi::settings_get_str('hipchat_auth_token');
+ if (!$auth_token) {
+ return;
+ }
+ my $r = HTTP::Request->new('GET',
+ "https://api.hipchat.com/v1/users/list?auth_token=$auth_token");
+ my $response = $ua->request($r);
+
+ my $hipchat_users = from_json($response->decoded_content)->{users};
+ 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);