From 650d2b479d6aa079f41c620c15b47764ac91595a Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Sun, 13 Jan 2013 12:51:02 +0100 Subject: skype: convert msg test to a mock-based one --- protocols/skype/HACKING | 22 + protocols/skype/Makefile | 2 +- protocols/skype/t/Makefile | 33 - protocols/skype/t/bitlbee.conf | 0 protocols/skype/t/call-bitlbee.mock | 11 + protocols/skype/t/call-skyped.mock | 24 + protocols/skype/t/info-bitlbee.mock | 9 + protocols/skype/t/info-skyped.mock | 44 ++ protocols/skype/t/irssi/livetest-irssi.sh | 109 --- protocols/skype/t/irssi/skype-call.test | 13 - protocols/skype/t/irssi/skype-info.test | 12 - protocols/skype/t/irssi/skype-login.test | 10 - protocols/skype/t/irssi/skype-msg.test | 17 - protocols/skype/t/irssi/trigger.pl | 1225 ----------------------------- protocols/skype/t/livetest-bitlbee.sh | 116 --- protocols/skype/t/login-bitlbee.mock | 7 + protocols/skype/t/login-skyped.mock | 14 + protocols/skype/t/msg-bitlbee.mock | 9 + protocols/skype/t/msg-skyped.mock | 47 ++ protocols/skype/test.py | 79 ++ 20 files changed, 267 insertions(+), 1536 deletions(-) delete mode 100644 protocols/skype/t/Makefile delete mode 100644 protocols/skype/t/bitlbee.conf create mode 100644 protocols/skype/t/call-bitlbee.mock create mode 100644 protocols/skype/t/call-skyped.mock create mode 100644 protocols/skype/t/info-bitlbee.mock create mode 100644 protocols/skype/t/info-skyped.mock delete mode 100755 protocols/skype/t/irssi/livetest-irssi.sh delete mode 100644 protocols/skype/t/irssi/skype-call.test delete mode 100644 protocols/skype/t/irssi/skype-info.test delete mode 100644 protocols/skype/t/irssi/skype-login.test delete mode 100644 protocols/skype/t/irssi/skype-msg.test delete mode 100644 protocols/skype/t/irssi/trigger.pl delete mode 100755 protocols/skype/t/livetest-bitlbee.sh create mode 100644 protocols/skype/t/login-bitlbee.mock create mode 100644 protocols/skype/t/login-skyped.mock create mode 100644 protocols/skype/t/msg-bitlbee.mock create mode 100644 protocols/skype/t/msg-skyped.mock create mode 100755 protocols/skype/test.py (limited to 'protocols/skype') diff --git a/protocols/skype/HACKING b/protocols/skype/HACKING index 68ea44e8..a6ff8290 100644 --- a/protocols/skype/HACKING +++ b/protocols/skype/HACKING @@ -16,3 +16,25 @@ run python skyped.py -n -d 4) irssi + +== Tests + +The plugin is tested with a mocked IRC client and a mocked skyped. To add a new +test, the following steps are necessary: + +1) Add a new -skyped.mock file: just do the test manually, copy&paste the +skyped output and clean it up, so Alice talks to Bob. You can test the created +mock file by starting skyped with the -m option, and testing it from an IRC +client manually. + +2) Add a new -bitlbee.mock file: do the test manually from irssi, and use: + +/connect -rawlog rawlog localhost + +Then clean up the rawlog: the input lines are parsed as matching patterns, so +boring prefix/suffix text can be left out, non-interesting lines can be +deleted. The output lines still have to be strict IRC commands, as usual. + +3) Add the new test to test.py and run it! + +// vim: ft=asciidoc diff --git a/protocols/skype/Makefile b/protocols/skype/Makefile index 8bd3464f..6548bfb9 100644 --- a/protocols/skype/Makefile +++ b/protocols/skype/Makefile @@ -23,7 +23,7 @@ check: perl checkpatch.pl --show-types --ignore LONG_LINE,CAMELCASE --no-tree --file skype.c test: all - $(MAKE) -C t/ all + ./test.py doc: $(MANPAGES) diff --git a/protocols/skype/t/Makefile b/protocols/skype/t/Makefile deleted file mode 100644 index 9c5e95f9..00000000 --- a/protocols/skype/t/Makefile +++ /dev/null @@ -1,33 +0,0 @@ -PORT=9876 -BITLBEE=/usr/sbin/bitlbee - -export TEST_SKYPE_ID=user -export TEST_SKYPE_PASSWORD=pass - -testfiles := $(wildcard irssi/*.test) -tests := $(patsubst %.test,%,$(testfiles)) - -.PHONY: $(tests) - -all: $(tests) - @echo "passed $$(echo $(testfiles)|wc -w) tests." - -$(tests): % : %.test - @echo "--- Running test $@ ---"; \ - if [ -r "$(BITLBEE)" -a -x "$(BITLBEE)" ]; then \ - bitlbee_binary="$(BITLBEE)"; \ - else \ - bitlbee_basename=`basename $(BITLBEE)`; \ - bitlbee_binary=`which $$bitlbee_basename`; \ - fi; \ - if ! ./livetest-bitlbee.sh "$$bitlbee_binary" $(PORT) irssi/livetest-irssi.sh $< >$@.log; then \ - echo Test failed, log: ;\ - cat $@.log;\ - exit 1;\ - fi;\ - echo "--- OK ---" ;\ - sleep 1 -clean: - rm -r irssi/*.log bitlbeetest.pid dotirssi livetest - - diff --git a/protocols/skype/t/bitlbee.conf b/protocols/skype/t/bitlbee.conf deleted file mode 100644 index e69de29b..00000000 diff --git a/protocols/skype/t/call-bitlbee.mock b/protocols/skype/t/call-bitlbee.mock new file mode 100644 index 00000000..e480e7f3 --- /dev/null +++ b/protocols/skype/t/call-bitlbee.mock @@ -0,0 +1,11 @@ +>> NOTICE AUTH +<< NICK alice +<< USER alice alice localhost :alice +>> PRIVMSG &bitlbee +<< PRIVMSG &bitlbee :account add skype alice foo +<< PRIVMSG &bitlbee :account skype on +>> :bob!bob@skype.com JOIN :&bitlbee +<< PRIVMSG bob :CALL +>> PRIVMSG &bitlbee :skype - You are currently ringing the user bob. +<< PRIVMSG bob :HANGUP +>> PRIVMSG &bitlbee :skype - You cancelled the call to the user bob. diff --git a/protocols/skype/t/call-skyped.mock b/protocols/skype/t/call-skyped.mock new file mode 100644 index 00000000..16e28ee2 --- /dev/null +++ b/protocols/skype/t/call-skyped.mock @@ -0,0 +1,24 @@ +>> SEARCH GROUPS CUSTOM +<< GROUPS 48, 49 +>> SEARCH FRIENDS +<< USERS echo123, bob, bitlbee-skype +>> SET USERSTATUS ONLINE +<< USERSTATUS ONLINE +>> SET USERSTATUS ONLINE +<< USERSTATUS ONLINE +>> GET USER echo123 ONLINESTATUS +<< USER echo123 ONLINESTATUS OFFLINE +>> GET USER bob ONLINESTATUS +<< USER bob ONLINESTATUS ONLINE +>> GET USER bitlbee-skype ONLINESTATUS +<< USER bitlbee-skype ONLINESTATUS OFFLINE +>> CALL bob +<< CALL 178 STATUS UNPLACED +<< CALL 178 STATUS ROUTING +<< CALL 178 STATUS RINGING +>> GET CALL 178 PARTNER_HANDLE +<< CALL 178 PARTNER_HANDLE bob +>> SET CALL 178 STATUS FINISHED +<< CALL 178 STATUS CANCELLED +>> GET CALL 178 PARTNER_HANDLE +<< CALL 178 PARTNER_HANDLE bob diff --git a/protocols/skype/t/info-bitlbee.mock b/protocols/skype/t/info-bitlbee.mock new file mode 100644 index 00000000..a8896f6d --- /dev/null +++ b/protocols/skype/t/info-bitlbee.mock @@ -0,0 +1,9 @@ +>> NOTICE AUTH +<< NICK alice +<< USER alice alice localhost :Alice +>> PRIVMSG &bitlbee +<< PRIVMSG &bitlbee :account add skype alice foo +<< PRIVMSG &bitlbee :account skype on +>> :bob!bob@skype.com JOIN :&bitlbee +<< PRIVMSG &bitlbee :info bob +>> PRIVMSG &bitlbee :Full Name: Bob diff --git a/protocols/skype/t/info-skyped.mock b/protocols/skype/t/info-skyped.mock new file mode 100644 index 00000000..217a2b23 --- /dev/null +++ b/protocols/skype/t/info-skyped.mock @@ -0,0 +1,44 @@ +>> SEARCH GROUPS CUSTOM +<< GROUPS 48, 49 +>> SEARCH FRIENDS +<< USERS echo123, bob, bitlbee-skype +>> SET USERSTATUS ONLINE +<< USERSTATUS ONLINE +>> SET USERSTATUS ONLINE +<< USERSTATUS ONLINE +>> GET USER echo123 ONLINESTATUS +<< USER echo123 ONLINESTATUS ONLINE +>> GET USER bob ONLINESTATUS +<< USER bob ONLINESTATUS ONLINE +>> GET USER bitlbee-skype ONLINESTATUS +<< USER bitlbee-skype ONLINESTATUS OFFLINE +>> GET USER bob FULLNAME +<< USER bob FULLNAME Bob +>> GET USER bob PHONE_HOME +<< USER bob PHONE_HOME +>> GET USER bob PHONE_OFFICE +<< USER bob PHONE_OFFICE +>> GET USER bob PHONE_MOBILE +<< USER bob PHONE_MOBILE +>> GET USER bob NROF_AUTHED_BUDDIES +<< USER bob NROF_AUTHED_BUDDIES 145 +>> GET USER bob TIMEZONE +<< USER bob TIMEZONE 90000 +>> GET USER bob LASTONLINETIMESTAMP +<< USER bob LASTONLINETIMESTAMP 1358023469 +>> GET USER bob SEX +<< USER bob SEX MALE +>> GET USER bob LANGUAGE +<< USER bob LANGUAGE hu Hungarian +>> GET USER bob COUNTRY +<< USER bob COUNTRY hu Hungary +>> GET USER bob PROVINCE +<< USER bob PROVINCE +>> GET USER bob CITY +<< USER bob CITY Budapest +>> GET USER bob HOMEPAGE +<< USER bob HOMEPAGE +>> GET USER bob ABOUT +<< USER bob ABOUT +>> GET USER bob BIRTHDAY +<< USER bob BIRTHDAY 19781108 diff --git a/protocols/skype/t/irssi/livetest-irssi.sh b/protocols/skype/t/irssi/livetest-irssi.sh deleted file mode 100755 index a8e136cf..00000000 --- a/protocols/skype/t/irssi/livetest-irssi.sh +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/env bash -ISCRIPT=$1 -OPT=$2 - -[ -n "$ISCRIPT" ] || { echo Syntax: `basename "$0"` irssi-test-script; exit 1; } - -# Load variables from test -eval `sed -e '1,/^###/!d;/^###/d' "$ISCRIPT"` - -#if [ "$OPT" == "checkvars" ]; then echo $TESTNEEDEDVARS; fi -RET=0 - -# Check if we have the neccessary environment variables for this test -for var in $TESTNEEDEDVARS; do - if [ -z `eval echo \$\{$var\}` ]; then - if [ "$OPT" != "checkvars" ]; then - echo Need environment variable "$var" for this test. - exit 66 - else - echo $var - RET=66 - fi - fi -done - -# if we got this far we're OK -if [ "$OPT" == "checkvars" ]; then exit $RET; fi - -[ -n "$PORT" ] || { echo 'Need the bitlbee listening port as environment variable PORT'; exit 1; } - -# Setup the irssi dir -( - rm -r dotirssi - mkdir -p dotirssi/scripts dotirssi/logs - cp "`dirname $0`"/trigger.pl dotirssi/scripts && - echo 'script load trigger.pl' >dotirssi/startup -) &>/dev/null || { echo Failed to setup irssi testdir; exit 1; } - -# write irssi config - -echo ' - -aliases = { - runtest = "'`sed -e "1,/^###/d;s/@LOGIN@/$TESTLOGIN/;s/@PASSWORD@/$TESTPASSWORD/" "$ISCRIPT" | tr '\n' ';'`'"; - expectbee = "/trigger add -publics -channels &bitlbee -regexp"; - expectjoin = "/trigger add -joins -masks *!$0@* $1-"; - expectmsg = "/trigger add -privmsgs -masks *!$0@* $1-"; -}; - -servers = ( { address = "localhost"; chatnet = "local"; port = "'$PORT'"; autoconnect="yes";}); - -settings = { - settings_autosave = "no"; - core = { real_name = "bitlbee-test"; user_name = "bitlbee-test"; nick = "bitlbeetest"; }; - "fe-text" = { actlist_sort = "refnum"; }; -}; - -chatnets = { local = { type = "IRC"; autosendcmd = "/runtest"; }; }; - -logs = { -"dotirssi/logs/status.log" = { auto_open = "yes"; level = "ALL"; items = ( { type = "window"; name = "1"; } ); }; -"dotirssi/logs/control.log" = { auto_open = "yes"; level = "ALL"; items = ( { type = "target"; name = "&bitlbee"; } ); }; -' >dotirssi/config - -for nick in $TESTLOGNICKS; do - echo ' - "dotirssi/logs/'$nick'.log" = { auto_open = "yes"; level = "ALL"; items = ( { type = "target"; name = "'$nick'"; } ); }; - ' >>dotirssi/config -done - -echo '};' >>dotirssi/config - -# Go! - -echo Running irssi... -screen -D -m irssi --config=dotirssi/config --home=dotirssi/ & - -# output logs - -submitlogs() { - perl -p -i -e "s/$TESTLOGIN/---TESTLOGIN---/;s/$TESTPASSWORD/---TESTPASSWORD---/" dotirssi/logs/*.log - - if [ "$OPT" == "tgz" ]; then - tar czf "`dirname $0`"/"`basename "$ISCRIPT"`".logs.tgz dotirssi/logs/*.log - elif [ "$OPT" == "ctest" ]; then - echo CTEST_FULL_OUTPUT - for log in dotirssi/logs/*.log; do - echo -n '" - done - else - echo Test logs: dotirssi/logs/*.log - fi -} - -# timeout stuff - -t=$TESTDURATION -intval=1 -while (( t >= intval )); do - sleep $intval - kill -0 $! &>/dev/null || { echo screen/irssi terminated.; submitlogs; bash -c "cd dotirssi/logs && $TESTCHECKRESULT" >/dev/null; exit $?; } - t=$(( t - $intval )) -done -echo Killing screen/irssi... -kill $! -submitlogs -exit 22 diff --git a/protocols/skype/t/irssi/skype-call.test b/protocols/skype/t/irssi/skype-call.test deleted file mode 100644 index 8f502a59..00000000 --- a/protocols/skype/t/irssi/skype-call.test +++ /dev/null @@ -1,13 +0,0 @@ -TESTNEEDEDVARS="TEST_SKYPE_ID TEST_SKYPE_PASSWORD" -TESTDURATION=60 -TESTCHECKRESULT="grep '\[Test Passed\]' status.log" -TESTLOGIN="$TEST_SKYPE_ID" -TESTPASSWORD="$TEST_SKYPE_PASSWORD" -### Test receiving call output -/expectbee 'Welcome to the BitlBee' -command 'msg $$C register testing' -/expectbee 'Account successfully created' -command 'msg $$C account add skype @LOGIN@ @PASSWORD@' -/expectbee 'Account successfully added' -command 'msg $$C account 0 set test_join true' -/expectbee 'test_join' -command 'msg $$C account 0 on' -/expectjoin echo123 -command 'ctcp echo123 call' -/expectbee 'You are currently ringing the user' -command 'ctcp echo123 hangup' -/expectbee '(You cancelled the call|You finished the call)' -command 'quit Test Passed' diff --git a/protocols/skype/t/irssi/skype-info.test b/protocols/skype/t/irssi/skype-info.test deleted file mode 100644 index e8507321..00000000 --- a/protocols/skype/t/irssi/skype-info.test +++ /dev/null @@ -1,12 +0,0 @@ -TESTNEEDEDVARS="TEST_SKYPE_ID TEST_SKYPE_PASSWORD" -TESTDURATION=60 -TESTCHECKRESULT="grep '\[Test Passed\]' status.log" -TESTLOGIN="$TEST_SKYPE_ID" -TESTPASSWORD="$TEST_SKYPE_PASSWORD" -### Test receiving info output -/expectbee 'Welcome to the BitlBee' -command 'msg $$C register testing' -/expectbee 'Account successfully created' -command 'msg $$C account add skype @LOGIN@ @PASSWORD@' -/expectbee 'Account successfully added' -command 'msg $$C account 0 set test_join true' -/expectbee 'test_join' -command 'msg $$C account 0 on' -/expectjoin echo123 -command 'msg $$C info echo123' -/expectbee 'Full Name: Echo / Sound Test Service' -command 'quit Test Passed' diff --git a/protocols/skype/t/irssi/skype-login.test b/protocols/skype/t/irssi/skype-login.test deleted file mode 100644 index ca627002..00000000 --- a/protocols/skype/t/irssi/skype-login.test +++ /dev/null @@ -1,10 +0,0 @@ -TESTNEEDEDVARS="TEST_SKYPE_ID TEST_SKYPE_PASSWORD" -TESTDURATION=10 -TESTCHECKRESULT="grep '\[Test Passed\]' status.log" -TESTLOGIN="$TEST_SKYPE_ID" -TESTPASSWORD="$TEST_SKYPE_PASSWORD" -### Test login -/expectbee 'Welcome to the BitlBee' -command 'msg $$C register testing' -/expectbee 'Account successfully created' -command 'msg $$C account add skype @LOGIN@ @PASSWORD@' -/expectbee 'Account successfully added' -command 'msg $$C account 0 on' -/expectbee 'Logged in' -command 'quit Test Passed' diff --git a/protocols/skype/t/irssi/skype-msg.test b/protocols/skype/t/irssi/skype-msg.test deleted file mode 100644 index d35615cd..00000000 --- a/protocols/skype/t/irssi/skype-msg.test +++ /dev/null @@ -1,17 +0,0 @@ -TESTNEEDEDVARS="TEST_SKYPE_ID TEST_SKYPE_PASSWORD" -TESTDURATION=60 -TESTCHECKRESULT="grep '\[Test Passed\]' status.log" -TESTLOGIN="$TEST_SKYPE_ID" -TESTPASSWORD="$TEST_SKYPE_PASSWORD" -### Test sending and receiving messages -/expectbee 'Welcome to the BitlBee' -command 'msg $$C register testing' -/expectbee 'Account successfully created' -command 'msg $$C account add skype @LOGIN@ @PASSWORD@' -/expectbee 'Account successfully added' -command 'msg $$C account 0 set test_join true' -/expectbee 'test_join' -command 'msg $$C account 0 on' -# use builtin test service -/expectjoin echo123 -command 'msg $$C echo123: ping, say pong' -/expectbee 'pong' -command 'quit Test Passed' -# use a public bot as well, just in case the above one would fail -/expectjoin echo123 -command 'msg $$C add skype pam_bot' -/expectjoin pam_bot -command 'msg $$C pam_bot: pambot help' -/expectbee 'PamBot, thanks for chatting with me' -command 'quit Test Passed' diff --git a/protocols/skype/t/irssi/trigger.pl b/protocols/skype/t/irssi/trigger.pl deleted file mode 100644 index 02f8951f..00000000 --- a/protocols/skype/t/irssi/trigger.pl +++ /dev/null @@ -1,1225 +0,0 @@ -# trigger.pl - execute a command or replace text, triggered by an event in irssi -# Do /TRIGGER HELP or look at http://wouter.coekaerts.be/irssi/ for help - -# Copyright (C) 2002-2006 Wouter Coekaerts -# -# This program 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 2 of the License, or -# (at your option) any later version. -# -# This program 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 this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - -use strict; -use Irssi 20020324 qw(command_bind command_runsub command signal_add_first signal_continue signal_stop signal_remove); -use Text::ParseWords; -use IO::File; -use vars qw($VERSION %IRSSI); - -$VERSION = '1.0'; -%IRSSI = ( - authors => 'Wouter Coekaerts', - contact => 'wouter@coekaerts.be', - name => 'trigger', - description => 'execute a command or replace text, triggered by an event in irssi', - license => 'GPLv2 or later', - url => 'http://wouter.coekaerts.be/irssi/', - changed => '$LastChangedDate: 2006-01-23 13:10:19 +0100 (Mon, 23 Jan 2006) $', -); - -sub cmd_help { - Irssi::print (<<'SCRIPTHELP_EOF', MSGLEVEL_CLIENTCRAP); - -TRIGGER LIST -TRIGGER SAVE -TRIGGER RELOAD -TRIGGER MOVE -TRIGGER DELETE -TRIGGER CHANGE ... -TRIGGER ADD ... - -When to match: -On which types of event to trigger: - These are simply specified by -name_of_the_type - The normal IRC event types are: - publics, %|privmsgs, pubactions, privactions, pubnotices, privnotices, joins, parts, quits, kicks, topics, invites, nick_changes, dcc_msgs, dcc_actions, dcc_ctcps - mode_channel: %|a mode on the (whole) channel (like +t, +i, +b) - mode_nick: %|a mode on someone in the channel (like +o, +v) - -all is an alias for all of those. - Additionally, there is: - rawin: %|raw text incoming from the server - send_command: %|commands you give to irssi - send_text: %|lines you type that aren't commands - beep: %|when irssi beeps - notify_join: %|someone in you notify list comes online - notify_part: %|someone in your notify list goes offline - notify_away: %|someone in your notify list goes away - notify_unaway: %|someone in your notify list goes unaway - notify_unidle: %|someone in your notify list stops idling - -Filters (conditions) the event has to satisfy. They all take one parameter. -If you can give a list, seperate elements by space and use quotes around the list. - -pattern: %|The message must match the given pattern. ? and * can be used as wildcards - -regexp: %|The message must match the given regexp. (see man perlre) - %|if -nocase is given as an option, the regexp or pattern is matched case insensitive - -tags: %|The servertag must be in the given list of tags - -channels: %|The event must be in one of the given list of channels. - Examples: %|-channels '#chan1 #chan2' or -channels 'IRCNet/#channel' - %|-channels 'EFNet/' means every channel on EFNet and is the same as -tags 'EFNet' - -masks: %|The person who triggers it must match one of the given list of masks - -hasmode: %|The person who triggers it must have the give mode - Examples: %|'-o' means not opped, '+ov' means opped OR voiced, '-o&-v' means not opped AND not voiced - -hasflag: %|Only trigger if if friends.pl (friends_shasta.pl) or people.pl is loaded and the person who triggers it has the given flag in the script (same syntax as -hasmode) - -other_masks - -other_hasmode - -other_hasflag: %|Same as above but for the victim for kicks or mode_nick. - -What to do when it matches: - -command: Execute the given Irssi-command - %|You are able to use $1, $2 and so on generated by your regexp pattern. - %|For multiple commands ; (or $;) can be used as seperator - %|The following variables are also expanded: - $T: %|Server tag - $C: %|Channel name - $N: %|Nickname of the person who triggered this command - $A: %|His address (foo@bar.com), - $I: %|His ident (foo) - $H: %|His hostname (bar.com) - $M: %|The complete message - ${other}: %|The victim for kicks or mode_nick - ${mode_type}: %|The type ('+' or '-') for a mode_channel or mode_nick - ${mode_char}: %|The mode char ('o' for ops, 'b' for ban,...) - ${mode_arg} : %|The argument to the mode (if there is one) - %|$\X, with X being one of the above expands (e.g. $\M), escapes all non-alphanumeric characters, so it can be used with /eval or /exec. Don't use /eval or /exec without this, it's not safe. - - -replace: %|replaces the matching part with the given replacement in the event (requires a -regexp or -pattern) - -once: %|remove the trigger if it is triggered, so it only executes once and then is forgotten. - -stop: %|stops the signal. It won't get displayed by Irssi. Like /IGNORE - -debug: %|print some debugging info - -Other options: - -disabled: %|Same as removing it, but keeps it in case you might need it later - -name: %|Give the trigger a name. You can refer to the trigger with this name in add/del/change commands - -Examples: - Knockout people who do a !list: - /TRIGGER ADD %|-publics -channels "#channel1 #channel2" -nocase -regexp ^!list -command "KN $N This is not a warez channel!" - React to !echo commands from people who are +o in your friends-script: - /TRIGGER ADD %|-publics -regexp '^!echo (.*)' -hasflag '+o' -command 'say echo: $1' - Ignore all non-ops on #channel: - /TRIGGER ADD %|-publics -actions -channels "#channel" -hasmode '-o' -stop - Send a mail to yourself every time a topic is changed: - /TRIGGER ADD %|-topics -command 'exec echo $\N changed topic of $\C to: $\M | mail you@somewhere.com -s topic' - - -Examples with -replace: - %|Replace every occurence of shit with sh*t, case insensitive: - /TRIGGER ADD %|-all -nocase -regexp shit -replace sh*t - %|Strip all colorcodes from *!lamer@*: - /TRIGGER ADD %|-all -masks *!lamer@* -regexp '\x03\d?\d?(,\d\d?)?|\x02|\x1f|\x16|\x06' -replace '' - %|Never let *!bot1@foo.bar or *!bot2@foo.bar hilight you - %|(this works by cutting your nick in 2 different parts, 'myn' and 'ick' here) - %|you don't need to understand the -replace argument, just trust that it works if the 2 parts separately don't hilight: - /TRIGGER ADD %|-all masks '*!bot1@foo.bar *!bot2@foo.bar' -regexp '(myn)(ick)' -nocase -replace '$1\x02\x02$2' - %|Avoid being hilighted by !top10 in eggdrops with stats.mod (but show your nick in bold): - /TRIGGER ADD %|-publics -regexp '(Top.0\(.*\): 1.*)(my)(nick)' -replace '$1\x02$2\x02\x02$3\x02' - %|Convert a Windows-1252 Euro to an ISO-8859-15 Euro (same effect as euro.pl): - /TRIGGER ADD %|-regexp '\x80' -replace '\xA4' - %|Show tabs as spaces, not the inverted I (same effect as tab_stop.pl): - /TRIGGER ADD %|-all -regexp '\t' -replace ' ' -SCRIPTHELP_EOF -} # / - -my @triggers; # array of all triggers -my %triggers_by_type; # hash mapping types on triggers of that type -my $recursion_depth = 0; -my $changed_since_last_save = 0; - -############### -### formats ### -############### - -Irssi::theme_register([ - 'trigger_header' => 'Triggers:', - 'trigger_line' => '%#$[-4]0 $1', - 'trigger_added' => 'Trigger $0 added: $1', - 'trigger_not_found' => 'Trigger {hilight $0} not found', - 'trigger_saved' => 'Triggers saved to $0', - 'trigger_loaded' => 'Triggers loaded from $0' -]); - -######################################### -### catch the signals & do your thing ### -######################################### - -# trigger types with a message and a channel -my @allchanmsg_types = qw(publics pubactions pubnotices pubctcps pubctcpreplies parts quits kicks topics); -# trigger types with a message -my @allmsg_types = (@allchanmsg_types, qw(privmsgs privactions privnotices privctcps privctcpreplies dcc_msgs dcc_actions dcc_ctcps)); -# trigger types with a channel -my @allchan_types = (@allchanmsg_types, qw(mode_channel mode_nick joins invites)); -# trigger types in -all -my @all_types = (@allmsg_types, qw(mode_channel mode_nick joins invites nick_changes)); -# trigger types with a server -my @all_server_types = (@all_types, qw(rawin notify_join notify_part notify_away notify_unaway notify_unidle)); -# all trigger types -my @trigger_types = (@all_server_types, qw(send_command send_text beep)); -#trigger types that are not in -all -#my @notall_types = grep {my $a=$_; return (!grep {$_ eq $a} @all_types);} @trigger_types; -my @notall_types = qw(rawin notify_join notify_part notify_away notify_unaway notify_unidle send_command send_text beep); - -my @signals = ( -# "message public", SERVER_REC, char *msg, char *nick, char *address, char *target -{ - 'types' => ['publics'], - 'signal' => 'message public', - 'sub' => sub {check_signal_message(\@_,1,$_[0],$_[4],$_[2],$_[3],'publics');}, -}, -# "message private", SERVER_REC, char *msg, char *nick, char *address -{ - 'types' => ['privmsgs'], - 'signal' => 'message private', - 'sub' => sub {check_signal_message(\@_,1,$_[0],undef,$_[2],$_[3],'privmsgs');}, -}, -# "message irc action", SERVER_REC, char *msg, char *nick, char *address, char *target -{ - 'types' => ['privactions','pubactions'], - 'signal' => 'message irc action', - 'sub' => sub { - if ($_[4] eq $_[0]->{nick}) { - check_signal_message(\@_,1,$_[0],undef,$_[2],$_[3],'privactions'); - } else { - check_signal_message(\@_,1,$_[0],$_[4],$_[2],$_[3],'pubactions'); - } - }, -}, -# "message irc notice", SERVER_REC, char *msg, char *nick, char *address, char *target -{ - 'types' => ['privnotices','pubnotices'], - 'signal' => 'message irc notice', - 'sub' => sub { - if ($_[4] eq $_[0]->{nick}) { - check_signal_message(\@_,1,$_[0],undef,$_[2],$_[3],'privnotices'); - } else { - check_signal_message(\@_,1,$_[0],$_[4],$_[2],$_[3],'pubnotices'); - } - } -}, -# "message join", SERVER_REC, char *channel, char *nick, char *address -{ - 'types' => ['joins'], - 'signal' => 'message join', - 'sub' => sub {check_signal_message(\@_,-1,$_[0],$_[1],$_[2],$_[3],'joins');} -}, -# "message part", SERVER_REC, char *channel, char *nick, char *address, char *reason -{ - 'types' => ['parts'], - 'signal' => 'message part', - 'sub' => sub {check_signal_message(\@_,4,$_[0],$_[1],$_[2],$_[3],'parts');} -}, -# "message quit", SERVER_REC, char *nick, char *address, char *reason -{ - 'types' => ['quits'], - 'signal' => 'message quit', - 'sub' => sub {check_signal_message(\@_,3,$_[0],undef,$_[1],$_[2],'quits');} -}, -# "message kick", SERVER_REC, char *channel, char *nick, char *kicker, char *address, char *reason -{ - 'types' => ['kicks'], - 'signal' => 'message kick', - 'sub' => sub {check_signal_message(\@_,5,$_[0],$_[1],$_[3],$_[4],'kicks',{'other'=>$_[2]});} -}, -# "message topic", SERVER_REC, char *channel, char *topic, char *nick, char *address -{ - 'types' => ['topics'], - 'signal' => 'message topic', - 'sub' => sub {check_signal_message(\@_,2,$_[0],$_[1],$_[3],$_[4],'topics');} -}, -# "message invite", SERVER_REC, char *channel, char *nick, char *address -{ - 'types' => ['invites'], - 'signal' => 'message invite', - 'sub' => sub {check_signal_message(\@_,-1,$_[0],$_[1],$_[2],$_[3],'invites');} -}, -# "message nick", SERVER_REC, char *newnick, char *oldnick, char *address -{ - 'types' => ['nick_changes'], - 'signal' => 'message nick', - 'sub' => sub {check_signal_message(\@_,-1,$_[0],undef,$_[1],$_[3],'nick_changes');} -}, -# "message dcc", DCC_REC *dcc, char *msg -{ - 'types' => ['dcc_msgs'], - 'signal' => 'message dcc', - 'sub' => sub {check_signal_message(\@_,1,$_[0]->{'server'},undef,$_[0]->{'nick'},undef,'dcc_msgs'); - } -}, -# "message dcc action", DCC_REC *dcc, char *msg -{ - 'types' => ['dcc_actions'], - 'signal' => 'message dcc action', - 'sub' => sub {check_signal_message(\@_,1,$_[0]->{'server'},undef,$_[0]->{'nick'},undef,'dcc_actions');} -}, -# "message dcc ctcp", DCC_REC *dcc, char *cmd, char *data -{ - 'types' => ['dcc_ctcps'], - 'signal' => 'message dcc ctcp', - 'sub' => sub {check_signal_message(\@_,1,$_[0]->{'server'},undef,$_[0]->{'nick'},undef,'dcc_ctcps');} -}, -# "server incoming", SERVER_REC, char *data -{ - 'types' => ['rawin'], - 'signal' => 'server incoming', - 'sub' => sub {check_signal_message(\@_,1,$_[0],undef,undef,undef,'rawin');} -}, -# "send command", char *args, SERVER_REC, WI_ITEM_REC -{ - 'types' => ['send_command'], - 'signal' => 'send command', - 'sub' => sub { - sig_send_text_or_command(\@_,1); - } -}, -# "send text", char *line, SERVER_REC, WI_ITEM_REC -{ - 'types' => ['send_text'], - 'signal' => 'send text', - 'sub' => sub { - sig_send_text_or_command(\@_,0); - } -}, -# "beep" -{ - 'types' => ['beep'], - 'signal' => 'beep', - 'sub' => sub {check_signal_message(\@_,-1,undef,undef,undef,undef,'beep');} -}, -# "event ", SERVER_REC, char *args, char *sender_nick, char *sender_address -{ - 'types' => ['mode_channel', 'mode_nick'], - 'signal' => 'event mode', - 'sub' => sub { - my ($server, $event_args, $nickname, $address) = @_; - my ($target, $modes, $modeargs) = split(/ /, $event_args, 3); - return if (!$server->ischannel($target)); - my (@modeargs) = split(/ /,$modeargs); - my ($pos, $type, $event_type, $arg) = (0, '+'); - foreach my $char (split(//,$modes)) { - if ($char eq "+" || $char eq "-") { - $type = $char; - } else { - if ($char =~ /[Oovh]/) { # mode_nick - $event_type = 'mode_nick'; - $arg = $modeargs[$pos++]; - } elsif ($char =~ /[beIqdk]/ || ( $char =~ /[lfJ]/ && $type eq '+')) { # chan_mode with arg - $event_type = 'mode_channel'; - $arg = $modeargs[$pos++]; - } else { # chan_mode without arg - $event_type = 'mode_channel'; - $arg = undef; - } - check_signal_message(\@_,-1,$server,$target,$nickname,$address,$event_type,{ - 'mode_type' => $type, - 'mode_char' => $char, - 'mode_arg' => $arg, - 'other' => ($event_type eq 'mode_nick') ? $arg : undef - }); - } - } - } -}, -# "notifylist joined", SERVER_REC, char *nick, char *user, char *host, char *realname, char *awaymsg -{ - 'types' => ['notify_join'], - 'signal' => 'notifylist joined', - 'sub' => sub {check_signal_message(\@_, 5, $_[0], undef, $_[1], $_[2].'@'.$_[3], 'notify_join', {'realname' => $_[4]});} -}, -{ - 'types' => ['notify_part'], - 'signal' => 'notifylist left', - 'sub' => sub {check_signal_message(\@_, 5, $_[0], undef, $_[1], $_[2].'@'.$_[3], 'notify_left', {'realname' => $_[4]});} -}, -{ - 'types' => ['notify_unidle'], - 'signal' => 'notifylist unidle', - 'sub' => sub {check_signal_message(\@_, 5, $_[0], undef, $_[1], $_[2].'@'.$_[3], 'notify_unidle', {'realname' => $_[4]});} -}, -{ - 'types' => ['notify_away', 'notify_unaway'], - 'signal' => 'notifylist away changed', - 'sub' => sub {check_signal_message(\@_, 5, $_[0], undef, $_[1], $_[2].'@'.$_[3], ($_[5] ? 'notify_away' : 'notify_unaway'), {'realname' => $_[4]});} -}, -# "ctcp msg", SERVER_REC, char *args, char *nick, char *addr, char *target -{ - 'types' => ['pubctcps', 'privctcps'], - 'signal' => 'ctcp msg', - 'sub' => sub { - my ($server, $args, $nick, $addr, $target) = @_; - if ($target eq $server->{'nick'}) { - check_signal_message(\@_, 1, $server, undef, $nick, $addr, 'privctcps'); - } else { - check_signal_message(\@_, 1, $server, $target, $nick, $addr, 'pubctcps'); - } - } -}, -# "ctcp reply", SERVER_REC, char *args, char *nick, char *addr, char *target -{ - 'types' => ['pubctcpreplies', 'privctcpreplies'], - 'signal' => 'ctcp reply', - 'sub' => sub { - my ($server, $args, $nick, $addr, $target) = @_; - if ($target eq $server->{'nick'}) { - check_signal_message(\@_, 1, $server, undef, $nick, $addr, 'privctcps'); - } else { - check_signal_message(\@_, 1, $server, $target, $nick, $addr, 'pubctcps'); - } - } -} -); - -sub sig_send_text_or_command { - my ($signal, $iscommand) = @_; - my ($line, $server, $item) = @$signal; - my ($channelname,$nickname,$address) = (undef,undef,undef); - if ($item && (ref($item) eq 'Irssi::Irc::Channel' || ref($item) eq 'Irssi::Silc::Channel')) { - $channelname = $item->{'name'}; - } elsif ($item && ref($item) eq 'Irssi::Irc::Query') { # TODO Silc query ? - $nickname = $item->{'name'}; - $address = $item->{'address'} - } - # TODO pass context also for non-channels (queries and other stuff) - check_signal_message($signal,0,$server,$channelname,$nickname,$address,$iscommand ? 'send_command' : 'send_text'); - -} - -my %filters = ( -'tags' => { - 'types' => \@all_server_types, - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - - if (!defined($server)) { - return 0; - } - my $matches = 0; - foreach my $tag (split(/ /,$param)) { - if (lc($server->{'tag'}) eq lc($tag)) { - $matches = 1; - last; - } - } - return $matches; - } -}, -'channels' => { - 'types' => \@allchan_types, - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - - if (!defined($channelname) || !defined($server)) { - return 0; - } - my $matches = 0; - foreach my $trigger_channel (split(/ /,$param)) { - if (lc($channelname) eq lc($trigger_channel) - || lc($server->{'tag'}.'/'.$channelname) eq lc($trigger_channel) - || lc($server->{'tag'}.'/') eq lc($trigger_channel)) { - $matches = 1; - last; # this channel matches, stop checking channels - } - } - return $matches; - } -}, -'masks' => { - 'types' => \@all_types, - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return (defined($nickname) && defined($address) && defined($server) && $server->masks_match($param, $nickname, $address)); - } -}, -'other_masks' => { - 'types' => ['kicks', 'mode_nick'], - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return 0 unless defined($extra->{'other'}); - my $other_address = get_address($extra->{'other'}, $server, $channelname); - return defined($other_address) && $server->masks_match($param, $extra->{'other'}, $other_address); - } -}, -'hasmode' => { - 'types' => \@all_types, - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return hasmode($param, $nickname, $server, $channelname); - } -}, -'other_hasmode' => { - 'types' => ['kicks', 'mode_nick'], - 'sub' => sub { - my ($param,$signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return defined($extra->{'other'}) && hasmode($param, $extra->{'other'}, $server, $channelname); - } -}, -'hasflag' => { - 'types' => \@all_types, - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return 0 unless defined($nickname) && defined($address) && defined($server); - my $flags = get_flags ($server->{'chatnet'},$channelname,$nickname,$address); - return defined($flags) && check_modes($flags,$param); - } -}, -'other_hasflag' => { - 'types' => ['kicks', 'mode_nick'], - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return 0 unless defined($extra->{'other'}); - my $other_address = get_address($extra->{'other'}, $server, $channelname); - return 0 unless defined($other_address); - my $flags = get_flags ($server->{'chatnet'},$channelname,$extra->{'other'},$other_address); - return defined($flags) && check_modes($flags,$param); - } -}, -'mode_type' => { - 'types' => ['mode_channel', 'mode_nick'], - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return (($param) eq $extra->{'mode_type'}); - } -}, -'mode_char' => { - 'types' => ['mode_channel', 'mode_nick'], - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return (($param) eq $extra->{'mode_char'}); - } -}, -'mode_arg' => { - 'types' => ['mode_channel', 'mode_nick'], - 'sub' => sub { - my ($param, $signal,$parammessage,$server,$channelname,$nickname,$address,$condition,$extra) = @_; - return (($param) eq $extra->{'mode_arg'}); - } -} -); - -sub get_address { - my ($nick, $server, $channel) = @_; - my $nickrec = get_nickrec($nick, $server, $channel); - return $nickrec ? $nickrec->{'host'} : undef; -} -sub get_nickrec { - my ($nick, $server, $channel) = @_; - return unless defined($server) && defined($channel) && defined($nick); - my $chanrec = $server->channel_find($channel); - return $chanrec ? $chanrec->nick_find($nick) : undef; -} - -sub hasmode { - my ($param, $nickname, $server, $channelname) = @_; - my $nickrec = get_nickrec($nickname, $server, $channelname); - return 0 unless defined $nickrec; - my $modes = - ($nickrec->{'op'} ? 'o' : '') - . ($nickrec->{'voice'} ? 'v' : '') - . ($nickrec->{'halfop'} ? 'h' : '') - ; - return check_modes($modes, $param); -} - -# list of all switches -my @trigger_switches = (@trigger_types, qw(all nocase stop once debug disabled)); -# parameters (with an argument) -my @trigger_params = qw(pattern regexp command replace name); -# list of all options (including switches) for /TRIGGER ADD -my @trigger_add_options = (@trigger_switches, @trigger_params, keys(%filters)); -# same for /TRIGGER CHANGE, this includes the -no