From a94e8565eabf8e5269c7d2b86b5ecfa09971bfc0 Mon Sep 17 00:00:00 2001
From: Stefan Sayer <stefan.sayer@googlemail.com>
Date: Fri, 20 Feb 2015 21:18:15 +0100
Subject: [PATCH 4/7] dsm:mod_groups: functions to get group participants and
 size

---
 apps/dsm/mods/mod_groups/ModGroups.cpp | 40 +++++++++++++++++++++++++++++++++-
 apps/dsm/mods/mod_groups/ModGroups.h   |  3 ++-
 doc/dsm/mods/Readme.mod_groups.txt     |  3 +++
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/apps/dsm/mods/mod_groups/ModGroups.cpp b/apps/dsm/mods/mod_groups/ModGroups.cpp
index 60a1edd..9af9a79 100644
--- a/apps/dsm/mods/mod_groups/ModGroups.cpp
+++ b/apps/dsm/mods/mod_groups/ModGroups.cpp
@@ -47,7 +47,8 @@ MOD_ACTIONEXPORT_BEGIN(MOD_CLS_NAME) {
   DEF_CMD("groups.join", GroupsJoinAction);
   DEF_CMD("groups.leave", GroupsLeaveAction);
   DEF_CMD("groups.leaveAll", GroupsLeaveAllAction);
-  // DEF_CMD("groups.get", GroupsGetAction);
+  DEF_CMD("groups.get", GroupsGetAction);
+  DEF_CMD("groups.getSize", GroupsGetSizeAction);
   // DEF_CMD("groups.getMembers", GroupsGetMembersAction);
   DEF_CMD("groups.postEvent", GroupsPostEventAction);
 
@@ -125,6 +126,43 @@ EXEC_ACTION_START(GroupsLeaveAllAction) {
   GroupsModule::leave_all_groups(ltag);
 } EXEC_ACTION_END;
 
+CONST_ACTION_2P(GroupsGetAction, '=', false);
+EXEC_ACTION_START(GroupsGetAction) {
+  string var = par1;
+  if (var.size() && var[0]=='$') var.erase(0,1);
+  string groupname = resolveVars(par2, sess, sc_sess, event_params);
+  GroupsModule::groups_mut.lock();
+  GroupMap::iterator grp = GroupsModule::groups.find(groupname);
+  int i=0;
+  if (grp != GroupsModule::groups.end()) {
+    for (set<string>::iterator it =
+	   grp->second.begin(); it != grp->second.end(); it++) {
+      sc_sess->var[var+"["+int2str(i)+"]"] = *it;
+      i++;
+    }
+  }
+  GroupsModule::groups_mut.unlock();
+  DBG("get %d group members of '%s' in $%s[]\n", i, groupname.c_str(), var.c_str());
+
+} EXEC_ACTION_END;
+
+CONST_ACTION_2P(GroupsGetSizeAction, '=', false);
+EXEC_ACTION_START(GroupsGetSizeAction) {
+  string var = par1;
+  if (var.size() && var[0]=='$') var.erase(0,1);
+  string groupname = resolveVars(par2, sess, sc_sess, event_params);
+  DBG("posting event to group '%s'\n", groupname.c_str());
+  GroupsModule::groups_mut.lock();
+  int size = 0;
+  GroupMap::iterator grp = GroupsModule::groups.find(groupname);
+  if (grp != GroupsModule::groups.end()) {
+    size = grp->second.size();
+  }
+  GroupsModule::groups_mut.unlock();
+  sc_sess->var[var] = int2str(size);
+  DBG("get group '%s' size $%s=%d\n", groupname.c_str(), var.c_str(), size);
+} EXEC_ACTION_END;
+
 CONST_ACTION_2P(GroupsPostEventAction, ',', true);
 EXEC_ACTION_START(GroupsPostEventAction) {
   string groupname = resolveVars(par1, sess, sc_sess, event_params);
diff --git a/apps/dsm/mods/mod_groups/ModGroups.h b/apps/dsm/mods/mod_groups/ModGroups.h
index 4305e9a..37c1c0f 100644
--- a/apps/dsm/mods/mod_groups/ModGroups.h
+++ b/apps/dsm/mods/mod_groups/ModGroups.h
@@ -60,7 +60,8 @@ DECLARE_MODULE_END;
 DEF_ACTION_1P(GroupsJoinAction);
 DEF_ACTION_1P(GroupsLeaveAction);
 DEF_ACTION_1P(GroupsLeaveAllAction);
-/* DEF_ACTION_1P(GroupsGetAction); */
+DEF_ACTION_2P(GroupsGetAction);
+DEF_ACTION_2P(GroupsGetSizeAction);
 /* DEF_ACTION_1P(GroupsGetMembersAction); */
 DEF_ACTION_2P(GroupsPostEventAction);
 
diff --git a/doc/dsm/mods/Readme.mod_groups.txt b/doc/dsm/mods/Readme.mod_groups.txt
index 63248a1..4551229 100644
--- a/doc/dsm/mods/Readme.mod_groups.txt
+++ b/doc/dsm/mods/Readme.mod_groups.txt
@@ -21,6 +21,9 @@ Actions:
   groups.postEvent(groupname, var1;var2)  -  post event to groupname with var1 and var2
   groups.postEvent(groupname, var)  -  post event to groupname with all variables
 
+  groups.get(arrayname=groupname)   - get all ltags of troupname in arrayname
+  groups.getSize(varname=groupname)   - get size of group in varname
+ 
 
 Example:
   import(mod_groups);
-- 
1.9.3 (Apple Git-50)

