summaryrefslogtreecommitdiff
path: root/notificator.go
blob: d57e93147e1ad7ac947bde949246084200fab18c (plain)
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
// Copyright (C) 2018 LEAP
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"os"
	"path"
	"runtime"
	"time"

	"0xacab.org/leap/go-dialog"
	"github.com/skratchdot/open-golang/open"
)

const (
	donationText = `The %s service is expensive to run. Because we don't want to store personal information about you, there is no accounts or billing for this service. But if you want the service to continue, donate at least $5 each month.
	
Do you want to donate now?`
	aboutText = `%[1]s is an easy, fast, and secure VPN service from riseup.net. %[1]s does not require a user account, keep logs, or track you in any way.
	    
This service paid for entirely by donations from users like you. Please donate at https://riseup.net/vpn/donate.
		
By using this application, you agree to the Terms of Service available at https://riseup.net/tos. This service is provide as-is, without any warranty, and is intended for people who work to make the world a better place.


%[1]v version: %[2]s`
	missingAuthAgent = `Could not find a polkit authentication agent. Please run one and try again.`
	errorStartingVPN = `Can't connect to %s: %v`
	notRunning       = `Is bitmaskd running? Start bitmask and try again.`
	svgFileName      = "riseupvpn.svg"
)

type notificator struct {
	conf *systrayConfig
}

func newNotificator(conf *systrayConfig) *notificator {
	n := notificator{conf}
	go n.donations()
	return &n
}

func (n *notificator) donations() {
	for {
		time.Sleep(time.Hour)
		if n.conf.needsNotification() {
			letsDonate := dialog.Message(printer.Sprintf(donationText, applicationName)).
				Title(printer.Sprintf("Donate")).
				Icon(getIconPath()).
				YesNo()
			n.conf.setNotification()
			if letsDonate {
				open.Run("https://riseup.net/vpn/donate")
				n.conf.setDonated()
			}
		}
	}
}

func (n *notificator) about(version string) {
	dialog.Message(printer.Sprintf(aboutText, applicationName, version)).
		Title(printer.Sprintf("About")).
		Icon(getIconPath()).
		Info()
}

func (n *notificator) bitmaskNotRunning() {
	dialog.Message(printer.Sprintf(notRunning)).
		Title(printer.Sprintf("Can't contact bitmask")).
		Icon(getIconPath()).
		Error()
}

func (n *notificator) authAgent() {
	dialog.Message(printer.Sprintf(missingAuthAgent)).
		Title(printer.Sprintf("Missing authentication agent")).
		Icon(getIconPath()).
		Error()
}

func (n *notificator) errorStartingVPN(err error) {
	dialog.Message(printer.Sprintf(errorStartingVPN, applicationName, err)).
		Title(printer.Sprintf("Error starting VPN")).
		Icon(getIconPath()).
		Error()
}

func getIconPath() string {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		gopath = path.Join(os.Getenv("HOME"), "go")
	}

	if runtime.GOOS == "windows" {
		icoPath := `C:\Program Files\RiseupVPN\riseupvpn.ico`
		if fileExist(icoPath) {
			return icoPath
		}
		icoPath = path.Join(gopath, "src", "0xacab.org", "leap", "riseup_vpn", "assets", "riseupvpn.ico")
		if fileExist(icoPath) {
			return icoPath
		}
		return ""
	}

	if runtime.GOOS == "darwin" {
		icnsPath := "/Applications/RiseupVPN.app/Contents/Resources/app.icns"
		if fileExist(icnsPath) {
			return icnsPath
		}
		icnsPath = path.Join(gopath, "src", "0xacab.org", "leap", "riseup_vpn", "assets", "riseupvpn.icns")
		if fileExist(icnsPath) {
			return icnsPath
		}
		return ""
	}

	snapPath := os.Getenv("SNAP")
	if snapPath != "" {
		return snapPath + "/snap/gui/riseupvpn.svg"
	}

	wd, _ := os.Getwd()
	svgPath := path.Join(wd, svgFileName)
	if fileExist(svgPath) {
		return svgPath
	}

	svgPath = "/usr/share/riseupvpn/riseupvpn.svg"
	if fileExist(svgPath) {
		return svgPath
	}

	svgPath = path.Join(gopath, "src", "0xacab.org", "leap", "bitmask-systray", svgFileName)
	if fileExist(svgPath) {
		return svgPath
	}

	return ""
}

func fileExist(filePath string) bool {
	_, err := os.Stat(filePath)
	return err == nil
}