summaryrefslogtreecommitdiff
path: root/elisp/erbot/erbeng.el
blob: 7aa7aa3a76a52eabefde17bead67354e47411994 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
;;; erbeng.el --- 
;; Time-stamp: <2007-11-23 11:30:11 deego>
;; Copyright (C) 2002 D. Goel
;; Emacs Lisp Archive entry
;; Filename: erbeng.el
;; Package: erbeng
;; Author: D. Goel <deego@gnufans.org>
;; Version: 0.0DEV
;; URL:  http://www.emacswiki.org/cgi-bin/wiki.pl?ErBot
 

(defvar erbeng-home-page
  "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 erbeng-version "0.0dev")

;;==========================================
;;; Code:

(require 'cl)
(defgroup erbeng nil 
  "The group erbeng"
   :group 'applications)
(defcustom erbeng-before-load-hooks nil "" :group 'erbeng)
(defcustom erbeng-after-load-hooks nil "" :group 'erbeng)

(defcustom erbeng-reply-timeout 20
  "Time after which the bot times out...")

(run-hooks 'erbeng-before-load-hooks)





(defvar erbeng-msg nil )
(defvar erbeng-proc nil)
(defvar erbeng-nick nil)
(defvar erbeng-tgt nil)
(defvar erbeng-localp nil)
(defvar erbeng-userinfo nil)


(defvar erbot-show-type-p t
  "Whether to show type of non-string objects when replying...

The old behavior was equivalent to having this as nil.")


;;;###autoload
(defun erbeng-main (msg proc nick tgt localp userinfo)
  " The main function: Takes a line of message and generates a reply to it. 
The result is a string.  If the result is 'noreply, that means: Do NOT reply...
The last field localp is here for historical reasons, and shall be
ignored...

One very important criterion here should be:

erbot should learn to avoid runaway talks with other bots.  For this
reason: 

 [a] it should take a break every now and then, say: a 1-minute break
after every 1000 commands.  It should probably announce its break.
AND/OR 
 [b] It should learn to reply only 99 out of 100 times.  Moreover,
before it shuts up, it should let any humans know what it is doing.
tgt, nick and sspec will probably mostly remain unused...  

proc == name of the process in the channel
tgt == channel
nick == who from
userninfo looks like (\"deego\" \"user\" \"24-197......\")
sspec looks like: [\"PRIVMSG\"
\"deego!~user@24-197-159-102.charterga.net\" \"#testopn\" \"hi erbot\" \nil
nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
nil nil\ nil nil nil nil nil nil nil nil]

"
  (let* 
      (
       (erbeng-nick nick)
       (erbeng-msg msg)
       (erbeng-proc proc)
       (erbeng-tgt tgt)
       (erbeng-localp localp)
       (erbeng-userinfo userinfo)
       (fs-found-query-p nil)
       (fs-internal-addressedatlast nil)
       (fs-internal-message-sans-bot-name fs-internal-message-sans-bot-name)
       (fsi-prestring fsi-prestring)
       tmpvar
       parsed-msg rep
       (fs-msg fs-msg)
       (fs-msglist fs-msglist)
       (fs-msgsansbot fs-msgsansbot)
       (fs-msglistsansbot fs-msglistsansbot)
       (fs-lispa fs-lispa)
       (fs-lispb fs-lispb)
       (fs-lispc fs-lispc)
       (fs-lispd fs-lispd)
       (fs-lispe fs-lispe)
       )
    ;;(concat nick ": " "/leave Test.. one big big test...")
    ;;(erbutils-ignore-errors
    
    ;; this can also modify fs-found-query
    (setq parsed-msg 
	  (or (condition-case tmpvar
		  (fs-parse msg proc nick tgt localp userinfo)
		(error 
		 ;;"(error \"Please supply a completed lisp form\")"
		 ;; Note that this could be bad: 
		 ;; someone may not even be referring to the bot here:
		 (if 
		     fs-internal-parse-error-p
		     (format "(error %S )" 
			     (error-message-string tmpvar))
		   (format "(fs-english-only %S)" msg))
		 
		 ))
	      (and (featurep 'erbmsg)
		   erbot-erbmsg-p
		   (erbmsg-parse msg proc nick tgt localp userinfo))))
    
    ;;(if (and (first parsed-msg) erbot-nick
    ;;        (string= (first parsed-msg)
    ;;           erbot-nick))
    ;; parsed-msg will never be null if the msg was addressed to fsbot..
    (if
	parsed-msg
	(progn
	  (setq rep 
		;;(erbutils-ignore-errors
		(with-timeout 
		    (erbeng-reply-timeout
		     "overall timeout")
		  (erbutils-ignore-errors
		   (erbeng-get-reply parsed-msg proc nick tgt )))
		)
	  (cond
	   ((string= "noreply" (format "%s" rep)) 'noreply)
	   ((and (stringp rep) (not (equal rep ""))) (format "%s%s"
							     fsi-prestring
							     rep))
	   (t 
	    (cond
	     (erbot-show-type-p
	      (format "%s%S  ..(%s)" fsi-prestring rep (type-of rep)))
	     ((equal "" rep) "EMPTY STRING RETURNED")
	     (t (format "%s%S" fsi-prestring rep))))))
      
      'noreply)))



(defun erbeng-lisp-object-p (msg) 
  (setq msg (ignore-errors (erbn-read msg)))
  (and (listp msg)
       (let ((fir (format "%s" (first msg))))
   (or
    (string-match "concat" fir)
    (string-match "regexp-quote" fir)
   ;; want to allow fs-rq to show the regexp without quoting..
   ;;(string-match "fs-rq" fir)
    ))))



;(defun erbeng-init-parse (msg)
;  (if (equal 0 (string-match "," msg))
;      (setq msg (concat "erbot " 
;     (substring msg 1 (length msg)))))
;  (let ((spl (split-string msg)))
;    (if (> (length spl) 0)
; (erbeng-init-frob-split-string spl)
;      nil)));;;

;;; ;(defun erbeng-init-frob-split-string (spl)
;;; ;  "spl is the split string ..;;;;

;;; ;now, we do not need to split wrt commas... in fact, that will be
;;; ;dangerous, and can spoil the meanings of commas inside parse
;;; ;commands...;;

;;; ;converts all accepted formats to look like this:
  

;;; ; \(\"erbot\" \"foo\" \"bar\"\)

;;; ;"
;;;  ; (let* ((do-again t)
;;;   ; (new-spl
;;;    ; (cond
;;;     ; ;; , foo bar
;;;      ;((string= (first spl) ",")
;;;       (cons erbot-nick (cdr spl)))
;;;      ((equal 
;;;        (string-match "," (first spl)) 0)
;;;       (cons erbot-nick
;;;       (append (split-string (first spl) ",")
;;;         (cdr spl))))
;;;      ((equal
;;;        ;; erbot:
;;;        (string-match (concat erbot-nick ":") (first spl)) 0)
;;;       (append (split-string (first spl) ":")
;;;         (cdr spl)))
;;;      ((equal 
;;;        ;; fdbot,
;;;        (string-match (concat erbot-nick ",") (first spl)) 0)
;;;       (append (split-string (first spl) ",")
;;;         (cdr spl)))
;;;      (t (progn (setq do-again nil) spl)))))
;;;     (if do-again
;;;   (erbeng-init-frob-split-string new-spl)
;;;       ;; removed the extra ""  etc. and all , ; erc. etc. 
;;;       (split-string
;;;        (mapconcat 'identity
;;;       new-spl " ")
;;;        "[ \f\t\n\r\v,;]+"))))




(defun erbeng-get-reply (msg &optional proc nick tgt &rest foo)
  "  ;; now assumes that the msg is (a string) in lisp format... and this just
  ;; evals it.."
  (eval (erbn-read msg)))
;  (let* (
;  (lispmsg 
;   (erbeng-read (erbutils-stringify msg))))
;    (if (and lispmsg (listp lispmsg))
; (erblisp-process-msg proc nick tgt 
;          lispmsg)
;      (let ((englispmsg (fs-parse-english msg proc nick)))
; (erblisp-process-msg proc nick tgt englispmsg)))))



(defun erbeng-read (msg)
  (ignore-errors (erbn-read msg)))










;; proposed register syntax..
(defun erbeng-register-syntax (fsbot-command priority check &optional
					     remap)

"If CHECK is a function, then it is passed the user input as a
string...  If it claims a match, it should return (t arglist).
Arglist is a list of arguments to be passed to the FSBOT-COMMAND.

If CHECK is a regexp, it is matched against the user input string.  If
it is a match, all the submatches 1....n (NOT 0) are passed to the
function as arguments in that order, except that you can remap using
the optional REMAP list.  That list is a list of numbers, like, say (0
1 2 4).<-- this tells us that the arguments to be passed to the
fsbot-command are the regexp-matches 0,1,2 4 in that order. If REMAP
is not supplied, you can see that the default value is more-or-less
equivalent to (1 2 3 4....)

"
nil)


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



;;; erbeng.el ends here