Article View: news.software.readers
Article #32789Re: slrn wish: Regex filter for auto-subscribe
From: J.B. Nicholson-Owens
Date: Tue, 15 Apr 1997 00:00
Date: Tue, 15 Apr 1997 00:00
276 lines
9942 bytes
9942 bytes
rhh@ct.picker.com (Randall Hopper) wrote:
> However, I can easily define a regex of some groups I have no desire
> to subscribe to.
I've written something that you might find handy. As the docs say, it works
with your unsubscribe_new_groups variable to do a reasonable thing. Stick
it in a file and load it either with "interpret" from your slrn.rc or
autoload it from some S-Lang code somewhere.
The trick is in the AUTO_SUB_UNSUB_REGEX variable. I'll leave the details
below. The easy way to get it working:
variable AUTO_SUB_UNSUB_REGEX = "^alt\.";
auto_sub_unsub ();
should go in some S-Lang file where it'll run on group mode startup. Slrn's
unsubscribe_new_groups setting and AUTO_SUB_UNSUB_REGEX are intended to be
complimentary -- if you automatically subscribe to all new groups,
AUTO_SUB_UNSUB_REGEX describes the groups you want to automatically
unsubscribe. It should be okay if you run auto_sub_unsub() more than once,
since it only works on new newsgroups (and since slrn no longer downloads
new group info when you refresh the newsgroup list). However, it shouldn't
be necessary to run auto_sub_unsub more than once.
Please forgive the weird code formatting, I had to do something to get it
under 80 columns for posting (this is the only time I wish I hadn't
suggested JED enforce the >80 column rule). As unusual as the formatting
is, it should run okay.
I hope this fills your need.
% -*- mode: slang; mode: fold -*-
% author: J.B. Nicholson-Owens
% version: 1.2
% Full documentation %{{{
%
% Automatically subscribe/unsubscribe groups that match a regexp. This
% macro does not affect the newness of the group (groups still remain
% marked as "new" after this function is complete). This macro only works
% on new newsgroups.
%
% Requires:
% AUTO_SUB_UNSUB_REGEX: comma separated regex(es) describing groups to
% toggle subscribe or unsubscribe status.
%
% This macro works with your unsubscribe_new_groups setting (default is 0,
% new groups are unsubscribed, according to slrn.rc documentation).
%
% Examples:
%
% AUTO_SUB_UNSUB_REGEX = "^clari\.weather\.,^news\.software\.,pokey-lokey";
% automatically toggles subscription status of:
% clari.weather.*
% news.software.*
% any newsgroup with "pokey-lokey" in its name
%
% To INSTALL, call auto_sub_unsub() from group_mode_startup_hook().
% sub_unsub_edit_regex(), sub_unsub_add_current_group() and
% sub_unsub_del_current_group() are optional and can be bound to keys.
%
% find_first_sub_group() is designed to be called from
% group_mode_startup_hook() and is completely optional as well. It leaves
% the pointer at the first subscribed group.
%}}}
% Change log %{{{
% v1.2: added UI for editing AUTO_SUB_UNSUB_REGEX from inside slrn:
% add_current_group ()
% del_current_group ()
% edit_sub_unsub_regex ()
%
% added sub_unsub_find_first_sub_group () to leave the pointer at the
% first subscribed group (small, perhaps silly but handy if you
% unsubscribe from most new groups).
%
% v1.1: made function use one variable (AUTO_SUB_UNSUB_REGEX) instead of
% two by using unsubscribe_new_groups setting from slrnrc file.
% Now, regex variable describes groups to toggle. Made regex compare
% case insensitive (newsgroups names are supposed to be case
% insensitive). Fixed subscriber/unsubscriber.
%
% v1.0: new macro. Only tested on Unix.
%}}}
define auto_sub_unsub () %{{{
{
% set defaults for AUTO_SUB_UNSUB_REGEX if the user hasn't set it
if (orelse
{ get_group_flags () & ~ (GROUP_NEW_GROUP_FLAG); } % no new newsgroups
{ is_defined ("AUTO_SUB_UNSUB_REGEX") == 0; } % or no regex defined
{ not (strlen (AUTO_SUB_UNSUB_REGEX)); }) % or regex == Null_String
return; % no actionable setting, nothing to do
variable
element = 0, % current element of regex list
regex = Null_String, % current regex
new_group_count = 0, % number of new newsgroups
toggle_count = 0, % number of new newsgroups toggled
message = Null_String, % message to user summarizing changes
slrnrc_unsubscribe = get_variable_value ("unsubscribe_new_groups");
% For each new group, see if that group should remain subscribed or
% unsubscribed by seeing if any of the regex(es) in AUTO_SUB_UNSUB_REGEX
% match the group name. If so, toggle the group's sub/unsub status.
% record the group where this started (might not be the top)
variable started_newsgroup = current_newsgroup ();
do
{
if (get_group_flags () & ~ (GROUP_NEW_GROUP_FLAG)) {
!if (group_down_n (1))
return;
continue; % group is not new
}
while (regex = strlow (extract_element (AUTO_SUB_UNSUB_REGEX, element, ',')),
strlen (regex)) {
if (string_match (strlow (current_newsgroup ()), regex, 1)) {
% found a matching regex, change subscribe status but
% leave the group as new (some other function may use
% the new tag later).
%
% the nested if (get_group_flags) prevents undesirable
% action if this is run more than once per fetching of
% new groups (e.g., this is mistakenly put in
% group_mode_hook() instead of group_mode_startup_hook()
if (slrnrc_unsubscribe) {
if (get_group_flags () & GROUP_UNSUBSCRIBED) {
% make grp subscribed & new
set_group_flags (~ (get_group_flags ())
| GROUP_UNSUBSCRIBED
| GROUP_NEW_GROUP_FLAG);
toggle_count++;
}
} else {
if (get_group_flags () & ~ (GROUP_UNSUBSCRIBED)) {
% make grp unsubscribed & new
set_group_flags (get_group_flags ()
| GROUP_UNSUBSCRIBED
| GROUP_NEW_GROUP_FLAG);
toggle_count++;
}
}
break;
} % if (string_match...
element++; % next regex
} % while (regex...
element = 0; % reset regex pointer for next newsgroup
new_group_count++;
}
while (group_down_n (1));
% go to group where you started
call ("group_bob"); % reset newsgroup pointer to top
while (not (case (current_newsgroup (), started_newsgroup)))
!if (group_down_n (1))
error (Sprintf ("Can\'t find %s!", started_newsgroup, 1));
% summarize the results
if (toggle_count)
{
if (slrnrc_unsubscribe)
% slrnrc says to subscribe to new groups,
% count reflects unsubscribed groups
message = "unsubscribed";
else
% slrnrc says to unsubscribe to new groups,
% count reflects subscribed groups
message = "subscribed";
message (Sprintf ("Of %i new groups, %i groups are %s",
new_group_count, toggle_count, message, 3));
}
}
%}}}
define sub_unsub_edit_regex () %{{{
{
variable new_regex = Null_String;
if (is_defined ("AUTO_SUB_UNSUB_REGEX"))
new_regex = read_mini ("Edit AUTO_SUB_UNSUB_REGEX",
Null_String,
strcat (AUTO_SUB_UNSUB_REGEX, ","));
else
{
variable newsgroup_str =
Sprintf ("^%s$",
str_quote_string (current_newsgroup(), ".", '\\'),
1);
new_regex = read_mini ("Edit AUTO_SUB_UNSUB_REGEX",
Null_String,
newsgroup_str);
}
!if (strlen (new_regex))
return;
AUTO_SUB_UNSUB_REGEX =
Sprintf ("%s,%s", AUTO_SUB_UNSUB_REGEX, new_regex, 2);
}
%}}}
define sub_unsub_add_current_group () %{{{
{
variable
newsgroup = current_newsgroup (),
newsgroup_str =
Sprintf ("^%s$", str_quote_string (newsgroup, ".", '\\'), 1);
AUTO_SUB_UNSUB_REGEX =
Sprintf ("%s,%s", AUTO_SUB_UNSUB_REGEX, newsgroup_str, 2);
message (strcat (newsgroup, " added to AUTO_SUB_UNSUB_REGEX"));
}
%}}}
define sub_unsub_del_current_group () %{{{
{
!if (strlen (AUTO_SUB_UNSUB_REGEX))
return; % nothing to do
% find the literal that matches the group name
variable
newsgroup_str =
Sprintf ("^%s$",
str_quote_string (strlow (current_newsgroup ()), ".", '\\'),
1),
new_sub_unsub_regex = Null_String, % temp AUTO_SUB_UNSUB_REGEX
regex = Null_String, % regex being examined
element = 0;
% rebuild the temp regex var w/o the current newsgroup
while (regex =
strlow (extract_element (AUTO_SUB_UNSUB_REGEX, element, ',')),
strlen (regex))
if (not (case (regex, newsgroup_str)))
new_sub_unsub_regex = Sprintf ("%s,%s", new_sub_unsub_regex, regex);
}
%}}}
define sub_unsub_find_first_sub_group () %{{{
{
variable started_newsgroup = current_newsgroup ();
if (case (started_newsgroup, Null_String))
error ("No newsgroups!");
% find the topmost subscribed group
while (get_group_flags () & GROUP_UNSUBSCRIBED)
{
!if (group_down_n (1))
% return ptr to where it started
{
call ("bob");
while (0 == case (current_newsgroup (), started_newsgroup))
() = group_down_n (1);
error ("No subscribed groups!");
}
}
}
%}}}
Technical Details
Message-ID:
<slrn5l82a6.blu.jbn@pop-a-wheelie.midwest.net>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-mailReferences:
<5ir4ct$iu1$1@iagnews.iagnet.net>Show Full Headers
Complete Article Headers
Message-ID: <slrn5l82a6.blu.jbn@pop-a-wheelie.midwest.net> Subject: Re: slrn wish: Regex filter for auto-subscribe Date: Tue, 15 Apr 1997 00:00:00 +0000 From: jbn@pop-a-wheelie.midwest.net (J.B. Nicholson-Owens) References: <5ir4ct$iu1$1@iagnews.iagnet.net> 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: 235063597 Organization: Organizing Organisms & Organs Newsgroups: news.software.readers