-
Notifications
You must be signed in to change notification settings - Fork 81
/
main.go
162 lines (129 loc) · 3.13 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"runtime/pprof"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/coyim/coyim/config"
"github.com/coyim/coyim/gui"
"github.com/coyim/coyim/gui/settings"
"github.com/coyim/coyim/i18n"
"github.com/coyim/coyim/sasl"
"github.com/coyim/coyim/session"
"github.com/coyim/coyim/xmpp"
"github.com/coyim/gotk3adapter/gdka"
"github.com/coyim/gotk3adapter/gliba"
"github.com/coyim/gotk3adapter/gtka"
"github.com/coyim/gotk3adapter/pangoa"
)
var coyimVersion = "<UNSET>"
// BuildCommit contains which commit the build was based on
var BuildCommit = "UNKNOWN"
// BuildShortCommit contains which commit in short format the build was based on
var BuildShortCommit = "UNKNOWN"
// BuildTag contains which tag - if any - the build was based on
var BuildTag = "(no tag)"
// BuildTimestamp contains the timestamp in GMT when the build was made
var BuildTimestamp = "UNKNOWN"
func init() {
mainInit()
}
func mainInit() {
if BuildTag == "(no tag)" || BuildTag == "" {
coyimVersion = BuildCommit
} else {
coyimVersion = BuildTag
}
}
var logFile *os.File
func initLogFile(name string) {
var err error
logFile, err = os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.WithError(err).WithField("file", name).Error("Couldn't open file for logging")
return
}
log.SetOutput(logFile)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
logFile.Close()
os.Exit(1)
}()
}
func initLog() {
log.SetLevel(log.InfoLevel)
if *config.DebugFlag {
log.SetLevel(log.DebugLevel)
}
if *config.TraceFlag {
log.SetLevel(log.TraceLevel)
}
if *config.LogFile != "" {
initLogFile(*config.LogFile)
}
log.SetReportCaller(*config.DebugFunctionCalls)
}
func printVersion() {
fmt.Printf("CoyIM version %s (commit: %v built at: %v)\n", coyimVersion, BuildShortCommit, BuildTimestamp)
}
var osCreate = os.Create
func startProfileIfNecessary() {
if *config.CPUProfile != "" {
f, err := osCreate(*config.CPUProfile)
if err != nil {
log.WithError(err).Fatal("could not create CPU profile")
}
if err := pprof.StartCPUProfile(f); err != nil {
log.WithError(err).Fatal("could not start CPU profile")
}
}
}
func stopProfileIfNecessary() {
if *config.CPUProfile != "" {
pprof.StopCPUProfile()
}
}
func printFinalNewline() {
_, _ = os.Stdout.Write([]byte("\n"))
}
func main() {
flag.Parse()
if *config.VersionFlag {
printVersion()
return
}
sasl.Debug = *config.DebugFlag
startProfileIfNecessary()
defer stopProfileIfNecessary()
initLog()
log.WithField("version", coyimVersion).Info("Welcome to CoyIM!")
initTranslations()
runClient()
printFinalNewline()
if logFile != nil {
logFile.Close()
}
}
type looper interface {
Loop()
}
var createGTK = func(g gui.Graphics) looper {
return gui.NewGTK(coyimVersion, session.Factory, xmpp.DialerFactory, g, hooks(), translationsDirectory())
}
func runClient() {
g := gui.CreateGraphics(
gtka.Real,
gliba.Real,
gdka.Real,
pangoa.Real,
extraGraphics,
)
i18n.InitLocalization(gliba.Real)
settings.InitSettings(gliba.Real)
createGTK(g).Loop()
}