Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 148 lines (124 sloc) 2.71 kb
011bfc4 @grahamking Split out server connection to be it's own server
authored
1 package main
2
3 import (
33eb549 @grahamking Porting to Go 1
authored
4 "encoding/json"
5 "errors"
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
6 "log"
011bfc4 @grahamking Split out server connection to be it's own server
authored
7 "strings"
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
8 "time"
011bfc4 @grahamking Split out server connection to be it's own server
authored
9 )
10
8e81ec2 @grahamking Error handling
authored
11 var (
33eb549 @grahamking Porting to Go 1
authored
12 ELSHORT = errors.New("Line too short")
13 ELMALFORMED = errors.New("Malformed line")
8e81ec2 @grahamking Error handling
authored
14 )
15
011bfc4 @grahamking Split out server connection to be it's own server
authored
16 type Line struct {
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
17 Raw string
18 Received string
19 User string
20 Host string
21 Command string
22 Args []string
23 Content string
24 IsCTCP bool
25 Channel string
011bfc4 @grahamking Split out server connection to be it's own server
authored
26 }
27
28 func (self *Line) String() string {
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
29 return string(self.AsJson())
011bfc4 @grahamking Split out server connection to be it's own server
authored
30 }
31
32 // Current line as json
33 func (self *Line) AsJson() []byte {
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
34 jsonData, err := json.Marshal(self)
35 if err != nil {
b7579df @grahamking Refactoring
authored
36 log.Println("Error on json Marshal of "+self.Raw, err)
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
37 }
0f9b7bb @grahamking Renaming go-connect -> hatcogd, go-join -> hjoin
authored
38 // client expects lines to have an ending
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
39 jsonData = append(jsonData, '\n')
40 return jsonData
011bfc4 @grahamking Split out server connection to be it's own server
authored
41 }
42
43 // Takes a raw string from IRC server and parses it
33eb549 @grahamking Porting to Go 1
authored
44 func ParseLine(data string) (*Line, error) {
011bfc4 @grahamking Split out server connection to be it's own server
authored
45
46 var line *Line
47 var prefix, command, trailing, user, host, raw string
48 var args, parts []string
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
49 var isCTCP bool
011bfc4 @grahamking Split out server connection to be it's own server
authored
50
51 data = sane(data)
52
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
53 if len(data) <= 2 {
54 return nil, ELSHORT
55 }
8e81ec2 @grahamking Error handling
authored
56
011bfc4 @grahamking Split out server connection to be it's own server
authored
57 raw = data
58 if data[0] == ':' { // Do we have a prefix?
59 parts = strings.SplitN(data[1:], " ", 2)
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
60 if len(parts) != 2 {
61 return nil, ELMALFORMED
62 }
8e81ec2 @grahamking Error handling
authored
63
011bfc4 @grahamking Split out server connection to be it's own server
authored
64 prefix = parts[0]
65 data = parts[1]
66
67 if strings.Contains(prefix, "!") {
68 parts = strings.Split(prefix, "!")
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
69 if len(parts) != 2 {
70 return nil, ELMALFORMED
71 }
011bfc4 @grahamking Split out server connection to be it's own server
authored
72 user = parts[0]
73 host = parts[1]
8e81ec2 @grahamking Error handling
authored
74
011bfc4 @grahamking Split out server connection to be it's own server
authored
75 } else {
76 host = prefix
77 }
78 }
79
80 if strings.Index(data, " :") != -1 {
81 parts = strings.SplitN(data, " :", 2)
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
82 if len(parts) != 2 {
83 return nil, ELMALFORMED
84 }
011bfc4 @grahamking Split out server connection to be it's own server
authored
85 data = parts[0]
86 args = strings.Split(data, " ")
87
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
88 trailing = parts[1]
8e81ec2 @grahamking Error handling
authored
89
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
90 // IRC CTCP uses ascii null byte
91 if len(trailing) > 0 && trailing[0] == '\001' {
92 isCTCP = true
93 }
8e81ec2 @grahamking Error handling
authored
94 trailing = sane(trailing)
95
011bfc4 @grahamking Split out server connection to be it's own server
authored
96 } else {
97 args = strings.Split(data, " ")
98 }
8e81ec2 @grahamking Error handling
authored
99
011bfc4 @grahamking Split out server connection to be it's own server
authored
100 command = args[0]
101 args = args[1:len(args)]
102
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
103 channel := ""
104 for _, arg := range args {
105 if strings.HasPrefix(arg, "#") {
106 channel = arg
107 break
108 }
109 }
110
111 if len(channel) == 0 {
112 if command == "PRIVMSG" {
113 // A /query message, fake the user as the channel
114 channel = user
115 } else if command == "JOIN" {
116 // JOIN commands say which channel in content part of msg
117 channel = trailing
118 }
119 }
120
121 if strings.HasPrefix(trailing, "ACTION") {
122 // Received a /me line
123 parts = strings.SplitN(trailing, " ", 2)
124 if len(parts) != 2 {
125 return nil, ELMALFORMED
126 }
127 trailing = parts[1]
128 command = "ACTION"
129 } else if strings.HasPrefix(trailing, "VERSION") {
130 trailing = ""
131 command = "VERSION"
132 }
011bfc4 @grahamking Split out server connection to be it's own server
authored
133
134 line = &Line{
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
135 Raw: raw,
33eb549 @grahamking Porting to Go 1
authored
136 Received: time.Now().Format(time.RFC3339),
4da68c9 @grahamking Improved private message support, not quite done. Also gofmt all files
authored
137 User: user,
138 Host: host,
139 Command: command,
140 Args: args,
141 Content: trailing,
142 IsCTCP: isCTCP,
143 Channel: channel,
011bfc4 @grahamking Split out server connection to be it's own server
authored
144 }
145
8e81ec2 @grahamking Error handling
authored
146 return line, nil
011bfc4 @grahamking Split out server connection to be it's own server
authored
147 }
Something went wrong with that request. Please try again.