Debian Codegraph @

src/fe-common/xep/fe-delay.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2009 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include "module.h"
19 #include "levels.h"
20 #include "module-formats.h"
21 #include "printtext.h"
22 #include "settings.h"
23 #include "signals.h"
24 #include "window-items.h"
25 #include "fe-messages.h"
26 #include "fe-queries.h"
27 #include "fe-common/core/module-formats.h"
28 #include "fe-common/core/fe-messages.h"
29 #include "fe-common/irc/module-formats.h"
30
31 #include "xmpp-servers.h"
32 #include "rosters-tools.h"
33 #include "xep/muc.h"
34
35 static void
36 sig_message_delay(SERVER_REC *server, const char *msg, const char *nick,
37 const char *target, time_t *t, gpointer gpointer_type)
38 {
39 void *item;
40 char *text, *freemsg = NULL;
41 char stamp[BUFSIZ];
42 int level, type;
43
44 g_return_if_fail(server != NULL);
45 g_return_if_fail(msg != NULL);
46 g_return_if_fail(nick != NULL);
47 g_return_if_fail(target != NULL);
48
49 type = GPOINTER_TO_INT(gpointer_type);
50 level = MSGLEVEL_NO_ACT | MSGLEVEL_NOHILIGHT
51 | (type == SEND_TARGET_CHANNEL ? MSGLEVEL_PUBLIC : MSGLEVEL_MSGS);
52 item = type == SEND_TARGET_CHANNEL ?
53 (void *)get_muc((XMPP_SERVER_REC *)server, target) :
54 query_find(server, nick);
55
56 if (settings_get_bool("emphasis"))
57 msg = freemsg = expand_emphasis(item, msg);
58
59 /* MUC */
60 if (type == SEND_TARGET_CHANNEL) {
61 CHANNEL_REC *chanrec = item;
62 int print_channel;
63 char *nickmode;
64
65 print_channel = chanrec == NULL ||
66 !window_item_is_active((WI_ITEM_REC *)chanrec);
67 if (!print_channel
68 && settings_get_bool("print_active_channel")
69 && window_item_window((WI_ITEM_REC *)chanrec)->items->next
70 != NULL)
71 print_channel = TRUE;
72 nickmode = channel_get_nickmode(chanrec, nick);
73
74 text = !print_channel ?
75 format_get_text(CORE_MODULE_NAME, NULL, server,
76 target, TXT_PUBMSG, nick, msg, nickmode) :
77 format_get_text(CORE_MODULE_NAME, NULL, server,
78 target, TXT_PUBMSG_CHANNEL, nick, target, msg,
79 nickmode);
80
81 g_free(nickmode);
82
83 /* General */
84 } else
85 text = format_get_text(CORE_MODULE_NAME, NULL, server,
86 target, item == NULL ? TXT_MSG_PRIVATE :
87 TXT_MSG_PRIVATE_QUERY, nick, nick, msg);
88
89 if (strftime(stamp, sizeof(stamp)-1,
90 settings_get_str("xmpp_timestamp_format"), localtime(t)) == 0)
91 stamp[sizeof(stamp)-1] = '\0';
92
93 printformat_module(MODULE_NAME, server, target,
94 level, XMPPTXT_MESSAGE_TIMESTAMP,
95 stamp, text);
96
97 g_free_not_null(freemsg);
98 g_free(text);
99 }
100
101 static void
102 sig_message_delay_action(SERVER_REC *server, const char *msg, const char *nick,
103 const char *target, time_t *t, gpointer gpointer_type)
104 {
105 void *item;
106 char *text, *freemsg = NULL;
107 char stamp[BUFSIZ];
108 int level, type;
109
110 g_return_if_fail(server != NULL);
111 g_return_if_fail(msg != NULL);
112 g_return_if_fail(nick != NULL);
113 g_return_if_fail(target != NULL);
114
115 type = GPOINTER_TO_INT(gpointer_type);
116 level = MSGLEVEL_ACTIONS | MSGLEVEL_NO_ACT | MSGLEVEL_NOHILIGHT
117 | (type == SEND_TARGET_CHANNEL ? MSGLEVEL_PUBLIC : MSGLEVEL_MSGS);
118 item = type == SEND_TARGET_CHANNEL ?
119 (void *)get_muc((XMPP_SERVER_REC *)server, target) :
120 query_find(server, nick);
121
122 if (settings_get_bool("emphasis"))
123 msg = freemsg = expand_emphasis(item, msg);
124
125 /* MUC */
126 if (type == SEND_TARGET_CHANNEL) {
127 if (item && window_item_is_active(item))
128 text = format_get_text(IRC_MODULE_NAME, NULL, server,
129 target, IRCTXT_ACTION_PUBLIC, nick, msg);
130 else
131 text = format_get_text(IRC_MODULE_NAME, NULL, server,
132 target, IRCTXT_ACTION_PUBLIC_CHANNEL, nick,
133 target, msg);
134
135 /* General */
136 } else
137 text = format_get_text(IRC_MODULE_NAME, NULL, server,
138 nick, (item == NULL) ? IRCTXT_ACTION_PRIVATE :
139 IRCTXT_ACTION_PRIVATE_QUERY, nick, nick, msg);
140
141 if (strftime(stamp, sizeof(stamp)-1,
142 settings_get_str("xmpp_timestamp_format"), localtime(t)) == 0)
143 stamp[sizeof(stamp)-1] = '\0';
144
145 printformat_module(MODULE_NAME, server, target, level,
146 XMPPTXT_MESSAGE_TIMESTAMP, stamp, text);
147
148 g_free(freemsg);
149 }
150
151 void
152 fe_delay_init(void)
153 {
154 settings_add_str("xmpp_lookandfeel", "xmpp_timestamp_format",
155 "%Y-%m-%d %H:%M");
156 signal_add("message xmpp delay", sig_message_delay);
157 signal_add("message xmpp delay action", sig_message_delay_action);
158 }
159
160 void
161 fe_delay_deinit(void)
162 {
163 signal_remove("message xmpp delay", sig_message_delay);
164 signal_remove("message xmpp delay action", sig_message_delay_action);
165 }