root/climm/doc/README.tcl

Revision 2856, 10.9 KB (checked in by kuhlmann, 2 years ago)

fix svn properties to get correct svn id; update reference in de climmcmds accordingly

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
21. Preface.
3
4Tcl is easy yet powerful scripting language. climm provides Tcl support
5both for evaluating Tcl constructions from climm command line and
6writing Tcl "hooks" to handle different events (incoming messages,
7status changes, etc). This short HOWTO provides an introduction to
8writing Tcl scripts for climm. I assume the reader has basic knowledge
9of Tcl itself - just like I have. ;)
10
11Why this HOWTO? When trying to implement a simple hook I discovered
12that the documentation for this climm functionality is rather sparse
13and then I had to study climm sources to get my task done. As not
14everybody can read C source code freely (well, at least I cannot) I
15decided to try to cover the theme. Hope my efforts will help somebody.
16
17Ah, and of course comments and additions are welcome. Reach me at
18jeff < at > macloue.com.
19
202. Prerequisites - compiling climm with Tcl support.
21
22If your system contains a usable Tcl 8.4 installation climm build
23system will detect it and use automatically. However you can disable
24Tcl support with --disable-tcl switch - this may give your build a
25small performance boost.
26
273. Basics.
28
29When climm is compiled with Tcl support it creates an internal Tcl
30interpreter upon startup. The user can use this interpreter to execute
31tcl code by using one of the following commands at climm promt:
32
33        tcl <command>
34
35Executes inline Tcl <command> in climm Tcl interpreter.
36
37Arbitrary Tcl commands can be used, for example:
38
39climm> tcl puts [expr 1+2]
403
41climm>
42
43Unlike tclsh climm doesn't automatically echo the result of evaluation
44so the following command prints nothing:
45
46climm> tcl expr 1+2
47climm>
48
49So remember to "puts" the result of your calculations!
50
51        tclscript <file>
52
53Executes <file> as a Tcl script in scope of climm Tcl interpreter.
54
55Supposing that the following script is stored in $HOME/.climm/hello.tcl:
56
57puts "Hello, world"
58
59the following command will provide:
60
61climm> tclscript .climm/hello.tcl
62Hello, world
63climm>
64
65You can also execute Tcl script upon climm startup by adding option
66"tclscript" to "[General]" section of your climmrc file. In this case
67script path can be given relative to climm base directory (usually
68$HOME/.climm).
69
704. Tcl extensions provided by climm.
71
72As most application do, climm provides some extensions to common Tcl
73syntax. The extension command is "climm" and you can get a short usage
74description by "climm help" ("tcl climm help" or even "tcl help" from
75the climm prompt). I will describe "climm" subcommands in slightly more
76depth below, following Tcl man pages syntax.
77
78        climm exec <cmd>
79
80Executes <cmd> as if it is given at climm command prompt. So, the
81following code:
82
83climm exec "msg Commander Good day, commander!"
84
85will send message "Good day, commander!" to Commander contact.
86
87        climm nick <uin>
88
89Finds contact name for <uin>. For example, this code:
90
91climm nick 48130434
92
93will return my nickname in your contact list (or empty string if I'm
94not there).
95
96The following commands deal with hooks that can be installed into
97climm. I will describe them later.
98
99        climm receive <script> ?<contact>?
100
101Installs message hook <script>, invoked on every incoming message or if
102<contact> is given - on incoming messages from <contact>.
103
104        climm unreceive ?<contact>?
105
106Removes global message hook or message hook for <contact>.
107
108[NB: Actually you need to provide exactly the same arguments as to
109"climm receive" command to successfully remove the hook.]
110
111        climm event <script>
112
113Installs an event hook.
114
115        climm unevent
116
117Removes event hook.
118
119[NB: Actually you need to provide exactly the same arguments as to
120"climm event" command to successfully remove the hook.]
121
122        climm hooks
123
124Returns list (in Tcl meaning, see list(n)) of hooks installed. The list
125consists of two-element lists with first element being a hook type
126(either "event hook" for event hook or contact name for receive hooks)
127and second showing the script which is executed.
128
1295. Hooks - basic ideas and examples.
130
131When compiled with Tcl support climm can execute different Tcl commands
132upon certain events. As said above, "climm receive" command can be used
133to make given <script> to be executed upon incoming message arrival.
134Actually as far as I can guess from climm source code climm just
135executes the following construct:
136
137        <script> <uin> <message>
138
139when message <message> is received from UIN <uin>. This requires
140<script> to be no more than a name of procedure accepting two
141parameters - or a construction like that:
142
143puts "some string"; return;
144
145This will make the code executed as follows:
146
147puts "some string"; return; 48130434 {Hello!}
148
149and hook arguments are ignored - simple but ugly, isn't it? And not as
150flexible as a procedure too - isn't it useful to know whom we received
151a message from?
152
153But enough chit-chat, let's try to write some hooks already!
154
1555.1. Message hooks
156
157Message hooks are simple. Unlike event hooks there are always two
158arguments to the script. Let's write a simple procedure and attach it
159as a message hook.
160
161proc msghook {uin message} {puts "Oh, message from $uin!"}
162climm receive msghook
163
164In climm prompt this will look as follows:
165
166climm> tcl proc msghook {uin message} {puts "Oh, message from $uin!"}
167climm> tcl climm receive msghook
168
169Let's try to test it:
170
171climm> msg 48130434 test
17214:39:13              48130434 {<< test
173Oh, message from 48130434!
17414:39:14              48130434 >>} test
175
176It works, doesn't it? Then let's try to remove the hook:
177
178climm> tcl climm unreceive msghook
179
180The hook is now removed.
181
182This hook was global, let's make it filter UINs.
183
184climm> tcl climm receive msghook Larry_Laffer
185climm> msg 48130434 test
18614:42:35              48130434 {<< test
18714:42:35              48130434 >>} test
188
189The hook is not executed. To remove it we need to give the following
190command:
191
192climm> tcl climm unreceive msghook Larry_Laffer
193
1945.2. Event hooks
195
196Event hooks are complex. At the moment of writing climm doesn't
197distinguish different event types - there is only one global event hook
198and it is called with variable number of parameters. The first is
199always an event type - so it's up to hook procedure to decide whether
200to continue processing or return. Second is always UIN of the contact
201who triggered the event. Other parameters can vary so precautions need
202to be made not to receive many error messages.
203
2045.2.1. Event: status change.
205
206Let's try to implement simple "status change" event handler. Status
207change event happens every time a contact changes its status
208(naturally) and executes the following Tcl code:
209
210        <event handler script> status <uin> <status>
211
212So, let's write something like this:
213
214proc evhandler {type uin args} {
215        if { $type != "status" } then return
216        set status [lindex $args 0]
217        set nick [climm nick $uin]
218        puts "$nick changes status to $status"
219}
220
221This procedure analyzes event type and if it is a status change event
222extracts status value and prints a message. Let's attach it as an event
223handler:
224
225climm event evhandler
226
227If the procedure is stored in file $HOME/.climm/evhandler.tcl the
228following commands will do the trick:
229
230climm> tclscript .climm/evhandler.tcl
231climm> tcl climm event evhandler
232
233Then, when a contact changes his/her status, we will get the following:
234
23515:17:46               Contact1 changed status to occupied.
236Contact1 changes status to occupied
237
238Or even:
239
24015:19:39              Contact2 logged off.
241Contact2 changes status to logged_off
242
243Going offline is also a status change.
244
2456. Hooks reference.
246
2476.1. Message hook.
248
249Install syntax:
250
251        climm receive <script> ?<contact>?
252
253Remove syntax:
254
255        climm unreceive <script> ?<contact>?
256
257Tcl code executed upon message arrival (from <contact> if specified):
258
259        <script> <uin> <message>
260
2616.2. Event hooks.
262
263Install syntax:
264
265        climm event <script>
266
267Remove syntax:
268
269        climm unevent <script>
270
271Tcl code executed upon event:
272
273        <script> <type> <uin> ?<arg1>? ...
274
275List of optional arguments depends on event <type>.
276
2776.2.1. Event: status change.
278
279When a contact <uin> changes his status to <status> the following Tcl
280code is executed:
281
282        <script> status <uin> <status>
283
2846.2.2. Event: message received.
285
286This event is triggered when incoming message arrives. Although it's
287better to use message hooks to handle events of this type you can
288handle messages through the event hook as well - it may useful in some
289cases because message and event hooks are invoked independently.
290
291When a message <message> is received from user <uin> the following Tcl
292code executed is as follows:
293
294        <script> message <uin> <message>
295
2966.2.3. Event: incoming file request.
297
298This event is triggered when file transfer request arrives. The
299following Tcl code is executed:
300
301        <script> file_request <uin> <file name> <bytes> <ref>
302
303FIXME: I don't know much about ICQ file transfers, so can somebody tell
304me what the <ref> is?
305
3066.2.4. Event: added to contact list.
307
308This event is triggered when you are added to somebody's contact list.
309When user <uin> adds you to his contact list the following Tcl code is
310executed:
311
312        <script> contactlistadded <uin>
313
3146.2.5. Event: authorization.
315
316Events of this group are triggered when authorization messages arrive.
317When auth message arrives from user <uin> the following Tcl code is
318executed:
319
320        <script> authorization <uin> <authtype>
321
322<authtype> can be:
323
324 - request - when incoming auth request is received from user <uin>;
325 - refused - if your auth request is refused by user <uin>;
326 - granted - if your auth request is granted by user <uin>.
327
3286.2.6. Event: Web message arrived.
329
330This event is triggered when you receive a message from ICQ web site
331("web message"). The following Tcl code is executed:
332
333        <script> web <uin> <t0> <t3> <t4> <t5>
334
335FIXME: add t0-t5 parameters description; not sure what <uin> is in this
336case.
337
3386.2.7. Event: Mail message arrived.
339
340This event is triggered when you receive a ICQ Mail message. The
341following Tcl code is executed:
342
343        <script> mail <uin> <t0> <t3> <t4> <t5>
344
345FIXME: add t0-t5 parameters description; not sure what <uin> is in this case
346
3476.2.8. Event: SSL.
348
349Events of this group are triggered at various stage of SSL connection setup.
350
351When it is detected that the user <uin>'s client doesn't support SSL
352channel the following Tcl code is executed:
353
354        <script> ssl <uin> no_candidate
355
356When it is detected that the user <uin>'s client supports SSL channel
357the following Tcl code is executed:
358
359        <script> ssl <uin> candidate
360
361When SSL connection is tried to user <uin>, the following Tcl code is
362executed if remote hasn't SSL connetion enabled:
363
364        <script> ssl <uin> failed precondition
365
366When SSL connection is tried to user <uin>, the following Tcl code is
367executed if SSL connection initialization fails:
368
369        <script> ssl <uin> failed init
370
371When SSL connection is tried to user <uin>, the following Tcl code is
372executed if SSL key exchange fails:
373
374        <script> ssl <uin> failed key
375
376When SSL connection is tried to user <uin>, the following Tcl code is
377executed if SSL handshake fails:
378
379        <script> ssl <uin> failed handshake
380
381And, finally, when SSL connection to user <uin> is successfully
382established the following Tcl code is executed:
383
384        <script> ssl <uin> ok
385
386If the SSL connection to <uin> fails at some stage finally the
387following Tcl code is executed:
388
389        <script> ssl <uin> incapable
Note: See TracBrowser for help on using the browser.