Article View: news.software.readers
Article #43238[slrn] auto_sub_unsub -- a slrn macro for automatic group subscription/unsubscription
From: Dawn Owens-Nicholson
Date: Tue, 15 Sep 1998 00:00
Date: Tue, 15 Sep 1998 00:00
429 lines
13406 bytes
13406 bytes
Would you like to automatically subscribe and unsubscribe to newsgroups by
making a list of regular expressions and subscribing (or unsubscribing) to
the new groups that match the regexes? This macro set will let you do that.
% Automatic sub/unsub from newsgroups % -*- mode: slang; mode: fold -*-
%
% Copyright 1998 J.B. Nicholson-Owens
% Title: auto_sub_unsub.sl
% Author: J.B. Nicholson-Owens
% Version: 2.0
% Requires: slrn 0.9.5.2 or greater, S-Lang 1.0 or greater
#iffalse % Documentation
Would you like to automatically subscribe and unsubscribe to newsgroups by
making a list of regular expressions and subscribing (or unsubscribing) to
the new groups that match the regexes? This macro set will let you do that.
This is an update to a macro set I wrote a while ago. It is self-contained;
unlike the previous version, this file holds all the code necessary to work.
To install this macro set:
(1) Save a copy of this file (or article) as "auto_sub_unsub.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 keys to access the functions:
interpret "auto_sub_unsub.sl"
(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):
define group_mode_startup_hook ()
{
if (is_defined ("auto_sub_unsub_group_mode_startup_hook"))
eval ("auto_sub_unsub_group_mode_startup_hook ();");
}
If you already have a group_mode_startup_hook() defined, add the lines
above starting with "if" and "eval". This will run the appropriate
code when you enter group mode after starting slrn.
(4) Quit and restart slrn. slrn will tell you it has loaded this file.
(5) From here, there are two ways to proceed. Ultimately, you will want a
list of S-Lang regular expressions to be used in determining which
groups are to be automatically subscribed or unsubscribed (depending on
whether you automatically unsubscribe from new groups or not). The
optional steps below list the details to manually build the regex list.
You can include the following lines in your slrn.rc after the
"interpret" command that loads this macro set:
setkey article auto_sub_unsub_add "^A"
setkey article auto_sub_unsub_edit "^E"
Then press ^A (Control-a) to add a regular expression, ^E to edit and
delete entries in your regular expression list. By default, the list
will be saved for you each time you make changes to it.
Optional steps:
(1) If you want to save the regex list yourself rather than have the macro
save it automatically, you can include the following lines in an S-Lang
file (I recommend a file called "my_preferences.sl" in your homedir if
you don't have a better place to put it):
auto_sub_unsub_save_frequency = 1;
setkey article auto_sub_unsub_save_as "\e#"
(2) You can make a regular expression list in a text editor instead of
building one from inside slrn. To do so, put each regular expression on
a line by itself. Comments are acceptable on lines by themselves, as
are blank lines. Comments and blank lines are skipped when read:
% A newsgroup hierarchy
^alt\.hokey-pokey\.
% A couple of specific groups
^local\.wine\.making$
^local\.wine\.drinking$
The default regex list file is "$HOME/.slrn-auto_sub_unsub_list". If you
want to use a different file you can:
* Set the AUTO_SUB_UNSUB environment variable before starting slrn.
This macro set will use this variable's value instead of the
default.
* Bind auto_sub_unsub_save_as to a key (as detailed in optional
step 1).
* Include the following in an S-Lang file (perhaps
"$HOME/my_preferences.sl"):
auto_sub_unsub_regex_file = "/path/to/your/regex/list";
If you do either of these optional steps, don't forget to make slrn load your
preferences. Append the following line to your slrn.rc if it's not already
there:
interpret "my_preferences.sl"
Restart slrn and you should see your preferences take effect. Enjoy the
macro set!
To remove (un-install) this macro set:
(1) Comment out or delete the following three lines from your slrn.rc:
interpret "auto_sub_unsub.sl"
interpret "my_hooks.sl"
interpret "my_preferences.sl"
You will want to keep the my_hooks and my_preferences lines in your
slrn.rc uncommented if you have other hooks or preferences.
(2) Comment out or delete the references to auto_sub_unsub* functions in
your hooks:
define group_mode_startup_hook ()
{
if (is_defined ("auto_sub_unsub_group_mode_startup_hook"))
eval ("auto_sub_unsub_group_mode_startup_hook ();");
}
would become:
define group_mode_startup_hook ()
{
% if (is_defined ("auto_sub_unsub_group_mode_startup_hook"))
% eval ("auto_sub_unsub_group_mode_startup_hook ();");
}
or, if this is the only line in the hook, you could remove the hook
definition entirely.
(3) Delete the regex list ($HOME/.slrn-auto_sub_unsub_list unless you've
specified otherwise). If you used an environment variable to point to
your regex list, you can delete that too (you probably set this in your
shell's startup files or on the command line, see your shell's
documentation for details on removing environment variables).
(4) Quit and restart slrn. Automatic subscription/unsubscription should no
longer work.
#endif
variable % all var should be file-local
asu_current_file = "", % regex list filename in use
asu_list = NULL, % regex list
asu_tail = NULL; % regex list tail node
!if (is_defined ("auto_sub_unsub_save_frequency"))
eval ("variable auto_sub_unsub_save_frequency = 1;");
!if (is_defined ("auto_sub_unsub_regex_file"))
eval ("variable auto_sub_unsub_regex_file = NULL;");
define auto_sub_unsub ()
{
!if (is_group_mode ())
error (_function_name () + " doesn\'t work in group mode.");
variable
lp = asu_list,
unsubscribe_new_groups = get_variable_value ("unsubscribe_new_groups"),
starting_newsgroup = current_newsgroup (),
group_flags = 0;
if (orelse
{ strlen (starting_newsgroup) == 0 } % no newsgroups listed
{ lp == NULL }) % no regexes listed
return (); % nothing to do
call ("bob");
while (group_flags = get_group_flags (), group_flags & GROUP_NEW_GROUP_FLAG)
{
while (lp != NULL)
{
if (string_match (current_newsgroup (), lp.regex, 1))
{
if (unsubscribe_new_groups)
set_group_flags (group_flags & ~GROUP_UNSUBSCRIBED);
else
set_group_flags (group_flags | GROUP_UNSUBSCRIBED);
break;
}
else % reset to original status
{
if (unsubscribe_new_groups)
set_group_flags (group_flags | GROUP_UNSUBSCRIBED);
else
set_group_flags (group_flags & ~GROUP_UNSUBSCRIBED);
lp = lp.next;
}
}
lp = asu_list;
!if (group_down_n (1)) break; % go to next newsgroup
}
while (current_newsgroup () != starting_newsgroup)
() = group_search (starting_newsgroup);
}
define save (file) % should be file-local
{
variable saving_message = "Saving group auto-subscription regex list...";
message (saving_message);
variable fp = fopen (file, "w");
if (fp == NULL)
verror ("Can\'t save to %s: %s", file, errno_string (errno));
variable lp = asu_list;
while (lp != NULL)
{
if (fputs (lp.regex + "\n", fp) == -1)
verror ("Couldn\'t write to %s: %s", file, errno_string (errno));
lp = lp.next;
}
if (fclose (fp) == -1)
verror ("Couldn\'t close %s: %s", file, errno_string (errno));
message (saving_message + "done");
}
define changed () % should be file-local
{
auto_sub_unsub ();
if (auto_sub_unsub_save_frequency) save (asu_current_file);
}
define delete (regex) % should be file-local
{
variable
prev = NULL,
lp = asu_list;
while (lp != NULL)
{
if (lp.regex == regex)
{
if (lp == asu_list) % deleting the head node
{
if (lp == asu_tail) asu_tail = NULL;
asu_list = asu_list.next;
lp = asu_list;
}
else % deleting tail or other node
{
if (lp == asu_tail) asu_tail = prev;
prev.next = lp.next;
}
changed ();
return ();
}
prev = lp;
lp = lp.next;
}
}
define add (regex) % should be file-local
{
variable lp = asu_list;
while (lp != NULL)
{
if (regex == lp.regex) return ();
lp = lp.next;
}
variable new = struct { regex, next };
new.regex = regex;
new.next = NULL;
if (asu_list == NULL) asu_list = new;
else asu_tail.next = new;
asu_tail = new;
}
define load (file) % should be file-local
{
if (file == "") return ();
variable fp = fopen (file, "r");
if (fp == NULL) return ();
variable
old_asu_list = asu_list,
regex = "";
asu_list = NULL;
while (fgets (®ex, fp) != -1)
{
regex = strtrim (regex);
if (string_match (regex, "^%", 1)) continue;
!if (strlen (regex)) continue;
% Alternate code for above two lines:
% !if (orelse
% { string_match (regex, "^%", 1) } % skip comment lines
% { string_match (regex, "^$", 1) }) % skip empty lines
add (regex);
}
() = fclose (fp);
asu_current_file = file;
}
define auto_sub_unsub_save_as ()
{
variable file = read_mini ("Save to file", "", asu_current_file);
if (strlen (file))
{
save (file);
if (file != asu_current_file) asu_current_file = file;
}
}
define auto_sub_unsub_edit () % Edit regex list
{
ERROR_BLOCK
{
_clear_error ();
return ();
}
variable regex = "";
if (asu_list == NULL)
{
regex = strtrim (read_mini ("Add a regex", "", ""));
if (strlen (regex))
{
add (regex);
changed ();
}
return ();
}
variable
count = 0,
lp = asu_list,
new_regex = "";
"Edit the regex list"; % build the prompt
while (lp != NULL)
{
lp.regex;
count++;
lp = lp.next;
}
loop (2) count;
regex = select_list_box ();
!if (strlen (regex)) return ();
new_regex = strtrim (read_mini ("Edit regex", "", regex));
if (new_regex == "")
{
delete (regex);
message (regex + " has been deleted.");
changed ();
return ();
}
else if (new_regex == regex) return ();
lp = asu_list;
while (lp != NULL) % modify regex in place
{
if (lp.regex == regex)
{
lp.regex = new_regex;
break;
}
else lp = lp.next;
}
changed ();
}
define auto_sub_unsub_add_current_group ()
{
variable ng = current_newsgroup ();
!if (strlen (ng)) error ("No current newsgroup!");
ng = str_quote_string (ng, "\\^$[]*.+?", '\\');
variable str = strtrim (read_mini ("Add a regex", "", "^" + ng + "$"));
if (strlen (str)) add (str);
changed ();
}
define auto_sub_unsub_delete_current_group ()
{
variable ng = current_newsgroup ();
!if (strlen (ng)) error ("No current newsgroup!");
ng = str_quote_string (ng, "\\^$[]*.+?", '\\');
variable str = strtrim (read_mini ("Delete a regex", "", "^" + ng + "$"));
if (strlen (str)) delete (str);
changed ();
}
define auto_sub_unsub_group_mode_startup_hook ()
{
variable
file = make_home_filename (".slrn-auto_sub_unsub_list"),
env = getenv ("AUTO_SUB_UNSUB");
if (env != NULL) file = env;
else if (auto_sub_unsub_regex_file != NULL)
file = auto_sub_unsub_regex_file;
load (file);
auto_sub_unsub ();
}
define auto_sub_unsub_quit ()
{
if (auto_sub_unsub_save_frequency) save (asu_current_file);
}
Technical Details
Message-ID:
<slrn6vthe7.11g.bluemoon@ux6.cso.uiuc.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-mailShow Full Headers
Complete Article Headers
Message-ID: <slrn6vthe7.11g.bluemoon@ux6.cso.uiuc.edu> Subject: [slrn] auto_sub_unsub -- a slrn macro for automatic group subscription/unsubscription Date: Tue, 15 Sep 1998 00:00:00 +0000 From: bluemoon@ux6.cso.uiuc.edu (Dawn Owens-Nicholson) 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: 391457195 Organization: University of Illinois at Urbana-Champaign User-Agent: slrn/0.9.5.3 (UNIX) Newsgroups: news.software.readers Summary: A macro for slrn to automatically (un)subscribe to newsgroups based on regular expression matching.