/
agent.go
130 lines (106 loc) · 3.72 KB
/
agent.go
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
/*
agent.go - Prototype notification agent
Copyright (c) 2015, 2017 Jim Fenton
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/jimfenton/notif-agent/notif"
_ "github.com/lib/pq"
"io/ioutil"
"os"
)
type AgentDbCfg struct {
Host string `json:"host"`
User string `json:"user"`
Dbname string `json:"dbname"`
Password string `json:"password"`
}
// Find an user record by ID
func findUser(db *sql.DB, userID int, user *notif.Userinfo) error {
var twilioSID sql.NullString
var twilioToken sql.NullString
var twilioFrom sql.NullString
err := db.QueryRow(`SELECT id,email_username,email_server,email_port,email_authentication,email_security,twilio_sid,twilio_token,twilio_from,count,latest,created,user_id FROM userext WHERE user_id = $1`, userID).Scan(&user.Id,
&user.EmailUsername,
&user.EmailServer,
&user.EmailPort,
&user.EmailAuthentication,
&user.EmailSecurity,
&twilioSID,
&twilioToken,
&twilioFrom,
&user.Count,
&user.Latest,
&user.Created,
&user.UserID)
user.TwilioSID = twilioSID.String
user.TwilioToken = twilioToken.String
user.TwilioFrom = twilioFrom.String
return err
}
func findSite(db *sql.DB, site *notif.Siteinfo) error {
var twilioSID sql.NullString
var twilioToken sql.NullString
var twilioFrom sql.NullString
err := db.QueryRow(`SELECT twilio_sid,twilio_token,twilio_from FROM site`).Scan(&twilioSID,
&twilioToken,
&twilioFrom)
site.TwilioSID = twilioSID.String
site.TwilioToken = twilioToken.String
site.TwilioFrom = twilioFrom.String
return err
}
func main() {
var user notif.Userinfo
var site notif.Siteinfo
var adc AgentDbCfg
dat, err := ioutil.ReadFile("/etc/notifs/agent.cfg") //keeps passwords out of source code
err = json.Unmarshal(dat, &adc)
if err != nil {
fmt.Println("DB config unmarshal error:", err)
os.Exit(1)
}
// Database parameters are stored in JSON form in /etc/notifs/agent.cfg
// Sample configuration:
// {"host":"localhost","dbname":"notifs","user":"notifs","password":"whatever"}
db, err := sql.Open("postgres", fmt.Sprintf("user=%s dbname=%s host=%s password=%s", adc.User, adc.Dbname, adc.Host, adc.Password))
if err != nil {
fmt.Println("Can't connect to database:", err)
os.Exit(1)
}
defer db.Close()
//Collect site configuration info
err = findSite(db, &site)
if err != nil {
fmt.Println("Can't retrieve site configuration info:", err) // non-fatal for now at least
}
// Channel for notif collectors
cc := make(chan notif.Notif, 10)
go collectNative(db, cc) //Listen for native notifs
for notif := range cc {
err := findUser(db, notif.UserID, &user)
if err != nil {
fmt.Println("Can't retrieve user info for push:", err) // non-fatal
} else {
ProcessRules(notif, db, user, site)
}
}
}