1// This program is free software: you can redistribute it and/or modify 2// it under the terms of the GNU Affero General Public License as 3// published by the Free Software Foundation, either version 3 of the 4// License, or (at your option) any later version. 5// 6// This program is distributed in the hope that it will be useful, but 7// WITHOUT ANY WARRANTY; without even the implied warranty of 8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 9// Affero General Public License for more details.10//11// You should have received a copy of the GNU Affero General Public12// License along with this program. If not, see <http://www.gnu.org/licenses/>.1314package irc1516import (17 "strings"18)1920type Sender struct {21 Name string22 Host string23}2425type Message struct {26 Sender Sender27 Receiver string28 Command string29 Data string30}3132func parseMessage(line string) (msg Message) {33 if len(strings.Fields(line)) < 2 {34 return35 }3637 if strings.HasPrefix(line, ":") {38 idx := strings.Index(line, " ")39 msg.Sender = Sender{Name: line[1:idx]}4041 user := strings.Split(msg.Sender.Name, "!")42 if len(user) >= 2 {43 msg.Sender.Name = user[0]44 msg.Sender.Host = user[1]45 }4647 line = line[idx+1:]48 }4950 idx := strings.Index(line, " ")51 msg.Command = strings.ToLower(line[:idx])52 line = line[idx+1:]5354 if strings.Contains(line, " ") {55 idx = strings.Index(line, ":")56 if idx >= 0 {57 msg.Receiver = strings.TrimSpace(line[0:idx])58 msg.Data = line[idx+1:]59 }60 } else {61 msg.Data = line62 }6364 msg.Data = strings.TrimSpace(msg.Data)65 return66}