Initial Commit
[packages] / xemacs-packages / zenirc / src / zenirc-random-nick.el
1 ;;;
2 ;;;
3 ;;; zenirc-random-nick.el --- Choose random nicks for ZenIRC
4
5 ;;; Copyright (C) 1994 Ben A. Mesander
6 ;;; Copyright (C) 1994, 1996, 1997 Per Persson
7
8 ;;; Author: Ben A. Mesander <ben@gnu.ai.mit.edu>
9 ;;;         Per Persson <pp@sno.pp.se>
10 ;;; Maintainer: pp@sno.pp.se
11 ;;; Keywords: extensions
12 ;;; Created: Sun Aug 14 20:20:05 MDT 1994
13
14 ;;; This program is free software; you can redistribute it and/or modify
15 ;;; it under the terms of the GNU General Public License as published by
16 ;;; the Free Software Foundation; either version 2, or (at your option)
17 ;;; any later version.
18 ;;;
19 ;;; This program is distributed in the hope that it will be useful,
20 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;;; GNU General Public License for more details.
23 ;;;
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with this program; if not, you can either send email to this
26 ;;; program's maintainer or write to: The Free Software Foundation,
27 ;;; Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.
28
29 ;;; Commentary:
30
31 ;;; This is really annoying.
32
33 ;;; Code:
34
35 (require 'zenirc)
36
37 (random t)
38
39 ;; note- the rfc lies about valid characters in nicks.
40 ;; it appears the allowed character set is:
41 ;; [0-9][A-^][a-|] and - and _
42 ;; 0-9 and - cannot start a nick
43
44 (defun zenirc-random-nick-char ()
45   (let ((c (abs (random 69)))) ; mmm, 69
46     (cond ((= 0 c) "_") 
47           ((< c 10) (char-to-string (+ c 47))) ; 0-9
48           ((< c 40) (char-to-string (+ c 55))) ; A-^
49           (t (char-to-string (+ c 57))))))     ; a-|
50
51 (defun zenirc-random-nick-string (numchars)
52   (let ((i 1) (str (zenirc-random-nick-char)))
53     (if (< numchars 0)
54         ""
55       (while (and (< (string-to-char str) 58)  ; can't start with
56                   (> (string-to-char str) 47)) ; [0-9]
57         (setq str (zenirc-random-nick-char)))
58       (while (< i numchars)
59         (setq str (concat str (zenirc-random-nick-char)))
60         (setq i (1+ i)))
61       str)))
62
63 (defun zenirc-random-nick ()
64   (setq zenirc-nick (zenirc-random-nick-string 9)))
65 \f
66 ;;; Code to automatically change nickname every now and then, this is
67 ;;; _really_ annoying. mmm, annoying code.
68 ;;;
69 ;;;             <poxaV> yes, i agree, it's annoying.
70 ;;;
71 (defvar zenirc-change-nick-interval '(0 600)
72   "How often to change your random nickname. The default is 600
73 seconds or 10 minutes.")
74 (make-variable-buffer-local 'zenirc-change-nick-interval)
75
76 (defvar zenirc-last-nick-change '(0 0)
77   "The time the last change was made in a ZenIRC buffer.")
78 (make-variable-buffer-local 'zenirc-last-nick-change)
79
80 ;; Should this really be optional? B-)
81 (defvar zenirc-change-nick-mode nil
82   "If zenirc-random-nick should change nickname automatically.")
83 (make-variable-buffer-local 'zenirc-change-nick-mode)
84
85 (defun zenirc-change-random-nick (proc now)
86   "Change nickname in the specified process PROC every 
87 zenirc-change-nick-interval seconds."
88   (if zenirc-change-nick-mode
89       (if (zenirc-time< zenirc-change-nick-interval
90                         (zenirc-time-diff now zenirc-last-nick-change))
91           (progn
92             (process-send-string proc (concat 
93                                        "NICK " 
94                                        (zenirc-random-nick-string 9)
95                                        "\n"))
96             (setq zenirc-last-nick-change now)))))
97 \f
98 (zenirc-add-hook 'zenirc-timer-hook 'zenirc-change-random-nick)
99 (zenirc-add-hook 'zenirc-mode-hook 'zenirc-random-nick)
100
101 (provide 'zenirc-random-nick)
102
103 ;;; End of zenirc-random-nick.el