home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume23 / sigrnd / part01 next >
Encoding:
Text File  |  1991-10-22  |  16.7 KB  |  488 lines

  1. Newsgroups: comp.sources.misc
  2. From: mgleason@cse.unl.edu (Mike Gleason)
  3. Subject:  v23i104:  sigrnd - signature randomizer v4.0, Part01/01
  4. Message-ID: <1991Oct22.033901.2170@sparky.imd.sterling.com>
  5. X-Md4-Signature: d59824a28359926011ab30707bf5a81d
  6. Date: Tue, 22 Oct 1991 03:39:01 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: mgleason@cse.unl.edu (Mike Gleason)
  10. Posting-number: Volume 23, Issue 104
  11. Archive-name: sigrnd/part01
  12. Environment: UNIX, News
  13.  
  14. sigrnd 4.0: yet another .signature randomizer, by mike gleason, NCEMRSoft.
  15.  
  16. Features:
  17.    + Not a shell script.
  18.    + You are no longer limited to one puny .signature!
  19.    + It doesn't have to know how many sigs you have in advance.
  20.    + It can choose a signature for a specific newsgroup, if you use
  21.      the 'nn' newsreader.
  22.    + For nn users, comes with a patch for the aux script, so you can
  23.      randomize every time you post, instead of everytime you read news.
  24.      The aux script (theoretically) works for all users whether they are
  25.      aware of sigrnd or not; so if you can edit the global aux script, or
  26.      are the superuser, you can make sigrnd available to everyone by patching
  27.      the aux script and putting sigrnd in a public directory.
  28.    + Falls back to the regular .signature file if an error occurs or the user
  29.      doesn't have extra sigs.
  30.    + Can be used to randomly pick quotes, fortunes, etc.
  31.  
  32. Flags:
  33.    1. By default, sigrnd picks a signature from a directory called ".sigs"
  34.       in the user's home directory, and outputs the picked sig to standard
  35.       out.
  36.    2. If you use the -d (or -g) flag, sigrnd will look in that directory,
  37.       and pick a file.
  38.    3. If you use the -n flag, sigrnd won't print the selected file, but
  39.       instead it will only print the name of the file it picked.
  40.  
  41. Setup:
  42.    1. Unshar/Untar this archive if necessary.
  43.    2. If you are using the nn newsreader, grab a copy of the 'aux' script.
  44.       This should be located somewhere around where the 'nn' executable is.
  45.       For me, 'nn' is in /usr/local/news/bin, and the 'aux' script is in
  46.       /usr/local/news/lib.  
  47.    3. Type 'make.'  If you get errors, you may need to try different
  48.       combinations of including <sys/dir.h>, <sys/dirent.h>, and <dirent.h>.
  49.    4. If you're using the patched aux script for yourself (i.e. not system
  50.       wide) store the script in a nice place.  Then, in your nn 'init' file,
  51.       add these lines: "set news-script /my/path/aux", and
  52.       "set mail-script /my/path/aux," substituting the real path of the
  53.       patched aux script.  (Note: you MAY need to edit the aux script
  54.       depending what program appends the signature to the post.  If it lets
  55.       inews do it (the default), have sigrnd replace the regular sig file,
  56.       like "sigrnd > $HOME/.signature."  If the aux script did it by
  57.       "cat .signature >> $WORK", use "sigrnd >> $WORK.")
  58.    5. Create a whole bunch of signatures, and stash them in a directory
  59.       called ".sigs".  If you're using nn, you can get sigrnd to pick from
  60.       different sets of sigs by creating subdirectories within ~/.sigs.
  61.       For example, I could create a subdirectory called "comp.sys.amiga.misc,"
  62.       and put specific signatures related to that group only in there.  Then,
  63.       when I post to comp.sys.amiga.misc, sigrnd gets called with
  64.       "sigrnd -g comp.sys.amiga.misc" and only picks from that set of sigs.
  65.       This could come in handy if I don't want my "Macs rule" signatures
  66.       getting tacked on to posts in the amiga forums :-)  Also, if I post to
  67.       news.software.nn, and I don't have a corresponding subdirectory, sigrnd
  68.       looks at the top level of ~/.sigs, and picks;  so put signatures that
  69.       apply to any group in ~/.sigs, and make subdirectories for groups that
  70.       you want to use specific signatures with.
  71.    6. If you aren't using nn, I recommend making a csh alias like:
  72.           alias readnews "sigrnd > ~/.signature ; rn"
  73.       Of course, substitute "rn" with the name of the newsreader you use.
  74.  
  75. Happy randomizing,
  76. mike gleason = mgleason@cse.unl.edu
  77.  
  78.  
  79. #! /bin/sh
  80. # This is a shell archive.  Remove anything before this line, then unpack
  81. # it by saving it into a file and typing "sh file".  To overwrite existing
  82. # files, type "sh file -c".  You can also feed this as standard input via
  83. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  84. # will see the following message at the end:
  85. #        "End of shell archive."
  86. # Contents:  sigrnd.readme sigrnd.c aux.patch Makefile
  87. # Wrapped by mgleason@cse on Wed Oct 16 05:24:51 1991
  88. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  89. if test -f 'sigrnd.readme' -a "${1}" != "-c" ; then 
  90.   echo shar: Will not clobber existing file \"'sigrnd.readme'\"
  91. else
  92. echo shar: Extracting \"'sigrnd.readme'\" \(3603 characters\)
  93. sed "s/^X//" >'sigrnd.readme' <<'END_OF_FILE'
  94. Xsigrnd 4.0: yet another .signature randomizer, by mike gleason, NCEMRSoft.
  95. X
  96. XFeatures:
  97. X   + Not a shell script.
  98. X   + You are no longer limited to one puny .signature!
  99. X   + It doesn't have to know how many sigs you have in advance.
  100. X   + It can choose a signature for a specific newsgroup, if you use
  101. X     the 'nn' newsreader.
  102. X   + For nn users, comes with a patch for the aux script, so you can
  103. X     randomize every time you post, instead of everytime you read news.
  104. X     The aux script (theoretically) works for all users whether they are
  105. X     aware of sigrnd or not; so if you can edit the global aux script, or
  106. X     are the superuser, you can make sigrnd available to everyone by patching
  107. X     the aux script and putting sigrnd in a public directory.
  108. X   + Falls back to the regular .signature file if an error occurs or the user
  109. X     doesn't have extra sigs.
  110. X   + Can be used to randomly pick quotes, fortunes, etc.
  111. X
  112. XFlags:
  113. X   1. By default, sigrnd picks a signature from a directory called ".sigs"
  114. X      in the user's home directory, and outputs the picked sig to standard
  115. X      out.
  116. X   2. If you use the -d (or -g) flag, sigrnd will look in that directory,
  117. X      and pick a file.
  118. X   3. If you use the -n flag, sigrnd won't print the selected file, but
  119. X      instead it will only print the name of the file it picked.
  120. X
  121. XSetup:
  122. X   1. Unshar/Untar this archive if necessary.
  123. X   2. If you are using the nn newsreader, grab a copy of the 'aux' script.
  124. X      This should be located somewhere around where the 'nn' executable is.
  125. X      For me, 'nn' is in /usr/local/news/bin, and the 'aux' script is in
  126. X      /usr/local/news/lib.  
  127. X   3. Type 'make.'  If you get errors, you may need to try different
  128. X      combinations of including <sys/dir.h>, <sys/dirent.h>, and <dirent.h>.
  129. X   4. If you're using the patched aux script for yourself (i.e. not system
  130. X      wide) store the script in a nice place.  Then, in your nn 'init' file,
  131. X      add these lines: "set news-script /my/path/aux", and
  132. X      "set mail-script /my/path/aux," substituting the real path of the
  133. X      patched aux script.  (Note: you MAY need to edit the aux script
  134. X      depending what program appends the signature to the post.  If it lets
  135. X      inews do it (the default), have sigrnd replace the regular sig file,
  136. X      like "sigrnd > $HOME/.signature."  If the aux script did it by
  137. X      "cat .signature >> $WORK", use "sigrnd >> $WORK.")
  138. X   5. Create a whole bunch of signatures, and stash them in a directory
  139. X      called ".sigs".  If you're using nn, you can get sigrnd to pick from
  140. X      different sets of sigs by creating subdirectories within ~/.sigs.
  141. X      For example, I could create a subdirectory called "comp.sys.amiga.misc,"
  142. X      and put specific signatures related to that group only in there.  Then,
  143. X      when I post to comp.sys.amiga.misc, sigrnd gets called with
  144. X      "sigrnd -g comp.sys.amiga.misc" and only picks from that set of sigs.
  145. X      This could come in handy if I don't want my "Macs rule" signatures
  146. X      getting tacked on to posts in the amiga forums :-)  Also, if I post to
  147. X      news.software.nn, and I don't have a corresponding subdirectory, sigrnd
  148. X      looks at the top level of ~/.sigs, and picks;  so put signatures that
  149. X      apply to any group in ~/.sigs, and make subdirectories for groups that
  150. X      you want to use specific signatures with.
  151. X   6. If you aren't using nn, I recommend making a csh alias like:
  152. X          alias readnews "sigrnd > ~/.signature ; rn"
  153. X      Of course, substitute "rn" with the name of the newsreader you use.
  154. X
  155. XHappy randomizing,
  156. Xmike gleason = mgleason@cse.unl.edu
  157. X
  158. X
  159. END_OF_FILE
  160. if test 3603 -ne `wc -c <'sigrnd.readme'`; then
  161.     echo shar: \"'sigrnd.readme'\" unpacked with wrong size!
  162. fi
  163. # end of 'sigrnd.readme'
  164. fi
  165. if test -f 'sigrnd.c' -a "${1}" != "-c" ; then 
  166.   echo shar: Will not clobber existing file \"'sigrnd.c'\"
  167. else
  168. echo shar: Extracting \"'sigrnd.c'\" \(4606 characters\)
  169. sed "s/^X//" >'sigrnd.c' <<'END_OF_FILE'
  170. X/* 
  171. X * sigrnd 4.0
  172. X * (C) 1989, 1991 by mike gleason, NCEMRSoft. (mgleason@cse.unl.edu)
  173. X */
  174. X
  175. X#include <sys/types.h>
  176. X#include <sys/param.h>
  177. X#include <sys/dir.h> /* May need to use <sys/dirent.h> and/or <dirent.h> too. */
  178. X#include <sys/stat.h>
  179. X#include <stdio.h>
  180. X#define SIGNATUREDIR ".sigs"
  181. X#define DOSUBDIRS        /* wanna look in subdirectories too? */
  182. X
  183. Xint printname = 0;
  184. Xchar *prog;
  185. Xint recurse = 1;
  186. Xchar path[MAXPATHLEN], homedir[MAXPATHLEN];
  187. Xextern char *getwd(), *getenv(), *optarg;
  188. Xextern void *malloc();
  189. X
  190. Xmain(argc, argv)
  191. X    int argc;  char **argv;
  192. X{
  193. X    char *sigdir, *cp, *defsigdir;
  194. X    int flag, verbose;
  195. X
  196. X    if ((cp = getenv("HOME")) == NULL) {
  197. X        defsigdir = SIGNATUREDIR;
  198. X        strcpy(homedir,"./");
  199. X    } else {
  200. X        strcpy(path, cp);
  201. X        strcat(path, "/");
  202. X        strcpy(homedir, path);
  203. X        strcat(path, SIGNATUREDIR);
  204. X        defsigdir = path;
  205. X    }
  206. X    sigdir = defsigdir;        /* look for sigs here if you don't specify */
  207. X    printname = 0;            /* cat file by default */
  208. X    prog = *argv;            /* save name of this program */
  209. X    
  210. X    while ((flag = getopt(argc, argv, "d:g:ncv")) != EOF) switch (flag) {
  211. X        case 'd':
  212. X        case 'g':
  213. X            sigdir = optarg;
  214. X            break;
  215. X        case 'n':
  216. X            printname = 1;
  217. X            break;
  218. X        case 'c':
  219. X            printname = 0;
  220. X            break;
  221. X        case 'v':
  222. X            verbose++;
  223. X            break;
  224. X        default:
  225. X            (void) fprintf(stderr,
  226. X"usages:\t%s [-d rootsigdir] [-v] > a.signature.file\n\
  227. X\t%s -n [-d rootsigdir] [-v]  (prints name of file picked only)\n\
  228. X\tDefault directory to randomize from is:\n\t\t\"%s\"\n\
  229. X\tVersion 4 brought to you by Mike Gleason, NCEMRSoft.\n",
  230. X                prog, prog, defsigdir);
  231. X            exit (1);
  232. X    }
  233. X
  234. X    /*
  235. X     * Don't report errors, because sigrnd could be a system-wide
  236. X     * standard.  If the users don't want to use (or don't know about)
  237. X     * sigrnd, just go ahead and use what they would expect,
  238. X     * ~/.signature.
  239. X     */
  240. X    srand((int) getpid());
  241. X    
  242. X
  243. X    if (chdir(sigdir) < 0) {
  244. X        if (chdir(defsigdir) < 0) exit(1);
  245. X        sigdir = defsigdir;
  246. X    }
  247. X
  248. X#ifdef DOSUBDIRS
  249. X    if (strcmp(getwd(sigdir), defsigdir)==0) {
  250. X        /* If we are using the root sig directory, we don't want
  251. X           to recurse, or else we might use a custom sig intended
  252. X           for a specific newsgroup (and not this one). */
  253. X        recurse = 0;
  254. X    }
  255. X#endif
  256. X    if (thrashdir()) {
  257. X        strcat(homedir, ".signature");
  258. X        printfile(homedir);
  259. X        if (verbose) {
  260. X            (void) fprintf(stderr, "By default, appended \"%s.\"\n", homedir);
  261. X            sleep(2);
  262. X        }
  263. X    } else if (verbose) {
  264. X        (void) fprintf(stderr, "Used custom signature \"%s.\"\n", path);
  265. X        sleep(2);
  266. X    }
  267. X    exit (0);
  268. X}    /* main */
  269. X
  270. X
  271. X
  272. Xthrashdir()
  273. X{
  274. X    register DIR  *dirp;
  275. X    register struct direct *dp;
  276. X    register int c, numentries, pick;
  277. X    struct stat st;
  278. X    int safeguard;
  279. X
  280. X    if ((dirp = opendir(".")) == NULL)
  281. X        return(1);
  282. X    numentries = 0;
  283. X    while ((dp = readdir(dirp)) != NULL)
  284. X        numentries++;
  285. X    if (numentries < 3)    {
  286. X        /* consider "." and ".." non-entries */
  287. X        closedir(dirp);
  288. X        return (-1);
  289. X    }
  290. X#define MAXLOOPS 1000
  291. X    for (safeguard=0, dp=NULL; safeguard<MAXLOOPS && dp==NULL; safeguard++) {
  292. X        rewinddir(dirp);
  293. X        pick = rand() % numentries + 1;
  294. X        while (--pick >= 0)
  295. X            if ((dp = readdir(dirp)) == NULL)
  296. X                break;
  297. X        if (dp != NULL) {            /* if we could open the dir entry */
  298. X            if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
  299. X                dp = NULL;
  300. X            else {                    /* if we have a non-dot entry */
  301. X                if (stat(dp->d_name, &st) < 0) {
  302. Xgak:
  303. X                    (void) closedir(dirp);
  304. X                    return (1);
  305. X                }
  306. X                if (S_ISDIR(st.st_mode)) {
  307. X#ifdef DOSUBDIRS
  308. X                    char *saved;
  309. X                    
  310. X                    if (recurse) {
  311. X                        if ((saved = malloc((size_t) MAXPATHLEN)))
  312. X                            (void) getwd(saved);
  313. X                        /* do subdirectories :-) */
  314. X                        if (chdir(dp->d_name) < 0)
  315. X                            goto gak;
  316. X                        if ((c = thrashdir()) == 1)
  317. X                            goto gak;
  318. X                        else if (c == -1) {
  319. X                            /* the one we picked was an empty dir. try again. */
  320. X                            dp = NULL;
  321. X                            if (saved) {
  322. X                                (void) chdir(saved);
  323. X                                free(saved);
  324. X                                saved = NULL;
  325. X                            }
  326. X                        }
  327. X                    } else
  328. X                        dp = NULL;
  329. X#else
  330. X                    dp = NULL;
  331. X#endif
  332. X                } else {            /* end dir; we have a file */
  333. X                    (void) getwd(path);
  334. X                    (void) strcat(path, "/");
  335. X                    (void) strcat(path, dp->d_name);
  336. X                    if (printname)
  337. X                        (void) puts(path);
  338. X                    else printfile(path);
  339. X                }                    /* end file */
  340. X            }                        /* end if we have a non-dot entry */
  341. X        }                            /* if we could open the dir entry */
  342. X    }                                /* end loop until we find a sig */
  343. X
  344. X    if (safeguard >= MAXLOOPS)
  345. X        (void) fprintf(stderr, "%s: infinite loop!\n", prog);
  346. X    (void) closedir(dirp);
  347. X    return (0);
  348. X}    /* thrashdir */
  349. X
  350. X
  351. X
  352. X
  353. Xprintfile(name)
  354. X    char *name;
  355. X{
  356. X    register int c;
  357. X    FILE *fp;
  358. X    
  359. X    if ((fp = fopen(name, "r")) == NULL)
  360. X        return (1);
  361. X    while ((c = fgetc(fp)) != EOF)
  362. X        (void) putchar(c);
  363. X    (void) fclose(fp);
  364. X    return (0);
  365. X}
  366. X
  367. X/* eof */
  368. END_OF_FILE
  369. if test 4606 -ne `wc -c <'sigrnd.c'`; then
  370.     echo shar: \"'sigrnd.c'\" unpacked with wrong size!
  371. fi
  372. # end of 'sigrnd.c'
  373. fi
  374. if test -f 'aux.patch' -a "${1}" != "-c" ; then 
  375.   echo shar: Will not clobber existing file \"'aux.patch'\"
  376. else
  377. echo shar: Extracting \"'aux.patch'\" \(1576 characters\)
  378. sed "s/^X//" >'aux.patch' <<'END_OF_FILE'
  379. X*** aux    Wed Oct 16 04:04:41 1991
  380. X--- aux.sigrnd    Wed Oct 16 03:59:34 1991
  381. X***************
  382. X*** 109,115 ****
  383. X  
  384. X  loop=true
  385. X  prompt=false
  386. X! pr="a)bort"
  387. X  if [ -n "${POSTER_ADR}" ] ; then
  388. X    pr="$pr c)c"
  389. X  fi
  390. X--- 109,115 ----
  391. X  
  392. X  loop=true
  393. X  prompt=false
  394. X! pr="** a)bort"
  395. X  if [ -n "${POSTER_ADR}" ] ; then
  396. X    pr="$pr c)c"
  397. X  fi
  398. X***************
  399. X*** 314,321 ****
  400. X    fi
  401. X    case $ans in
  402. X    ''|y*|Y*)
  403. X!     echo "-- " >> $WORK
  404. X!     cat $HOME/.signature >> $WORK
  405. X      ;;
  406. X    esac
  407. X  fi
  408. X--- 314,340 ----
  409. X    fi
  410. X    case $ans in
  411. X    ''|y*|Y*)
  412. X! # Depending on how your nn is configured, you will need to either concat
  413. X! # the signature into $WORK, or replace the ~/.signature file.  I _think_
  414. X! # if you aren't using NNTP, you must replace the ~/.sig file (otherwise this
  415. X! # script would append a signature, THEN inews would append one).  If you're
  416. X! # using NNTP, you will probably use this script to add the sig file to the
  417. X! # post.
  418. X! 
  419. X! # Set SIGRND to your signature randomizer, along with necessary flags. Note
  420. X! # that if the randomizer fails for some reason, it should use default to an
  421. X! # existing ~/.signature.  Also, if your randomizer has can handle sigs for
  422. X! # different newsgroups, it should try the default sig directory if that
  423. X! # group doesn't exist.
  424. X! 
  425. X!   SIGRND="/user/students/ugrad/mgleason/bin/sigrnd -g $HOME/.sigs/$GROUP"
  426. X! 
  427. X! # If NNTP: add sig dashes, then signature.
  428. X! #   echo "-- " >> $WORK
  429. X! #   $SIGRND >> $WORK
  430. X! 
  431. X! # else: replace the signature file.  Inews will append it on it's own.
  432. X!     $SIGRND > $HOME/.signature
  433. X      ;;
  434. X    esac
  435. X  fi
  436. END_OF_FILE
  437. if test 1576 -ne `wc -c <'aux.patch'`; then
  438.     echo shar: \"'aux.patch'\" unpacked with wrong size!
  439. fi
  440. # end of 'aux.patch'
  441. fi
  442. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  443.   echo shar: Will not clobber existing file \"'Makefile'\"
  444. else
  445. echo shar: Extracting \"'Makefile'\" \(516 characters\)
  446. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  447. X# Compiled exectuable's name.
  448. XFILE=sigrnd
  449. X
  450. X# Patch file to make the aux script run sigrnd.  If you aren't using nn,
  451. X# or don't want to patch it, simply make sure a file called aux isn't in
  452. X# this directory.
  453. XPCH=aux.patch
  454. X
  455. X# location of the nn 'aux' script to patch.
  456. XAUX=./aux
  457. X
  458. X# Your favorite C Compiler, and flags.
  459. XCC=cc
  460. XCFLAGS=-O
  461. XLFLAGS=-s
  462. X
  463. Xall:
  464. X    $(CC) $(CFLAGS) sigrnd.c -o $(FILE) $(LFLAGS)
  465. X    -patch -c $(AUX) < $(PCH)
  466. X
  467. Xshar:
  468. X    shar sigrnd.readme sigrnd.c aux.patch Makefile | \
  469. X    cat sigrnd.readme - > sigrnd.shar
  470. END_OF_FILE
  471. if test 516 -ne `wc -c <'Makefile'`; then
  472.     echo shar: \"'Makefile'\" unpacked with wrong size!
  473. fi
  474. # end of 'Makefile'
  475. fi
  476. echo shar: End of shell archive.
  477. exit 0
  478. --
  479. mike gleason, NCEMRSoft.                                  mgleason@cse.unl.edu
  480.                     "Fock you oss hoel" -- A. Schwartzenegger
  481.  
  482. exit 0 # Just in case...
  483. -- 
  484. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  485. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  486. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  487. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  488.