anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

memos.cpp (2799B)

      1 /* MemoServ functions.
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #include "services.h"
     13 #include "modules.h"
     14 #include "service.h"
     15 #include "memo.h"
     16 #include "users.h"
     17 #include "account.h"
     18 #include "regchannel.h"
     19 
     20 Memo::Memo() : Serializable("Memo")
     21 {
     22 	mi = NULL;
     23 	unread = receipt = false;
     24 }
     25 
     26 Memo::~Memo()
     27 {
     28 	if (mi)
     29 	{
     30 		std::vector<Memo *>::iterator it = std::find(mi->memos->begin(), mi->memos->end(), this);
     31 
     32 		if (it != mi->memos->end())
     33 			mi->memos->erase(it);
     34 	}
     35 }
     36 
     37 void Memo::Serialize(Serialize::Data &data) const
     38 {
     39 	data["owner"] << this->owner;
     40 	data.SetType("time", Serialize::Data::DT_INT); data["time"] << this->time;
     41 	data["sender"] << this->sender;
     42 	data["text"] << this->text;
     43 	data["unread"] << this->unread;
     44 	data["receipt"] << this->receipt;
     45 }
     46 
     47 Serializable* Memo::Unserialize(Serializable *obj, Serialize::Data &data)
     48 {
     49 	Anope::string owner;
     50 
     51 	data["owner"] >> owner;
     52 
     53 	bool ischan;
     54 	MemoInfo *mi = MemoInfo::GetMemoInfo(owner, ischan);
     55 	if (!mi)
     56 		return NULL;
     57 
     58 	Memo *m;
     59 	if (obj)
     60 		m = anope_dynamic_static_cast<Memo *>(obj);
     61 	else
     62 	{
     63 		m = new Memo();
     64 		m->mi = mi;
     65 	}
     66 
     67 	m->owner = owner;
     68 	data["time"] >> m->time;
     69 	data["sender"] >> m->sender;
     70 	data["text"] >> m->text;
     71 	data["unread"] >> m->unread;
     72 	data["receipt"] >> m->receipt;
     73 
     74 	if (obj == NULL)
     75 		mi->memos->push_back(m);
     76 	return m;
     77 }
     78 
     79 MemoInfo::MemoInfo() : memomax(0), memos("Memo")
     80 {
     81 }
     82 
     83 Memo *MemoInfo::GetMemo(unsigned index) const
     84 {
     85 	if (index >= this->memos->size())
     86 		return NULL;
     87 	Memo *m = (*memos)[index];
     88 	m->QueueUpdate();
     89 	return m;
     90 }
     91 
     92 unsigned MemoInfo::GetIndex(Memo *m) const
     93 {
     94 	for (unsigned i = 0; i < this->memos->size(); ++i)
     95 		if (this->GetMemo(i) == m)
     96 			return i;
     97 	return -1;
     98 }
     99 
    100 void MemoInfo::Del(unsigned index)
    101 {
    102 	if (index >= this->memos->size())
    103 		return;
    104 
    105 	Memo *m = this->GetMemo(index);
    106 
    107 	std::vector<Memo *>::iterator it = std::find(memos->begin(), memos->end(), m);
    108 	if (it != memos->end())
    109 		memos->erase(it);
    110 
    111 	delete m;
    112 }
    113 
    114 bool MemoInfo::HasIgnore(User *u)
    115 {
    116 	for (unsigned i = 0; i < this->ignores.size(); ++i)
    117 		if (u->nick.equals_ci(this->ignores[i]) || (u->Account() && u->Account()->display.equals_ci(this->ignores[i])) || Anope::Match(u->GetMask(), Anope::string(this->ignores[i])))
    118 			return true;
    119 	return false;
    120 }
    121 
    122 MemoInfo *MemoInfo::GetMemoInfo(const Anope::string &target, bool &ischan)
    123 {
    124 	if (!target.empty() && target[0] == '#')
    125 	{
    126 		ischan = true;
    127 		ChannelInfo *ci = ChannelInfo::Find(target);
    128 		if (ci != NULL)
    129 			return &ci->memos;
    130 	}
    131 	else
    132 	{
    133 		ischan = false;
    134 		NickAlias *na = NickAlias::Find(target);
    135 		if (na != NULL)
    136 			return &na->nc->memos;
    137 	}
    138 
    139 	return NULL;
    140 }