From 3d496ea58ddf32b083350a0f7834f59fca804489 Mon Sep 17 00:00:00 2001 From: norayr Date: Thu, 18 May 2017 03:55:31 +0400 Subject: [PATCH] parsing and several events; -- noch --- IRC.Mod | 53 ++++++++++++++++++++++++++++++++++++----------------- test.Mod | 50 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 79 insertions(+), 24 deletions(-) diff --git a/IRC.Mod b/IRC.Mod index cf0624c..3e9a999 100644 --- a/IRC.Mod +++ b/IRC.Mod @@ -28,9 +28,9 @@ CONST chnlist* = POINTER TO ARRAY OF chn; msg* = ARRAY msgLen OF CHAR; cbMessage* = PROCEDURE(VAR msg : ARRAY OF CHAR); (* cb stands for callback *) - cbPrivateMessage* = PROCEDURE (VAR msg, fromUser, fromIdent, ip: ARRAY OF CHAR); - cbPublicMessage* = PROCEDURE (VAR msg, fromUser, fromIdent, room, ip: ARRAY OF CHAR); - cbPublicMessageWithMention* = PROCEDURE(VAR msg, fromUser, fromIdent, room, ip: ARRAY OF CHAR); + cbPrivateMessage* = PROCEDURE (VAR msg, user, ident, host: ARRAY OF CHAR); + cbPublicMessage* = PROCEDURE (VAR msg, user, ident, rcpt, host: ARRAY OF CHAR); + cbPublicMessageWithMention* = PROCEDURE(VAR msg, user, ident, rcpt, host: ARRAY OF CHAR); (* rcpt is usually the room in case of public messages *) instance* = RECORD @@ -458,6 +458,25 @@ BEGIN RETURN b; END getHost; +PROCEDURE isMention(VAR nick, line: ARRAY OF CHAR): BOOLEAN; +VAR + i : INTEGER; + str: ARRAY 32 OF CHAR; +BEGIN + Strings.Extract(line, 0, Strings.Length(nick), str); + Out.String("str: "); Out.String(str); Out.Ln; + IF str = nick THEN + RETURN TRUE + ELSE + RETURN FALSE + END; +END isMention; + +PROCEDURE cutMentionFromMessage(VAR nick, msg: ARRAY OF CHAR); +BEGIN + Strings.Delete(msg, 0, Strings.Length(nick) + 2); +END cutMentionFromMessage; + PROCEDURE parse(VAR inst: instance; VAR line: ARRAY OF CHAR); VAR message: ARRAY msgLen OF CHAR; @@ -466,32 +485,32 @@ VAR messagetype: ARRAY 16 OF CHAR; rcpt: ARRAY 64 OF CHAR; b: BOOLEAN; + mn: BOOLEAN; BEGIN b := getUser(line, user); - - b := getMsgType(line, messagetype); - b := getRecipient(line, rcpt); b := getMsg(line, message); + IF messagetype = msgPRIVMSG THEN b := getUserName(user, username); b := getIdentName(user, identname); b := getHost(user, host); - - Out.String("username: "); Out.String(username); Out.String("|"); Out.Ln; - Out.String("identname: "); Out.String(identname); Out.String("|"); Out.Ln; - Out.String("host: "); Out.String(host); Out.String("|"); Out.Ln; END; - Out.String("user: "); Out.String(user); Out.String("|"); Out.Ln; - Out.String("message type: "); Out.String(messagetype); Out.String("|"); Out.Ln; - Out.String("recipient: "); Out.String(rcpt); Out.String("|"); Out.Ln; - Out.String("message: "); Out.String(message); Out.String("|"); Out.Ln; - - inst.callbackSimple(line); + IF rcpt = inst.nick THEN (* private message *) + inst.callbackPrivate(message, username, identname, host); + ELSE + mn := isMention(inst.nick, message); + IF mn THEN + cutMentionFromMessage(inst.nick, message); + inst.callbackPublicMention(message, username, identname, rcpt, host); + ELSE + inst.callbackPublic(message, username, identname, rcpt, host); + END; + END; END parse; @@ -508,7 +527,7 @@ BEGIN b := FALSE; ELSE IF serverMsg(line) THEN (* string starts with ':' *) - IF rplWelcome(line) THEN (* strting contains '001' *) + IF rplWelcome(line) THEN (* string contains '001' *) ModeAndJoin(inst); ELSE parse(inst, line); diff --git a/test.Mod b/test.Mod index aee1913..e332e19 100644 --- a/test.Mod +++ b/test.Mod @@ -3,13 +3,46 @@ MODULE test; IMPORT IRC, Out, Strings := ooc2Strings; -PROCEDURE clbk(VAR msg : ARRAY OF CHAR); +PROCEDURE onMessage(VAR msg : ARRAY OF CHAR); BEGIN -Out.String("callback procedure is running, youhoo!"); Out.Ln; -Out.String("input:"); Out.Ln; -Out.String(msg); Out.String("|"); Out.Ln; -Out.Ln; -END clbk; + Out.String("callback procedure is running, youhoo!"); Out.Ln; + Out.String("input:"); Out.Ln; + Out.String(msg); Out.String("|"); Out.Ln; + Out.Ln; +END onMessage; + +PROCEDURE onPrivateMessage(VAR msg, user, ident, host: ARRAY OF CHAR); +BEGIN + Out.String("*** private message ***"); Out.Ln; + Out.String("message: '"); Out.String(msg); Out.Char("'"); Out.Ln; + Out.String("user: '"); Out.String(user); Out.Char("'"); Out.Ln; + Out.String("ident: '"); Out.String(ident); Out.Char("'"); Out.Ln; + Out.String("host: '"); Out.String(host); Out.Char("'"); Out.Ln; + Out.String("*** that's it ***"); Out.Ln; +END onPrivateMessage; + +PROCEDURE onPublicMessage(VAR msg, user, ident, rcpt, host: ARRAY OF CHAR); +BEGIN + Out.String("*** public message ***"); Out.Ln; + Out.String("message: '"); Out.String(msg); Out.Char("'"); Out.Ln; + Out.String("user: '"); Out.String(user); Out.Char("'"); Out.Ln; + Out.String("ident: '"); Out.String(ident); Out.Char("'"); Out.Ln; + Out.String("recipient: '"); Out.String(rcpt); Out.Char("'"); Out.Ln; + Out.String("host: '"); Out.String(host); Out.Char("'"); Out.Ln; + Out.String("*** that's it ***"); Out.Ln; +END onPublicMessage; + +PROCEDURE onPublicMessageWithMention(VAR msg, user, ident, rcpt, host: ARRAY OF CHAR); +BEGIN + Out.String("*** public message, bot name mentioned ***"); Out.Ln; + Out.String("message: '"); Out.String(msg); Out.Char("'"); Out.Ln; + Out.String("user: '"); Out.String(user); Out.Char("'"); Out.Ln; + Out.String("ident: '"); Out.String(ident); Out.Char("'"); Out.Ln; + Out.String("recipient: '"); Out.String(rcpt); Out.Char("'"); Out.Ln; + Out.String("host: '"); Out.String(host); Out.Char("'"); Out.Ln; + Out.String("*** that's it ***"); Out.Ln; +END onPublicMessageWithMention; + PROCEDURE testBot; VAR @@ -22,7 +55,10 @@ BEGIN inst.nick := "vocbot"; inst.host := "irc.freenode.net"; inst.port := "6667"; - inst.callbackSimple := clbk; + inst.callbackSimple := onMessage; + inst.callbackPrivate := onPrivateMessage; + inst.callbackPublic := onPublicMessage; + inst.callbackPublicMention := onPublicMessageWithMention;