Skip to content

Commit cfd1d9d

Browse files
committed
auth, greet msg, encrypt made private
* tests added for new functions * some cmds added to example * readme updated
1 parent 5815158 commit cfd1d9d

File tree

4 files changed

+137
-43
lines changed

4 files changed

+137
-43
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ import (
3737
)
3838

3939
func main() {
40-
pop, err := pop3.Connect("mail.pop3.com:110")
40+
pop, err := pop3.Connect("mail.pop3.com:110", nil, false)
4141
if err != nil {
4242
log.Fatalf(err.Error())
4343
}
4444

45-
fmt.Println(pop.GreetingMsg) // Message starts with "+OK"
46-
fmt.Println(pop.IsAuthorized) // true
45+
fmt.Println(pop.GreetingMsg()) // Message starts with "+OK"
46+
fmt.Println(pop.IsAuthorized()) // true
47+
fmt.Println(pop.IsEncrypted()) // false
4748

4849
// USER command
4950
username := os.Getenv("POP3_USER") // Read from env
@@ -57,61 +58,61 @@ func main() {
5758
password := os.Getenv("POP3_PASSWORD") // Read from env
5859
pass, err := pop.Pass(password)
5960
if err != nil {
60-
log.Fatalf(err.Error())
61+
log.Fatalf(err.Error())
6162
}
6263
fmt.Println(pass)
6364

6465
// STAT command
6566
stat, err := pop.Stat()
6667
if err != nil {
67-
log.Fatalf(err.Error())
68+
log.Fatalf(err.Error())
6869
}
6970
fmt.Println(stat)
7071

7172
// LIST command
72-
s, _ := pop.List()
73-
if len(s) == 0 {
74-
fmt.Println(s)
73+
l, _ := pop.List()
74+
if len(l) == 0 {
75+
fmt.Println(l)
7576
}
7677

7778
// LIST <mail-num> command
78-
l, _ := pop.List(1)
79-
fmt.Println(l[0])
79+
ll, _ := pop.List(1)
80+
fmt.Println(ll[0])
8081

8182
// DELE command
8283
dele, err := pop.Dele("1")
8384
if err != nil {
84-
log.Fatalf(err.Error())
85+
log.Fatalf(err.Error())
8586
}
8687
fmt.Println(dele)
8788

8889
// RETR command
8990
retr, err := pop.Retr("1")
9091
if err != nil {
91-
log.Fatalf(err.Error())
92+
log.Fatalf(err.Error())
9293
}
9394
for _, m := range retr {
94-
fmt.Println(m)
95+
fmt.Println(m)
9596
}
9697

9798
// NOOP command
9899
noop, err := pop.Noop()
99100
if err != nil {
100-
log.Fatalf(err.Error())
101+
log.Fatalf(err.Error())
101102
}
102103
fmt.Println(noop)
103104

104105
// RSET command
105106
rset, err := pop.Rset()
106107
if err != nil {
107-
log.Fatalf(err.Error())
108+
log.Fatalf(err.Error())
108109
}
109110
fmt.Println(rset)
110111

111112
// QUIT state
112113
q, err := pop.Quit()
113114
if err != nil {
114-
log.Fatalf(err.Error())
115+
log.Fatalf(err.Error())
115116
}
116117
fmt.Println(q) // Prints: "QUIT"
117118
}

example/main.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,43 @@ import (
44
"fmt"
55
"github.com/gozeloglu/gop-3/pop3"
66
"log"
7+
"os"
8+
)
9+
10+
const (
11+
userKey = "POP3_USER"
12+
passwordKey = "POP3_PASSWORD"
713
)
814

915
func main() {
10-
pop, err := pop3.Connect("pop3.mail.com:110", nil, false)
16+
pop, err := pop3.Connect("pop.gmail.com:995", nil, true)
1117
if err != nil {
1218
log.Fatalf(err.Error())
1319
}
14-
fmt.Print(pop.GreetingMsg)
15-
fmt.Println(pop.IsAuthorized)
20+
fmt.Print(pop.GreetingMsg()) // Message starts with "+OK"
21+
fmt.Println(pop.IsAuthorized()) // true
22+
fmt.Println(pop.IsEncrypted()) // true
23+
24+
username := os.Getenv(userKey)
25+
u, err := pop.User(username) // USER command
26+
if err != nil {
27+
log.Fatalf(err.Error())
28+
}
29+
fmt.Println(u) // Starts with "+OK"
30+
31+
password := os.Getenv(passwordKey)
32+
p, err := pop.Pass(password) // PASS command
33+
if err != nil {
34+
log.Fatalf(err.Error())
35+
}
36+
fmt.Println(p) // Starts with "+OK"
37+
38+
stat, _ := pop.Stat() // STAT command
39+
fmt.Println(stat) // Starts with "+OK". Returns total msg number and size.
40+
41+
list, _ := pop.List() // LIST command
42+
fmt.Println(list) // Array of message number and size
43+
44+
list, _ = pop.List(1) // LIST <arg> command
45+
fmt.Println(list) // 1st message size
1646
}

pop3/auth.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ type Client struct {
1818
// Addr is POP3 address.
1919
Addr string
2020

21-
// GreetingMsg keeps server response in AUTHORIZATION state.
22-
GreetingMsg string
21+
// greetingMsg keeps server response in AUTHORIZATION state.
22+
greetingMsg string
2323

24-
// IsAuthorized keeps status of AUTHORIZATION state.
25-
IsAuthorized bool
24+
// isAuthorized keeps status of AUTHORIZATION state.
25+
isAuthorized bool
2626

27-
// IsEncrypted stands for whether mail server encrypted with TLS.
28-
IsEncrypted bool
27+
// isEncrypted stands for whether mail server encrypted with TLS.
28+
isEncrypted bool
2929
}
3030

3131
const (
@@ -66,9 +66,9 @@ func connectPOP3TLS(addr string, config *tls.Config) (Client, error) {
6666
c := &Client{
6767
Conn: nil,
6868
Addr: "",
69-
GreetingMsg: "",
70-
IsAuthorized: false,
71-
IsEncrypted: true,
69+
greetingMsg: "",
70+
isAuthorized: false,
71+
isEncrypted: true,
7272
}
7373

7474
tlsConn, err := tls.Dial("tcp", addr, config)
@@ -93,9 +93,9 @@ func connectPOP3(addr string) (Client, error) {
9393
c := &Client{
9494
Conn: nil,
9595
Addr: "",
96-
GreetingMsg: "",
97-
IsAuthorized: false,
98-
IsEncrypted: false,
96+
greetingMsg: "",
97+
isAuthorized: false,
98+
isEncrypted: false,
9999
}
100100

101101
conn, err := net.Dial("tcp", addr)
@@ -138,8 +138,8 @@ func (c *Client) readGreetingMsg() error {
138138
e := "not authorized to POP3 server"
139139
return fmt.Errorf(e)
140140
}
141-
c.GreetingMsg = resp
142-
c.IsAuthorized = true
141+
c.greetingMsg = resp
142+
c.isAuthorized = true
143143

144144
return nil
145145
}
@@ -239,7 +239,26 @@ func (c *Client) readQuitResp() (string, error) {
239239
func (c *Client) changeClientState() {
240240
c.Conn = nil
241241
c.Addr = ""
242-
c.GreetingMsg = ""
243-
c.IsAuthorized = false
242+
c.greetingMsg = ""
243+
c.isAuthorized = false
244244

245245
}
246+
247+
// GreetingMsg returns the greeting message which
248+
// server response when connected to mail server.
249+
// The message is returned in AUTHORIZATION state.
250+
func (c *Client) GreetingMsg() string {
251+
return c.greetingMsg
252+
}
253+
254+
// IsAuthorized returns the information that
255+
// keeps the status of AUTHORIZATION state.
256+
func (c *Client) IsAuthorized() bool {
257+
return c.isAuthorized
258+
}
259+
260+
// IsEncrypted returns the information whether
261+
// the server is encrypted with TLS.
262+
func (c *Client) IsEncrypted() bool {
263+
return c.isEncrypted
264+
}

pop3/auth_test.go

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func TestConnect(t *testing.T) {
3939
t.Errorf(err.Error())
4040
}
4141

42-
if !pop.IsAuthorized {
43-
t.Errorf("Expected: %v, got: %v", true, pop.IsAuthorized)
42+
if !pop.IsAuthorized() {
43+
t.Errorf("Expected: %v, got: %v", true, pop.isAuthorized)
4444
}
4545

4646
if pop.Addr != addr {
@@ -60,8 +60,8 @@ func TestConnectTLS(t *testing.T) {
6060
t.Errorf(err.Error())
6161
}
6262

63-
if !pop.IsAuthorized {
64-
t.Errorf("Expected: %v, got: %v", true, pop.IsAuthorized)
63+
if !pop.IsAuthorized() {
64+
t.Errorf("Expected: %v, got: %v", true, pop.IsAuthorized())
6565
}
6666

6767
if pop.Addr != addr {
@@ -86,8 +86,8 @@ func TestClient_Quit(t *testing.T) {
8686
t.Errorf(err.Error())
8787
}
8888

89-
if pop.IsAuthorized != false {
90-
t.Errorf("Expected c.IsAuthorized %v, got: %v", false, pop.IsAuthorized)
89+
if pop.IsAuthorized() != false {
90+
t.Errorf("Expected c.IsAuthorized %v, got: %v", false, pop.IsAuthorized())
9191
}
9292

9393
if !strings.Contains(got, ok) {
@@ -108,11 +108,55 @@ func TestClientTLS_Quit(t *testing.T) {
108108
t.Errorf(err.Error())
109109
}
110110

111-
if popTLS.IsAuthorized != false {
112-
t.Errorf("expected popTLS.IsAuthorized: %v, got: %v", false, popTLS.IsAuthorized)
111+
if popTLS.IsAuthorized() != false {
112+
t.Errorf("expected popTLS.IsAuthorized: %v, got: %v", false, popTLS.IsAuthorized())
113113
}
114114

115115
if !strings.Contains(got, ok) {
116116
t.Errorf("expected: %s, got: %s", ok, got)
117117
}
118118
}
119+
120+
func TestClient_IsEncrypted(t *testing.T) {
121+
pop, err := Connect("pop.gmail.com:995", nil, true)
122+
if err != nil {
123+
t.Errorf(err.Error())
124+
}
125+
126+
if !pop.IsEncrypted() {
127+
t.Errorf("expected: %v, got: %v", true, pop.IsEncrypted())
128+
}
129+
}
130+
131+
func TestClient_IsNotEncrypted(t *testing.T) {
132+
pop, err := Connect("mail.pop3.com:110", nil, false)
133+
if err != nil {
134+
t.Errorf(err.Error())
135+
}
136+
137+
if pop.IsEncrypted() {
138+
t.Errorf("expected: %v, got: %v", false, pop.IsEncrypted())
139+
}
140+
}
141+
142+
func TestClient_IsAuthorized(t *testing.T) {
143+
pop, err := Connect("mail.pop3.com:110", nil, false)
144+
if err != nil {
145+
t.Errorf(err.Error())
146+
}
147+
148+
if !pop.IsAuthorized() {
149+
t.Errorf("expected: %v, got: %v", true, pop.IsAuthorized())
150+
}
151+
}
152+
153+
func TestClient_GreetingMsg(t *testing.T) {
154+
pop, err := Connect("mail.pop3.com:110", nil, false)
155+
if err != nil {
156+
t.Errorf(err.Error())
157+
}
158+
159+
if !strings.HasPrefix(pop.GreetingMsg(), ok) {
160+
t.Errorf("expected: %s, got: %s", ok, pop.GreetingMsg())
161+
}
162+
}

0 commit comments

Comments
 (0)