1 /**
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * irc/cache_entry.cc
8 * (c) 2008 Murat Deligonul
9 */
10
11 #include "autoconf.h"
12
13 #include <list>
14 #include <vector>
15 #include <algorithm>
16 #include <functional>
17 #include "irc/cache_entry.h"
18 #include "irc/channel.h"
19 #include "debug.h"
20
21 namespace irc {
22
23 /**
24 * Update nickname for a cache_entry.
25 * This requires going through all referenced channels,
26 * finding nickname entry in hash table and updating pointer of
27 * old key to new one.
28 */
change_nick(const char * new_nick)29 void cache_entry::change_nick(const char * new_nick)
30 {
31 DEBUG("acache::change_nick(): updating old channel pointers of '%s'->'%s' [%zd channels]\n",
32 addr.nick(), new_nick, channels.size());
33 std::vector<int> flags;
34 std::list<channel *>::const_iterator iter = channels.begin(),
35 e = channels.end();
36
37 while (iter != e) {
38 channel * c = *iter;
39 flags.push_back(c->nick_flags(this));
40 c->del_nick(this);
41 ++iter;
42 }
43
44 addr.set_nick(new_nick);
45
46 int i = 0;
47 iter = channels.begin();
48 for (; iter != e; ++iter) {
49 (*iter)->add_nick(this, flags[i++]);
50 }
51 }
52
53 /**
54 * Destructor:
55 * Remove all references to this cache_entry
56 */
~cache_entry()57 cache_entry::~cache_entry()
58 {
59 DEBUG("cache_entry::~cache_entry() [%p]\n", this);
60 std::for_each(channels.begin(), channels.end(),
61 std::bind2nd(std::mem_fun(&channel::del_nick), this));
62 }
63
64 } // namespace irc
65