-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore_helper.go
449 lines (373 loc) · 12.5 KB
/
core_helper.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package purple
/*
// core.c encapse libpurple's core init
#include <libpurple/purple.h>
#include <glib.h>
#include "core_helper.h"
extern PurpleEventLoopUiOps *gopurple_get_loopops();
extern void gopurple_connect_to_signals(void);
*/
import "C"
import "unsafe"
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"github.com/kitech/colog"
)
var purple_debug = false
func init() {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
log.SetFlags(log.Flags() | log.Lshortfile)
colog.Register()
flag.BoolVar(&purple_debug, "purple-debug", purple_debug, "enable purple debug.")
}
type CoreCallbacks struct {
ConnectProgress func(gc *Connection, text string, step int, stepCount int)
Connected func(gc *Connection)
Disconnected func(gc *Connection)
Notice func(gc *Connection, text string)
ReportDisconnect func(gc *Connection, text string)
NetworkConnected func()
NetworkDisconnected func()
ReportDisconnectReason func(gc *Connection, reason int, text string)
}
var ccbs = &Callbacks // CoreCallbacks
type ConnSignals struct {
SignedOn func(gc *Connection /*, data unsafe.Pointer*/)
SignedOff func(gc *Connection)
}
type BlistSignals struct {
BuddySignedOn func(buddy *Buddy)
BuddySignedOff func(buddy *Buddy)
}
type ConvSignals struct {
ReceivedIMMsg func(ac *Account, sender, message string, conv *Conversation, flags int)
ReceivedChatMsg func(ac *Account, sender, message string, conv *Conversation, flags int)
ChatJoined func(conv *Conversation)
ChatLeft func(conv *Conversation)
// PurpleAccount *account, char **sender,
// char **message, PurpleConversation *conv,
// PurpleMessageFlags *flags
ReceivingChatMsg func(ac *Account, sender, message *string, conv *Conversation, flags *int) bool
}
type AccountSignals struct{}
type CoreSignals struct {
ConnSignals
BlistSignals
ConvSignals
AccountSignals
}
var csigs = &Signals // CoreSignals
// public
// usage:
// purple.Signals.xxx = func(){}
var Signals CoreSignals
var Callbacks CoreCallbacks
type PurpleCore struct {
accountUiOps C.PurpleAccountUiOps
connUiOps C.PurpleConnectionUiOps
convUiOps C.PurpleConversationUiOps
loopUiOps C.PurpleEventLoopUiOps
coreUiOps C.PurpleCoreUiOps
loop *C.GMainLoop
ccbs *CoreCallbacks
csigs *CoreSignals
}
func NewPurpleCore() *PurpleCore {
this := &PurpleCore{}
this.setupCore()
return this
}
func (this *PurpleCore) SetCallbacks(cbs CoreCallbacks) {
this.ccbs = &cbs
ccbs = &cbs
}
func (this *PurpleCore) SetSignals(sigs CoreSignals) {
this.csigs = &sigs
csigs = &sigs
}
func (this *PurpleCore) initUiOps() {
if true {
// wow, done
this.connUiOps.connect_progress = (*[0]byte)((unsafe.Pointer)(C.gopurple_connect_progress))
this.connUiOps.connected = (*[0]byte)((unsafe.Pointer)(C.gopurple_connected))
this.connUiOps.disconnected = (*[0]byte)((unsafe.Pointer)(C.gopurple_disconnected))
this.connUiOps.notice = (*[0]byte)((unsafe.Pointer)(C.gopurple_notice))
this.connUiOps.report_disconnect_reason = (*[0]byte)((unsafe.Pointer)(C.gopurple_report_disconnect_reason))
this.connUiOps.report_disconnect = (*[0]byte)((unsafe.Pointer)(C.gopurple_report_disconnect))
this.connUiOps.network_connected = (*[0]byte)((unsafe.Pointer)(C.gopurple_network_connected))
this.connUiOps.network_disconnected = (*[0]byte)((unsafe.Pointer)(C.gopurple_network_disconnected))
// log.Printf("%+v\n", this.connUiOps)
}
if true {
this.accountUiOps.request_authorize = go2cfn(C.gopurple_request_authorize)
}
C.purple_connections_set_ui_ops(&this.connUiOps)
C.purple_accounts_set_ui_ops(&this.accountUiOps)
C.purple_conversations_set_ui_ops(&this.convUiOps)
if false {
this.accountUiOps.request_authorize = nil
}
}
func (this *PurpleCore) initCoreOps() {
if true {
this.loopUiOps.timeout_add = go2cfn(C.g_timeout_add)
this.loopUiOps.timeout_remove = go2cfn(C.g_source_remove)
this.loopUiOps.input_add = nil
this.loopUiOps.input_remove = go2cfn(C.g_source_remove)
this.loopUiOps.input_get_error = nil
this.loopUiOps.timeout_add_seconds = go2cfn(C.g_timeout_add_seconds)
}
C.purple_core_set_ui_ops(&this.coreUiOps)
C.purple_eventloop_set_ui_ops(&this.loopUiOps)
}
func (this *PurpleCore) initLibpurple() {
if false {
home := fmt.Sprintf("%s/%s", os.Getenv("HOME"), CUSTOM_USER_DIRECTORY)
C.purple_util_set_user_dir(CCString(home).Ptr)
C.purple_debug_set_enabled(C.FALSE)
}
C.purple_debug_set_enabled(go2cBool(purple_debug))
// this.initCoreOps()
C.purple_core_set_ui_ops(&this.coreUiOps)
// C.purple_eventloop_set_ui_ops(&this.loopUiOps)
C.purple_eventloop_set_ui_ops(C.gopurple_get_loopops())
C.purple_plugins_add_search_path(CCString(CUSTOM_PLUGIN_PATH).Ptr)
if cok := C.purple_core_init(CCString(UI_ID).Ptr); cok != C.TRUE {
log.Println("libpurple initialization failed. Dumping core." +
"Please report this!\n")
os.Exit(-1)
}
// TODO 是否把这些加载分开到其他函数,这样子灵活性有点差
C.purple_set_blist(C.purple_blist_new())
C.purple_blist_load()
C.purple_prefs_load()
C.purple_plugins_load_saved(CCString(PLUGIN_SAVE_PREF).Ptr)
C.purple_pounces_load()
// 防止出现信号叠加,多次回调通知
if false {
C.gopurple_connect_to_signals() // c version
} else {
this.connect_to_signals() // go version
}
}
func (this *PurpleCore) connect_to_signals() {
ConnectToSignals()
}
func ConnectToSignals() {
if true {
// signals
signalConnect(C.purple_connections_get_handle(), "signed-on",
(unsafe.Pointer)(C.gopurple_signed_on))
signalConnect(C.purple_connections_get_handle(), "signed-off",
(unsafe.Pointer)(C.gopurple_signed_off))
signalConnect(C.purple_blist_get_handle(), "buddy-signed-on",
(unsafe.Pointer)(C.gopurple_buddy_signed_on))
signalConnect(C.purple_blist_get_handle(), "buddy-signed-off",
(unsafe.Pointer)(C.gopurple_buddy_signed_off))
signalConnect(C.purple_conversations_get_handle(), "received-im-msg",
(unsafe.Pointer)(C.gopurple_received_im_msg))
signalConnect(C.purple_conversations_get_handle(), "received-chat-msg",
(unsafe.Pointer)(C.gopurple_received_chat_msg))
signalConnect(C.purple_conversations_get_handle(), "chat-joined",
(unsafe.Pointer)(C.gopurple_chat_joined))
signalConnect(C.purple_conversations_get_handle(), "chat-left",
(unsafe.Pointer)(C.gopurple_chat_left))
signalConnect(C.purple_conversations_get_handle(), "receiving-chat-msg", (unsafe.Pointer)(C.gopurple_receiving_chat_msg))
}
}
// 在这个setup之后,才能够调用purple函数。
func (this *PurpleCore) setupCore() {
this.ccbs = ccbs
this.csigs = csigs
this.loop = C.g_main_loop_new(nil, C.FALSE)
this.initUiOps()
this.initLibpurple()
}
func (this *PurpleCore) MainLoop() {
C.g_main_loop_run(this.loop)
// go func() { C.g_main_loop_run(this.loop) }()
// select {}
}
// get all accout
func (this *PurpleCore) AccountsGetAll() []*Account {
return AccountsGetAll()
}
func (this *PurpleCore) AccountsFind(name string, protocol string) *Account {
return AccountsFind(name, protocol)
}
// callbacks
//export gopurple_request_authorize
func gopurple_request_authorize(ac *C.PurpleAccount, remote_user *C.char,
id *C.char, alias *C.char, message *C.char, on_list C.gboolean,
authorize_cb C.PurpleAccountRequestAuthorizationCb,
deny_cb C.PurpleAccountRequestAuthorizationCb, user_data unsafe.Pointer) unsafe.Pointer {
log.Println("hehhe")
return nil
}
//export gopurple_connect_progress
func gopurple_connect_progress(gc *C.PurpleConnection, text *C.char,
step C.size_t, step_count C.size_t) {
log.Println("hehhe", C.GoString(text), step, step_count)
if ccbs.ConnectProgress != nil {
ccbs.ConnectProgress(newConnectionFrom(gc), C.GoString(text), int(step), int(step_count))
}
}
//export gopurple_connected
func gopurple_connected(gc *C.PurpleConnection) {
log.Println("hehhe")
if ccbs.Connected != nil {
ccbs.Connected(newConnectionFrom(gc))
}
}
//export gopurple_disconnected
func gopurple_disconnected(gc *C.PurpleConnection) {
log.Println("hehhe")
if ccbs.Disconnected != nil {
ccbs.Disconnected(newConnectionFrom(gc))
}
}
//export gopurple_notice
func gopurple_notice(gc *C.PurpleConnection, text *C.char) {
log.Println("hehhe")
if ccbs.Notice != nil {
ccbs.Notice(newConnectionFrom(gc), C.GoString(text))
}
}
//export gopurple_report_disconnect
func gopurple_report_disconnect(gc *C.PurpleConnection, text *C.char) {
log.Println("hehhe", C.GoString(text))
if ccbs.ReportDisconnect != nil {
ccbs.ReportDisconnect(newConnectionFrom(gc), C.GoString(text))
}
}
//export gopurple_network_connected
func gopurple_network_connected() {
log.Println("hehhe")
if ccbs.NetworkConnected != nil {
ccbs.NetworkConnected()
}
}
//export gopurple_network_disconnected
func gopurple_network_disconnected() {
log.Println("hehhe")
if ccbs.NetworkDisconnected != nil {
ccbs.NetworkDisconnected()
}
}
//export gopurple_report_disconnect_reason
func gopurple_report_disconnect_reason(gc *C.PurpleConnection,
reason C.PurpleConnectionError, text *C.char) {
log.Println("hehhe", reason, C.GoString(text))
if ccbs.ReportDisconnectReason != nil {
ccbs.ReportDisconnectReason(newConnectionFrom(gc), int(reason), C.GoString(text))
}
}
// signals
//export gopurple_signed_on
func gopurple_signed_on(gc *C.PurpleConnection, data unsafe.Pointer) {
log.Println("hehhe", gc, data)
if csigs.SignedOn != nil {
csigs.SignedOn(newConnectionFrom(gc))
}
}
//export gopurple_signed_off
func gopurple_signed_off(gc *C.PurpleConnection, data unsafe.Pointer) {
log.Println("hehhe", gc, data)
if csigs.SignedOff != nil {
csigs.SignedOff(newConnectionFrom(gc))
}
}
//export gopurple_buddy_signed_on
func gopurple_buddy_signed_on(buddy *C.PurpleBuddy) {
log.Println("hehhe")
if csigs.BuddySignedOn != nil {
csigs.BuddySignedOn(newBuddyFrom(buddy))
}
}
//export gopurple_buddy_signed_off
func gopurple_buddy_signed_off(buddy *C.PurpleBuddy) {
log.Println("hehhe")
if csigs.BuddySignedOff != nil {
csigs.BuddySignedOff(newBuddyFrom(buddy))
}
}
//export gopurple_buddy_away
func gopurple_buddy_away() { log.Println("hehhe") }
//export gopurple_buddy_idle
func gopurple_buddy_idle() { log.Println("hehhe") }
//export gopurple_buddy_typing
func gopurple_buddy_typing() { log.Println("hehhe") }
//export gopurple_buddy_typed
func gopurple_buddy_typed() { log.Println("hehhe") }
//export gopurple_buddy_typing_stopped
func gopurple_buddy_typing_stopped() { log.Println("hehhe") }
//export gopurple_account_authorization_requested
func gopurple_account_authorization_requested() { log.Println("hehhe") }
//export gopurple_dbus_method_called
func gopurple_dbus_method_called() { log.Println("hehhe") }
//export gopurple_received_im_msg
func gopurple_received_im_msg(ac *C.PurpleAccount, sender *C.char, buffer *C.char,
chat *C.PurpleConversation, flags C.PurpleMessageFlags) {
log.Println("hehhe")
if csigs.ReceivedIMMsg != nil {
csigs.ReceivedIMMsg(newAccountFrom(ac), C.GoString(sender),
C.GoString(buffer), newConversationFrom(chat), int(flags))
}
}
//export gopurple_received_chat_msg
func gopurple_received_chat_msg(ac *C.PurpleAccount, sender *C.char, buffer *C.char,
chat *C.PurpleConversation, flags C.PurpleMessageFlags, data unsafe.Pointer) {
if csigs.ReceivedChatMsg != nil {
csigs.ReceivedChatMsg(newAccountFrom(ac), C.GoString(sender),
C.GoString(buffer), newConversationFrom(chat), int(flags))
}
}
//export gopurple_chat_joined
func gopurple_chat_joined(conv *C.PurpleConversation) {
log.Println("hehhe")
if csigs.ChatJoined != nil {
csigs.ChatJoined(newConversationFrom(conv))
}
}
//export gopurple_chat_left
func gopurple_chat_left(conv *C.PurpleConversation) {
log.Println("hehhe")
if csigs.ChatLeft != nil {
csigs.ChatLeft(newConversationFrom(conv))
}
}
//export gopurple_receiving_chat_msg
func gopurple_receiving_chat_msg(account *C.PurpleAccount, sender **C.char,
message **C.char, conv *C.PurpleConversation, flags *C.PurpleMessageFlags) C.gboolean {
if csigs.ReceivingChatMsg != nil {
sender_go := C.GoString(*sender)
message_go := C.GoString(*message)
var flags_go int = int(*flags)
sender_go_p := &sender_go
message_go_p := &message_go
r := csigs.ReceivingChatMsg(
newAccountFrom(account),
sender_go_p, message_go_p,
newConversationFrom(conv),
&flags_go)
if sender_go_p == nil || message_go_p == nil || r {
C.free((unsafe.Pointer)(*sender))
C.free((unsafe.Pointer)(*message))
return go2cBool(r) // canceled message
}
if !r {
C.free((unsafe.Pointer)(*sender))
C.free((unsafe.Pointer)(*message))
*sender = C.CString(sender_go)
*message = C.CString(message_go)
}
return go2cBool(r)
}
// TRUE if the message should be canceled, or FALSE otherwise.
return go2cBool(false)
}