summary refs log tree commit diff stats
path: root/src/fe-gtk/plugin-notification.c
blob: 3ac89244590a714918b3e74067d4d0e957061840 (plain) (blame)
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
/* HexChat
 * Copyright (C) 2015 Patrick Griffis.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include <glib.h>

#include "../common/hexchat-plugin.h"
#include "../common/inbound.h" /* For alert_match_word() */
#include "notifications/notification-backend.h"

static hexchat_plugin *ph;

static gboolean
should_alert (void)
{
	int omit_away, omit_focused, omit_tray;

	if (hexchat_get_prefs (ph, "gui_focus_omitalerts", NULL, &omit_focused) == 3 && omit_focused)
	{
		const char *status = hexchat_get_info (ph, "win_status");

		if (status && !g_strcmp0 (status, "active"))
			return FALSE;
	}

	if (hexchat_get_prefs (ph, "away_omit_alerts", NULL, &omit_away) == 3 && omit_away)
	{
		if (hexchat_get_info (ph, "away"))
			return FALSE;
	}

	if (hexchat_get_prefs (ph, "gui_tray_quiet", NULL, &omit_tray) == 3 && omit_tray)
	{
		int tray_enabled;

		if (hexchat_get_prefs (ph, "gui_tray", NULL, &tray_enabled) == 3 && tray_enabled)
		{
			const char *status = hexchat_get_info (ph, "win_status");

			if (status && g_strcmp0 (status, "hidden") != 0)
				return FALSE;
		}
	}

	return TRUE;
}

static gboolean
is_ignored (char *nick)
{
	const char *no_hilight;

	if (hexchat_get_prefs (ph, "irc_no_hilight", &no_hilight, NULL) == 1 && no_hilight)
	{
		return alert_match_word (nick, (char*)no_hilight);
	}
	return FALSE;
}

static void
show_notification (const char *title, const char *text)
{
	char *stripped_title, *stripped_text;

	/* Strip all colors */
	stripped_title = hexchat_strip (ph, title, -1, 7);
	stripped_text = hexchat_strip (ph, text, -1, 7);
	
	notification_backend_show (stripped_title, stripped_text);

	hexchat_free (ph, stripped_title);
	hexchat_free (ph, stripped_text);
}

static void
show_notificationf (const char *text, const char *format, ...)
{
	va_list args;
	char *buf;

	va_start (args, format);
	buf = g_strdup_vprintf (format, args);
	va_end (args);

	show_notification (buf, text);
	g_free (buf);
}

static int
incoming_hilight_cb (char *word[], gpointer userdata)
{
	int hilight;

	if (hexchat_get_prefs (ph, "input_balloon_hilight", NULL, &hilight) == 3 && hilight && should_alert())
	{
		show_notificationf (word[2], _("Highlighted message from: %s (%s)"), word[1], hexchat_get_info (ph, "channel"));
	}
	return HEXCHAT_EAT_NONE;
}

static int
incoming_message_cb (char *word[], gpointer userdata)
{
	int message;

	if (hexchat_get_prefs (ph, "input_balloon_chans", NULL, &message) == 3 && message && should_alert ())
	{
		show_notificationf (word[2], _("Channel message from: %s (%s)"), word[1], hexchat_get_info (ph, "channel"));
	}
	return HEXCHAT_EAT_NONE;
}

static int
incoming_priv_cb (char *word[], gpointer userdata)
{
	int priv;

	if (hexchat_get_prefs (ph, "input_balloon_priv", NULL, &priv) == 3 && priv && should_alert ())
	{
		const char *network = hexchat_get_info (ph, "network");
		if (!network)
			network = hexchat_get_info (ph, "server");

		if (userdata != NULL) /* Special event */
		{
			if (GPOINTER_TO_INT (userdata) == 3)
			{
				if (!is_ignored (word[2]))
					show_notificationf (word[1], _("File offer from: %s (%s)"), word[2], network);
			}
			else if (GPOINTER_TO_INT (userdata) == 2)
			{
				if (!is_ignored (word[2]))
					show_notificationf (word[1], _("Invited to channel by: %s (%s)"), word[2], network);
			}
			else
			{
				if (!is_ignored (word[1]))
					show_notificationf (word[2], _("Notice from: %s (%s)"), word[1], network);
			}
		}
		else
			show_notificationf (word[2], _("Private message from: %s (%s)"), word[1], network);
	}
	return HEXCHAT_EAT_NONE;
}

static int
tray_cmd_cb (char *word[], char *word_eol[], gpointer userdata)
{
	if (word[2] && !g_ascii_strcasecmp (word[2], "-b") && word[3] && word[4])
	{
		if (should_alert ())
			show_notification (word[3], word_eol[4]);
		return HEXCHAT_EAT_ALL;
	}

	return HEXCHAT_EAT_NONE;
}

int
notification_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
{
	const char* error = NULL;

	ph = plugin_handle;
	*plugin_name = "";
	*plugin_desc = "";
	*plugin_version = "";

	if (!notification_backend_init (&error))
	{
		if (error)
			hexchat_printf(plugin_handle, "Failed loading notification plugin: %s\n", error);
		return 0;
	}

	hexchat_hook_print (ph, "Channel Msg Hilight", HEXCHAT_PRI_LOWEST, incoming_hilight_cb, NULL);
	hexchat_hook_print (ph, "Channel Action Hilight", HEXCHAT_PRI_LOWEST, incoming_hilight_cb, NULL);

	hexchat_hook_print (ph, "Channel Message", HEXCHAT_PRI_LOWEST, incoming_message_cb, NULL);
	hexchat_hook_print (ph, "Channel Action", HEXCHAT_PRI_LOWEST, incoming_message_cb, NULL);
	hexchat_hook_print (ph, "Channel Notice", HEXCHAT_PRI_LOWEST, incoming_message_cb, NULL);

	hexchat_hook_print (ph, "Private Message", HEXCHAT_PRI_LOWEST, incoming_priv_cb, NULL);
	hexchat_hook_print (ph, "Private Message to Dialog", HEXCHAT_PRI_LOWEST, incoming_priv_cb, NULL);
	hexchat_hook_print (ph, "Private Action", HEXCHAT_PRI_LOWEST, incoming_priv_cb, NULL);
	hexchat_hook_print (ph, "Private Action to Dialog", HEXCHAT_PRI_LOWEST, incoming_priv_cb, NULL);

	/* Special events treated as priv */
	hexchat_hook_print (ph, "Notice", HEXCHAT_PRI_LOWEST, incoming_priv_cb, GINT_TO_POINTER (1));
	hexchat_hook_print (ph, "Invited", HEXCHAT_PRI_LOWEST, incoming_priv_cb, GINT_TO_POINTER (2));
	hexchat_hook_print (ph, "DCC Offer", HEXCHAT_PRI_LOWEST, incoming_priv_cb, GINT_TO_POINTER (3));

	hexchat_hook_command (ph, "TRAY", HEXCHAT_PRI_HIGH, tray_cmd_cb, NULL, NULL);
	
	return 1;
}


int
notification_plugin_deinit (void)
{
	notification_backend_deinit ();
	return 1;
}