# Type: Input # Category: Aliases # Description: Allows to define aliases # Author: Andriy Lesyuk sub Aliases_Load($) { my $user = shift; my %aliases = ( ); my $filename = sprintf("%s/%s.aliases", $main::config->Get('users', 'data'), $user->GetJabberUserID); if (open(ALIASES, '<', $filename)) { my $line = 1; while () { if (/^s\|([^|]+)\|([^|]+)\|g\n$/) { my ($string, $replace) = ($1, $2); $string =~ s/^\\b/(\\A| )/; $string =~ s/\\b$/( |\\Z)/; $aliases{$string} = $replace; } else { $main::logger->Log("invalid line %d in %s", $line, $filename); } $line++; } close(ALIASES); } return \%aliases; } sub Aliases_Save($$) { my ($user, $aliases) = @_; my $filename = sprintf("%s/%s.aliases", $main::config->Get('users', 'data'), $user->GetJabberUserID); if (open(ALIASES, '>:utf8', $filename)) { foreach my $alias (sort keys %{$aliases}) { my $string = $alias; $string =~ s/^\Q(\A| )\E/\\b/; $string =~ s/\Q( |\Z)\E$/\\b/; printf(ALIASES "s|%s|%s|g\n", $string, $aliases->{$alias}); } close(ALIASES); } } sub Aliases_Remove($) { my $user = shift; my $filename = sprintf("%s/%s.aliases", $main::config->Get('users', 'data'), $user->GetJabberUserID); unlink($filename); } new Orangutan::Context( response => sub { my ($context, $user, $dummy, $message) = @_; my @result = ( ); my @regexps = ( 'Use ["\']?(.+?)["\']? (?:when|if) I (?:type|send) (?:(?:message|(text|phrase|words?|substring)) )?["\']?(.+?)["\']?!*\.*', '(?:When|if) I (?:type|send) (?:(?:message|(text|phrase|words?|substring)) )?["\']?(.+?)["\']? '. '(?:use|treat)(?: (?:it|this))?(?: as)? ["\']?(.+?)["\']?!*\.*', '(?:Always )?replace (?:(?:message|(text|phrase|words?|substring)) )?["\']?(.+?)["\']? with ["\']?(.+?)["\']?!*\.*', '(?:Always )?treat (?:(?:message|(text|phrase|words?|substring)) )?["\']?(.+?)["\']? as ["\']?(.+?)["\']?!*\.*', '(?:(?:Message|(text|phrase|words?|substring)) )?["\']?(.+?)["\']? means ["\']?(.+?)["\']?!*\.*' ); $message =~ s!\n! !sg; $message =~ s!^\s+!!; $message =~ s!\s+$!!; $message =~ s!\s{2,}! !g; for (my $i = 0; $i < scalar @regexps; $i++) { if ($message =~ /^$regexps[$i]$/i) { push(@result, 1); if ($i == 0) { push(@result, $2); push(@result, $3); push(@result, $1); } else { push(@result, $1); push(@result, $2); push(@result, $3); } last; } } return @result; }, handler => sub { my ($context, $user, $item, $substring, $string, $replace) = @_; if ($string !~ /^[ -{}~]+$/) { $user->SendMessage('Invalid characters in source string...'); return; } elsif ($replace !~ /^[ -{}~]+$/) { $user->SendMessage('Invalid characters in replacement string...'); return; } $string =~ s!^\s+!!; $string =~ s!\s+$!!; $string =~ s!\s{2,}! !g; $replace =~ s!^\s+!!; $replace =~ s!\s+$!!; $replace =~ s!\s{2,}! !g; my $newalias; if ($substring) { $newalias = '(\A| )'.quotemeta($string).'( |\Z)'; } else { $newalias = '^'.quotemeta($string).'$'; } my $aliases = $context->GetField('aliases'); if (lc($string) eq lc($replace)) { $user->SendMessage([ 'Not sure why you are telling me this...', 'This makes sense...', 'Definitely!..', 'Right...' ]); return; } else { my $found = 0; if (defined($aliases)) { foreach my $alias (keys %{$aliases}) { if (lc($alias) eq lc($newalias)) { delete($aliases->{$alias}); $found = 1; last; } } } if ($found) { $user->SendMessage([ 'Ok... Will use this one.', 'No problem.' ]); } else { $user->SendMessage([ 'Ok. Thanks for letting me know...', 'Ok. I will.', 'No problem.', 'I got it!' ]); } if (defined($aliases)) { $aliases->{$newalias} = $replace; } else { $aliases = { }; $aliases->{$newalias} = $replace; $context->SetField('aliases', $aliases); } Aliases_Save($user, $aliases); } }, filter => sub { my ($context, $user, $message) = @_; my $aliases = $context->GetField('aliases'); foreach my $alias (keys %{$aliases}) { if (${$message} =~ s|$alias|(defined($1)?$1:'').$aliases->{$alias}.(defined($2)?$2:'')|gie) { return 1; } } return 0; }, init => sub { my ($context) = @_; foreach my $username ($main::users->GetUsers) { my $user = $main::users->GetUser($username); my $aliases = $user->GetResponse($context->GetID); if ($aliases) { $aliases->SetField('aliases', Aliases_Load($user)); } } }, onsubscription => sub { my ($context, $user, $type, $status) = @_; if ($status eq 'unsubscribe') { Aliases_Remove($user); } }, help => { title => 'How can I specify an alias?', question => '^(?:How )?(?:(?:can|do) I |to )specify (?:(?:the|an?) )?alias(?:es)?\?*!*\.*$', answer => "Orangutan allows to specify translation rules or aliases. ". "An alias is a text which is transparently translated to a different text ". "this way allowing to specify a short form for a longer request. ". "When setting an alias user must specify source text, replacement text ". "and, optionally, should it be a message or just a phrase in a message. ". "By default, alias is set for the whole message.\n". "Some samples:\n". " o Use \"Activity is Research\" when I type \"Researching\"\n". " o If I type word \"FB\" use it as \"Fixing bugs on #869\"\n". " o Replace text \"Critical issue\" with #236\n". " o Always treat \"Bye\" as \"Done\"\n". " o Message todo means \"Show my issues\"\n". "To let Orangutan know that an alias should be used for a part of a message ". "specify \"words\" or \"text\"...", weight => 1210 }, copy => 1 ); # kate: syntax perl