瀏覽代碼

add a type for CTCP-encoded strings, and NOTICEs for error cases

tags/v0.1.0
Edmund Huber 10 年之前
父節點
當前提交
34b01b115e
共有 5 個文件被更改,包括 38 次插入14 次删除
  1. 1
    1
      irc/commands.go
  2. 4
    0
      irc/reply.go
  3. 9
    0
      irc/strings.go
  4. 23
    12
      irc/theater.go
  5. 1
    1
      irc/types.go

+ 1
- 1
irc/commands.go 查看文件

@@ -966,7 +966,7 @@ func NewTheaterCommand(args []string) (Command, error) {
966 966
 		return &TheaterActionCommand{
967 967
 			channel: NewName(args[1]),
968 968
 			asNick:  NewName(args[2]),
969
-			action:  NewText(args[3]),
969
+			action:  NewCTCPText(args[3]),
970 970
 		}, nil
971 971
 	} else {
972 972
 		return nil, ErrParseCommand

+ 4
- 0
irc/reply.go 查看文件

@@ -99,6 +99,10 @@ func RplPrivMsg(source Identifiable, target Identifiable, message Text) string {
99 99
 	return NewStringReply(source, PRIVMSG, "%s :%s", target.Nick(), message)
100 100
 }
101 101
 
102
+func RplCTCPAction(source Identifiable, target Identifiable, action CTCPText) string {
103
+	return RplPrivMsg(source, target, NewText(fmt.Sprintf("\x01ACTION %s\x01", action)))
104
+}
105
+
102 106
 func RplNotice(source Identifiable, target Identifiable, message Text) string {
103 107
 	return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message)
104 108
 }

+ 9
- 0
irc/strings.go 查看文件

@@ -64,3 +64,12 @@ func NewText(str string) Text {
64 64
 func (text Text) String() string {
65 65
 	return string(text)
66 66
 }
67
+
68
+// CTCPText is text suitable escaped for CTCP.
69
+type CTCPText string
70
+
71
+var ctcpEscaper = strings.NewReplacer("\x00", "\x200", "\n", "\x20n", "\r", "\x20r")
72
+
73
+func NewCTCPText(str string) CTCPText {
74
+	return CTCPText(ctcpEscaper.Replace(str))
75
+}

+ 23
- 12
irc/theater.go 查看文件

@@ -51,9 +51,12 @@ func (m *TheaterIdentifyCommand) HandleServer(s *Server) {
51 51
 		return
52 52
 	}
53 53
 
54
-	if !channel.members.AnyHasMode(Theater) {
55
-		channel.members[client][Theater] = true
54
+	if channel.members.AnyHasMode(Theater) {
55
+		client.Reply(RplNotice(s, client, "someone else is +T in this channel"))
56
+		return
56 57
 	}
58
+
59
+	channel.members[client][Theater] = true
57 60
 }
58 61
 
59 62
 type TheaterPrivMsgCommand struct {
@@ -69,6 +72,7 @@ func (cmd *TheaterPrivMsgCommand) String() string {
69 72
 }
70 73
 func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
71 74
 	client := m.Client()
75
+
72 76
 	if !m.channel.IsChannel() {
73 77
 		client.ErrNoSuchChannel(m.channel)
74 78
 		return
@@ -80,10 +84,13 @@ func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
80 84
 		return
81 85
 	}
82 86
 
83
-	if channel.members.HasMode(client, Theater) {
84
-		for member := range channel.members {
85
-			member.Reply(RplPrivMsg(TheaterClient(m.asNick), channel, m.message))
86
-		}
87
+	if !channel.members.HasMode(client, Theater) {
88
+		client.Reply(RplNotice(s, client, "you are not +T"))
89
+		return
90
+	}
91
+
92
+	for member := range channel.members {
93
+		member.Reply(RplPrivMsg(TheaterClient(m.asNick), channel, m.message))
87 94
 	}
88 95
 }
89 96
 
@@ -91,7 +98,7 @@ type TheaterActionCommand struct {
91 98
 	BaseCommand
92 99
 	channel Name
93 100
 	asNick  Name
94
-	action  Text
101
+	action  CTCPText
95 102
 }
96 103
 
97 104
 func (cmd *TheaterActionCommand) String() string {
@@ -100,7 +107,8 @@ func (cmd *TheaterActionCommand) String() string {
100 107
 
101 108
 func (m *TheaterActionCommand) HandleServer(s *Server) {
102 109
 	client := m.Client()
103
-	if m.channel.IsChannel() {
110
+
111
+	if !m.channel.IsChannel() {
104 112
 		client.ErrNoSuchChannel(m.channel)
105 113
 		return
106 114
 	}
@@ -111,9 +119,12 @@ func (m *TheaterActionCommand) HandleServer(s *Server) {
111 119
 		return
112 120
 	}
113 121
 
114
-	if channel.members.HasMode(client, Theater) {
115
-		for member := range channel.members {
116
-			member.Reply(RplPrivMsg(TheaterClient(m.asNick), channel, NewText(fmt.Sprintf("\001ACTION %s\001", m.action))))
117
-		}
122
+	if !channel.members.HasMode(client, Theater) {
123
+		client.Reply(RplNotice(s, client, "you are not +T"))
124
+		return
125
+	}
126
+
127
+	for member := range channel.members {
128
+		member.Reply(RplCTCPAction(TheaterClient(m.asNick), channel, m.action))
118 129
 	}
119 130
 }

+ 1
- 1
irc/types.go 查看文件

@@ -85,7 +85,7 @@ func (members MemberSet) HasMode(member *Client, mode ChannelMode) bool {
85 85
 
86 86
 func (members MemberSet) AnyHasMode(mode ChannelMode) bool {
87 87
 	for _, modes := range members {
88
-		if modes[Theater] {
88
+		if modes[mode] {
89 89
 			return true
90 90
 		}
91 91
 	}

Loading…
取消
儲存