-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSwiftPluginExample.swift
108 lines (87 loc) · 3.26 KB
/
SwiftPluginExample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* We create a class that inherits from NSObject so we need Foundation. */
import Foundation
/* As Textual creates a new instance of our primary class when the plugin
loads, it must inherit NSObject to allow proper initialization. */
/* THOPluginProtocol is the protocol available for plugin specific callbacks.
It is appended to our new class object to inform Swift that we conform to it.
Methods that you override must be declared as Objective-C or Textual wont see
that they exist and not call out to them. */
@objc
class TPI_SwiftPluginExample: NSObject, THOPluginProtocol
{
@objc
let subscribedServerInputCommands = ["privmsg", "notice"]
@objc
func didReceiveServerInput(_ inputObject: THOPluginDidReceiveServerInputConcreteObject, on client: IRCClient)
{
/* Swift provides a very powerful switch statement so
it is easier to use that for identifying commands than
using an if statement if more than the two are added. */
switch (inputObject.messageCommand) {
case "PRIVMSG":
handleIncomingPrivateMessageCommand(inputObject, on: client)
case "NOTICE":
handleIncomingNoticeCommand(inputObject, on: client)
default:
return
}
}
func handleIncomingPrivateMessageCommand(_ inputObject: THOPluginDidReceiveServerInputConcreteObject, on client: IRCClient)
{
/* Get message sequence of incoming message. */
let messageReceived = inputObject.messageSequence
let messageParamaters = inputObject.messageParamaters
/* Get channel that message was sent from. */
/* The first paramater of the PRIVMSG command is always
the channel the message was targetted to. */
let senderChannel = client.findChannel(messageParamaters[0])
/* Do not accept private messages. */
if (senderChannel?.isChannel != true) {
return
}
/* Get sender of message. */
let messageSender = inputObject.senderNickname
/* Ignore this user, he's kind of a jerk. :-( */
if messageSender.hasPrefix("Alex") {
return
}
/* Compare it against a specific value. */
if (messageReceived == "do you know what time it is?" ||
messageReceived == "does anybody know what time it is?")
{
/* Format message. */
let formattedString = ("\(messageSender), the time where I am is: \(formattedDateTimeString())")
/* Invoke the client on the main thread when sending. */
performBlock(onMainThread: {
client.sendPrivmsg(formattedString, to: senderChannel!)
})
}
}
func handleIncomingNoticeCommand(_ inputObject: THOPluginDidReceiveServerInputConcreteObject, on client: IRCClient)
{
// Not implemented
}
/* Support a new command in text field. */
@objc
let subscribedUserInputCommands = ["datetime"]
@objc
func userInputCommandInvoked(on client: IRCClient, command commandString: String, messageString: String)
{
guard let selectedChannel = masterController().mainWindow.selectedChannel else {
return
}
let formattedString = ("The current time is: \(formattedDateTimeString())")
performBlock(onMainThread: {
client.sendPrivmsg(formattedString, to: selectedChannel)
})
}
/* Helper functions. */
func formattedDateTimeString() -> String
{
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .full
let formattedDate = dateFormatter.string(from: Date())
return formattedDate
}
}