🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.readers
1 messages
1 total messages Started by Dawn Owens-Nicholson Mon, 10 May 1999 00:00
[slrn] Macro set: URIReference.sl -- post and look up references in Usenet archives
#48474
Author: Dawn Owens-Nicholson
Date: Mon, 10 May 1999 00:00
526 lines
17496 bytes
I recently posted this, but Dejanews changed its name to "deja.com", and I
thought it would be easier for people to use it w/o having to apply diffs.
It's not that long (520 lines).

% URIReference.sl                             % -*- mode: slang; mode: fold -*-
%
% Copyright � 1998 J.B. Nicholson-Owens
% Author: J.B. Nicholson-Owens
% Title: URIReference.sl
% Version: 1.1
% Requires: slrn 0.9.5.5 or greater, S-Lang 1.3.5 or greater

#iffalse % Documentation                                                %{{{

This macro set lets slrn users:

   * append URIs to their posts so referenced message-IDs are easier to find.

   * read the article referenced with a message-ID by prompting them
     similarly to slrn's prompt for URLs.

   * scans a string for message-IDs and produces an array of the IDs found
     in appearance order. (see URIReference->msgid_scan())

To install this macro set:

(1) Save a copy of this file (or article) as "URIReference.sl" somewhere so
    slrn can find it.  Your home directory will do if you don't have a
    better place for it.

(2) Append the following lines to your slrn.rc ($HOME/.slrnrc on Unix) to
    make slrn load this file and set up some keys to access the functions:

       interpret "URIReference.sl"
       setkey article URIReference->browse_msgids "^U"

(3) Put the following lines in a hooks file (if you don't know what this is
    or if you don't have one, make a file called "my_hooks.sl" in your
    homedir):

        public define post_filter_hook (file)
        {
           URIReference->post_filter_hook (file);
        }

(4) Quit slrn and restart slrn.  The new functionality is now available.
    When posting, hit "F" to run the filter and "E" to confirm the results.
    Hit ^U (Control-U) to view a list of message-IDs encoded as URIs for
    Usenet archives.

Optional steps:

(1) Preferences:  URIReference->set_preference()

  % Default settings shown below
  URIReference->set_preference ("archives", "deja.com");
  URIReference->set_preference ("duplicate_msgids_allowed", 0);
  URIReference->set_preference ("skip_headers", 1);
  URIReference->set_preference ("skip_quotes", 0);
  URIReference->set_preference ("skip_signature", 1);
  URIReference->set_preference ("single_URI_intro_string",
                                "\nThe referenced article can be found:\n");
  URIReference->set_preference ("plural_URI_intro_string",
                                "\nThe referenced articles can be found:\n");
  URIReference->set_preference ("skip_mentioned_message-IDs", 1);
  URIReference->set_preference ("skip_mentioned_URIs", 0);

  Set any of these preferences with the set_preference() function by placing
  set_preference() lines in a file and loading that file after this macro
  set is loaded.

  URIReference->set_preference ("archives", "deja.com");

  This preference is the list of Usenet archives you want to generate URIs
  for.  It is a whitespace separated list from the following archives:

    deja.com deja.com-search

  Others can be added with URIReference->add_archive().

  URIReference->set_preference ("duplicate_msgids_allowed", 0);

  If the same message-ID is mentioned in the article more than once and you
  don't mind it appearing as a URI in the generated list once per archive,
  set this preference to 1.  Normally, one mention per archive is
  sufficient.

  URIReference->set_preference ("skip_headers", 1);

  If you want the headers of an article searched for message-IDs as well as
  the body of the article, set this to 0.  Normally, one would want to skip
  the headers and begin searching at the first line of the body text.

  URIReference->set_preference ("skip_quotes", 0);

  Similar to skip_headers, this preference controls skipping quoted
  material. Quoted material is any line that begins with ">" with any amount
  of leading whitespace before the greater-than sign.  In the future, this
  will probably use the same quote regular expressions you set in the
  slrnrc.

  URIReference->set_preference ("skip_signature", 1);

  Similar to skip_quotes and skip_headers, this preference controls skipping
  the signature.  A signature is the part of the body text that begins with
  the topmost occurrence of "-- " at the beginning of the line.

  URIReference->set_preference ("single_URI_intro_string",
                                "\nThe referenced article can be found:\n");
  URIReference->set_preference ("plural_URI_intro_string",
                                "\nThe referenced articles can be found:\n");

  The single URI intro string preceeds a 1-URI list, the plural preceeds a 2
  or more URI list.  Of course, neither is used if there are no URIs to
  list.

  URIReference->set_preference ("skip_mentioned_message-IDs", 1);

  This macro can skip message-IDs that have already been mentioned somewhere
  else in the article.  If it looks like a message-ID has been mentioned in
  a URI somewhere in the article, URIs for that message-ID will not be
  listed.  For instance:

  In the article somewhere, the following is mentioned:

     Pat: Did you see 3Cabc@123.com%3E?
     Chris: No, but I saw <abc@123.com>.

  If skip_mentioned_message-IDs is set to nonzero, the generated URI list
  will not include an entry for <abc@123.com> because it the http-encoded
  form of that message-ID is in the article elsewhere.  This is set to 1 by
  default in case you accidentally filter the article more than once and so
  you don't mention already-mentioned URIs at unknown archives (it is
  unlikely someone would mention the http-escaped version of a message-ID
  unless they were listing a Usenet archive URI).

  URIReference->set_preference ("skip_mentioned_URIs", 0);

  This preference works similarly to skip_mentioned_message-IDs except that
  it targets complete URIs--for example:

     Pat: Did you see http://www.deja.com/msgid.xp?MID=%3Cabc@123.com%3E?
     Chris: No, thanks for the pointer!

   If skip_mentioned_URIs is set to nonzero, <abc@123.com> at deja.com will
   not be included in the list.

(2) Add archives with URIReference->add_archive().

    URIReference->add_archive (String_Type title, String_Type URI);

  The URI is actually a partial URI--it must have a single "%s" where the
  escaped message-ID will be substituted.

(3) URIReference->msgid_scan() will scan a string for message-IDs and return
    all the message-IDs it found (in the order in which it found them),
    including duplicates, in a string array:

       variable message_IDs = URIReference->msgid_scan (article_as_string ());

    will collect all the message-IDs in the current article in message_IDs.

(4) URIReference->browse_msgids() works just like slrn's browse_urls, except
    browse_msgids() lets you choose from a list of message-IDs at the
    archives you chose in the archives preference (see "archives" preference
    above).

To uninstall this macro set:

(1) Comment out or remove the URIReference.sl interpret command from your
    slrn.rc:

       % interpret "URIReference.sl"

(2) Comment out or remove the URIReference line from your
    post_filter_hook():

        public define post_filter_hook (file)
        {
           % URIReference->post_filter_hook (file);
        }

    If this is the only line in the hook definition, you can remove the
    entire definition.

(3) Comment out or remove any URIReference preferences:

        % URIReference->set_preference ("archives", "deja.com");
	% URIReference->set_preference ("duplicate_msgids_allowed", 0);
	% URIReference->set_preference ("skip_headers", 1);
	  .
	  .

(4) Quit and restart slrn.  You should no longer have URIReference
    functionality.

Change log:
1.0: Released.
1.1: * Minor change to code and documentation--Dejanews became deja.com.  URIs
       were updated with new information.
       "Dejanews" archive became "deja.com"
       "Dejanews-search" archive became "deja.com-search"
     * Added URIReference->version().

#endif %}}}

implements ("URIReference");

private variable
   archives = Assoc_Type[String_Type],
   prefs = Assoc_Type[];

archives["deja.com"]        = "http://www.deja.com/msgid.xp?MID=%s";
archives["deja.com-search"] = "http://search.deja.com/msgid.xp?MID=%s";

prefs["archives"] = "deja.com";
prefs["duplicate_msgids_allowed"] = 0;
prefs["skip_headers"] = 1;
prefs["single_URI_intro_string"] = "\nThe referenced article can be found:\n";
prefs["plural_URI_intro_string"] = "\nThe referenced articles can be found:\n";
prefs["skip_mentioned_message-IDs"] = 1;
prefs["skip_mentioned_URIs"] = 0;
prefs["skip_signature"] = 1;
prefs["skip_quotes"] = 0;

private define http_escape (raw_url)                                    %{{{
{
   % Escape characters for use in an HTTP scheme URL.
   %
   % e.g., <slrn76marj.jpb.bluemoon@ux6.cso.uiuc.edu> becomes
   %       %3Cslrn76marj.jpb.bluemoon@ux6.cso.uiuc.edu%3E
   %
   % Message-ID matching from Usefor Message-ID draft.  This should be
   % changed as needed.  (I know it's not recommended to use a draft as a
   % reference, but I needed /something/ reasonable that would match only
   % message-IDs).  I let message-IDs have periods before the @, something
   % the Usefor draft doesn't allow.

   variable
     n = 0,
     ch = 0,
     url = "";

   for (n = 0; n < strlen (raw_url); n++)        % from RFC1738
     {
	if (string_match (char (raw_url[n]), "[-A-Za-z0-9$_.+!*'(),@]", 1))
	  url += char (raw_url[n]);
	else url += sprintf ("%%%02X", raw_url[n]);
     }

   return (url);
}

%}}}
private define regexp_quote (s)                                         %{{{
{
   return (str_quote_string (s, "\\^$[]*.+?", '\\'));
}

%}}}

static  define version ()                                     { return (1.1); }
static  define set_preference (preference, value)                       %{{{
{
   preference = string (preference);
   !if (assoc_key_exists (prefs, preference))
     error ("Preference does not exist: " + preference);
   variable desired_type = typeof (@prefs[preference]);
   if (typeof (value) != desired_type)
     verror ("Wrong type for %s: This preference wants %s not %s",
             preference,
             string (desired_type),
             string (typeof (value)));

   if (preference == "archives")
     {
	value = strtrim (value);
	!if (strlen (value))
	  {
	     message ("URIReference macro set requires Usenet archives, using default.");
	     return ();
	  }

	variable archive = "";
	foreach (strtok (value, "^-A-Za-z0-9."))
	  {
	     archive = ();
	     !if (assoc_key_exists (archives, archive))
	       verror ("%s is an invalid archive", archive);
	  }
     }

   prefs[preference] = value;
}

%}}}
static  define add_archive (name, uri)                                  %{{{
{
   variable this_function = "URIReference->" + _function_name ();

   if (orelse
	{ typeof (name) != String_Type } { typeof (uri) != String_Type }
	{ name == "" }                   { uri == "" }
       	{ _NARGS != 2 })
     usage (this_function + " (String_Type name, String_Type uri);");

   uri = strtrim (uri);

   if (assoc_key_exists (archives, name))
     verror ("%s: The %s archive already exists, use another name",
	     this_function, name);
   !if (string_match (uri, "[a-zA-Z][a-zA-Z0-9]*://.+", 1))
     error (this_function + ": The URI is syntatically invalid.");
   !if (string_match (uri, "%s", 1))
     verror ("%s: The URI must have one (and only one) \"%%s\" in it.",
	     this_function);

   archives[name] = uri;
}

%}}}
static  define msgid_scan (body)                                        %{{{
{
   variable
     msgid_regexp = "\\(<[^.][^][()<>@;:,]+@[-a-zA-Z0-9.]+[-a-zA-Z0-9]>\\)",
     pos = 1,
     len = 0,
     item = "",
     count = 0,
     item_hash = Assoc_Type[String_Type],
     t = 0,
     list = NULL;

   while (string_match (body, msgid_regexp, pos))
     {
	(pos, len) = string_match_nth (1);
	item = substr (body, pos + 1, len);
	pos += len + 1;

	item_hash[string (count)] = item;
	count++;
     }

   list = String_Type[count];
   for (t = 0; t < count; t++) list[t] = item_hash[string (t)];
   return (list);
}

%}}}
static  define post_filter_hook (file)                                  %{{{
{
   !if (strlen (@prefs["archives"])) return ();

   variable fp = fopen (file, "r+");

   if (fp == NULL)
     verror ("%s: Open error: %s", _function_name (), errno_string (errno));

   variable
     line = "",
     article = "",
     pos = 1,
     len = 0,
     msgid = "",
     escaped_msgid = "",
     uri = "",
     archive = "",
     count = 0,
     t = 0,
     msgid_hash = Assoc_Type [Null_Type],
     user_archives = strtok (@prefs["archives"], "^-A-Za-z0-9."),
     msgid_list = NULL,
     msgid_tail = NULL,
     uri_list = NULL,
     uri_tail = NULL,
     node = NULL,
     prev = NULL;

   EXIT_BLOCK
     if (fclose (fp) == -1)
       verror ("%s: Close error: %s",
	       _function_name (), errno_string (errno));

   if (@prefs["skip_headers"])
     while (fgets (&line, fp) != -1) if (strlen (line) == 1) break;
   while (fgets (&line, fp) != -1)
     {
	if (line == "-- ") break;
	if (andelse
	     { @prefs["skip_quotes"] }
	     { string_match (line, "^[ \t]*>", 1) })
	  continue;

	article += substr (line, 1, strlen (line) - 1);
     }
   !if (@prefs["skip_signature"])
     while (fgets (&line, fp) != -1)
       article += substr (line, 1, strlen (line) - 1);

   foreach (msgid_scan (article))
     {
	msgid = ();

	!if (@prefs["duplicate_msgids_allowed"]) msgid_hash[msgid] = NULL;

	node = struct { msgid, next };
	node.msgid = msgid;
	node.next = NULL;
	if (msgid_list == NULL) msgid_list = node;
	else msgid_tail.next = node;
	msgid_tail = node;
     }

   prev = msgid_list;
   !if (@prefs["duplicate_msgids_allowed"]) foreach (msgid_list)
     {
	node = ();
	msgid = node.msgid;

	if (assoc_key_exists (msgid_hash, msgid))
	  {
	     prev = node;
	     assoc_delete_key (msgid_hash, msgid);
	  }
	else prev.next = node.next;
     }

   foreach (user_archives)
     {
	archive = ();
	foreach (msgid_list)
	  {
	     node = ();
	     escaped_msgid = http_escape (node.msgid);
	     uri = sprintf (archives[archive], escaped_msgid);

	     if (orelse
		  { andelse
		       { @prefs["skip_mentioned_message-IDs"] }
		       { string_match (article, regexp_quote (escaped_msgid), 1) } }
		  { andelse
		       { @prefs["skip_mentioned_URIs"] }
		       { string_match (article, regexp_quote (uri), 1) } })
	       continue;

	     node = struct { uri, next };
	     node.uri = uri;
	     node.next = NULL;
	     if (uri_list == NULL) uri_list = node;
	     else uri_tail.next = node;
	     uri_tail = node;

	     count++;
	  }
     }

   switch (count)
     { case 0: return (); }
     { case 1: t = @prefs["single_URI_intro_string"]; }
     {         t = @prefs["plural_URI_intro_string"]; }

   if (fputs (t, fp) == -1)
     verror ("%s: Write error: %s", _function_name (), errno_string (errno));

   foreach (uri_list)
     {
	node = ();

	if (fputs (node.uri + "\n", fp) == -1)
	  verror ("%s: Write error: %s",
		  _function_name (), errno_string (errno));
     }
}

%}}}
static  define browse_msgids ()                                         %{{{
{
   if (is_group_mode ())
     error (_function_name () + " doesn\'t work in group mode!");

   variable
     msgids = msgid_scan (article_as_string ()),
     num_msgids = length (msgids);

   !if (num_msgids) error ("No message-IDs found.");

   variable
     user_archives = strtok (@prefs["archives"], "^-A-Za-z0-9."),
     num_uris = num_msgids * length (user_archives),
     uris = String_Type[num_uris],
     count = 0,
     archive = "",
     msgid = "",
     browser = get_variable_value ("non_Xbrowser"),
     uri = "";

   if (get_variable_value ("mouse")) browser = get_variable_value ("Xbrowser");

   foreach (user_archives)
     {
	archive = ();
	foreach (msgids)
	  {
	     msgid = ();
	     uris[count] = sprintf (archives[archive], http_escape (msgid));
	     count++;
	  }
     }

   if (num_uris > 1)
     {
	"URI";
	foreach (uris);
	num_uris;
	1;
	uri = select_list_box ();
	!if (strlen (uri)) return ();
     }
   else uri = uris[0];
   uri = read_mini ("Browse (^G aborts)", "", uri);
   () = system (sprintf (browser, uri));
}

%}}}

Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads