Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / silc / core / silc-expandos.c
1 /*
2
3   silc-expandos.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "module.h"
21 #include "misc.h"
22 #include "expandos.h"
23 #include "settings.h"
24
25 #include "chatnets.h"
26 #include "servers-setup.h"
27 #include "channels-setup.h"
28 #include "silc-servers.h"
29 #include "silc-channels.h"
30 #include "silc-queries.h"
31 #include "silc-nicklist.h"
32
33 EXPANDO_FUNC old_expando_usermode,
34             old_expando_cumode,
35             old_expando_cumode_space;
36
37 /* User mode in active server */
38
39 static char *expando_usermode(SERVER_REC *server, void *item, int *free_ret)
40 {
41   SILC_SERVER_REC *s = SILC_SERVER(server);
42   static char modes[128], stat[128];
43   bool se;
44
45   if (!s) {
46     if (old_expando_usermode)
47       return old_expando_usermode(server, item, free_ret);
48     else
49       return "";
50   }
51
52   memset(modes, 0, sizeof(modes));
53   memset(stat, 0, sizeof(stat));
54
55   if (s->umode & SILC_UMODE_GONE)
56     strcat(stat, "g");
57   if (s->umode & SILC_UMODE_INDISPOSED)
58     strcat(stat, "i");
59   if (s->umode & SILC_UMODE_BUSY)
60     strcat(stat, "b");
61   if (s->umode & SILC_UMODE_PAGE)
62     strcat(stat, "p");
63   if (s->umode & SILC_UMODE_HYPER)
64     strcat(stat, "h");
65   if (s->umode & SILC_UMODE_ROBOT)
66     strcat(stat, "t");
67   if (s->umode & SILC_UMODE_ANONYMOUS)
68     strcat(stat, "?");
69   if (s->umode & SILC_UMODE_BLOCK_PRIVMSG)
70     strcat(stat, "P");
71   if (s->umode & SILC_UMODE_REJECT_WATCHING)
72     strcat(stat, "w");
73   if (s->umode & SILC_UMODE_BLOCK_INVITE)
74     strcat(stat, "I");
75
76   se = strlen(stat) > 0;
77   snprintf(modes, sizeof(modes) - 1, "%s%s%s%s",
78            ((s->umode & SILC_UMODE_SERVER_OPERATOR) ? "Server Operator" :
79             (s->umode & SILC_UMODE_ROUTER_OPERATOR) ? "Router Operator" : ""),
80            se ? "[" : "", se ? stat : "", se ? "]" : "");
81
82   return modes;
83 }
84
85 /* Expands to your usermode on channel */
86
87 static char *expando_cumode(SERVER_REC *server, void *item, int *free_ret)
88 {
89   SILC_SERVER_REC *s = SILC_SERVER(server);
90       
91   if (!s) {
92     if (old_expando_cumode)
93       return old_expando_cumode(server, item, free_ret);
94     else
95       return ""; 
96   }
97
98   if (IS_SILC_CHANNEL(item) && CHANNEL(item)->ownnick) {
99     SILC_NICK_REC *nick = (SILC_NICK_REC *)CHANNEL(item)->ownnick;
100     return (nick->op && nick->founder) ? "*@" :
101       nick->op ? "@" : nick->founder ? "*" : "";
102   }
103
104   return "";
105 }
106
107 static char *expando_cumode_space(SERVER_REC *server, void *item, 
108                                   int *free_ret)
109 {
110   SILC_SERVER_REC *s = SILC_SERVER(server);
111   char *ret;
112
113   if (!s) {
114     if (old_expando_cumode_space)
115       return old_expando_cumode_space(server, item, free_ret);   
116     else
117       return "";
118   }
119
120   ret = expando_cumode(server, item, free_ret);
121   return *ret == '\0' ? " " : ret;
122 }
123
124 static char *expando_silc_version(SERVER_REC *server, void *item,
125                                  int *free_ret)
126 {
127   return VERSION;
128 }
129
130 void silc_expandos_init(void)
131 {
132   old_expando_usermode = expando_find_long("usermode");
133   old_expando_cumode = expando_find_long("cumode");
134   old_expando_cumode_space = expando_find_long("cumode_space");
135   expando_create("usermode", expando_usermode,
136                  "window changed", EXPANDO_ARG_NONE,
137                  "window server changed", EXPANDO_ARG_WINDOW,
138                  "user mode changed", EXPANDO_ARG_SERVER, NULL);
139   expando_create("cumode", expando_cumode,
140                  "window changed", EXPANDO_ARG_NONE,
141                  "window item changed", EXPANDO_ARG_WINDOW,
142                  "nick mode changed", EXPANDO_ARG_WINDOW_ITEM, NULL);
143   expando_create("cumode_space", expando_cumode_space,
144                  "window changed", EXPANDO_ARG_NONE,
145                  "window item changed", EXPANDO_ARG_WINDOW,
146                  "nick mode changed", EXPANDO_ARG_WINDOW_ITEM, NULL);
147   expando_create("j", expando_silc_version, NULL);
148 }
149
150 void silc_expandos_deinit(void)
151 {
152   expando_destroy("j", expando_silc_version);
153   expando_destroy("usermode", expando_usermode);
154   expando_destroy("cumode", expando_cumode);
155   expando_destroy("cumode_space", expando_cumode_space);
156   if (old_expando_usermode)
157     expando_create("usermode", old_expando_usermode, NULL);
158   if (old_expando_cumode)
159     expando_create("cumode", old_expando_cumode, NULL);
160   if (old_expando_cumode_space)
161     expando_create("cumode_space", old_expando_cumode_space, NULL);
162 }