5 Commits 090f4a9474 ... d61375cb7f

Author SHA1 Message Date
  Mikael Nordfeldth d61375cb7f Group autocompletion now fills in hostname part 6 years ago
  Mikael Nordfeldth 4c8a8848ed Groups can now be server-specific with !osm@gnusocial.de vs. !osm@sn.jonkman.ca 6 years ago
  Mikael Nordfeldth 00cbc852b0 Make use of variable preMention signal method 6 years ago
  Mikael Nordfeldth 8dd06cd8d8 Harmonize webfinger formatting and enable variable pre-mention character 6 years ago
  Mikael Nordfeldth 20801a32f7 Logging term used HubSub instead of WebSub. 6 years ago

+ 4 - 1
lib/nickname.php

@@ -54,7 +54,10 @@ class Nickname
      * We could probably use an email regex here, but mainly we are interested
      * in matching it in our URLs, like https://social.example/user@example.com
      */
-    const WEBFINGER_FMT = '[0-9a-zA-Z_]{1,64}\@[0-9a-zA-Z_-.]{3,255}';
+    const WEBFINGER_FMT = '(?:\w+[\w\-\_\.]*)?\w+\@'.URL_REGEX_DOMAIN_NAME;
+
+    // old one without support for -_. in nickname part:
+    // const WEBFINGER_FMT = '[0-9a-zA-Z_]{1,64}\@[0-9a-zA-Z_-.]{3,255}';
 
     /**
      * Regex fragment for checking a canonical nickname.

+ 6 - 6
lib/util.php

@@ -768,7 +768,7 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
             }
         }
 
-        $matches = common_find_mentions_raw($text);
+        $matches = common_find_mentions_raw($text, '@');
 
         foreach ($matches as $match) {
             try {
@@ -847,9 +847,8 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
                                 'url' => $url);
         }
 
-        preg_match_all('/'.Nickname::BEFORE_MENTIONS.'!(' . Nickname::DISPLAY_FMT . ')/',
-                       $text, $hmatches, PREG_OFFSET_CAPTURE);
-        foreach ($hmatches[1] as $hmatch) {
+        $hmatches = common_find_mentions_raw($text, '!');
+        foreach ($hmatches as $hmatch) {
             $nickname = Nickname::normalize($hmatch[0]);
             $group = User_group::getForNickname($nickname, $sender);
 
@@ -879,9 +878,10 @@ function common_find_mentions($text, Profile $sender, Notice $parent=null)
  * Should generally not be called directly; for use in common_find_mentions.
  *
  * @param string $text
+ * @param string $preMention Character(s) that signals a mention ('@', '!'...)
  * @return array of PCRE match arrays
  */
-function common_find_mentions_raw($text)
+function common_find_mentions_raw($text, $preMention='@')
 {
     $tmatches = array();
     preg_match_all('/^T (' . Nickname::DISPLAY_FMT . ') /',
@@ -891,7 +891,7 @@ function common_find_mentions_raw($text)
 
     $atmatches = array();
     // the regexp's "(?!\@)" makes sure it doesn't matches the single "@remote" in "@remote@server.com"
-    preg_match_all('/'.Nickname::BEFORE_MENTIONS.'@(' . Nickname::DISPLAY_FMT . ')\b(?!\@)/',
+    preg_match_all('/'.Nickname::BEFORE_MENTIONS.preg_quote($preMention, '/').'(' . Nickname::DISPLAY_FMT . ')\b(?!\@)/',
                    $text,
                    $atmatches,
                    PREG_OFFSET_CAPTURE);

+ 4 - 2
plugins/Autocomplete/actions/autocomplete.php

@@ -66,7 +66,9 @@ class AutocompleteAction extends Action
         foreach($this->groups as $group){
             $max = max($max,strtotime($group->modified));
         }
-        return $max;
+        // but maybe this file has been modified after that and could
+        // respond differently
+        return max($max, filemtime(__FILE__));
     }
 
     /**
@@ -161,7 +163,7 @@ class AutocompleteAction extends Action
             $acct = $profile->getAcctUri();
             $identifier = explode(':', $profile->getAcctUri(), 2)[1];
             $results[] = array(
-                'value' => '!'.$group->getNickname(),
+                'value' => '!'.$identifier,
                 'nickname' => $group->getNickname(),
                 'acct_uri' => $acct,
                 'label'=> "${identifier} (".$group->getFullname().")",

+ 77 - 12
plugins/OStatus/OStatusPlugin.php

@@ -258,14 +258,15 @@ class OStatusPlugin extends Plugin
 
     /**
      * Webfinger matches: @user@example.com or even @user--one.george_orwell@1984.biz
+     * @param   string  $text       The text from which to extract webfinger IDs
+     * @param   string  $preMention Character(s) that signals a mention ('@', '!'...)
      *
-     * @return  array   The matching IDs (without @ or acct:) and each respective position in the given string.
+     * @return  array   The matching IDs (without $preMention) and each respective position in the given string.
      */
-    static function extractWebfingerIds($text)
+    static function extractWebfingerIds($text, $preMention='@')
     {
         $wmatches = array();
-        // Maybe this should harmonize with lib/nickname.php and Nickname::WEBFINGER_FMT
-        $result = preg_match_all('/(?<!\S)@((?:\w+[\w\-\_\.]*)?\w+@'.URL_REGEX_DOMAIN_NAME.')/',
+        $result = preg_match_all('/(?<!\S)'.preg_quote($preMention, '/').'('.Nickname::WEBFINGER_FMT.')/',
                        $text,
                        $wmatches,
                        PREG_OFFSET_CAPTURE);
@@ -279,15 +280,17 @@ class OStatusPlugin extends Plugin
 
     /**
      * Profile URL matches: @example.com/mublog/user
+     * @param   string  $text       The text from which to extract URL mentions
+     * @param   string  $preMention Character(s) that signals a mention ('@', '!'...)
      *
      * @return  array   The matching URLs (without @ or acct:) and each respective position in the given string.
      */
-    static function extractUrlMentions($text)
+    static function extractUrlMentions($text, $preMention='@')
     {
         $wmatches = array();
         // In the regexp below we need to match / _before_ URL_REGEX_VALID_PATH_CHARS because it otherwise gets merged
         // with the TLD before (but / is in URL_REGEX_VALID_PATH_CHARS anyway, it's just its positioning that is important)
-        $result = preg_match_all('/(?:^|\s+)@('.URL_REGEX_DOMAIN_NAME.'(?:\/['.URL_REGEX_VALID_PATH_CHARS.']*)*)/',
+        $result = preg_match_all('/(?:^|\s+)'.preg_quote($preMention, '/').'('.URL_REGEX_DOMAIN_NAME.'(?:\/['.URL_REGEX_VALID_PATH_CHARS.']*)*)/',
                        $text,
                        $wmatches,
                        PREG_OFFSET_CAPTURE);
@@ -312,9 +315,9 @@ class OStatusPlugin extends Plugin
     {
         $matches = array();
 
-        foreach (self::extractWebfingerIds($text) as $wmatch) {
+        foreach (self::extractWebfingerIds($text, '@') as $wmatch) {
             list($target, $pos) = $wmatch;
-            $this->log(LOG_INFO, "Checking webfinger '$target'");
+            $this->log(LOG_INFO, "Checking webfinger person '$target'");
             $profile = null;
             try {
                 $oprofile = Ostatus_profile::ensureWebfinger($target);
@@ -332,7 +335,7 @@ class OStatusPlugin extends Plugin
 
             assert($profile instanceof Profile);
 
-            $text = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target)
+            $displayName = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target)
                     ? $profile->getNickname()   // TODO: we could do getBestName() or getFullname() here
                     : $target;
             $url = $profile->getUri();
@@ -341,7 +344,69 @@ class OStatusPlugin extends Plugin
             }
             $matches[$pos] = array('mentioned' => array($profile),
                                    'type' => 'mention',
-                                   'text' => $text,
+                                   'text' => $displayName,
+                                   'position' => $pos,
+                                   'length' => mb_strlen($target),
+                                   'url' => $url);
+        }
+
+        // Doing groups in a separate routine because webfinger lookups don't work
+        // remotely until everyone updates etc. etc.
+        foreach (self::extractWebfingerIds($text, '!') as $wmatch) {
+            list($target, $pos) = $wmatch;
+            list($target_nickname, $target_hostname) = explode('@', parse_url($target, PHP_URL_PATH));
+            $this->log(LOG_INFO, sprintf('Checking webfinger group %s as user %s on server %s', $target, $target_nickname, $target_hostname));
+
+            $profile = null;
+            if ($target_hostname === mb_strtolower(common_config('site', 'server'))) {
+                try {
+                    $profile = Local_group::getKV('nickname', $target_nickname)->getProfile();
+                } catch (NoSuchGroupException $e) {
+                    // referenced a local group which does not exist, so not returning it as a mention
+                    $this->log(LOG_ERR, "Local group lookup failed: " . _ve($e->getMessage()));
+                    continue;
+                }
+            } else {
+                // XXX: Superhacky. Domain name can be incorrectly matched
+                //      here. But since users are only members of groups
+                //      they trust (of course they are!), the likelihood of
+                //      a mention-hijacking is very very low... for now.
+                $possible_groups = new User_group();
+                $possible_groups->nickname = $target_nickname;
+                if (!$possible_groups->find()) {
+                    common_debug('No groups at all found with nickname: '._ve($target_nickname));
+                    continue;
+                }
+                while ($possible_groups->fetch()) {
+                    if (!$sender->isMember($possible_groups)) {
+                        continue;
+                    }
+                    $group_hostname = mb_strtolower(parse_url($possible_groups->mainpage, PHP_URL_HOST));
+                    if ($target_hostname === $group_hostname) {
+                        common_debug(sprintf('Found group with nick@host (%s@%s) matching %s', _ve($possible_groups->nickname), _ve($group_hostname), _ve($target)));
+                        $profile = $possible_groups->getProfile();
+                        break;
+                    }
+                }
+                $possible_groups->free();
+                if (!$profile instanceof Profile) {
+                    common_debug('Found groups with correct nickname but not hostname for: '._ve($target));
+                    continue;
+                }
+            }
+
+            assert($profile instanceof Profile);
+
+            $displayName = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target)
+                    ? $profile->getNickname()   // TODO: we could do getBestName() or getFullname() here
+                    : $target;
+            $url = $profile->getUri();
+            if (!common_valid_http_url($url)) {
+                $url = $profile->getUrl();
+            }
+            $matches[$pos] = array('mentioned' => array($profile),
+                                   'type' => 'group',
+                                   'text' => $displayName,
                                    'position' => $pos,
                                    'length' => mb_strlen($target),
                                    'url' => $url);
@@ -357,11 +422,11 @@ class OStatusPlugin extends Plugin
                     $oprofile = Ostatus_profile::ensureProfileURL($url);
                     if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
                         $profile = $oprofile->localProfile();
-                        $text = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target) ?
+                        $displayName = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target) ?
                                 $profile->nickname : $target;
                         $matches[$pos] = array('mentioned' => array($profile),
                                                'type' => 'mention',
-                                               'text' => $text,
+                                               'text' => $displayName,
                                                'position' => $pos,
                                                'length' => mb_strlen($target),
                                                'url' => $profile->getUrl());

+ 3 - 3
plugins/OStatus/lib/huboutqueuehandler.php

@@ -58,12 +58,12 @@ class HubOutQueueHandler extends QueueHandler
             $success = $sub->push($atom);
             // The reason I split these up is because I want to see how the algorithm acts in practice.
             if ($success) {
-                common_debug('HubSub push completed successfully!');
+                common_debug('WebSub push completed successfully!');
             } else {
-                common_debug('HubSub push failed with an HTTP error.');
+                common_debug('WebSub push failed with an HTTP error.');
             }
             if ($sub->getErrors()>0) {
-                common_debug('Resetting HubSub push error count following successful reset.');
+                common_debug('Resetting WebSub push error count following successful reset.');
                 $sub->resetErrors();
             }
         } catch (AlreadyFulfilledException $e) {