Project

General

Profile

Scripting » History » Version 157

Per Amundsen, 08/14/2012 11:53 AM

1 149 Per Amundsen
h1. Notice: this is info for 1.8.10 and higher.
2 39 Per Amundsen
3 1 Per Amundsen
h1. Scripting
4
5 148 Per Amundsen
*[[Scripting BETA]] Click here to see scripting in the latest beta versions.*
6 1 Per Amundsen
7 149 Per Amundsen
You can write full scripts in Commands -> Edit Scripts or you can create one liners in Commands -> Edit Commands
8 38 Per Amundsen
9 149 Per Amundsen
h1. Custom Commands:
10 1 Per Amundsen
11 149 Per Amundsen
Custom Commands are created in Commands -> Edit Commands
12 1 Per Amundsen
13 149 Per Amundsen
Custom commands consists of differents ways to execute a script either by making your own command, or use a hotkey, you can also enter a full script as a one liner (for legacy reasons)
14 1 Per Amundsen
15 150 Per Amundsen
*Alias*
16 149 Per Amundsen
<pre>
17
/hello /msg $channel hello everybody
18
</pre>
19 1 Per Amundsen
20 149 Per Amundsen
This creates a alias named "/hello", everytime you type "/hello" the script "/msg $channel hello everybody" will be executed, you can use any script features here.
21 1 Per Amundsen
22 150 Per Amundsen
*Hot keys*
23 149 Per Amundsen
<pre>
24
Ctrl&r /msg $channel hello everybody i pressed 'ctrl' and 'r'
25
</pre>
26 1 Per Amundsen
27 149 Per Amundsen
Every time you press ctrl + r, the script "/msg $channel hello everybody i pressed 'ctrl' and 'r'" will be executed, you can use any script features her.
28 1 Per Amundsen
29 149 Per Amundsen
You can comment out a command by putting a # in front of it.
30 1 Per Amundsen
31 149 Per Amundsen
It is also possible to use a full script, but it has to be on one line, and the script editor is preferred.
32 1 Per Amundsen
33 149 Per Amundsen
h1. Scripts:
34 1 Per Amundsen
35 150 Per Amundsen
*Events:*
36 149 Per Amundsen
There are different ways to listen to the irc client/server events for legacy reasons.
37
<pre>
38
OnEvent if ($event == PRIVMSG) { /EXECUTE }
39
OnBeforeEvent if ($event == PRIVMSG) { /EXECUTE }
40 1 Per Amundsen
41 149 Per Amundsen
On PRIVMSG { /EXECUTE }
42
OnBefore PRIVMSG { /EXECUTE }
43 1 Per Amundsen
44 149 Per Amundsen
OnBefore OnCommand { /EXECUTE }
45
OnCommand /mode { /EXECUTE }
46
</pre>
47 1 Per Amundsen
48 150 Per Amundsen
*Available events:*
49 149 Per Amundsen
<pre>
50
OnLoad / Called when the script is loaded
51
OnUnload / Called when the script is unloaded
52
OnReload / Called when the script is reloaded
53
OnConnecting / Called when a server is connecting
54
OnLookingUp / Called when a server is looking up the hostname
55
OnConnected / Called when a server is connected
56
OnDisconnect / Called when a server gets disconnected
57
OnCommand / Called whenever a user types a /slash command in the client ($0- will hold the full command, $0 will be the first word, $1 the second and so on)
58 1 Per Amundsen
59 149 Per Amundsen
OnCTCPRequest / Called when a user recives a CTCP request
60
OnCTCPReply / Called when a user recives a CTCP reply
61
OnDCCRequest / Called when a user recived a DCC request
62 1 Per Amundsen
63 149 Per Amundsen
OnNickChanged / Called when a user's nick changes, will only trigger on the user unlike NICK who triggers for everyone
64
OnSongChanged / Called when a song is changed in the selected media player
65 1 Per Amundsen
66 149 Per Amundsen
OnDeVoice / Called when a user gets devoiced
67
OnVoice / Called when a user gets voiced
68
OnDeHop / Called when a user gets dehalfoppeed
69
OnHop / Called when a user gets halfopped
70
OnOp / Called when a user gets opped
71
OnDeOp / Called when a user gets deopped
72
OnDeOwner/ Called when a user gets owner deopped
73
OnOwner/ Called when a user gets owner opped
74
OnDeSop / Called when a user gets special deopped
75
OnSop / Called when a user gets special opped
76 1 Per Amundsen
77 149 Per Amundsen
MODE / Called whenever a channel or user mode is changed
78
JOIN / Called whenever a user joins a channel
79
PART / Called when a user parts a channel 
80
NICK / Called when a user changes their nick
81
TOPIC / Called when a topic is set/changed
82
KICK / Called when any user gets kicked
83
NOTICE / Called when any notice is recived
84
QUIT / Called when a user quits irc
85
PRIVMSG / Called when any message is recived
86
WHOIS / Called when any whois reply is recived
87
LIST / Called when a user gets a /list
88
KILL / Called when a user gets killed
89
ACTION / Called when a user recives a ACTION (/me) message
90 1 Per Amundsen
91 149 Per Amundsen
<irc numeric> / Called when a raw irc line with <irc numeric> is recived
92
<irc textual> / Called when a raw irc line with <irc textual> is recived
93
</pre>
94 1 Per Amundsen
95 150 Per Amundsen
*if/else if/else blocks:*
96 1 Per Amundsen
97 149 Per Amundsen
You can use any combination of if, else if, else and while
98
<pre>
99
OnEvent if ($event == PRIVMSG) { 
100
	if (%test == null) { 
101
		/echo Hello world
102
		if (%test == null) { 
103
			/echo Hello world
104
		}
105
		else /echo Hello World
106
	} else if (%test == null) { 
107
		/echo Hello world
108
	} else (%test == null) { 
109
		/echo Hello world 
110
	} 
111 1 Per Amundsen
112 149 Per Amundsen
	if (%test == null) { 
113
		/echo Hello world
114
		while (%test == null) {
115
			/echo Hello world
116
		}
117
	}
118 1 Per Amundsen
119 149 Per Amundsen
	if(%test == null)/echo Hello World
120
	else if(%test == null)/echo Hello World
121
	else/echo Hello world
122
	
123
	if(%test == null){/echo Hello World
124
	}else if(%test == null){/echo Hello World
125
	}else{/echo Hello World
126
	}
127 1 Per Amundsen
128 149 Per Amundsen
	if(%test == null){/echo Hello World}else if(%test == null){/echo Hello World}else{/echo Hello World}
129
}
130
</pre>
131 1 Per Amundsen
132 150 Per Amundsen
*while blocks:*
133 151 Per Amundsen
<pre>
134 149 Per Amundsen
On JOIN {
135
	var %t = 0
136
	/echo There are ($user($chan, 0)) users in ($chan)
137
	
138
	while (%t < $user($chan, 0)) {
139
		%t++
140
		var %nick = $user($chan, %t)
141
		/echo User (%t) is (%nick) and is $iif(%nick isop $chan, opped, not opped)
142
	}
143
}
144
</pre>
145 1 Per Amundsen
146 150 Per Amundsen
*goto/label:*
147 1 Per Amundsen
148
<pre>
149 149 Per Amundsen
OnCommand /goto {
150
	var %loop = 0 
151
	if ($1 == 1) {
152
		goto 1
153
	} else if ($1 == 2) {
154
		goto 2
155
	} else if ($1 == loop) {
156
		:3
157
		/echo you typed loop
158
		%loop++
159
		if (%loop < 5) {
160
			goto 3
161
		}
162
		return
163
	} else {
164
		goto 4
165
	}
166 1 Per Amundsen
167 149 Per Amundsen
	:1
168
	/echo You typed 1
169
	return
170
	:2
171
	/echo You typed 2
172
	return
173
	:4
174
	/echo The end
175
	return
176
}
177
</pre>
178 1 Per Amundsen
179 150 Per Amundsen
*client variables:*
180 152 Per Amundsen
<pre>
181 1 Per Amundsen
$event / current event, e.g PRIVMSG 001 MODE and so forth
182
$channel / the channel the event occurred on, if any
183
$msg / the message to the channel/user or the message in a raw irc line e.g whois [kr0n] is a registered nick
184
$nick / the nick the event was sent from, can be a irc.server.com, a nick or null
185
$me / my current nick
186
$network / the network the event occured on e.g Quakenet
187
$ident / the from user ident if any
188
$host / the from user hostname if any
189
$myident / my ident
190
$myhost / my host
191
$server / host from the server e.g irc.server.com
192 149 Per Amundsen
$now / returns unixtime/ctime from current time.
193
$active / returns the current window Status/#channel/Nick.
194
$activeserver / returns an id for current server
195
$status / returns current server status
196
$crlf / returns newline \r\n
197
$0-$9 / will return parts of the $msg, $<number>- will combine parts of the $msg from 0 to <number>. $0- will return everything
198
$! / returns how many $0 $1 etc variables are filled (not sure if final name of it)
199
$raw0-$raw9 / will return parts of the raw message, $raw<number>- will combine parts of the raw message from 0 to <number>. $raw0- will return everything
200 1 Per Amundsen
$r! / returns how many $raw0 $raw1 etc variables are filled (not sure if final name of it)
201 152 Per Amundsen
</pre>
202 153 Per Amundsen
*user set variables:*
203 149 Per Amundsen
var %variable = 4242, %variable2 = 4343; will create local variables that gets deleted when the script is done.
204
using += instead of = will append to the variable, if both variable and new value is numbers it will combine them to a new number %variable += 4242.
205 1 Per Amundsen
206 149 Per Amundsen
If a variable is created without var, the variable will be available to all scripts, saved to a file and restored when AdiIRC is started.
207
208 157 Per Amundsen
Commands for manipulating variables:
209 1 Per Amundsen
<pre>
210 149 Per Amundsen
/set {-u seconds/-d] [%var] [value] / create or update a variable with value -u seconds, will delete the variable after X seconds, -d will decrease its value by 1 every second, then remove it
211 1 Per Amundsen
212 149 Per Amundsen
/unset [var] / deletes a variable
213 1 Per Amundsen
214 149 Per Amundsen
/inc {-u seconds/-d] [%var] [value] / increases a variable with value (only if value and var is ints) -u seconds, will delete the variable after X seconds, -d will decrease its value by 1 every second, then remove it
215 1 Per Amundsen
216 149 Per Amundsen
/dec {-u seconds/-d] [%var] [value] / decreases a variable with value (only if value and var is ints) -u seconds, will delete the variable after X seconds, -d will decrease its value by 1 every second, then remove it
217 1 Per Amundsen
218 149 Per Amundsen
/vars shows a list of all variables and their values
219
</pre>
220 1 Per Amundsen
221 150 Per Amundsen
*functions:*
222 149 Per Amundsen
Several functions are exsists, they are all recursive and you can use any %variable or $variable as parameters:
223
They are also usable inside if () else if () while () statements.
224
All variable numbers are floats and all functions supports floats for precise calculations.
225 36 Per Amundsen
226
<pre>
227 149 Per Amundsen
$replace(text, text2, text3) / replace all occurrences of text2 in text with text3
228
$upper(text) / return text uppercase
229
$lower(text) return text lowercase
230
$mid(text, startpos, endpos) / return part of text from startpos to endpos
231
$substr(text, startpos, endpos) / return part of text from startpos to endpos
232
$left(text, pos) / return pos characters starting from left of the text
233
$right(text, pos) / return pos characters starting from right of the text
234
$remove(text, text2) / replace all occurrences of text2 from text
235
$len(text) / return length of text
236
$count(text, text2) / counts all occurrences of text2 in text
237
$pos(text, text2) / returns first occurrences position of text2 in text
238
$lastpos(text, text2) / returns last occurrences position of text2 in text
239
$strip(text) / removes all color and font tags
240
$repeat(text, times) / repeats text X times
241
$insert(text, text2, pos) / inserts text2 into pos of text
242
$chr(num) / returns ascii character from the number num
243
$char(num) / returns ascii character from the number num
244 1 Per Amundsen
245 149 Per Amundsen
$calc(formula) / calculate any variation of +-*/
246
$formatdate(date, text) / formats a unix timestamp into date using date variables %d %m %y etc
247
$fdate(date, text) / formats a unix timestamp into date using date variables %d %m %y etc
248
$ctime(datestamp) / converts most variations of a date stamp to unix/ctime
249
$datediff(ctime1, ctime2) / diffs two unix/ctime and fills the $datematch array with values
250
$datematch(num) / returns part of a $datediff, 0 = milliseconds, 1 = seconds, 2 = minutes, 3 = hours, 4 = days
251
$host(nick) / returns the hostmask of nick
252
$ident(nick) / returns the ident of nick
253
$(number) / dynamically gets a $0 $1 $2 variable e.g $(1) is same as $1 (not sure if final function name)
254
$cond(cond, execute1, execute2) / checks if cond is true then executes execute1, else executes execute2, will return string if not at the begining of the line
255
$iif(cond, execute1, execute2) / checks if cond is true then executes execute1, else executes execute2, will return string if not at the begining of the line
256
$round(num, decimals) / rounds down a float to X decimals
257
$regex(text, pattern) / does a regular expression test if text matches pattern, then returns the matched part
258
$regmatch(num) / returns the captured group at pos num from a $regex. 0 returns group count
259
$regreplace(text, pattern, text2) / replace any occurence in text of patterh with text2 where pattern is a regular expression
260 3 Per Amundsen
261 149 Per Amundsen
$file(path) / reads file to end and returns the entire output without newlines
262
$fileloop(path) / reads through a file one line at the time, line increases +1 every time the same file is called
263
$floop(path) / reads through a file one line at the time, line increases +1 every time the same file is called
264
$filerandom(path) / returns a random line from a file
265
$frand(path) / returns a random line from a file
266
$fread(name) / reads a line from current pos in file named name
267
$fileread(name) / reads a line from current pos in file named name
268
$freadc(name) / reads a char/byte from current pos in file named name
269
$freadchar(name) / reads a char/byte from current pos in file named name
270
$fsize(file) / returns size off file in bytes
271
$filesize(file) / returns size off file in bytes
272
$fpos(name) / returns current position/byte in file named name
273
$filepos(name) / returns current position/byte in file named name
274
$flines(file) / returns amount of lines in file
275
$filelines(file) / returns amount of lines in file
276
$fileexists(file) / returns if file exists or not
277
$isfile(file) / returns if file exists or not
278 1 Per Amundsen
279 149 Per Amundsen
$chan(num) / if num is 0 returns how many channels you are joined on this server else returns channel name in position num
280
$user(#chan, num) / if num is 0 returns how many users are on this #chan else returns nick in position num
281
$nick(#chan, num) / if num is 0 returns how many users are on this #chan else returns nick in position num
282
$server(num) / if num is 0 returns how many servers you are connected to else returns server id in position num
283 1 Per Amundsen
284 149 Per Amundsen
$sread(name) / reads available bytes from socket named name (on sockread)
285
$sockread(name) / reads available bytes from socket named name (on sockread)
286
$sbytes(name) / returns amount of available bytes to be read from socket named name
287
$sockbytes(name) / returns amount of available bytes to be read from socket named name
288 1 Per Amundsen
</pre>
289
290 155 Per Amundsen
h1. Example scripts
291 154 Per Amundsen
292
Simple kickcounter script:
293
294
<pre>
295
OnBefore OnCommand { 
296
	if ($0 != /kick)  return
297
		
298
	if (%kickcount == null) %kickcount = 0
299
300
	%kickcount++
301
		 
302
	if ($2 == null) { 
303
		/kick $channel $1 Kick number %kickcount
304
		halt
305
	} 
306
}
307
</pre>
308
309
Kickban example
310
<pre>
311
OnCommand /kb {
312
	if (!$1) {
313
		/echo /kb - Nick missing
314
		return
315
	}
316
317
	var %msg = $iif($channel, $2-, $3-)
318
	var %chan = $iif($channel, $channel, $2)
319
		
320
	# Set this for default ban reason, or remove for no default reason
321
	# Can be shortened to %msg = $iif(%msg, %msg, GTFO)
322
	if (%msg == null) %msg = GTFO
323
324
	if ($me isop %chan) {
325
		/raw MODE %chan +b *!$ident($1)@$host($1)
326
		/raw KICK %chan $1 %msg
327
	} 
328
	else /echo You are not oper on %chan
329
}
330
</pre>
331
332
Simple calculator script:
333
<pre>
334
OnCommand /calc {
335
	if (!$1) {
336
		/echo /calc - Parameters missing
337
		return
338
	}
339
		
340
	# typing /calc -p <expression> sends output to channel
341
	if ($1 == -p) {
342
		/msg $channel Calculating : $2-
343
		/msg $channel Result is : $calc($2-)
344
	} else {
345
		/echo Calculating : $1-
346
		/echo Result is : $calc($1-)
347
	}
348
}
349
</pre>
350
351
Colored version
352
<pre>
353
OnCommand /calc {
354
	if (!$1) {
355
		/echo /calc - Parameters missing
356
		return
357
	}
358
	
359
	# typing /calc -p <expression> sends output to channel
360
	if ($1 == -p) {
361
		/msg $channel $chr(3)4Calculating : $2-
362
		/msg $channel $chr(3)4Result is : $calc($2-)
363
	} else {
364
		/echo $chr(3)4Calculating : 4$1-
365
		/echo $chr(3)4Result is : $calc($1-)
366
	}
367
}*/
368
</pre>
369
370
CTCP flood detection example
371
372
<pre>
373
On OnCTCPRequest {
374
	if (%count == null) /set -u 10 %count 1
375
	else /inc -u 10 %count 1
376
377
	if (%count > 4) /ignore  -u 30 -t $nick!$ident@$host
378
}
379
</pre>
380
381
Mass mode example
382
383
<pre>
384
OnCommand /mass {
385
	if (!$2) {
386
		/echo /mass - Parameters missing [+/-<mode> <nick> <nick> <nick>]
387
		return
388
	}
389
390
	%len = 2
391
	
392
	# equal to while (%len <= $count(%0-, $chr(32)))
393
	while (%len <= $!) {
394
		if ($(%len) ison $channel) /mode $channel $1 $(%len)
395
		/inc %len
396
	}
397
}
398
</pre>
399
400
Shows info about servers, channels and users
401
<pre>
402
On JOIN {
403
	var %s = $server(0), %c = 0, %u = 0, %t = 0, %c2 = 0;
404
	
405
	while (%t < %s) {
406
		%t++
407
		/setserver $server(%t);
408
		%c += $chan(0)
409
	
410
		%c2 = 0
411
		while (%c2 < $chan(0)) {
412
			%c2++
413
			%u += $user($chan(%c2), 0)
414
		}
415
	}
416
417
	/echo You are on (%s) servers, (%c) channels with (%u) users
418
}
419
</pre>
420
421 156 Per Amundsen
It is possible to use scripts as functions.
422
These functions are fully nested like the client functions.
423 154 Per Amundsen
424
Lets say you make a /mycalc like this.
425
<pre>
426
OnCommand /mycalc {
427
	return $calc($0+$1);
428
}
429
</pre>
430
431
Then you can call this function with eiter /mycalc <number> <number> the normal way or $mycalc(<number, <number>)
432
Typing /testcalc will show the result.
433
<pre>
434
OnCommand /testcalc {
435
	/echo $0 + $1 is $mycalc($0, $1);
436
	/echo 5 + 4 is $mycalc(5, 4);
437
}
438
</pre>
439
440
Simple convert temperature C to F or F to C
441
/temp C 20 will print 68 F
442
443
<pre>
444
OnCommand /temp {
445
	if ($1 == C) /echo $calc(($2 * 9/5) + 32) F
446
	else if ($1 == F) /echo $round($calc(($2 - 32) * 5/9), 1) C
447
	else /echo Temp missing
448
}
449
</pre>
450
451
Test if input contains a link
452
<pre>
453
OnCommand /testlink {
454
	if (!$1) {
455
		/echo Link missing
456
		return
457
	}
458
459
	/echo $iif($regex($1, (?i)\\b((?:[a-z][\\w-]+:(?:/\{1\,3\}|[a-z0-9%])|www\\d\{0\,3\}[.]|[a-z0-9.\\-]+[.][a-z]\{2\,4\}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]\\\{\\\}\\\;:'".\,<>?«»“”‘’]))), yes, no)
460
}
461
462
</pre>
463
464
Retrives plot summary from captured imbd links
465
<pre>
466
On PRIVMSG {
467
	var %reg = $regex($0-, http://www\\.imdb\\.com(/title/[a-z0-9]+/))
468
	if (!%reg) { return }
469
		
470
	/sockclose imdb
471
	%text = null
472
	%imdb = $regmatch(1)plotsummary
473
	%imdbchan = $channel
474
	/sockopen imdb www.imdb.com 80
475
}
476
477
On SockOpen {
478
	if ($1 != imdb) { return }
479
480
	/sockwrite -n imdb GET %imdb HTTP/1.1
481
	/sockwrite -n imdb Host: www.imdb.com
482
	/sockwrite -n imdb User-Agent: Mozilla/5.0 (Windows NT 6.1\; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.24 Safari/536.5
483
	/sockwrite -n imdb Referer: http://www.imdb.com
484
	/sockwrite -n imdb Accept: text/xml,application/xml,application/xhtml+xml,text/html\;q=0.9,text/plain\;q=0.8,video/x-mng,image/png,image/jpeg,image/gif\;q=0.2,text/css,*\/*\;q=0.1
485
	/sockwrite -n imdb Accept-Language: en-us, en\;q=0.50
486
	/sockwrite -n imdb Connection: Close$crlf
487
}
488
489
On SockRead {
490
	if ($1 != imdb) { return }
491
		
492
	%text += $sockread(imdb)
493
}
494
495
On SockClose {
496
	if ($1 != imdb) { return }
497
	
498
	if ($regex(%text, <p class="plotpar">([\\s\\S]*?)<i>)) {
499
		/msg %imdbchan $regmatch(1)
500
	}
501
502
	%text = null
503
}
504
</pre>
505
506
An example showing the difference between dates
507
<pre>
508
OnCommand /test {
509
	$datediff($ctime(1/1 2042), $now)
510
	
511
	var %text = Difference is
512
	%text += $chr(32)$datematch(4) $iif($datematch(4) == 1, day, days)
513
	%text += $chr(32)$datematch(3) $iif($datematch(3) == 1, hour, hours) 
514
	%text += $chr(32)$datematch(2) $iif($datematch(3) == 1, minut, minutes) 
515
    
516
	/echo %text
517
}
518
</pre>
519
</pre>