[project @ 2006-06-29 00:17:49 by unknown_lamer]
[clinton/bobotpp.git] / scripts / bobot-utils.scm
CommitLineData
ce02032f 1;;; This file is automatically loaded by Bobot++. This is required for
2;;; the bot to function.
6e78bd19 3
4;;; This file is covered by the GPL version 2 or (at your option) any
5;;; later version
6
ce02032f 7;;; the-bot-module must be available to guile-user so that scripts
8;;; loaded with Interp::Load have access to the bot: procedures
9(module-use! (resolve-module '(guile-user) #f)
10 the-bot-module)
6e78bd19 11
c7d9fb19 12(use-modules (srfi srfi-13))
13
ce02032f 14(define-public %bot:loadpath (list
15 (string-append (getenv "HOME")
16 "/.bobotpp/scripts/")
17 bot:sys-scripts-dir))
eb3864fb 18
ce02032f 19(define-public %bot:load-extensions %load-extensions)
6e78bd19 20
c7d9fb19 21;;; bot:log: Write as many messages as you want to the log. If the
ce02032f 22;;; arg is a thunk it will be executed and it's output will be
23;;; written to the log
24(define-public (bot:log . messages)
25 (for-each
26 (lambda (x)
27 (if (thunk? x)
133eff7a 28 (display (x) (bot:logport))
29 (display x (bot:logport))))
c7d9fb19 30 messages)
ce02032f 31 (bot:flushport))
6e78bd19 32
ce02032f 33(define-public (bot:load file)
34 (let path-loop ((load-path %bot:loadpath))
3d05febb 35 (cond ((not (null? load-path))
36 (if (not
37 (let ext-loop ((extensions %bot:load-extensions))
38 (if (not (null? extensions))
39 (if (catch 'system-error
40 (lambda ()
41 (load
42 (string-append (car load-path)
43 file
44 (car extensions))))
45 (lambda args
46 #f ))
47 #t
48 (ext-loop (cdr extensions))))))
49 (path-loop (cdr load-path))))
50 (else
51 (begin (bot:log "ERROR: File " file " Not Found!\n") #f)))))
ce02032f 52
53(define-public (bot:load-module module-spec)
54 (let ((module->string
55 (lambda (module)
56 (apply
57 (lambda (s . rest)
58 (string-append
59 s
60 (apply string-append
61 (map (lambda (str) (string-append "/" str)) rest))))
62 (map symbol->string module))))
63 (new-module
64 (make-module))
65 (old-module (current-module)))
66 (module-use! new-module the-bot-module)
67 (set-current-module new-module)
68 (bot:load (module->string module-spec))
69 (set-current-module old-module)
70 new-module))
71
72(define-public (bot:use-module module-spec)
73 (module-use! (current-module)
74 (bot:load-module module-spec)))
75
6e78bd19 76
77;;; REGEX UTILS
78
79;;; match-not-channel adds a prefix regex to your regex so it doesn't
80;;; match the sender or channel in a PUBLIC message
ce02032f 81(define-public (bot:match-not-channel regex)
db098a03 82 (string-append "^[[:graph:]]*[&#+!][^ ,\a]+ [[:graph:][:space:]]*"
528799bd 83 regex))
6e78bd19 84
85;;; match-to-me matches text that was addressed to the bot with a
86;;; ':',',', or nothing after the bot name
db098a03 87
88;(define-public (bot:match-to-me r) r)
ce02032f 89(define-public (bot:match-to-me regex)
90 (string-append (bot:match-not-channel (bot:getnickname))
6e78bd19 91 "[[:space:][:graph:]]*" regex))
92
db098a03 93(define-public (bot:sent-to-me? message)
4edefeb6 94 (let ((to-me (make-regexp (bot:match-to-me ""))))
db098a03 95 (if (regexp-exec to-me message) #t #f)))
4edefeb6 96
6e78bd19 97;;;; string-utils
ce02032f 98(define-public str-app string-append) ; shorter
6e78bd19 99
100;;; Message sending utils
101
c7d9fb19 102;;; Returns the CTCP quoted message
103;;; Input _MUST NOT_ contain the trailing \r\n
104;;; (it is added by the message sending code)
029f60d9 105(define-public (bot:ctcp-quote message)
c7d9fb19 106 ;; TODO: Not very efficient, it may be worth reimplementing
107 (let ((ls (string->list message)))
108 (string-concatenate
109 (map (lambda (chr) ; CTCP level quoting
110 (case (char->integer chr)
528799bd 111 ((#o134) (string (integer->char #o134) (integer->char
112 #o134)))
c7d9fb19 113 ((#o01) (string (integer->char #o134) #\a)) ; X-DELIM
114 (else (string chr))))
115 (string->list
116 (string-concatenate
117 (map (lambda (chr) ; Low-level
118 (let ((m-quote (integer->char #o20)))
119 (case chr
120 ((m-quote) (string m-quote m-quote))
121 ((#\nul) (string m-quote #\0))
122 ((#\nl) (string m-quote #\n))
123 ((#\cr) (string m-quote #\r))
124 (else (string chr)))))
125 ls)))))))
126
127
6e78bd19 128
129;;; DEPRECATED FUNCTION NAMES
130;;; These are provided for backwards compatibility
c7d9fb19 131;;; and will be removed in the 3.0 release
ae97d6ec 132(begin-deprecated
133
134 (define-macro (_deprecated-fun old-name new-name)
135 `(define-public ,old-name
136 (lambda args
2427f0c9 137 (let ((old-error
138 (set-current-error-port (bot:logport))))
139 (issue-deprecation-warning
140 (string-append
141 (symbol->string ',old-name)
142 " is a deprecated function. Please use "
143 (symbol->string ',new-name) " instead."))
144 (bot:flushport)
145 (set-current-error-port old-error))
146 (apply ,new-name args))))
ae97d6ec 147
148 (_deprecated-fun bot-load bot:load)
149 (_deprecated-fun bot-action bot:action)
150 (_deprecated-fun bot-adduser bot:adduser)
151 (_deprecated-fun bot-addserver bot:addserver)
152 (_deprecated-fun bot-addshit bot:addshit)
153 (_deprecated-fun bot-ban bot:ban)
154 (_deprecated-fun bot-cycle bot:cycle)
155 (_deprecated-fun bot-deban bot:deban)
156 (_deprecated-fun bot-delserver bot:delserver)
157 (_deprecated-fun bot-deluser bot:deluser)
158 (_deprecated-fun bot-delshit bot:delshit)
159 (_deprecated-fun bot-deop bot:deop)
160 (_deprecated-fun bot-die bot:die)
161 (_deprecated-fun bot-do bot:do)
162 (_deprecated-fun bot-invite bot:invite)
163 (_deprecated-fun bot-join bot:join)
164 (_deprecated-fun bot-keep bot:keep)
165 (_deprecated-fun bot-kick bot:kick)
166 (_deprecated-fun bot-kickban bot:kickban)
167 (_deprecated-fun bot-lock bot:lock)
168 (_deprecated-fun bot-logport bot:logport)
169 (_deprecated-fun bot-mode bot:mode)
170 (_deprecated-fun bot-msg bot:msg)
171 (_deprecated-fun bot-nextserver bot:nextserver)
172 (_deprecated-fun bot-nick bot:nick)
173 (_deprecated-fun bot-op bot:op)
174 (_deprecated-fun bot-part bot:part)
175 (_deprecated-fun bot-reconnect bot:reconnect)
176 (_deprecated-fun bot-say bot:say)
177 (_deprecated-fun bot-server bot:server)
178 (_deprecated-fun bot-setversion bot:setversion)
179 (_deprecated-fun bot-tban bot:tban)
180 (_deprecated-fun bot-tkban bot:tkban)
181 (_deprecated-fun bot-topic bot:topic)
182 (_deprecated-fun bot-unlock bot:unlock)
183 (_deprecated-fun bot-getnickname bot:getnickname)
184 (_deprecated-fun bot-getserver bot:getserver)
185 (_deprecated-fun bot-getserverlist bot:getserverlist)
186 (_deprecated-fun bot-flush bot:flush)
187 (_deprecated-fun bot-flushport bot:flushport)
188 (_deprecated-fun bot-random bot:random)
189 (_deprecated-fun bot-addcommand bot:addcommand)
190 (_deprecated-fun bot-delcommand bot:delcommand)
191 (_deprecated-fun bot-addhook bot:addhook)
192 (_deprecated-fun bot-addtimer bot:addtimer)
193 (_deprecated-fun bot-deltimer bot:deltimer)
194
195 (define-public hooks/leave hooks/part))