summaryrefslogtreecommitdiff
path: root/src/plugins/scripts/ruby/weechat-ruby.c
blob: 52d7b77d001e2e0bc0e9106e2355bd33d083a194 (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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*
 * Copyright (c) 2003-2005 by FlashCode <flashcode@flashtux.org>
 * See README for License detail, AUTHORS for developers list.
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* wee-ruby.c: Ruby plugin support for WeeChat */


#include <ruby.h>

#include <stdlib.h>
#include <string.h>
#undef _
#include "../../weechat-plugin.h"
#include "../weechat-script.h"


char plugin_name[]        = "Ruby";
char plugin_version[]     = "0.1";
char plugin_description[] = "Ruby scripts support";

t_weechat_plugin *ruby_plugin;

t_plugin_script *ruby_scripts = NULL;
t_plugin_script *ruby_current_script = NULL;
char *ruby_current_script_filename = NULL;


/*
 * weechat_ruby_exec: execute a Ruby script
 */

int
weechat_ruby_exec (t_weechat_plugin *plugin,
                   t_plugin_script *script,
                   char *function, char *server, char *arguments)
{
    /* make gcc happy */
    (void) plugin;
    (void) script;
    (void) function;
    (void) server;
    (void) arguments;
    
    /* TODO: exec Ruby script */
    return 0;
}

/*
 * weechat_ruby_handler: general message and command handler for Ruby
 */

int
weechat_ruby_handler (t_weechat_plugin *plugin,
                      char *server, char *command, char *arguments,
                      char *handler_args, void *handler_pointer)
{
    /* make gcc happy */
    (void) command;
    
    weechat_ruby_exec (plugin, (t_plugin_script *)handler_pointer,
                       handler_args, server, arguments);
    return 1;
}

/*
 * weechat_ruby_register: startup function for all WeeChat Ruby scripts
 */

static VALUE
weechat_ruby_register (VALUE class, VALUE name, VALUE version,
                       VALUE shutdown_func, VALUE description)
{
    char *c_name, *c_version, *c_shutdown_func, *c_description;
    
    /* make gcc happy */
    (void) class;
    
    if (NIL_P (name) || NIL_P (version) || NIL_P (shutdown_func) || NIL_P (description))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"register\" function");
        return INT2FIX (0);
    }
    
    Check_Type (name, T_STRING);
    Check_Type (version, T_STRING);
    Check_Type (shutdown_func, T_STRING);
    Check_Type (description, T_STRING);
    
    c_name = STR2CSTR (name);
    c_version = STR2CSTR (version);
    c_shutdown_func = STR2CSTR (shutdown_func);
    c_description = STR2CSTR (description);
    
    if (weechat_script_search (ruby_plugin, &ruby_scripts, c_name))
    {
        /* error: another scripts already exists with this name! */
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to register "
                                    "\"%s\" script (another script "
                                    "already exists with this name)",
                                    c_name);
        return INT2FIX (0);
    }
    
    /* register script */
    ruby_current_script = weechat_script_add (ruby_plugin,
                                              &ruby_scripts,
                                              (ruby_current_script_filename) ?
                                              ruby_current_script_filename : "",
                                              c_name, c_version, c_shutdown_func,
                                              c_description);
    if (ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby: registered script \"%s\", "
                                    "version %s (%s)",
                                    c_name, c_version, c_description);
    }
    else
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to load script "
                                    "\"%s\" (not enough memory)",
                                    c_name);
        return INT2FIX (0);
    }
    
    return INT2FIX (1);
}

/*
 * weechat_ruby_print: print message into a buffer (current or specified one)
 */

static VALUE
weechat_ruby_print (VALUE class, VALUE message, VALUE channel_name,
                    VALUE server_name)
{
    char *c_message, *c_channel_name, *c_server_name;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to print message, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_message = NULL;
    c_channel_name = NULL;
    c_server_name = NULL;
    
    if (NIL_P (message))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"print\" function");
        return INT2FIX (0);
    }
    
    Check_Type (message, T_STRING);
    if (!NIL_P (channel_name))
        Check_Type (channel_name, T_STRING);
    if (!NIL_P (server_name))
        Check_Type (server_name, T_STRING);
    
    c_message = STR2CSTR (message);
    if (!NIL_P (channel_name))
        c_channel_name = STR2CSTR (channel_name);
    if (!NIL_P (server_name))
        c_server_name = STR2CSTR (server_name);
    
    ruby_plugin->printf (ruby_plugin,
                         c_server_name, c_channel_name,
                         "%s", c_message);
    
    return INT2FIX (1);
}

/*
 * weechat_ruby_print_infobar: print message to infobar
 */

static VALUE
weechat_ruby_print_infobar (VALUE class, VALUE delay, VALUE message)
{
    int c_delay;
    char *c_message;

    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to print infobar message, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_delay = 1;
    c_message = NULL;
    
    if (NIL_P (delay) || NIL_P (message))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"print_infobar\" function");
        return INT2FIX (0);
    }
    
    Check_Type (delay, T_FIXNUM);
    Check_Type (message, T_STRING);
    
    c_delay = FIX2INT (delay);
    c_message = STR2CSTR (message);
    
    ruby_plugin->infobar_printf (ruby_plugin, c_delay, c_message);
    
    return INT2FIX (1);
}

/*
 * weechat_ruby_command: send command to server
 */

static VALUE
weechat_ruby_command (VALUE class, VALUE command, VALUE channel_name,
                      VALUE server_name)
{
    char *c_command, *c_channel_name, *c_server_name;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to run command, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_command = NULL;
    c_channel_name = NULL;
    c_server_name = NULL;
    
    if (NIL_P (command))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"command\" function");
        return INT2FIX (0);
    }
    
    Check_Type (command, T_STRING);
    if (!NIL_P (channel_name))
        Check_Type (channel_name, T_STRING);
    if (!NIL_P (server_name))
        Check_Type (server_name, T_STRING);
    
    c_command = STR2CSTR (command);
    if (!NIL_P (channel_name))
        c_channel_name = STR2CSTR (channel_name);
    if (!NIL_P (server_name))
        c_server_name = STR2CSTR (server_name);
    
    ruby_plugin->exec_command (ruby_plugin,
                               c_server_name, c_channel_name,
                               c_command);
    
    return INT2FIX (1);
}

/*
 * weechat_ruby_add_message_handler: add handler for messages
 */

static VALUE
weechat_ruby_add_message_handler (VALUE class, VALUE message, VALUE function)
{
    char *c_message, *c_function;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to add message handler, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_message = NULL;
    c_function = NULL;
    
    if (NIL_P (message) || NIL_P (function))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"add_message_handler\" function");
        return INT2FIX (0);
    }
    
    Check_Type (message, T_STRING);
    Check_Type (function, T_STRING);
    
    c_message = STR2CSTR (message);
    c_function = STR2CSTR (function);
    
    if (ruby_plugin->msg_handler_add (ruby_plugin, c_message,
                                      weechat_ruby_handler, c_function,
                                      (void *)ruby_current_script))
        return INT2FIX (1);
    
    return INT2FIX (0);
}

/*
 * weechat_add_command_handler: define/redefines commands
 */

static VALUE
weechat_ruby_add_command_handler (VALUE class, VALUE command, VALUE function,
                                  VALUE description, VALUE arguments,
                                  VALUE arguments_description)
{
    char *c_command, *c_function, *c_description, *c_arguments;
    char *c_arguments_description;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to add command handler, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_command = NULL;
    c_function = NULL;
    c_description = NULL;
    c_arguments = NULL;
    c_arguments_description = NULL;
    
    if (NIL_P (command) || NIL_P (function))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"add_command_handler\" function");
        return INT2FIX (0);
    }
    
    Check_Type (command, T_STRING);
    Check_Type (function, T_STRING);
    if (!NIL_P (description))
        Check_Type (description, T_STRING);
    if (!NIL_P (arguments))
        Check_Type (arguments, T_STRING);
    if (!NIL_P (arguments_description))
        Check_Type (arguments_description, T_STRING);
    
    c_command = STR2CSTR (command);
    c_function = STR2CSTR (function);
    if (!NIL_P (description))
        c_description = STR2CSTR (description);
    if (!NIL_P (arguments))
        c_arguments = STR2CSTR (arguments);
    if (!NIL_P (arguments_description))
        c_arguments_description = STR2CSTR (arguments_description);
    
    if (ruby_plugin->cmd_handler_add (ruby_plugin,
                                      c_command,
                                      c_description,
                                      c_arguments,
                                      c_arguments_description,
                                      weechat_ruby_handler,
                                      c_function,
                                      (void *)ruby_current_script))
        return INT2FIX (1);
    
    return INT2FIX (0);
}

/*
 * weechat_remove_handler: remove a handler
 */

static VALUE
weechat_ruby_remove_handler (VALUE class, VALUE command, VALUE function)
{
    char *c_command, *c_function;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to remove handler, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_command = NULL;
    c_function = NULL;
    
    if (NIL_P (command) || NIL_P (function))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"remove_handler\" function");
        return INT2FIX (0);
    }
    
    Check_Type (command, T_STRING);
    Check_Type (function, T_STRING);
    
    c_command = STR2CSTR (command);
    c_function = STR2CSTR (function);
    
    weechat_script_remove_handler (ruby_plugin, ruby_current_script,
                                   c_command, c_function);
    
    return INT2FIX (1);
}

/*
 * weechat_ruby_get_info: get various infos
 */

static VALUE
weechat_ruby_get_info (VALUE class, VALUE arg, VALUE server_name,
                       VALUE channel_name)
{
    char *c_arg, *c_server_name, *c_channel_name, *info;
    VALUE return_value;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to get info, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_arg = NULL;
    c_server_name = NULL;
    c_channel_name = NULL;
    
    if (NIL_P (arg))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"get_info\" function");
        return INT2FIX (0);
    }
    
    Check_Type (arg, T_STRING);
    if (!NIL_P (server_name))
        Check_Type (server_name, T_STRING);
    if (!NIL_P (channel_name))
        Check_Type (channel_name, T_STRING);
    
    c_arg = STR2CSTR (arg);
    if (!NIL_P (server_name))
        c_server_name = STR2CSTR (server_name);
    if (!NIL_P (channel_name))
        c_channel_name = STR2CSTR (channel_name);
    
    if (c_arg)
    {
        info = ruby_plugin->get_info (ruby_plugin, c_arg,
                                      c_server_name, c_channel_name);
        
        if (info)
        {
            return_value = rb_str_new2 (info);
            free (info);
            return return_value;
        }
    }
    
    return rb_str_new2 ("");
}

/*
 * weechat_ruby_get_dcc_info: get infos about DCC
 */

static VALUE
weechat_ruby_get_dcc_info (VALUE class)
{
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to get DCC info, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    /* TODO: get dcc info for Ruby */
    return INT2FIX (1);
}

/*
 * weechat_ruby_get_config: get value of a WeeChat config option
 */

static VALUE
weechat_ruby_get_config (VALUE class, VALUE option)
{
    char *c_option, *return_value;
    VALUE ruby_return_value;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to get config option, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_option = NULL;
    
    if (NIL_P (option))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"get_config\" function");
        return INT2FIX (0);
    }
    
    Check_Type (option, T_STRING);
    c_option = STR2CSTR (option);
    
    if (c_option)
    {
        return_value = ruby_plugin->get_config (ruby_plugin, c_option);
        
        if (return_value)
        {
            ruby_return_value = rb_str_new2 (return_value);
            free (return_value);
            return ruby_return_value;
        }
    }
    
    return rb_str_new2 ("");
}

/*
 * weechat_ruby_set_config: set value of a WeeChat config option
 */

static VALUE
weechat_ruby_set_config (VALUE class, VALUE option, VALUE value)
{
    char *c_option, *c_value;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to set config option, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_option = NULL;
    c_value = NULL;
    
    if (NIL_P (option))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"set_config\" function");
        return INT2FIX (0);
    }
    
    Check_Type (option, T_STRING);
    Check_Type (value, T_STRING);
    
    c_option = STR2CSTR (option);
    c_value = STR2CSTR (value);
    
    if (c_option && c_value)
    {
        if (ruby_plugin->set_config (ruby_plugin, c_option, c_value))
            return INT2FIX (1);
    }
    
    return INT2FIX (0);
}

/*
 * weechat_ruby_get_plugin_config: get value of a plugin config option
 */

static VALUE
weechat_ruby_get_plugin_config (VALUE class, VALUE option)
{
    char *c_option, *return_value;
    VALUE ruby_return_value;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to get plugin config option, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_option = NULL;
    
    if (NIL_P (option))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"get_plugin_config\" function");
        return INT2FIX (0);
    }
    
    Check_Type (option, T_STRING);
    c_option = STR2CSTR (option);
    
    if (c_option)
    {
        return_value = weechat_script_get_plugin_config (ruby_plugin,
                                                         ruby_current_script,
                                                         c_option);
        
        if (return_value)
        {
            ruby_return_value = rb_str_new2 (return_value);
            free (return_value);
            return ruby_return_value;
        }
    }
    
    return rb_str_new2 ("");
}

/*
 * weechat_ruby_set_plugin_config: set value of a plugin config option
 */

static VALUE
weechat_ruby_set_plugin_config (VALUE class, VALUE option, VALUE value)
{
    char *c_option, *c_value;
    
    /* make gcc happy */
    (void) class;
    
    if (!ruby_current_script)
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: unable to set plugin config option, "
                                    "script not initialized");
        return INT2FIX (0);
    }
    
    c_option = NULL;
    c_value = NULL;
    
    if (NIL_P (option))
    {
        ruby_plugin->printf_server (ruby_plugin,
                                    "Ruby error: wrong parameters for "
                                    "\"set_plugin_config\" function");
        return INT2FIX (0);
    }
    
    Check_Type (option, T_STRING);
    Check_Type (value, T_STRING);
    
    c_option = STR2CSTR (option);
    c_value = STR2CSTR (value);
    
    if (c_option && c_value)
    {
        if (weechat_script_set_plugin_config (ruby_plugin,
                                              ruby_current_script,
                                              c_option, c_value))
            return INT2FIX (1);
    }
    
    return INT2FIX (0);
}

/*
 * Ruby subroutines
 */

/* TODO: write Ruby functions interface */

/*
 * weechat_ruby_load: load a Ruby script
 */

int
weechat_ruby_load (t_weechat_plugin *plugin, char *filename)
{
    /* make gcc happy */
    (void) plugin;
    (void) filename;
    
    /* TODO: load & exec Ruby script */
    return 0;
}

/*
 * weechat_ruby_unload: unload a Ruby script
 */

void
weechat_ruby_unload (t_weechat_plugin *plugin, t_plugin_script *script)
{
    /* make gcc happy */
    (void) plugin;
    (void) script;
    
    /* TODO: unload a Ruby script */
}

/*
 * weechat_ruby_unload_name: unload a Ruby script by name
 */

void
weechat_ruby_unload_name (t_weechat_plugin *plugin, char *name)
{
    /* make gcc happy */
    (void) plugin;
    (void) name;
    
    /* TODO: unload a Ruby script by name */
}

/*
 * weechat_ruby_unload_all: unload all Ruby scripts
 */

void
weechat_ruby_unload_all (t_weechat_plugin *plugin)
{
    /* make gcc happy */
    (void) plugin;
    
    /* TODO: unload all Ruby scripts */
}

/*
 * weechat_ruby_cmd: /ruby command handler
 */

int
weechat_ruby_cmd (t_weechat_plugin *plugin,
                  char *server, char *command, char *arguments,
                  char *handler_args, void *handler_pointer)
{
    int argc, path_length, handler_found;
    char **argv, *path_script, *dir_home;
    t_plugin_script *ptr_script;
    t_plugin_handler *ptr_handler;
    
    /* make gcc happy */
    (void) server;
    (void) command;
    (void) handler_args;
    (void) handler_pointer;
    
    if (arguments)
        argv = plugin->explode_string (plugin, arguments, " ", 0, &argc);
    else
    {
        argv = NULL;
        argc = 0;
    }
    
    switch (argc)
    {
        case 0:
            /* list registered Ruby scripts */
            plugin->printf_server (plugin, "");
            plugin->printf_server (plugin, "Registered Ruby scripts:");
            if (ruby_scripts)
            {
                for (ptr_script = ruby_scripts;
                     ptr_script; ptr_script = ptr_script->next_script)
                {
                    plugin->printf_server (plugin, "  %s v%s%s%s",
                                           ptr_script->name,
                                           ptr_script->version,
                                           (ptr_script->description[0]) ? " - " : "",
                                           ptr_script->description);
                }
            }
            else
                plugin->printf_server (plugin, "  (none)");
            
            /* list Ruby message handlers */
            plugin->printf_server (plugin, "");
            plugin->printf_server (plugin, "Ruby message handlers:");
            handler_found = 0;
            for (ptr_handler = plugin->handlers;
                 ptr_handler; ptr_handler = ptr_handler->next_handler)
            {
                if ((ptr_handler->type == HANDLER_MESSAGE)
                    && (ptr_handler->handler_args))
                {
                    handler_found = 1;
                    plugin->printf_server (plugin, "  IRC(%s) => Ruby(%s)",
                                           ptr_handler->irc_command,
                                           ptr_handler->handler_args);
                }
            }
            if (!handler_found)
                plugin->printf_server (plugin, "  (none)");
            
            /* list Ruby command handlers */
            plugin->printf_server (plugin, "");
            plugin->printf_server (plugin, "Ruby command handlers:");
            handler_found = 0;
            for (ptr_handler = plugin->handlers;
                 ptr_handler; ptr_handler = ptr_handler->next_handler)
            {
                if ((ptr_handler->type == HANDLER_COMMAND)
                    && (ptr_handler->handler_args))
                {
                    handler_found = 1;
                    plugin->printf_server (plugin, "  /%s => Ruby(%s)",
                                           ptr_handler->command,
                                           ptr_handler->handler_args);
                }
            }
            if (!handler_found)
                plugin->printf_server (plugin, "  (none)");
            break;
        case 1:
            if (plugin->ascii_strcasecmp (plugin, argv[0], "autoload") == 0)
                weechat_script_auto_load (plugin, "ruby", weechat_ruby_load);
            else if (plugin->ascii_strcasecmp (plugin, argv[0], "reload") == 0)
            {
                weechat_ruby_unload_all (plugin);
                weechat_script_auto_load (plugin, "ruby", weechat_ruby_load);
            }
            else if (plugin->ascii_strcasecmp (plugin, argv[0], "unload") == 0)
                weechat_ruby_unload_all (plugin);
            break;
        case 2:
            if (plugin->ascii_strcasecmp (plugin, argv[0], "load") == 0)
            {
                /* load Ruby script */
                if ((strstr (argv[1], "/")) || (strstr (argv[1], "\\")))
                    path_script = NULL;
                else
                {
                    dir_home = plugin->get_info (plugin, "weechat_dir", NULL, NULL);
                    if (dir_home)
                    {
                        path_length = strlen (dir_home) + strlen (argv[1]) + 16;
                        path_script = (char *) malloc (path_length * sizeof (char));
                        if (path_script)
                            snprintf (path_script, path_length, "%s/ruby/%s",
                                      dir_home, argv[1]);
                        else
                            path_script = NULL;
                        free (dir_home);
                    }
                    else
                        path_script = NULL;
                }
                weechat_ruby_load (plugin, (path_script) ? path_script : argv[1]);
                if (path_script)
                    free (path_script);
            }
            else if (plugin->ascii_strcasecmp (plugin, argv[0], "unload") == 0)
            {
                /* unload Ruby script */
                weechat_ruby_unload_name (plugin, argv[1]);
            }
            else
            {
                plugin->printf_server (plugin,
                                       "Ruby error: unknown option for "
                                       "\"ruby\" command");
            }
            break;
        default:
            plugin->printf_server (plugin,
                                   "Ruby error: wrong argument count for \"ruby\" command");
    }
    
    if (argv)
        plugin->free_exploded_string (plugin, argv);
    
    return 1;
}

/*
 * weechat_plugin_init: initialize Ruby plugin
 */

int
weechat_plugin_init (t_weechat_plugin *plugin)
{
    ruby_plugin = plugin;
    
    plugin->printf_server (plugin, "Loading Ruby module \"weechat\"");
    
    /* TODO: initialize Ruby interpreter */
    
    
    plugin->cmd_handler_add (plugin, "ruby",
                             "list/load/unload Ruby scripts",
                             "[load filename] | [autoload] | [reload] | [unload]",
                             "filename: Ruby script (file) to load\n\n"
                             "Without argument, /ruby command lists all loaded Ruby scripts.",
                             weechat_ruby_cmd, NULL, NULL);
    
    plugin->mkdir_home (plugin, "ruby");
    plugin->mkdir_home (plugin, "ruby/autoload");
    
    weechat_script_auto_load (plugin, "ruby", weechat_ruby_load);
    
    return 1;
}

/*
 * weechat_plugin_end: shutdown Ruby interface
 */

void
weechat_plugin_end (t_weechat_plugin *plugin)
{
    /* unload all scripts */
    weechat_ruby_unload_all (plugin);
    
    /* TODO: free interpreter */
    
    
    ruby_plugin->printf_server (ruby_plugin,
                                "Ruby plugin ended");
}