Bladeren bron

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

tags/v0.1.0
Edmund Huber 10 jaren geleden
bovenliggende
commit
34b01b115e
5 gewijzigde bestanden met toevoegingen van 38 en 14 verwijderingen
  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 Bestand weergeven

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

+ 4
- 0
irc/reply.go Bestand weergeven

99
 	return NewStringReply(source, PRIVMSG, "%s :%s", target.Nick(), message)
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
 func RplNotice(source Identifiable, target Identifiable, message Text) string {
106
 func RplNotice(source Identifiable, target Identifiable, message Text) string {
103
 	return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message)
107
 	return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message)
104
 }
108
 }

+ 9
- 0
irc/strings.go Bestand weergeven

64
 func (text Text) String() string {
64
 func (text Text) String() string {
65
 	return string(text)
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 Bestand weergeven

51
 		return
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
 type TheaterPrivMsgCommand struct {
62
 type TheaterPrivMsgCommand struct {
69
 }
72
 }
70
 func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
73
 func (m *TheaterPrivMsgCommand) HandleServer(s *Server) {
71
 	client := m.Client()
74
 	client := m.Client()
75
+
72
 	if !m.channel.IsChannel() {
76
 	if !m.channel.IsChannel() {
73
 		client.ErrNoSuchChannel(m.channel)
77
 		client.ErrNoSuchChannel(m.channel)
74
 		return
78
 		return
80
 		return
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
 	BaseCommand
98
 	BaseCommand
92
 	channel Name
99
 	channel Name
93
 	asNick  Name
100
 	asNick  Name
94
-	action  Text
101
+	action  CTCPText
95
 }
102
 }
96
 
103
 
97
 func (cmd *TheaterActionCommand) String() string {
104
 func (cmd *TheaterActionCommand) String() string {
100
 
107
 
101
 func (m *TheaterActionCommand) HandleServer(s *Server) {
108
 func (m *TheaterActionCommand) HandleServer(s *Server) {
102
 	client := m.Client()
109
 	client := m.Client()
103
-	if m.channel.IsChannel() {
110
+
111
+	if !m.channel.IsChannel() {
104
 		client.ErrNoSuchChannel(m.channel)
112
 		client.ErrNoSuchChannel(m.channel)
105
 		return
113
 		return
106
 	}
114
 	}
111
 		return
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 Bestand weergeven

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

Laden…
Annuleren
Opslaan