🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #39941

[slrn] new version of save.sl out for testing

#39941
From: Jan Hlavacek
Date: Sun, 05 Apr 1998 00:00
380 lines
13533 bytes
I've completely rewritten my macros for saving articles into folders.
The new version uses slang v.1+,  and introduces completely new
interface.  It works on Unix,  but I tried to keep all filename material
in two or three separate functions,  so if somebody wants to provide
OS/2,  windows or VMS version,  it shouldn't be to difficult. 

There are some rather terse usage notes at the beginning of the file.
The file will also be available on
http://www.math.ohio-state.edu/%7Elahvak/slang/files/slrn/filename.sl

cat > filename.sl <<EOF
% This a set of S-Lang 1.0+ macros for flexible changing of default filenames
% for saved article in slrn.  It stemmed from save.sl,  but the interface is 
% completely changed.  Please email any questions and comments to
% <lahvak@math.mps.ohio-state.edu>
%
% Usage:
%
%% % load in the file, and assign key:
%% evalfile ("filename.sl");
%% definekey ("my_save", "o", "article");
%% 
%% % create filename conditions.  All the conditions are in form
%% %   condition_function (data,  foldername);
%% % where the data depend on which function are you using,  and foldername
%% % is a name of folder relative to the directory called "folders" under your
%% % save_directory directory.  The foldername can start with `/' or `~',  in 
%% % which case it will be an absolute pathname or file under your home 
%% % directory,  respectively.
%% % 
%% % The conditions will be tried starting with the LAST one and working back
%% % until one works,  so you want to put more general conditions first.
%% % After the filename is found or after all conditions are exhausted,  
%% % filename_hook is called.  If the filename is still empty,  the newsgroup 
%% % name in lowercase will be used.
%%
%% % Examples of conditions:
%% 
%% % If anything else fails,  save it by authors login name,  if you can find
%% % it (note the substitution pattern in the second argument):
%% Filename_Author_Regexp ("\\<\\([-a-zA-Z0-9._+]+\\)@", "\\1");
%%
%% % filenames starting with '/' are treated correctly as absolute pathnames:
%% Filename_Author_Substr ("nospam", "/dev/null");
%%
%% % Exact match for newsgroup name:
%% Filename_Newsgroup_Exact ("news.software.readers", "newsreaders");
%%
%% % Save us.jobs into jobs.us and osu.jobs into jobs.osu and so on:
%% Filename_Newsgroup_Regexp ("\\<\\([^.]+\\)\\.jobs", "jobs.\\1");
%%
%% % For perl newsgroups, throw away the "comp.lang.",  but preserve 
%% % the hierarchy under "perl.", so articles in comp.lang.perl.misc
%% % will be saved in perl.misc and so on:
%% Filename_Newsgroup_Regexp ("\\.\\(perl\\..+\\)$", "\\1");
%%
%% % There is only one newsgroup containing the word "czech",  so we can
%% % use substring match:
%% Filename_Newsgroup_Substr ("czech", "sccs");
%%
%% % This kind of filenames works well,  too:
%% Filename_Author_Substr ("lahvak", "~/important");
%% 
%% % This illustrate how to use "User_Match" conditions.
%% % You have to define an user_match function,  which takes 4 
%% % arguments: string to match in (like current newsgroup,  subject,
%% % From header and so on),  pattern to match,  name to return if
%% % the match was successful,  and user data,  which can be anything
%% % you want.  It must return the folder name if the match succeeded, 
%% % or empty string if it didn't.
%% % The following function works like filename_regexp,  but only in
%% % specified group.  The group name will be stored as user_data.
%% 
%% define match_reg_in_group (str, pat, name, group)
%% {
%%    if (is_substr (current_newsgroup (), group))
%%        return (filename_regexp (str, pat, name));
%%    return "";
%% }
%% 
%% % To use this function,  you call Filename_*_User_Match with 4 arguments:
%% % pattern to match,  which will be passed to the user_match function as 
%% % its second argument,  REFERENCE to the user_match function,  user_data,  
%% % which can be anything,  and which will be passed to user function as its 
%% % last argument,  and finally foldername,  which will become the third
%% % argument of the user_match function.
%% % 
%% % This saves anything with "software" in the subject into "linux.software",
%% % but only in "linux" newsgroups:
%% Filename_Subject_User_Match ("software", &match_reg_in_group, "linux", 
%%                              "linux.software");
%%
%% % This saves everything about rxvt into "rxvt.other".  However,  articles 
%% % about rxvt in "linux" newsgroups will get saved by the following
%% % condition,  so they will go to "rxvt.linux".
%% Filename_Subject_Substr ("rxvt", "rxvt.other");
%% Filename_Subject_User_Match ("rxvt", &match_reg_in_group, "linux", 
%%                              "rxvt.linux");
%%
%% % Save everything "smith" says in "linux" newsgroups:
%% Filename_Author_User_Match ("\<smith@", &match_reg_in_group, "linux", 
%%                             "linux.smith");
%% 
%% % Finally,  if this still isn't enough,  you can define filename_hook:
%% define filename_hook (group, subject, author, filename)
%% {
%% 	% filename is the filename as determined by the above rules.
%% 	% You can do anything here.  We will just make first letter of
%% 	% group uppercase and return that if filename is still empty.
%%      % You have to return the possibly modified filename.
%%      % You may for example want to verify that the filename we have
%%      % is indeed a legal filename,  an modify it if it isn't.
%% 
%%      !if (strlen (filename))
%%      {
%%           filename = strsub (group, 1, toupper (group[0]));
%%      }
%%      return filename;
%% }

typedef struct
{
   pattern,               % pattern to match
   what,                  % 'g' = group, 's' = subject, 'a' = author
   how_to_match,          % function,  se bellow
   name_if_matches,       % if it matches,  return this as a filename
   next,
   user_data              % for User_Match,  can be anything.
} Filename_Match_Type;

variable Filename_Match_List = NULL;

% This is used to build the list.  The most recent items are on the top,  so we
% can always override things easily.

define filename_add_to_list (list, str, what, proc, name, user_data)
{
   variable next = list;
   list = @Filename_Match_Type;
   list.pattern = str;
   list.what = what;
   list.how_to_match = proc;
   list.name_if_matches = name;
   list.next = next;
   list.user_data = user_data;
   return list;
}

% Three default match functions.  Each match function takes 4 arguments:
% string to match (current group,  subject and so on),  pattern, a name to
% return upon success,  and user data.

% Succedes if pattern is substring of str.
define filename_substr (str, pat, name, user_data)
{
   if (is_substr (str, pat)) return (name);
   return ("");
}

% Succedes if pattern exactly matches str.
define filename_exact (str, pat, name, user_data)
{
   !if (strcmp (str, pat)) return (name);
   return ("");
}

% Succedes if pattern matches str as a regexp.  The name can be a substitution
% pattern,  containing \0, \1 ... \9.  These are replaced in the following way:
% \0 is replaced by the entire match,  \n,  n = 1 ... 9, is replaced by n-th
% submatch (delimited by \( and \).  To include \,  name has to contain \\.
define filename_regexp (str, pat, name, user_data)
{
   variable n, ch, length = strlen(name);
   variable sstr, newname = "";
   variable pos, len;

   ERROR_BLOCK
   {
      _clear_error ();
      message ("Error in pattern matching, ignoring pattern " + pat);
      return "";
   }

   if (string_match (str, pat, 1))
   {
      for (n=0; n < length; n++)
       {
	  ch = name[n];
	  sstr = "";
	  if ((ch == '\\') and (n < length-1)) 
	  {
	     n++;
	     ch = name[n];
	     if (ch == '&') ch = 0;
	     if ((ch >= '0') and (ch <= '9'))
	     {
		(pos, len) = string_match_nth (ch - '0');
		sstr = substr (str, pos+1, len);
	     }
	  }
	  !if (strlen (sstr)) sstr = strsub (" ", 1, ch);
	  newname += sstr;
       }
   }
   return newname;
}

% Use these functions to add filename condition.  Se examples above.
define Filename_Newsgroup_Substr (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 'g',
   &filename_substr, name, NULL);
}

define Filename_Newsgroup_Regexp (pattern, subst)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, pattern,
   'g', &filename_regexp, subst, NULL);
}

define Filename_Newsgroup_Exact (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 'g',
   &filename_exact, name, NULL);
}

define Filename_Newsgroup_User_Match (str, fun, user_data, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 'g',
   fun, name, user_data);
}

define Filename_Subject_Substr (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 's',
   &filename_substr, name, NULL);
}

define Filename_Subject_Regexp (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 's',
   &filename_regexp, name, NULL);
}

define Filename_Subject_Exact (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 's',
   &filename_exact, name, NULL);
}

define Filename_Subject_User_Match (str, fun, user_data, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 's',
   fun, name, user_data);
}

define Filename_Author_Substr (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 'a',
   &filename_substr, name, NULL);
}

define Filename_Author_Regexp (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 'a',
   &filename_regexp, name, NULL);
}

define Filename_Author_Exact (str, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 'a',
   &filename_exact, name, NULL);
}

define Filename_Author_User_Match (str, fun, user_data, name)
{
   Filename_Match_List = filename_add_to_list (Filename_Match_List, str, 'a',
   fun, name, user_data);
}

% The following macro was posted by J.B. Nicholson-Owens
% (<jbn@pop-a-wheelie.midwest.net>):

define append_article_save (file)
{
   % Saves the current article to the named file, appending if the file
   % exists.

   if (is_group_mode) return ();        % this only works in article mode

   set_input_string (file);             % put filename in the response stack
   call ("save");                       % save article (into file)
}

% Takes bunch of strings and turns them into full path under your home
% directory.  (How to do this on VMS,  windows  and OS/2 ?)
define filename_create_home_path ()
{
   variable path = getenv ("HOME");
   variable fn;
   _stk_reverse (_NARGS);
   loop (_NARGS)
   {
      fn = ();
      path += ("/" + fn);
   }
   path;
}

define filename_complete_filename (file)
{
   % Append the name of the file to ${HOME}/News/
   % unless it starts with ~ or /
   
   if (file[0] == '~') 
      return (filename_create_home_path (substr (file, 2, -1)));
   if (file[0] == '/') return (file);
   
   filename_create_home_path (get_variable_value("save_directory"),
                          "folders", file);
}


define my_save ()
{
   % Saves article into a folder based on some criteria.
   % Offers the folder name to edit.
   
   if (is_group_mode) return ();
   
   variable group = current_newsgroup;
   variable subject = strlow (extract_article_header ("Subject"));
   variable author = strlow (extract_article_header ("From"));

   variable next = Filename_Match_List;
   variable file = "";
   variable what_to_match;

   while (next != NULL)
   {
      switch (next.what)
      {case 'g' : what_to_match = group;}
      {case 's' : what_to_match = subject;}
      {case 'a' : what_to_match = author;}

      file = @next.how_to_match (what_to_match, next.pattern,
				 next.name_if_matches, next.user_data);
      if (strlen (file)) break;
      next = next.next;
   }

   if (is_defined ("filename_hook"))
   {
      group;
      subject;
      author;
      file;
      eval ("filename_hook ()");
      file = ();
   }

   !if (strlen (file)) file = group;
   
   % Not very likely that I'll edit the name,  
   % if I don't like it,  I'll just type in a new one.
   file = read_mini ("Append to folder", file, "");
   
   append_article_save (filename_complete_filename (file));
}
EOF


-- 
Jan Hlav\'{a}\v{c}ek <lahvak@math.mps.ohio-state.edu>  (BCC's will bounce)
home page: http://www.math.ohio-state.edu/~lahvak/
slrn page: http://www.math.ohio-state.edu/~lahvak/software/slrn.html
slrn documentation: http://www.math.ohio-state.edu/~lahvak/software/slrn/

Technical Details
Message-ID: <slrn6ifkk6.jai.lahvak@mingus.mps.ohio-state.edu>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <slrn6ifkk6.jai.lahvak@mingus.mps.ohio-state.edu>
Subject: [slrn] new version of save.sl out for testing
Date: Sun, 05 Apr 1998 00:00:00 +0000
From: lahvak@math.mps.ohio-state.edu (Jan Hlavacek)
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!not-for-mail
X-Deja-AN: 341128200
Organization: Department of Mathematics, The Ohio State University
Reply-To: lahvak@math.mps.ohio-state.edu
Newsgroups: news.software.readers