File: protocol.c

package info (click to toggle)
irssi-plugin-xmpp 0.53-1~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,540 kB
  • sloc: ansic: 8,300; makefile: 108; perl: 58
file content (181 lines) | stat: -rw-r--r-- 5,327 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2007,2008,2009 Colin DIDIER
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "module.h"
#include "channels.h"
#include "signals.h"
#include "settings.h"

#include "xmpp-servers.h"
#include "rosters-tools.h"
#include "tools.h"
#include "xep/disco.h"

char *pgp_passwd = NULL;

static void
sig_set_presence(XMPP_SERVER_REC *server, const int show, const char *status,
    const int priority)
{
	LmMessage *lmsg;
	char *str;
	const char *pgp_keyid;

	g_return_if_fail(IS_XMPP_SERVER(server));
	if (!xmpp_presence_changed(show, server->show, status,
	    server->away_reason, priority, server->priority)) {
		signal_stop();
		return;
	}

	lmsg = lm_message_new(NULL, LM_MESSAGE_TYPE_PRESENCE);
	server->show = show;

	if (!xmpp_priority_out_of_bound(priority))
		server->priority = priority;

	if (show != XMPP_PRESENCE_AVAILABLE)
		lm_message_node_add_child(lmsg->node, "show",
		    xmpp_presence_show[server->show]);

	if(server->away_reason) g_free(server->away_reason);
	server->away_reason = NULL;

	if(!status) status = "";
	server->away_reason = g_strdup(status);
	str = xmpp_recode_out(server->away_reason);
	lm_message_node_add_child(lmsg->node, "status", str);
	if(!str) str = g_strdup("");

	if((pgp_keyid = settings_get_str("xmpp_pgp"))) {
		LmMessageNode *x;
		char *signature = call_gpg("-ab", str, NULL, 0, 1);
		disco_add_feature("jabber:x:signed");
		disco_add_feature("jabber:x:encrypted");

		if(signature) {
			x = lm_message_node_add_child(lmsg->node, "x", signature);
			lm_message_node_set_attribute(x, "xmlns", "jabber:x:signed");

			free(signature);
		}
	}

	g_free(str);

	str = g_strdup_printf("%d", server->priority);
	lm_message_node_add_child(lmsg->node, "priority", str);
	g_free(str);

	signal_emit("xmpp send presence", 2, server, lmsg);
	lm_message_unref(lmsg);
	if (show != XMPP_PRESENCE_AVAILABLE) /* away */
		signal_emit("event 306", 2, server, server->jid);
	else if (server->usermode_away) /* unaway */
		signal_emit("event 305", 2, server, server->jid);
}

static void
sig_recv_message(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
    const char *id, const char *from, const char *to)
{
	LmMessageNode *node, *encrypted;
	char *str, *subject;
	const char *from_stripped;

	/* assing from_stripped. If setting xmpp_strip_resource is
	   True and from is not from a channel private message set
	   from_stripped to same as from, but strip the
	   resource. Otherwise just duplicate from to
	   from_stripped. */
	str = xmpp_strip_resource(from);
	from_stripped =
		((settings_get_bool("xmpp_strip_resource")) &&
		 (channel_find(SERVER(server), str)) == NULL) ?
		g_strdup(str) : g_strdup(from);
	g_free(str);
	
	if ((type != LM_MESSAGE_SUB_TYPE_NOT_SET
	    && type != LM_MESSAGE_SUB_TYPE_HEADLINE
	    && type != LM_MESSAGE_SUB_TYPE_NORMAL
	    && type != LM_MESSAGE_SUB_TYPE_CHAT)
	    || server->ischannel(SERVER(server), from))
		return;
	node = lm_message_node_get_child(lmsg->node, "subject");
	if (node != NULL && node->value != NULL && *node->value != '\0') {
		str = xmpp_recode_in(node->value);
		subject = g_strconcat("Subject: ", str, (void *)NULL);
		g_free(str);
		signal_emit("message private", 4, server, subject, from_stripped, from);
		g_free(subject);
	}

	str = NULL;
	encrypted = lm_find_node(lmsg->node, "x", "xmlns", "jabber:x:encrypted");
	if(encrypted && encrypted->value) {
		/* TODO: indicate the message was encrypted */
		/* TODO: verify signatures */
		char *send_to_gpg = malloc(sizeof( \
			"-----BEGIN PGP MESSAGE-----\n\n" \
			"-----END PGP MESSAGE-----\n")+ \
			strlen(encrypted->value)+1 \
		);
		char *from_gpg;

		send_to_gpg[0] = '\0';
		strcat(send_to_gpg, "-----BEGIN PGP MESSAGE-----\n\n");
		strcat(send_to_gpg, encrypted->value);
		strcat(send_to_gpg, "-----END PGP MESSAGE-----\n");

		from_gpg = call_gpg("-d", send_to_gpg, NULL, 0, 0);
		if(from_gpg) {
			str = xmpp_recode_in(from_gpg);
			free(from_gpg);
		}

		free(send_to_gpg);
	} else {
		node = lm_message_node_get_child(lmsg->node, "body");
		if (node != NULL && node->value != NULL && *node->value != '\0') {
			str = xmpp_recode_in(node->value);
		}
	}
	if(str) {
		if (g_ascii_strncasecmp(str, "/me ", 4) == 0)
			signal_emit("message xmpp action", 5,
			    server, str+4, from_stripped, from,
			    GINT_TO_POINTER(SEND_TARGET_NICK));
		else
			signal_emit("message private", 4, server,
			    str, from_stripped, from);
		g_free(str);
	}
}

void
protocol_init(void)
{
	signal_add_first("xmpp set presence", sig_set_presence);
	signal_add("xmpp recv message", sig_recv_message);
}

void
protocol_deinit(void)
{
	signal_remove("xmpp set presence", sig_set_presence);
	signal_remove("xmpp recv message", sig_recv_message);
}