File: muc-nicklist.c

package info (click to toggle)
irssi-plugin-xmpp 0.54-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,952 kB
  • sloc: ansic: 8,831; makefile: 113; perl: 58
file content (181 lines) | stat: -rw-r--r-- 4,582 bytes parent folder | download
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 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 <string.h>

#include "module.h"
#include "signals.h"

#include "rosters.h"
#include "muc-affiliation.h"
#include "muc-nicklist.h"
#include "muc-role.h"

XMPP_NICK_REC *
xmpp_nicklist_insert(MUC_REC *channel, const char *nickname,
    const char *full_jid)
{
	XMPP_NICK_REC *rec;

	g_return_val_if_fail(IS_MUC(channel), NULL);
	g_return_val_if_fail(nickname != NULL, NULL);
	rec = g_new0(XMPP_NICK_REC, 1);
	rec->nick = g_strdup(nickname);
	rec->host = (full_jid != NULL) ?
	    g_strdup(full_jid) : g_strconcat(channel->name, "/", rec->nick, (void *)NULL);
	rec->show = XMPP_PRESENCE_AVAILABLE;
	rec->status = NULL;
	rec->affiliation = XMPP_AFFILIATION_NONE;
	rec->role = XMPP_ROLE_NONE;
	nicklist_insert(CHANNEL(channel), (NICK_REC *)rec);
	return rec;
}

static void
nick_hash_add(CHANNEL_REC *channel, NICK_REC *nick)
{
	NICK_REC *list;

	nick->next = NULL;
	list = g_hash_table_lookup(channel->nicks, nick->nick);
        if (list == NULL)
		g_hash_table_insert(channel->nicks, nick->nick, nick);
	else {
                /* multiple nicks with same name */
		while (list->next != NULL)
			list = list->next;
		list->next = nick;
	}
	/* move our own nick to beginning of the nick list.. */
	if (nick == channel->ownnick)
		nicklist_set_own(channel, nick);
}

static void
nick_hash_remove(CHANNEL_REC *channel, NICK_REC *nick)
{
	NICK_REC *list;

	list = g_hash_table_lookup(channel->nicks, nick->nick);
	if (list == NULL)
		return;
	if (list == nick || list->next == NULL) {
		g_hash_table_remove(channel->nicks, nick->nick);
		if (list->next != NULL)
			g_hash_table_insert(channel->nicks, nick->next->nick,
			    nick->next);
	} else {
		while (list->next != nick)
			list = list->next;
		list->next = nick->next;
	}
}

void
xmpp_nicklist_rename(MUC_REC *channel, XMPP_NICK_REC *nick,
    const char *oldnick, const char *newnick)
{

	g_return_if_fail(IS_MUC(channel));
	g_return_if_fail(IS_XMPP_NICK(nick));
	g_return_if_fail(oldnick != NULL);
	g_return_if_fail(newnick != NULL);
	/* remove old nick from hash table */
	nick_hash_remove(CHANNEL(channel), NICK(nick));
	g_free(nick->nick);
	nick->nick = g_strdup(newnick);
	/* add new nick to hash table */
	nick_hash_add(CHANNEL(channel), NICK(nick));
	signal_emit("nicklist changed", 3, channel, nick, oldnick);
	if (strcmp(oldnick, channel->nick) == 0) {
		nicklist_set_own(CHANNEL(channel), NICK(nick));
		g_free(channel->nick);
		channel->nick = g_strdup(newnick);
	}
}

gboolean
xmpp_nicklist_modes_changed(XMPP_NICK_REC *nick, int affiliation, int role)
{
	g_return_val_if_fail(IS_XMPP_NICK(nick), FALSE);
	return nick->affiliation != affiliation || nick->role != role;
}

void
xmpp_nicklist_set_modes(XMPP_NICK_REC *nick, int affiliation, int role)
{
	g_return_if_fail(IS_XMPP_NICK(nick));

	nick->affiliation = affiliation;
	nick->role = role;
	switch (affiliation) {
	case XMPP_AFFILIATION_OWNER:
		nick->prefixes[0] = '&';
		nick->prefixes[1] = '\0';
		nick->op = TRUE;
		break;
	case XMPP_AFFILIATION_ADMIN:
		nick->prefixes[0] = '\0';
		nick->op = TRUE;
		break;
	default:
		nick->prefixes[0] = '\0';
		nick->op = FALSE;
	}
	switch (role) {
	case XMPP_ROLE_MODERATOR:
		nick->voice = TRUE;
		nick->halfop = TRUE;
		break;
	case XMPP_ROLE_PARTICIPANT:
		nick->halfop = FALSE;
		nick->voice = TRUE;
		break;
	default:
		nick->halfop = FALSE;
		nick->voice = FALSE;
	}
}

void
xmpp_nicklist_set_presence(XMPP_NICK_REC *nick, int show, const char *status)
{
	g_return_if_fail(IS_XMPP_NICK(nick));
	nick->show = show;
	g_free(nick->status);
	nick->status = g_strdup(status);
}

static void
sig_nicklist_remove(MUC_REC *channel, XMPP_NICK_REC *nick)
{
	if (!IS_MUC(channel) || !IS_XMPP_NICK(nick))
		return;
	g_free(nick->status);
}

void
muc_nicklist_init(void)
{
	signal_add("nicklist remove", sig_nicklist_remove);
}

void
muc_nicklist_deinit(void)
{
	signal_remove("nicklist remove", sig_nicklist_remove);
}