summary refs log tree commit diff stats
path: root/compiler/ast.nim
Commit message (Collapse)AuthorAgeFilesLines
* Initial version of the hot-code reloading support for native targets (#10729)zah2019-02-261-4/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * squashed work by Zahary * squashing a ton of useful history... otherwise rebasing on top of upstream Nim after commit 82c009a2cbc5d07ab9a847f1c58228a20efaf219 would be impossible. * Code review changes; Working test suite (without code reloading enabled) * - documentation - implemented the HCR test - almost works... - fix the issue on Unix where for executable targets the source file for the main module of a project in nimcache was being overwritten with the binary itself (and thus the actual source code was lost) - fixing embedded paths to shared objects on unix (the "lib" prefix was being prepended to the entire path instead of just the filename) - other fixes - removing unnecessary includes since that file is already included in chcks.nim which is in turn included in system.nim (and previously was getting imported in chcks.nim but then system.nim improts something... and that breaks HCR (perhaps it could be fixed but it would be nice not to import anything in system)) * fix for clang & C++ - explicitly casting a function pointer to void* more stable mangling of parameter names when HCR is on the length of the static arrays in the DatInit functions is now part of the name of the variables, so when they get resized they get also recreated more stable mangling for inline functions - no longer depends on the module which first used them work on the new complicated HCR test - turned surprisingly complex - WIP test now successfully passes even when re-running `koch test` (previously when the nimcache wasn't cold that lead to errors) better documentation calling setStackBottomWith for PreMain passes over the HcrInit/DatInit/Init calls of all modules are now in the proper order (first all of one type, then all of the next). Also typeinfo globals are registered (created) in a single pass before the DatInit pass (because of the way generic instantiations are handled) Fix the test suite execution on macOs fix for being able to query the program arguments when using HCR on posix! other fixes * Bugfix: Fix a compilation error in C++ mode when a function pointer is converted to a raw pointer * basic documentation for the new hot code reloading semantics * Add change log entry * Don't re-execute the top-level statements while reloading JS code * fix a number of tests broken in a recent bugfix * Review changes * Added {.executeOnReload.} pragma that indicates top-level statements that should be executed on each reload. To make this work, I've modified the way the `if (hcr_init_) {...}` guards are produced in the init code. This still needs more work as the new guards seem to be inserted within the previously generated guards. This change also removes the need for `lastRegistedGlobal` in nimhcr. * Implemented the `signatureHash` magic and the `hasModuleChanged` API depending on it (the actual logic is not imlemented yet). * Add the "hcr" prefix to all HCR-related symbols in the system module. Added a new `hotcodereloading` module exporting the high-level API to the user. Besides being more hygienic, this was also required in order to make it possible to use macros in the high-level API. Without the split, `system` would have to import `macros`, which was going to produce the well-known init problems. * Attempted to solve the "GC markers problem". Crashes were expected with the previous code, because the GC markers were compiled as normal procs are registered in the GC. When their module is unloaded, dangling pointers will remain in the GC tables. To solve this issue, I don't register any GC markers when HCR is on, but I add them to the HCR globals metadata and I use a single marker registed in nimhcr during the initialization of the system module that will be responsible for marking all globals. * fix a compilation error * - implemented the hasModuleChanged functionality - tuples can be returned and broken into different vars in global scope - added comments for the closnig scopes of the if statements in the init proc - the new executeOnReload pragma works now! - other fixes * finally! fixing this hack in a proper way - declaring the destructor out of line (out of the class body) - we no longer need to forward-declare popCurrentExceptionEx * Force full module parsing This is a temporary hack that breaks some tests. I'll investigate later how these can be fixed. * tuples are now properly handled when global! * these comments mess up the codegen in debug mode when $n is not actually a new line (or something like that) - these labels are intended only for GOTO labels anyway... * "solved" the issue with the .pdb locks on windows when a binary is being debugged and hot code reloading is used at the same time * fixes after rebasing... * small fixes for the test * better handling of globals! no more compiler crashes for locals with the global pragma, also simplified code around loops in global scope which have local vars (actually globals) * we can now use the global pragma even for ... globals! * the right output * lets try those boehm GC tests * after the test is ran it will be at its starting state - no git modifications * clarification in the docs * removed unnecessary line directives for forward declarations of functions - they were causing trouble with hot code reloading when no semantic change propagates to the main module but a line directive got changed and thus the main module had to be recompiled since the .c code had changed * fixed bug! was inserting duplicate keys into the table and later was removing only 1 copy of all the duplicates (after a few reloads) * no longer breaking into DatInit code when not supposed to * fixes after rebasing * yet more fixes after rebasing * Update jssys.nim * Rework the HCR path-handling logic After reviewing the code more carefully, I've noticed that the old logic will be broken when the user overrides the '--out:f' compiler option. Besides fixing this issues, I took the opportunity to implement the missing '--outdir:d' option. Other changes: * ./koch test won't overwrite any HCR and RTL builds located in nim/lib * HCR and RTL are compiled with --threads:on by default * Clean up the globals registration logic * Handle non-flattened top-level stmtlists in JS as well * The HCR is not supported with the Boehm GC yet Also fixes some typos and the expected output of the HCR integration test * The GC marker procs are now properly used as trampolines * Fix the HCR integration test in release builds * Fix ./koch tools * this forward declaration doesn't seem to be necessary, and in fact breaks HCR because a 2nd function pointer is emitted for this externed/rtl func * the forward declaration I removed in the last commit was actually necessary * Attempt to make all tests green * Fix tgenscript * BAT file for running the HCR integration test on Windows [skip ci] * Fix the docgen tests * A final fix for Travis (hopefully)
* code cleanup: there is no tyOptRefAndreas Rumpf2019-02-221-1/+1
|
* it's spelt callsiteAndreas Rumpf2019-02-221-1/+1
|
* 32 bit fixes (#10608)Arne Döring2019-02-131-1/+8
|
* Implement {.booldefine.} (#10533)Oscar Nihlgård2019-02-031-1/+1
|
* gc: destructors is beginning to work (#10483)Andreas Rumpf2019-01-291-1/+0
| | | | | | | | | | | | | | | | * kochdocs.nim: code cleanup * docgen: nicer indentation * parser.nim: code cleanup * fixes #10458 * make tests green again * make =destroy mixins * gc:destructors: produced C code is almost working * --gc:destructors simple program compiles (but leaks memory) * gc:destructors make examples compile in C++ mode * destructors: string implementation bugfixes * strs.nim: minor code cleanup * destructors: builtin seqs are beginning to work * remove debugging helpers
* add custom pragma support for var and let symbols (#9582)jcosborn2019-01-071-0/+7
| | | | | | * add custom pragma support for var and let symbols * updated changelog for custom pragmas on var and let symbols * add oldast switch for backwards compatibility
* much simpler implementation of constant tuple declarationsAndreas Rumpf2018-12-191-2/+1
|
* Const tuple unpacking (#9964)Neelesh Chandola2018-12-191-1/+2
| | | | | | | | | | * tuple unpacking is now supported for consts * Move nkConstTuple to the end of TNodeKind * Add nnkConstTuple in macros.nim * Fix Formatting
* compiler refactoring: TSymSeq is gone for goodAraq2018-12-111-2/+1
|
* fixes nested gensym'ed parameters; fixes #9476Araq2018-12-081-3/+3
|
* More dead code (#9835)Jacek Sieka2018-12-041-2/+0
| | | | | | * remove more assignment flags * needsComplexAssignment > containsGarbageCollectedRef * remove OnStackShadowDup
* IC: further progressAraq2018-12-011-6/+6
|
* fixes #9675Andreas Rumpf2018-11-261-2/+2
|
* Remove dead code (#9777)Jacek Sieka2018-11-261-1/+0
| | | | | | | | | * gc/gc2: remove unused ref counting stuff * also removes some false threading support - hasSharedHeap is always false in gc/gc2 * gc: remove some threading cruft * remove asgnRefNoCycle * compiler: remove TLoc.dup (unused)
* Add isInstanceOf for generic procs to the macros module (#9730)cooldome2018-11-211-1/+2
|
* compiler cleanup: flag tfOldSchoolExprStmt is goneAraq2018-11-061-3/+0
|
* language cleanup: the .unchecked pragma is deadAraq2018-11-061-1/+0
|
* fixes tnre regressionAraq2018-10-291-1/+1
|
* make tstatic_t_bug compile with the C++ backendAraq2018-10-281-2/+2
|
* Change the order of compilation passes, transformation is made lazy at code ↵cooldome2018-10-181-2/+2
| | | | | | | | | | | | | | | gen (#8489) * Ast no transformation * Add getImplNoTransform to the macros module * progress on delaying transf * Fix methods tranformation * Fix lazy lambdalifting * fix create thread wrapper * transform for lambda lifting * improve getImplTransformed * Fix destructor tests * try to fix nimprof for linux
* Merge branch 'Fixes-7845' of https://github.com/cooldome/Nim into ↵Araq2018-10-151-6/+5
|\ | | | | | | cooldome-Fixes-7845
| * one more attemptcooldome2018-06-281-3/+5
| |
| * Fix failing testcooldome2018-06-261-1/+1
| |
| * Merge branch 'devel' into Fixes-7845cooldome2018-06-261-34/+45
| |\
| * | FIx test tembarrassing_generic_failurecooldome2018-06-121-1/+1
| | |
| * | Fixescooldome2018-06-121-5/+4
| | |
| * | Fixes 7845cooldome2018-06-101-7/+5
| | |
* | | implement sizeof and alignof operator (manually squashed #5664) (#9356)Timothee Cour2018-10-141-7/+3
| | |
* | | compiler: show name of instantiating context in error traces (#6763) (#9207)xzfc2018-10-111-0/+3
| | |
* | | Unchecked arrays now have their own type (#9267)LemonBoy2018-10-101-2/+4
| | |
* | | fixes unexpected transforming of runnableExamples (#9158)Steve Kellock2018-10-091-0/+5
| | |
* | | Try/Catch support for native JS exceptions (#8955)LemonBoy2018-10-091-2/+3
| | | | | | | | | | | | | | | | | | * Try/Catch support for native JS exceptions * Better tests
* | | Add sym owner to macros (#8253)cooldome2018-09-031-1/+1
| | |
* | | fixes #5745Araq2018-09-031-4/+8
| | |
* | | fixes merge conflictAraq2018-08-231-0/+1
|\ \ \
| * | | Fix #8126 (#8712)zah2018-08-211-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | This is a temporary fix that will be reworked in a follow up commit that aims to eliminate the tfExplicit flag from the compiler. The complete and proper fix was considered too risky for inclusion just before our 0.19 release.
* | | | even more strict isNil handling for strings/seqs in order to detect bugsAraq2018-08-221-19/+33
| | | |
* | | | fixes merge conflictAndreas Rumpf2018-08-191-6/+20
|\| | |
| * | | Merge branch 'devel' into araq-miscAndreas Rumpf2018-08-161-0/+8
| |\ \ \
| | * | | Fix unsound transform pass (#8633)LemonBoy2018-08-141-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | When a `var openArray[T]` function parameter goes trough the `transformAddrDeref` pass we may lose the `var` specifier, leading to nasty crashes at runtime.
| * | | | fixes more nil handling regressionsAraq2018-08-131-2/+2
| | | | |
| * | | | WIP: disallow 'nil' for strings and seqsAndreas Rumpf2018-08-131-3/+9
| |/ / /
| * | | fixes #6255, add `system.ashr` arithmetic right shift (#8547)andri lim2018-08-071-1/+1
| | | |
* | | | allows a destructor to be attached to a tyString/tySequenceAndreas Rumpf2018-07-271-3/+3
| | | |
* | | | ast.nim: remove space for unary operatorAndreas Rumpf2018-07-221-1/+1
| | | |
* | | | fix merge conflictAndreas Rumpf2018-07-181-1/+2
|\| | |
| * | | Custom pragmas in proc types (#8205)cooldome2018-07-091-1/+2
| | |/ | |/|
* / | WIP: strings/seqs based on destructorsAndreas Rumpf2018-07-171-0/+1
|/ /
* | allow referencing other parameters in default parameter valuesZahary Karadjov2018-06-161-1/+7
| | | | | | | | | | | | | | | | fix #7756 fix #1201 fix #7000 fix #3002 fix #1046
isanti/profani-tty/commit/src/server_events.c?id=9daefe170d7fa1f50620acdd0a9073b98ba373f9'>9daefe17 ^
56697996 ^
8d2d9236 ^


13a612cb ^
8d2d9236 ^


56697996 ^































13a612cb ^
8944a3b5 ^
56697996 ^


9daefe17 ^
a54518f2 ^


530c060d ^
9ba5a576 ^




8aba52f4 ^
a54518f2 ^
13a612cb ^








8944a3b5 ^
a54518f2 ^


8aba52f4 ^
78beccee ^




8aba52f4 ^
a54518f2 ^
13f0166a ^
c16871d1 ^
13f0166a ^
c16871d1 ^
c16871d1 ^


8aba52f4 ^
c16871d1 ^

13f0166a ^
c16871d1 ^



8aba52f4 ^
c16871d1 ^

13f0166a ^
c16871d1 ^
a54518f2 ^


8aba52f4 ^
a54518f2 ^
13f0166a ^



a54518f2 ^
4bcd766d ^

8aba52f4 ^
c16871d1 ^
13f0166a ^


c16871d1 ^


8aba52f4 ^
4bcd766d ^



ea267e20 ^


4bcd766d ^
ea267e20 ^
4bcd766d ^


ea267e20 ^


4bcd766d ^

ea267e20 ^


4bcd766d ^







8aba52f4 ^
4bcd766d ^
5610f70b ^
4bcd766d ^
4dc90334 ^
c16871d1 ^
4bcd766d ^
36331e49 ^
48ec7b3b ^
5e7d2f5f ^
4bcd766d ^


8ebc1d4a ^
4bcd766d ^
5610f70b ^
4bcd766d ^
a7a28506 ^
a2c62117 ^
4bcd766d ^
36331e49 ^
8ebc1d4a ^





48ec7b3b ^
5e7d2f5f ^
4bcd766d ^


8aba52f4 ^
4bcd766d ^
d25d6b45 ^
d12534c5 ^
4bcd766d ^


8aba52f4 ^
64521eb1 ^
d25d6b45 ^
d12534c5 ^



8aba52f4 ^
d12534c5 ^






8aba52f4 ^
d12534c5 ^


64521eb1 ^


8aba52f4 ^
78a1556f ^





8aba52f4 ^
4bcd766d ^

d25d6b45 ^
2af418fd ^



2af418fd ^
7344d3b8 ^
4dc48b4b ^
4bcd766d ^


8aba52f4 ^
d12534c5 ^



4dc48b4b ^
d12534c5 ^

78a1556f ^
8aba52f4 ^
78a1556f ^



4dc48b4b ^
78a1556f ^
d12534c5 ^

8aba52f4 ^
4e429fe7 ^


48ec7b3b ^
4e429fe7 ^


8aba52f4 ^
bf185d99 ^


4ba33cb1 ^

8aba52f4 ^
67755ca7 ^

ea511f8f ^
0e18b10b ^
6b6ed6a9 ^



0e18b10b ^
ea511f8f ^

d6a4af99 ^

ea511f8f ^


d6a4af99 ^




fac2b2cf ^
714faeb2 ^
fac2b2cf ^
d6a4af99 ^


36157367 ^

74a2d460 ^


36157367 ^

d6a4af99 ^
4dc90334 ^
d6a4af99 ^



4dc90334 ^
d6a4af99 ^
4dc90334 ^
d6a4af99 ^



ea511f8f ^


d6a4af99 ^

ea511f8f ^
ea511f8f ^
6b6ed6a9 ^

fc049c9e ^












6b6ed6a9 ^

0c24b53b ^
4dc48b4b ^
ea511f8f ^


8aba52f4 ^
77674beb ^

ea511f8f ^
77674beb ^








d8eb97ee ^

77674beb ^
ea511f8f ^
d8eb97ee ^

ea511f8f ^
77674beb ^
d8eb97ee ^



4dc48b4b ^
d8eb97ee ^

d6a4af99 ^
77674beb ^

d8eb97ee ^

9134a65e ^
d8eb97ee ^

4dc48b4b ^
d8eb97ee ^


77674beb ^
d8eb97ee ^



ea511f8f ^
d8eb97ee ^
4dc48b4b ^
77674beb ^

8b1d0bdc ^
fc049c9e ^












77674beb ^
4dc48b4b ^
ea511f8f ^
ca30ff39 ^
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644


                  
                                                            















                                                                       











                                                                                

   
                   
                   
 

                   
                         
                
                
                               
                           
                        
                        
 
                  
                    
      


                    
 

                  
    
                                               

                                                              
 
                  
                            
      
 

                                             
                                             
                              
                  
                                                  
                       
                                              





                                                           
                                                        
                          
 
 
    
                           
 


                                         

 
    
                           


                                        
                        

                          


    
                        


                                     

 
    
                                              
                                                        
                                                           
 
                                                          
                                                     
                                        
     


    
                                                

                               
                                        
                                             
            
                                                      
     


    
                                                                                                
 


                                             
     


    
                                                                        


                                                       


    
                                                                        


                                             








                                                                  
                                                                         




                                                    
                                                   

                                                  


    
                                                                   
 







                                                  






















































                                                                                        
                  
                     


                                                                                
                                                                   


                                                         































                                                                         
                                                               
                                      


                                      
      


    
                                                                                           




                                                         
                                                                      
 








                                                                
                                                         


    
                                              




                                    
                                           
 
                                         
                                      
                                                         
     


    
                                           

                                      
                                                         



     
                                             

                                      
                                                           
     


    
                                                                   
 



                                                       
 

    
                                                                                             
 


                                                                      


    
                                                             



                                            


                                                                                                                                   
                                                 
                                         


                             


                                                                  

                               


                                                                    







                          
                                                                  
 
                                                                         
 
                              
                                                      
     
 
                       
                                 


    
                                                                                               
 
                                                                                
 
                  
                                                            
     
 





                                      
                       
                                 


    
                                         
 
                    
                        


    
                                           
 
                    



                          
                                                                                                      






                                                       
                                                                                               


                                        


    
                                                                                               





                                        
                                                                             

                                                       
                                  



                                                                
     
                                       
                                 


    
                                                                                                      



                                                     
                                 

 
    
                                                                                                      



                                                     
                                 
 

    
                                                                        


                                                                          
                       


    
                                         


                          

    
                                                                                                 

                                                                                                                 
 
                                                                     



                                                      
 

                                        

                                             


                                            




                                      
 
                                          
 


                                      

                                                            


                                                  

         
                                          
                      



                                                                 
                                 
                                             
                          



                                                    


                                      

                                                
         
 

                                           












                                                                                                     

         
 
                                 


    
                                                                                                   

                                                                                                                 
 








                                                                 

                                                                                        
                                    
                                     

               
 
                             



                                                                 
                                     

               
 

                    

                                                                    
                                                                               

                                           
                                     


               
                       



                                                                    
         
                                           
                                     

                                                            
            












                                                                                                           
         
                                     
     
 
/*
 * server_events.c
 *
 * Copyright (C) 2012 - 2015 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity 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.
 *
 * Profanity 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 Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include <string.h>
#include <stdlib.h>

#include "config.h"

#include "chat_session.h"
#include "log.h"
#include "muc.h"
#include "config/preferences.h"
#include "config/account.h"
#include "roster_list.h"
#include "window_list.h"

#ifdef HAVE_LIBOTR
#include "otr/otr.h"
#endif
#ifdef HAVE_LIBGPGME
#include "pgp/gpg.h"
#endif

#include "ui/ui.h"

void
sv_ev_login_account_success(char *account_name)
{
    ProfAccount *account = accounts_get_account(account_name);

#ifdef HAVE_LIBOTR
    otr_on_connect(account);
#endif

    ui_handle_login_account_success(account);

    // attempt to rejoin rooms with passwords
    GList *curr = muc_rooms();
    while (curr) {
        char *password = muc_password(curr->data);
        if (password) {
            char *nick = muc_nick(curr->data);
            presence_join_room(curr->data, nick, password);
        }
        curr = g_list_next(curr);
    }
    g_list_free(curr);

    log_info("%s logged in successfully", account->jid);
    account_free(account);
}

void
sv_ev_roster_received(void)
{
    if (prefs_get_boolean(PREF_ROSTER)) {
        ui_show_roster();
    }
}

void
sv_ev_lost_connection(void)
{
    cons_show_error("Lost connection.");
    roster_clear();
    muc_invites_clear();
    chat_sessions_clear();
    ui_disconnected();
}

void
sv_ev_failed_login(void)
{
    cons_show_error("Login failed.");
    log_info("Login failed");
}

void
sv_ev_room_invite(jabber_invite_t invite_type,
    const char * const invitor, const char * const room,
    const char * const reason, const char * const password)
{
    if (!muc_active(room) && !muc_invites_contain(room)) {
        cons_show_room_invite(invitor, room, reason);
        muc_invites_add(room, password);
    }
}

void
sv_ev_room_broadcast(const char *const room_jid,
    const char * const message)
{
    if (muc_roster_complete(room_jid)) {
        ui_room_broadcast(room_jid, message);
    } else {
        muc_pending_broadcasts_add(room_jid, message);
    }
}

void
sv_ev_room_subject(const char * const room, const char * const nick, const char * const subject)
{
    muc_set_subject(room, subject);
    if (muc_roster_complete(room)) {
        ui_room_subject(room, nick, subject);
    }
}

void
sv_ev_room_history(const char * const room_jid, const char * const nick,
    GTimeVal tv_stamp, const char * const message)
{
    ui_room_history(room_jid, nick, tv_stamp, message);
}

void
sv_ev_room_message(const char * const room_jid, const char * const nick,
    const char * const message)
{
    ui_room_message(room_jid, nick, message);

    if (prefs_get_boolean(PREF_GRLOG)) {
        Jid *jid = jid_create(jabber_get_fulljid());
        groupchat_log_chat(jid->barejid, room_jid, nick, message);
        jid_destroy(jid);
    }
}

void
sv_ev_incoming_private_message(const char * const fulljid, char *message)
{
    ui_incoming_private_msg(fulljid, message, NULL);
}

void
sv_ev_outgoing_carbon(char *barejid, char *message)
{
    ui_outgoing_chat_msg_carbon(barejid, message);
}

void
sv_ev_incoming_carbon(char *barejid, char *resource, char *message)
{
    gboolean new_win = FALSE;
    ProfChatWin *chatwin = wins_get_chat(barejid);
    if (!chatwin) {
        ProfWin *window = wins_new_chat(barejid);
        chatwin = (ProfChatWin*)window;
        new_win = TRUE;
    }

    ui_incoming_msg(chatwin, resource, message, NULL, new_win);
    chat_log_msg_in(barejid, message);
}

void
sv_ev_incoming_message(char *barejid, char *resource, char *message, char *enc_message)
{
    gboolean new_win = FALSE;
    ProfChatWin *chatwin = wins_get_chat(barejid);
    if (!chatwin) {
        ProfWin *window = wins_new_chat(barejid);
        chatwin = (ProfChatWin*)window;
        new_win = TRUE;
    }

// OTR suported, PGP supported
#ifdef HAVE_LIBOTR
#ifdef HAVE_LIBGPGME
    prof_enc_t enc_mode = chatwin->enc_mode;
    if (enc_message) {
        if (enc_mode == PROF_ENC_OTR) {
            // TODO show error
        } else { // PROF_ENC_NONE, PROF_ENC_PGP
            char *decrypted = p_gpg_decrypt(barejid, enc_message);
            if (decrypted) {
                ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win);
                // TODO pgp message logger
                chat_log_msg_in(barejid, decrypted);
                chatwin->enc_mode = PROF_ENC_PGP;
            } else {
                ui_incoming_msg(chatwin, resource, message, NULL, new_win);
                chat_log_msg_in(barejid, message);
                chatwin->enc_mode = PROF_ENC_NONE;
            }
        }
    } else {
        if (enc_mode == PROF_ENC_PGP) {
            ui_incoming_msg(chatwin, resource, message, NULL, new_win);
            chat_log_msg_in(barejid, message);
            chatwin->enc_mode = PROF_ENC_NONE;
        } else {
            gboolean decrypted = FALSE;
            char *otr_res = otr_on_message_recv(barejid, resource, message, &decrypted);
            if (otr_res) {
                ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win);
                chat_log_otr_msg_in(barejid, otr_res, decrypted);
                otr_free_message(otr_res);
            }
        }
    }
    return;
#endif
#endif

// OTR supported, PGP unsupported
#ifdef HAVE_LIBOTR
#ifndef HAVE_LIBGPGME
    gboolean decrypted = FALSE;
    char *otr_res = otr_on_message_recv(barejid, resource, message, &decrypted);
    if (otr_res) {
        ui_incoming_msg(chatwin, resource, otr_res, NULL, new_win);
        chat_log_otr_msg_in(barejid, otr_res, decrypted);
        otr_free_message(otr_res);
    }
    return;
#endif
#endif

// OTR unsupported, PGP supported
#ifndef HAVE_LIBOTR
#ifdef HAVE_LIBGPGME
    prof_enc_t enc_mode = chatwin->enc_mode;
    if (enc_message) {
        char *decrypted = p_gpg_decrypt(jid->barejid, enc_message);
        if (decrypted) {
            ui_incoming_msg(chatwin, resource, decrypted, NULL, new_win);
            // TODO pgp message logger
            chat_log_msg_in(barejid, decrypted);
            chatwin->enc_mode = PROF_ENC_PGP;
        } else {
            ui_incoming_msg(chatwin, resource, message, NULL, new_win);
            chat_log_msg_in(barejid, message);
            chatwin->enc_mode = PROF_ENC_NONE;
        }
    } else {
        ui_incoming_msg(chatwin, resource, message, NULL, new_win);
        chat_log_msg_in(barejid, message);
        chatwin->enc_mode = PROF_ENC_NONE;
    }
    return;
#endif
#endif

// OTR unsupported, PGP unsupported
#ifndef HAVE_LIBOTR
#ifndef HAVE_LIBGPGME
    ui_incoming_msg(chatwin, resource, message, NULL, new_win);
    chat_log_msg_in(barejid, message);
    chatwin->enc_mode = PROF_ENC_NONE;
    return;
#endif
#endif
}

void
sv_ev_delayed_private_message(const char * const fulljid, char *message, GTimeVal tv_stamp)
{
    ui_incoming_private_msg(fulljid, message, &tv_stamp);
}

void
sv_ev_delayed_message(char *barejid, char *message, GTimeVal tv_stamp)
{
    gboolean new_win = FALSE;
    ProfChatWin *chatwin = wins_get_chat(barejid);
    if (!chatwin) {
        ProfWin *window = wins_new_chat(barejid);
        chatwin = (ProfChatWin*)window;
        new_win = TRUE;
    }

    ui_incoming_msg(chatwin, NULL, message, &tv_stamp, new_win);
    chat_log_msg_in_delayed(barejid, message, &tv_stamp);
}

void
sv_ev_message_receipt(char *barejid, char *id)
{
    ui_message_receipt(barejid, id);
}

void
sv_ev_typing(char *barejid, char *resource)
{
    ui_contact_typing(barejid, resource);
    if (ui_chat_win_exists(barejid)) {
        chat_session_recipient_typing(barejid, resource);
    }
}

void
sv_ev_paused(char *barejid, char *resource)
{
    if (ui_chat_win_exists(barejid)) {
        chat_session_recipient_paused(barejid, resource);
    }
}

void
sv_ev_inactive(char *barejid, char *resource)
{
    if (ui_chat_win_exists(barejid)) {
        chat_session_recipient_inactive(barejid, resource);
    }
}

void
sv_ev_gone(const char * const barejid, const char * const resource)
{
    ui_recipient_gone(barejid, resource);
    if (ui_chat_win_exists(barejid)) {
        chat_session_recipient_gone(barejid, resource);
    }
}

void
sv_ev_activity(const char * const barejid, const char * const resource, gboolean send_states)
{
    if (ui_chat_win_exists(barejid)) {
        chat_session_recipient_active(barejid, resource, send_states);
    }
}

void
sv_ev_subscription(const char *barejid, jabber_subscr_t type)
{
    switch (type) {
    case PRESENCE_SUBSCRIBE:
        /* TODO: auto-subscribe if needed */
        cons_show("Received authorization request from %s", barejid);
        log_info("Received authorization request from %s", barejid);
        ui_print_system_msg_from_recipient(barejid, "Authorization request, type '/sub allow' to accept or '/sub deny' to reject");
        if (prefs_get_boolean(PREF_NOTIFY_SUB)) {
            notify_subscription(barejid);
        }
        break;
    case PRESENCE_SUBSCRIBED:
        cons_show("Subscription received from %s", barejid);
        log_info("Subscription received from %s", barejid);
        ui_print_system_msg_from_recipient(barejid, "Subscribed");
        break;
    case PRESENCE_UNSUBSCRIBED:
        cons_show("%s deleted subscription", barejid);
        log_info("%s deleted subscription", barejid);
        ui_print_system_msg_from_recipient(barejid, "Unsubscribed");
        break;
    default:
        /* unknown type */
        break;
    }
}

void
sv_ev_contact_offline(char *barejid, char *resource, char *status)
{
    gboolean updated = roster_contact_offline(barejid, resource, status);

    if (resource && updated) {
        ui_contact_offline(barejid, resource, status);
    }

    rosterwin_roster();
    chat_session_remove(barejid);
}

void
sv_ev_contact_online(char *barejid, Resource *resource, GDateTime *last_activity, char *pgpsig)
{
    gboolean updated = roster_update_presence(barejid, resource, last_activity);

    if (updated) {
        ui_contact_online(barejid, resource, last_activity);
    }

#ifdef HAVE_LIBGPGME
    if (pgpsig) {
        p_gpg_verify(barejid, pgpsig);
    }
#endif

    rosterwin_roster();
    chat_session_remove(barejid);
}

void
sv_ev_leave_room(const char * const room)
{
    muc_leave(room);
    ui_leave_room(room);
}

void
sv_ev_room_destroy(const char * const room)
{
    muc_leave(room);
    ui_room_destroy(room);
}

void
sv_ev_room_destroyed(const char * const room, const char * const new_jid, const char * const password,
    const char * const reason)
{
    muc_leave(room);
    ui_room_destroyed(room, reason, new_jid, password);
}

void
sv_ev_room_kicked(const char * const room, const char * const actor, const char * const reason)
{
    muc_leave(room);
    ui_room_kicked(room, actor, reason);
}

void
sv_ev_room_banned(const char * const room, const char * const actor, const char * const reason)
{
    muc_leave(room);
    ui_room_banned(room, actor, reason);
}

void
sv_ev_room_occupant_offline(const char * const room, const char * const nick,
    const char * const show, const char * const status)
{
    muc_roster_remove(room, nick);

    char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC);
    if (g_strcmp0(muc_status_pref, "none") != 0) {
        ui_room_member_offline(room, nick);
    }
    prefs_free_string(muc_status_pref);
    occupantswin_occupants(room);
}

void
sv_ev_room_occupent_kicked(const char * const room, const char * const nick, const char * const actor,
    const char * const reason)
{
    muc_roster_remove(room, nick);
    ui_room_member_kicked(room, nick, actor, reason);
    occupantswin_occupants(room);
}

void
sv_ev_room_occupent_banned(const char * const room, const char * const nick, const char * const actor,
    const char * const reason)
{
    muc_roster_remove(room, nick);
    ui_room_member_banned(room, nick, actor, reason);
    occupantswin_occupants(room);
}

void
sv_ev_roster_update(const char * const barejid, const char * const name,
    GSList *groups, const char * const subscription, gboolean pending_out)
{
    roster_update(barejid, name, groups, subscription, pending_out);
    rosterwin_roster();
}

void
sv_ev_xmpp_stanza(const char * const msg)
{
    ui_handle_stanza(msg);
}

void
sv_ev_muc_self_online(const char * const room, const char * const nick, gboolean config_required,
    const char * const role, const char * const affiliation, const char * const actor, const char * const reason,
    const char * const jid, const char * const show, const char * const status)
{
    muc_roster_add(room, nick, jid, role, affiliation, show, status);
    char *old_role = muc_role_str(room);
    char *old_affiliation = muc_affiliation_str(room);
    muc_set_role(room, role);
    muc_set_affiliation(room, affiliation);

    // handle self nick change
    if (muc_nick_change_pending(room)) {
        muc_nick_change_complete(room, nick);
        ui_room_nick_change(room, nick);

    // handle roster complete
    } else if (!muc_roster_complete(room)) {
        if (muc_autojoin(room)) {
            ui_room_join(room, FALSE);
        } else {
            ui_room_join(room, TRUE);
        }

        iq_room_info_request(room, FALSE);

        muc_invites_remove(room);
        muc_roster_set_complete(room);

        // show roster if occupants list disabled by default
        if (!prefs_get_boolean(PREF_OCCUPANTS)) {
            GList *occupants = muc_roster(room);
            ui_room_roster(room, occupants, NULL);
            g_list_free(occupants);
        }

        char *subject = muc_subject(room);
        if (subject) {
            ui_room_subject(room, NULL, subject);
        }

        GList *pending_broadcasts = muc_pending_broadcasts(room);
        if (pending_broadcasts) {
            GList *curr = pending_broadcasts;
            while (curr) {
                ui_room_broadcast(room, curr->data);
                curr = g_list_next(curr);
            }
        }

        // room configuration required
        if (config_required) {
            muc_set_requires_config(room, TRUE);
            ui_room_requires_config(room);
        }

    // check for change in role/affiliation
    } else {
        if (prefs_get_boolean(PREF_MUC_PRIVILEGES)) {
            // both changed
            if ((g_strcmp0(role, old_role) != 0) && (g_strcmp0(affiliation, old_affiliation) != 0)) {
                ui_room_role_and_affiliation_change(room, role, affiliation, actor, reason);

            // role changed
            } else if (g_strcmp0(role, old_role) != 0) {
                ui_room_role_change(room, role, actor, reason);

            // affiliation changed
            } else if (g_strcmp0(affiliation, old_affiliation) != 0) {
                ui_room_affiliation_change(room, affiliation, actor, reason);
            }
        }
    }

    occupantswin_occupants(room);
}

void
sv_ev_muc_occupant_online(const char * const room, const char * const nick, const char * const jid,
    const char * const role, const char * const affiliation, const char * const actor, const char * const reason,
    const char * const show, const char * const status)
{
    Occupant *occupant = muc_roster_item(room, nick);

    const char *old_role = NULL;
    const char *old_affiliation = NULL;
    if (occupant) {
        old_role = muc_occupant_role_str(occupant);
        old_affiliation = muc_occupant_affiliation_str(occupant);
    }

    gboolean updated = muc_roster_add(room, nick, jid, role, affiliation, show, status);

    // not yet finished joining room
    if (!muc_roster_complete(room)) {
        return;
    }

    // handle nickname change
    char *old_nick = muc_roster_nick_change_complete(room, nick);
    if (old_nick) {
        ui_room_member_nick_change(room, old_nick, nick);
        free(old_nick);
        occupantswin_occupants(room);
        return;
    }

    // joined room
    if (!occupant) {
        char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC);
        if (g_strcmp0(muc_status_pref, "none") != 0) {
            ui_room_member_online(room, nick, role, affiliation, show, status);
        }
        prefs_free_string(muc_status_pref);
        occupantswin_occupants(room);
        return;
    }

    // presence updated
    if (updated) {
        char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC);
        if (g_strcmp0(muc_status_pref, "all") == 0) {
            ui_room_member_presence(room, nick, show, status);
        }
        prefs_free_string(muc_status_pref);
        occupantswin_occupants(room);

    // presence unchanged, check for role/affiliation change
    } else {
        if (prefs_get_boolean(PREF_MUC_PRIVILEGES)) {
            // both changed
            if ((g_strcmp0(role, old_role) != 0) && (g_strcmp0(affiliation, old_affiliation) != 0)) {
                ui_room_occupant_role_and_affiliation_change(room, nick, role, affiliation, actor, reason);

            // role changed
            } else if (g_strcmp0(role, old_role) != 0) {
                ui_room_occupant_role_change(room, nick, role, actor, reason);

            // affiliation changed
            } else if (g_strcmp0(affiliation, old_affiliation) != 0) {
                ui_room_occupant_affiliation_change(room, nick, affiliation, actor, reason);
            }
        }
        occupantswin_occupants(room);
    }
}