Communi  3.5.0
A cross-platform IRC framework written with Qt
QML bot example

The QML bot example is a simplified version of the bot example, written in QML. See the QML compatibility article for more QML specific details.

qmlbot.png
The QML bot example in action

The following snippet illustrates how IrcConnection is prepared and opened, and how a command is queued to automatically join a channel when connected.

1  connection: IrcConnection {
2  id: connection
3 
4  host: "irc.freenode.net"
5  userName: "communi"
6  nickName: "QmlBot" + Math.round(Math.random() * 9999)
7  realName: qsTr("Communi %1 QML bot example").arg(irc.version())
8 
9  Component.onCompleted: {
10  // queue a command to automatically join a channel when connected
11  sendCommand(command.createJoin(channel))
12  open()
13  }

The example utilizes IrcCommandParser for parsing commands from messages received from other clients. In order to take the parser in use, the supported commands must be teached, as illustrated below. Some of the commands are context sensitive, and the parser must be therefore kept aware of the list of channels the bot is on. This is easily achieved by binding the IrcCommandParser::channels property to the value of the IrcBufferModel::channels property. Furthermore, like a typical IRC bot, the commands are triggered using a slightly different syntax in queries and channels.

1  property IrcCommandParser parser: IrcCommandParser {
2  id: parser
3 
4  // keep the parser aware of the context
5  channels: model.channels
6 
7  // - on channel, respond to: "!<command> <params>" and "bot: <command> <params>"
8  // - in query, respond to: "!<command> <params>" and "<command> <params>"
9  triggers: connection.network.isChannel(target) ? ["!", connection.nickName + ":"] : ["!", ""]
10 
11  Component.onCompleted: {
12  // teach the bot some commands
13  parser.addCommand(IrcCommand.Nick, "NICK <nick>");
14  parser.addCommand(IrcCommand.Join, "JOIN <#channel> (<key>)");
15  parser.addCommand(IrcCommand.Part, "PART (<#channel>) (<message...>)");
16  parser.addCommand(IrcCommand.Quit, "QUIT (<message...>)");
17  parser.addCommand(IrcCommand.Message, "SAY [target] <message...>");
18  }
19  }

The bot listens to incoming messages via IrcConnection::messageReceived(). The current target is chosen based on whether the incoming message is a channel or a private message. Each message content is parsed for commands that were taught earlier.

1  onMessageReceived: {
2  if (message.type === IrcMessage.Private) {
3  // - in private, reply to the message sender
4  // - on channel, reply to the target channel
5  parser.target = message.private ? message.nick : message.target
6 
7  var command = parser.parse(message.content)
8  if (command) {
9  // send the command to the IRC server
10  sendCommand(command)
11  if (command.type === IrcCommand.Quit) {
12  // close the connection & quit the app when a !quit command was received
13  close()
14  Qt.quit()
15  }
16  }
17  }
18  }

Files: