New paste Repaste Download
#!/usr/bin/env -S runvenv .venv
#
# (c) 2025, Ryan A. Colyer
# Made available under the GPLv3 license
#
import maxminddb
import datetime
import pytz
import sys
import os
script_dir = os.path.dirname(os.path.realpath(__file__))
db = maxminddb.open_database(os.path.join(script_dir, 'GeoLite2-City.mmdb'))
if len(sys.argv) < 2:
  sys.exit(-1)
ipaddr = sys.argv[1]
try:
  geo = db.get(ipaddr)
except ValueError:
  # raised if maxminddb thinks this is not an ip address
  import socket
  try:
    # Fall back to treating it like a hostname.
    hostname = ipaddr
    ipaddr = socket.gethostbyname(hostname)
    geo = db.get(ipaddr)
  except Exception:
    sys.exit(-1)
reslist = []
tzoffset = ''
# Check for all the useful entries, failing gracefully on every missing one.
try:
  reslist.append(geo['city']['names']['en'])
except Exception:
  pass
try:
  reslist.extend([e['names']['en'] for e in geo['subdivisions']])
except Exception:
  pass
try:
  reslist.append(geo['country']['names']['en'])
except Exception:
  try:
    reslist.append(geo['registered_country']['names']['en'])
  except Exception:
    pass
try:
  offset = pytz.timezone(geo['location']['time_zone']).utcoffset(datetime.datetime.now()).total_seconds()/3600
  tzoffset = f'UTC{"+" if offset>0 else ""}{offset:g}'
  #latlong = f'{geo["location"]["latitude"]}, {geo["location"]["longitude"]}'
except Exception:
  pass
resstr = ', '.join({k:0 for k in reslist}.keys())
resstr = ': '.join([s for s in [resstr, tzoffset] if s])
if resstr:
  print(resstr, end='')
Filename: None. Size: 2kb. View raw, , hex, or download this file.
# Irssi TagInfo Join Plugin
#
# Installation:
#   - Save as ~/.irssi/scripts/taginfo_join.pl
#   - In Irssi: /script load taginfo_join.pl
#
# Configuration:
#   - /set taginfo_enabled ON|OFF (Default: ON)
#
# Requirements:
#   - An external command (e.g., 'taginfo') that accepts an IP address or
#     hostname as an argument and prints location information to standard
#     output.
use strict;
use warnings;
use Irssi;
use Irssi::Irc;
# --- Plugin Information ---
our $VERSION = '1.0';
our %IRSSI = (
    authors     => 'Me',
    contact     => '',
    name        => 'taginfo_join',
    description => 'Adds location info from external taginfo command to join messages based on WHOIS IP.',
    license     => 'GPLv3',
    url         => '',
);
Irssi::settings_add_bool('misc', 'taginfo_enabled', 1);
# Print the final join message with location
sub print_join_message {
    my ($info, $location) = @_;
    my $server = Irssi::server_find_tag($info->{server_tag});
    my $window = $server ? $server->channel_find($info->{channel}) : undef;
    if (defined($window)) {
        $window->printformat(MSGLEVEL_JOINS, 'join_location', $info->{nick}, $info->{userhost}, $location, $info->{channel});
    }
}
# Called when a user joins a channel
sub sig_message_join {
    my ($server, $channel, $nick, $address) = @_;
    return unless Irssi::settings_get_bool('taginfo_enabled');
    return if $nick eq $server->{nick}; # Don't process joins for self
    my $lc_nick = lc $nick;
    my $server_tag = $server->{tag};
    if ($address =~ /^.+?@([A-Za-z0-9\.:\-]+)$/) {
        my $hostname = "$1";
        my $info = {
            nick     => $nick,
            channel  => $channel,
            userhost => $address, # Full user@host
            server_tag => $server_tag,
            time     => time(),
        };
        my $location = `taginfo $hostname`;
        if ($location ne "") {
            print_join_message($info, $location);
            Irssi::signal_stop();
        }
    }
}
# Define the custom format for the join message with location
# $0 = nick, $1 = user@host, $2 = location, $3 = channel
Irssi::theme_register([
    'join_location',
    # This example mimics the default join but adds the location in parentheses.
    '{channick_hilight $0} {chanhost_hilight $1} ({channick_hilight $2}) has joined {channel $3}'
]);
# Register signals
Irssi::signal_add_first('message join', 'sig_message_join');
# Initial messages
Irssi::print("TagInfoJoin Plugin v$VERSION Loaded.", MSGLEVEL_CLIENTNOTICE);
Irssi::print(" - Use: /set taginfo_enabled ON|OFF", MSGLEVEL_CLIENTNOTICE);
Filename: None. Size: 3kb. View raw, , hex, or download this file.

This paste expires on 2025-05-06 17:52:10.981120. Pasted through web.