marvin

A simple and modular IRC bot

git clone https://git.8pit.net/marvin.git

 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 Public
12// License along with this program. If not, see <http://www.gnu.org/licenses/>.
13
14package irc
15
16import (
17	"strings"
18)
19
20type Sender struct {
21	Name string
22	Host string
23}
24
25type Message struct {
26	Sender   Sender
27	Receiver string
28	Command  string
29	Data     string
30}
31
32func parseMessage(line string) (msg Message) {
33	if len(strings.Fields(line)) < 2 {
34		return
35	}
36
37	if strings.HasPrefix(line, ":") {
38		idx := strings.Index(line, " ")
39		msg.Sender = Sender{Name: line[1:idx]}
40
41		user := strings.Split(msg.Sender.Name, "!")
42		if len(user) >= 2 {
43			msg.Sender.Name = user[0]
44			msg.Sender.Host = user[1]
45		}
46
47		line = line[idx+1:]
48	}
49
50	idx := strings.Index(line, " ")
51	msg.Command = strings.ToLower(line[:idx])
52	line = line[idx+1:]
53
54	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 = line
62	}
63
64	msg.Data = strings.TrimSpace(msg.Data)
65	return
66}