summaryrefslogtreecommitdiff
path: root/elisp/erbot/erbbdb.el
blob: 30684a504c373ebcdcb5911f96e69c49286f74bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
;;; erbbdb.el --- 
;; Time-stamp: <2007-11-23 11:30:13 deego>
;; Copyright (C) 2002 D. Goel
;; Emacs Lisp Archive entry
;; Filename: erbbdb.el
;; Package: erbbdb
;; Author: D. Goel <deego@gnufans.org>
;; Version: 0.0dev
;; URL:  http://www.emacswiki.org/cgi-bin/wiki.pl?ErBot
 

;; This file is NOT (yet) part of GNU Emacs.
 
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
 
;; This is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
 

;; See also:




(defvar erbbdb-version "0.0dev")

;;==========================================
;;; Code:
(ignore-errors (require 'bbdb))
(ignore-errors (require 'bbdb-com))
(ignore-errors (require 'bbdb-hooks))

(require 'erbc)

(defgroup erbbdb nil 
  "The group erbbdb"
   :group 'applications)
(defcustom erbbdb-before-load-hooks nil "." :group 'erbbdb)
(defcustom erbbdb-after-load-hooks nil "" :group 'erbbdb)
(run-hooks 'erbbdb-before-load-hooks)


(defun erbbdb-get-exact-notes (string)
  (erbbdb-get-regexp-notes (concat "^" (regexp-quote 
					(erbbdb-frob-main-entry string)
					) "$")))

(defun erbbdb-get-exact-name (string)
  (erbbdb-get-regexp-name (concat "^" (regexp-quote 
					(erbbdb-frob-main-entry string)
					) "$")))



(defun erbbdb-get-regexp-record (expr)
  "dsfdfdf"
  (let ((records
	 (bbdb-search (bbdb-records)
	  expr)))
    (first records)))

(defun erbbdb-get-record (str)
  (erbbdb-get-regexp-record 
   (concat "^" (regexp-quote 
		(erbbdb-frob-main-entry str)) "$")))

(defun erbbdb-get-regexp-name (expr)
  "used to get exact name, eg: the exact name of tmpa may be TmpA."
  (let ((record (car 
		 ;; this basically does an M-x  bbdb-name 
		 (bbdb-search (bbdb-records)
			      expr))))
    (if record
	(aref record 0)
      nil)))

(defun erbbdb-get-regexp-notes (expr)
  "currently: Assumes that there will be only one match for the expr
in bbdb...  Discards any further matches... 

If the notes are (), we want it to return nil, not a string.. so that
the calling function knows there's (effectively) no such record...

That is why we have the read below..

This of course, also means that the notes field had better contain a
lisp sexp.. and anythign after the sexp gets discarded...

If record exists but no notes exist, \"\" is returned. 
Else the string containing the notes is returned.
If no record exists, then a nil is returned. 
"
  (let ((record (car 
		 ;; this basically does an M-x  bbdb-name 
		 (bbdb-search (bbdb-records)
			      expr))))
    (if record
	(let* ((notes-notes (assq 'notes (bbdb-record-raw-notes record)))
	       (notes-string (cdr notes-notes)))
	  (or notes-string "")
	       ;;(if foo (read foo) nil)
	  )
      nil)))


(defun erbbdb-frob-main-entry (givenname)
    (let* ((sname (format "%s" givenname))
	   ;;(dname (downcase sname))
	   (dname sname)
	   (bname (split-string dname))
	   (name (mapconcat 'identity bname "-")))
      name))

(defun erbbdb-change (givenname notes)
  "also used by other functions in here.."
  
  (bbdb-records)

  (let* ((sname (format "%s" givenname))
	 ;;(dname (downcase sname))
	 (dname sname)
	 (bname (split-string dname))
	 (name (mapconcat 'identity bname "-")))
    ;;(let ((record
    ;;	 (vector 
    ;;	  ;; first name
    ;;	  name
    ;;	  ;;lastname
    ;;	  nil
    ;;	  nil
    ;;	  nil ;;company
    ;;	  nil ;;phones
    ;;	  nil ;; addrs
    ;;	  nil ;;net
    ;;	  (format "%s" notes)
    ;;	;  (make-vector bbdb-cache-length nil))))
    ;;    (bbdb-change-record record t))
    (let* ((record (erbbdb-get-record name)))
      (bbdb-record-set-notes record notes)
      (bbdb-change-record record t)
      (erbbdb-save))))

(defun erbbdb-save ()
  (when 
      erbbdb-save-p
      (bbdb-save-db)))

(defvar erbbdb-save-p t
  "Should normally be t, except inside special constructions. ")

  
(defun erbbdb-create (name newnotes)
  "also used by other functions in here.."
  (bbdb-records)
  (let ((record
	 (vector 
	  ;; first name
	  name
	  ;;lastname
	  nil
	  nil
	  nil ;;company
	  nil ;;phones
	  nil ;; addrs
	  nil ;;net
	  nil ;; (format "%s" newnotes)
	  (make-vector bbdb-cache-length nil))))
    (bbdb-record-set-notes record nil)
    (mapcar '(lambda (arg)
	       (erbbdb-add name arg))
	    newnotes)
    )
  (erbbdb-save))

(defun erbbdb-add (name note)
  (bbdb-records)
  (let* ((oldnotes
	  (erbbdb-get-exact-notes name))
	 (newnotes nil))

    ;; should almost always be the case.. except when nil..
    (if (stringp oldnotes)
	(setq oldnotes 
	      (ignore-errors (erbn-read oldnotes))))
    (setq newnotes (format "%S" (append oldnotes (list note))))
    (erbbdb-remove-not-really name)
    (erbbdb-change name newnotes)))


(defun erbbdb-remove-not-really (name)
  (erbbdb-change name nil))
(defun erbbdb-remove (givenname)
  "Remove the record implied by givenname from bbdb.."
  ;;(erbbdb-change name nil)
  (bbdb-records)
  (let* ((sname (format "%s" givenname))
	 ;;(dname (downcase sname))
	 (dname sname)
	 (bname (split-string dname))
	 (name (mapconcat 'identity bname "-")))
    (let* ((record (erbbdb-get-record name)))
      (when record
	(bbdb-delete-current-record record t)
	;;(bbdb-record-set-notes record notes)
	;;(bbdb-change-record record t)
	(erbbdb-save)))))

(provide 'erbbdb)
(run-hooks 'erbbdb-after-load-hooks)



;;; erbbdb.el ends here