1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// TODO: Something about redialing.
package google
import (
// "flag"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/rpc"
"os"
"os/exec"
"syscall"
"time"
)
func Dir() string {
dir := os.Getenv("HOME") + "/.goog"
st, err := os.Stat(dir)
if err != nil {
if err := os.Mkdir(dir, 0700); err != nil {
log.Fatal(err)
}
st, err = os.Stat(dir)
if err != nil {
log.Fatal(err)
}
}
if !st.IsDir() {
log.Fatalf("%s exists but is not a directory", dir)
}
if st.Mode()&0077 != 0 {
log.Fatalf("%s exists but allows group or other permissions: %#o", dir, st.Mode()&0777)
}
return dir
}
func Dial() (*Client, error) {
socket := Dir() + "/socket"
c, err := net.Dial("unix", socket)
if err == nil {
return &Client{rpc.NewClient(c)}, nil
}
log.Print("starting server")
os.Remove(socket)
runServer()
for i := 0; i < 50; i++ {
c, err = net.Dial("unix", socket)
if err == nil {
return &Client{rpc.NewClient(c)}, nil
}
time.Sleep(200e6)
if i == 0 {
log.Print("waiting for server...")
}
}
return nil, err
}
type Client struct {
client *rpc.Client
}
type Empty struct{}
func (g *Client) Ping() error {
return g.client.Call("goog.Ping", &Empty{}, &Empty{})
}
func (g *Client) Accounts() ([]string, error) {
var out []string
if err := g.client.Call("goog.Accounts", &Empty{}, &out); err != nil {
return nil, err
}
return out, nil
}
func runServer() {
cmd := exec.Command("googleserver", "serve")
cmd.SysProcAttr = &syscall.SysProcAttr{}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
}
type Config struct {
Account []*Account
}
type Account struct {
Email string
Password string
Nick string
}
func (cfg *Config) AccountByEmail(email string) *Account {
for _, a := range cfg.Account {
if a.Email == email {
return a
}
}
return nil
}
var Cfg Config
func ReadConfig() {
file := Dir() + "/config"
st, err := os.Stat(file)
if err != nil {
return
}
if st.Mode()&0077 != 0 {
log.Fatalf("%s exists but allows group or other permissions: %#o", file, st.Mode()&0777)
}
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
Cfg = Config{}
if err := json.Unmarshal(data, &Cfg); err != nil {
log.Fatal(err)
}
}
func WriteConfig() {
file := Dir() + "/config"
st, err := os.Stat(file)
if err != nil {
if err := ioutil.WriteFile(file, nil, 0600); err != nil {
log.Fatal(err)
}
st, err = os.Stat(file)
if err != nil {
log.Fatal(err)
}
}
if st.Mode()&0077 != 0 {
log.Fatalf("%s exists but allows group or other permissions: %#o", file, st.Mode()&0777)
}
data, err := json.MarshalIndent(&Cfg, "", "\t")
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(file, data, 0600); err != nil {
log.Fatal(err)
}
st, err = os.Stat(file)
if err != nil {
log.Fatal(err)
}
if st.Mode()&0077 != 0 {
log.Fatalf("%s allows group or other permissions after writing: %#o", file, st.Mode()&0777)
}
}
func Acct(name string) Account {
ReadConfig()
if name == "" {
if len(Cfg.Account) == 0 {
fmt.Fprintf(os.Stderr, "no accounts configured\n")
os.Exit(2)
}
return *Cfg.Account[0]
}
for _, a := range Cfg.Account {
if a.Email == name || a.Nick == name {
return *a
}
}
fmt.Fprintf(os.Stderr, "cannot find account %#q", name)
os.Exit(2)
panic("not reached")
}
|