summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/services.h5
-rw-r--r--src/channels.c8
-rw-r--r--src/chanserv.c57
-rw-r--r--src/memoserv.c50
-rw-r--r--src/nickserv.c57
5 files changed, 85 insertions, 92 deletions
diff --git a/include/services.h b/include/services.h
index 7bab47f98..ad67f1eb4 100644
--- a/include/services.h
+++ b/include/services.h
@@ -230,6 +230,7 @@ extern int strncasecmp(const char *, const char *, size_t);
#include <map>
#include <exception>
#include <list>
+#include <vector>
/** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
* When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
@@ -570,8 +571,8 @@ struct memo_ {
};
typedef struct {
- int16 memocount, memomax;
- Memo *memos;
+ int16 memomax;
+ std::vector<Memo *> memos;
} MemoInfo;
diff --git a/src/channels.c b/src/channels.c
index a91ae2b0f..392e155f5 100644
--- a/src/channels.c
+++ b/src/channels.c
@@ -1538,13 +1538,13 @@ void chan_adduser2(User * user, Channel * c)
if (get_ignore(user->nick) == NULL) {
if (c->ci && (check_access(user, c->ci, CA_MEMO))
- && (c->ci->memos.memocount > 0)) {
- if (c->ci->memos.memocount == 1) {
+ && (c->ci->memos.memos.size() > 0)) {
+ if (c->ci->memos.memos.size() == 1) {
notice_lang(s_MemoServ, user, MEMO_X_ONE_NOTICE,
- c->ci->memos.memocount, c->ci->name);
+ c->ci->memos.memos.size(), c->ci->name);
} else {
notice_lang(s_MemoServ, user, MEMO_X_MANY_NOTICE,
- c->ci->memos.memocount, c->ci->name);
+ c->ci->memos.memos.size(), c->ci->name);
}
}
/* Added channelname to entrymsg - 30.03.2004, Certus */
diff --git a/src/chanserv.c b/src/chanserv.c
index 2751a24c1..2f748cdd2 100644
--- a/src/chanserv.c
+++ b/src/chanserv.c
@@ -233,10 +233,10 @@ void get_chanserv_stats(long *nrec, long *memuse)
mem += strlen(ci->forbidreason) + 1;
if (ci->levels)
mem += sizeof(*ci->levels) * CA_SIZE;
- mem += ci->memos.memocount * sizeof(Memo);
- for (j = 0; j < ci->memos.memocount; j++) {
- if (ci->memos.memos[j].text)
- mem += strlen(ci->memos.memos[j].text) + 1;
+ mem += ci->memos.memos.size() * sizeof(Memo);
+ for (j = 0; j < ci->memos.memos.size(); j++) {
+ if (ci->memos.memos[j]->text)
+ mem += strlen(ci->memos.memos[j]->text) + 1;
}
if (ci->ttb)
mem += sizeof(*ci->ttb) * TTB_SIZE;
@@ -468,20 +468,19 @@ void load_cs_dbase(void)
}
SAFE(read_int16(&tmp16, f));
- ci->memos.memocount = static_cast<int16>(tmp16);
+ if (tmp16) ci->memos.memos.resize(tmp16);
SAFE(read_int16(&tmp16, f));
ci->memos.memomax = static_cast<int16>(tmp16);
- if (ci->memos.memocount) {
- Memo *memos;
- memos = static_cast<Memo *>(scalloc(sizeof(Memo) * ci->memos.memocount, 1));
- ci->memos.memos = memos;
- for (j = 0; j < ci->memos.memocount; j++, memos++) {
- SAFE(read_int32(&memos->number, f));
- SAFE(read_int16(&memos->flags, f));
+ if (!ci->memos.memos.empty()) {
+ for (j = 0; j < ci->memos.memos.size(); j++) {
+ ci->memos.memos[j] = new Memo;
+ Memo *memo = ci->memos.memos[j];
+ SAFE(read_int32(&memo->number, f));
+ SAFE(read_int16(&memo->flags, f));
SAFE(read_int32(&tmp32, f));
- memos->time = tmp32;
- SAFE(read = read_buffer(memos->sender, f));
- SAFE(read_string(&memos->text, f));
+ memo->time = tmp32;
+ SAFE(read = read_buffer(memo->sender, f));
+ SAFE(read_string(&memo->text, f));
}
}
@@ -581,7 +580,6 @@ void save_cs_dbase(void)
dbFILE *f;
int i, j;
ChannelInfo *ci;
- Memo *memos;
static time_t lastwarn = 0;
if (!(f = open_db(s_ChanServ, ChanDBName, "w", CHAN_VERSION)))
@@ -659,15 +657,15 @@ void save_cs_dbase(void)
} else {
SAFE(write_string(NULL, f));
}
- SAFE(write_int16(ci->memos.memocount, f));
+ SAFE(write_int16(ci->memos.memos.size(), f));
SAFE(write_int16(ci->memos.memomax, f));
- memos = ci->memos.memos;
- for (j = 0; j < ci->memos.memocount; j++, memos++) {
- SAFE(write_int32(memos->number, f));
- SAFE(write_int16(memos->flags, f));
- SAFE(write_int32(memos->time, f));
- SAFE(written = write_buffer(memos->sender, f));
- SAFE(write_string(memos->text, f));
+ for (j = 0; j < ci->memos.memos.size(); j++) {
+ Memo *memo = ci->memos.memos[j];
+ SAFE(write_int32(memo->number, f));
+ SAFE(write_int16(memo->flags, f));
+ SAFE(write_int32(memo->time, f));
+ SAFE(written = write_buffer(memo->sender, f));
+ SAFE(write_string(memo->text, f));
}
SAFE(write_string(ci->entry_message, f));
@@ -1731,12 +1729,13 @@ int delchan(ChannelInfo * ci)
if (debug >= 2) {
alog("debug: delchan() top of the memo list");
}
- if (ci->memos.memos) {
- for (i = 0; i < ci->memos.memocount; i++) {
- if (ci->memos.memos[i].text)
- delete [] ci->memos.memos[i].text;
+ if (!ci->memos.memos.empty()) {
+ for (i = 0; i < ci->memos.memos.size(); ++i) {
+ if (ci->memos.memos[i]->text)
+ delete [] ci->memos.memos[i]->text;
+ delete ci->memos.memos[i];
}
- free(ci->memos.memos);
+ ci->memos.memos.clear();
}
if (debug >= 2) {
alog("debug: delchan() done with the memo list");
diff --git a/src/memoserv.c b/src/memoserv.c
index df970565e..53709c680 100644
--- a/src/memoserv.c
+++ b/src/memoserv.c
@@ -99,29 +99,29 @@ void check_memos(User * u)
return;
}
- for (i = 0; i < nc->memos.memocount; i++) {
- if (nc->memos.memos[i].flags & MF_UNREAD)
+ for (i = 0; i < nc->memos.memos.size(); i++) {
+ if (nc->memos.memos[i]->flags & MF_UNREAD)
newcnt++;
}
if (newcnt > 0) {
notice_lang(s_MemoServ, u,
newcnt == 1 ? MEMO_HAVE_NEW_MEMO : MEMO_HAVE_NEW_MEMOS,
newcnt);
- if (newcnt == 1 && (nc->memos.memos[i - 1].flags & MF_UNREAD)) {
+ if (newcnt == 1 && (nc->memos.memos[i - 1]->flags & MF_UNREAD)) {
notice_lang(s_MemoServ, u, MEMO_TYPE_READ_LAST, s_MemoServ);
} else if (newcnt == 1) {
- for (i = 0; i < nc->memos.memocount; i++) {
- if (nc->memos.memos[i].flags & MF_UNREAD)
+ for (i = 0; i < nc->memos.memos.size(); i++) {
+ if (nc->memos.memos[i]->flags & MF_UNREAD)
break;
}
notice_lang(s_MemoServ, u, MEMO_TYPE_READ_NUM, s_MemoServ,
- nc->memos.memos[i].number);
+ nc->memos.memos[i]->number);
} else {
notice_lang(s_MemoServ, u, MEMO_TYPE_LIST_NEW, s_MemoServ);
}
}
- if (nc->memos.memomax > 0 && nc->memos.memocount >= nc->memos.memomax) {
- if (nc->memos.memocount > nc->memos.memomax)
+ if (nc->memos.memomax > 0 && nc->memos.memos.size() >= nc->memos.memomax) {
+ if (nc->memos.memos.size() > nc->memos.memomax)
notice_lang(s_MemoServ, u, MEMO_OVER_LIMIT, nc->memos.memomax);
else
notice_lang(s_MemoServ, u, MEMO_AT_LIMIT, nc->memos.memomax);
@@ -245,23 +245,22 @@ void memo_send(User * u, char *name, char *text, int z)
if (z == 0 || z == 3)
notice_lang(s_MemoServ, u, MEMO_X_GETS_NO_MEMOS, name);
- } else if (mi->memomax > 0 && mi->memocount >= mi->memomax
+ } else if (mi->memomax > 0 && mi->memos.size() >= mi->memomax
&& !is_servoper) {
if (z == 0 || z == 3)
notice_lang(s_MemoServ, u, MEMO_X_HAS_TOO_MANY_MEMOS, name);
} else {
u->lastmemosend = now;
- mi->memocount++;
- mi->memos = static_cast<Memo *>(srealloc(mi->memos, sizeof(Memo) * mi->memocount));
- m = &mi->memos[mi->memocount - 1];
+ m = new Memo;
+ mi->memos.push_back(m);
strscpy(m->sender, source, NICKMAX);
- if (mi->memocount > 1) {
- m->number = m[-1].number + 1;
+ if (mi->memos.size() > 1) {
+ m->number = mi->memos[mi->memos.size() - 2]->number + 1;
if (m->number < 1) {
int i;
- for (i = 0; i < mi->memocount; i++) {
- mi->memos[i].number = i + 1;
+ for (i = 0; i < mi->memos.size(); i++) {
+ mi->memos[i]->number = i + 1;
}
}
} else {
@@ -339,21 +338,16 @@ void memo_send(User * u, char *name, char *text, int z)
int delmemo(MemoInfo * mi, int num)
{
int i;
+ if (mi->memos.empty()) return 0;
- for (i = 0; i < mi->memocount; i++) {
- if (mi->memos[i].number == num)
+ for (i = 0; i < mi->memos.size(); i++) {
+ if (mi->memos[i]->number == num)
break;
}
- if (i < mi->memocount) {
- delete [] mi->memos[i].text; /* Deallocate memo text memory */
- mi->memocount--; /* One less memo now */
- if (i < mi->memocount) /* Move remaining memos down a slot */
- memmove(mi->memos + i, mi->memos + i + 1,
- sizeof(Memo) * (mi->memocount - i));
- if (mi->memocount == 0) { /* If no more memos, free array */
- free(mi->memos);
- mi->memos = NULL;
- }
+ if (i < mi->memos.size()) {
+ delete [] mi->memos[i]->text; /* Deallocate memo text memory */
+ delete mi->memos[i]; /* Deallocate the memo itself */
+ mi->memos.erase(mi->memos.begin() + i); /* Remove the memo pointer from the vector */
return 1;
} else {
return 0;
diff --git a/src/nickserv.c b/src/nickserv.c
index 4cb3a54fb..d8ed59e99 100644
--- a/src/nickserv.c
+++ b/src/nickserv.c
@@ -102,10 +102,10 @@ void get_core_stats(long *nrec, long *memuse)
mem += strlen(*accptr) + 1;
}
- mem += nc->memos.memocount * sizeof(Memo);
- for (j = 0; j < nc->memos.memocount; j++) {
- if (nc->memos.memos[j].text)
- mem += strlen(nc->memos.memos[j].text) + 1;
+ mem += nc->memos.memos.size() * sizeof(Memo);
+ for (j = 0; j < nc->memos.memos.size(); j++) {
+ if (nc->memos.memos[j]->text)
+ mem += strlen(nc->memos.memos[j]->text) + 1;
}
mem += sizeof(void *) * nc->aliases.count;
@@ -270,20 +270,19 @@ void load_ns_dbase(void)
}
SAFE(read_int16(&tmp16, f));
- nc->memos.memocount = static_cast<int16>(tmp16);
+ if (tmp16) nc->memos.memos.resize(tmp16);
SAFE(read_int16(&tmp16, f));
nc->memos.memomax = static_cast<int16>(tmp16);
- if (nc->memos.memocount) {
- Memo *memos;
- memos = static_cast<Memo *>(scalloc(sizeof(Memo) * nc->memos.memocount, 1));
- nc->memos.memos = memos;
- for (j = 0; j < nc->memos.memocount; j++, memos++) {
- SAFE(read_int32(&memos->number, f));
- SAFE(read_int16(&memos->flags, f));
+ if (!nc->memos.memos.empty()) {
+ for (j = 0; j < nc->memos.memos.size(); j++) {
+ nc->memos.memos[j] = new Memo;
+ Memo *memo = nc->memos.memos[j];
+ SAFE(read_int32(&memo->number, f));
+ SAFE(read_int16(&memo->flags, f));
SAFE(read_int32(&tmp32, f));
- memos->time = tmp32;
- SAFE(read_buffer(memos->sender, f));
- SAFE(read_string(&memos->text, f));
+ memo->time = tmp32;
+ SAFE(read_buffer(memo->sender, f));
+ SAFE(read_string(&memo->text, f));
}
}
@@ -388,7 +387,6 @@ void save_ns_dbase(void)
NickAlias *na;
NickCore *nc;
char **access;
- Memo *memos;
static time_t lastwarn = 0;
if (!(f = open_db(s_NickServ, NickDBName, "w", NICK_VERSION)))
@@ -414,15 +412,15 @@ void save_ns_dbase(void)
j++, access++)
SAFE(write_string(*access, f));
- SAFE(write_int16(nc->memos.memocount, f));
+ SAFE(write_int16(nc->memos.memos.size(), f));
SAFE(write_int16(nc->memos.memomax, f));
- memos = nc->memos.memos;
- for (j = 0; j < nc->memos.memocount; j++, memos++) {
- SAFE(write_int32(memos->number, f));
- SAFE(write_int16(memos->flags, f));
- SAFE(write_int32(memos->time, f));
- SAFE(write_buffer(memos->sender, f));
- SAFE(write_string(memos->text, f));
+ for (j = 0; j < nc->memos.memos.size(); j++) {
+ Memo *memo = nc->memos.memos[j];
+ SAFE(write_int32(memo->number, f));
+ SAFE(write_int16(memo->flags, f));
+ SAFE(write_int32(memo->time, f));
+ SAFE(write_buffer(memo->sender, f));
+ SAFE(write_string(memo->text, f));
}
SAFE(write_int16(nc->channelcount, f));
@@ -979,12 +977,13 @@ static int delcore(NickCore * nc)
free(nc->access);
}
- if (nc->memos.memos) {
- for (i = 0; i < nc->memos.memocount; i++) {
- if (nc->memos.memos[i].text)
- delete [] nc->memos.memos[i].text;
+ if (!nc->memos.memos.empty()) {
+ for (i = 0; i < nc->memos.memos.size(); ++i) {
+ if (nc->memos.memos[i]->text)
+ delete [] nc->memos.memos[i]->text;
+ delete nc->memos.memos[i];
}
- free(nc->memos.memos);
+ nc->memos.memos.clear();
}
delete nc;