summaryrefslogtreecommitdiff
path: root/apps/drwho/PMC_Ruser.cpp
blob: 79adac7b9ed1c015431eef20314d373f603e5d55 (plain)
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
// $Id$

#include "global.h"
#include "Options.h"
#include "HT_Client.h"
#include "PMC_Ruser.h"
#include "ace/ACE.h"
#include "ace/Log_Msg.h"

// This function is pretty much a no-op that just sets up the
// appropriate lookup function to use.

int
PMC_Ruser::encode (char *packet, int &packet_length)
{
  if (Options::get_opt (Options::DEBUG) != 0)
    ACE_DEBUG ((LM_DEBUG,
                "in PMC_Ruser::encode\n"));

  ACE_NEW_RETURN (this->ss,
                  HT_Client,
                  -1);

  SET_PACKET_TYPE (packet, Options::PROTO_RUSER);

  char *buf_ptr = SKIP_PACKET_TYPE (packet);

  *buf_ptr++ = char (Options::get_opt (Options::PRINT_LOGIN_NAME));

  packet_length = buf_ptr - packet;
  return 1;
}

// This method is responsible for transforming the msg from the server
// back into a form usable by the client.  Note that it reads the
// REAL_NAME from the packet (since the server placed it there)...

int
PMC_Ruser::decode (char *packet, int &packet_length)
{
  if (Options::get_opt (Options::DEBUG) != 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "in PMC_Ruser::decode, packet_length = %d\n",
                  packet_length));
      ACE_OS::write (ACE_STDERR, packet, packet_length);
      ACE_DEBUG ((LM_DEBUG,
                  "\n"));
    }
  char *cp = packet;
  int remote_users = 0;

  sscanf (cp,
          "Users   %d",
          &remote_users);

  this->increment_total_users (remote_users);

  for (cp = (char *) ACE::strend (cp);
       *cp != '\n';
       cp++)
    {
      char *host_name = cp;

      for (cp = (char *) ACE::strend (cp);
           *(cp = this->handle_protocol_entries (cp, host_name)) != '\t'; )
	continue;
    }

  return 1;
}

Protocol_Record *
PMC_Ruser::insert_protocol_info (Protocol_Record &protocol_record)
{
  Protocol_Record *prp = this->ss->insert (protocol_record.get_host (),
                                           MAXHOSTNAMELEN);
  Drwho_Node *current_node = protocol_record.get_drwho_list ();
  Drwho_Node *np = this->get_drwho_node (ACE::strnnew (current_node->get_login_name (),
                                                       MAXUSERIDNAMELEN),
                                         prp->drwho_list_);
  int length = ACE_OS::strlen (prp->get_host ());

  np->set_real_name (ACE::strnew (current_node->get_real_name ()));

  if (np->get_active_count () < current_node->get_active_count ())
    np->set_active_count (current_node->get_active_count ());
  if (np->get_inactive_count () < current_node->get_inactive_count())
    np->set_inactive_count (current_node->get_inactive_count ());

  if (length > this->max_key_length)
    this->max_key_length = length;

  return prp;
}

char *
PMC_Ruser::handle_protocol_entries (const char *cp,
                                    const char *host_name,
                                    const char *)
{
  static Protocol_Record protocol_record (1);
  Drwho_Node *current_node = protocol_record.get_drwho_list ();

  protocol_record.set_host (host_name);
  current_node->set_inactive_count (atoi (cp));
  current_node->set_active_count (atoi (cp = ACE_OS::strchr (cp, ' ') + 1));
  current_node->set_login_name (cp = ACE_OS::strchr (cp, ' ') + 1);
  current_node->set_real_name (cp = ACE_OS::strchr (cp, '\0') + 1);

  this->insert_protocol_info (protocol_record);

  return (char *) ACE::strend (cp);
}

void
PMC_Ruser::process (void)
{
  const char *(Drwho_Node::*get_name)(void);

  if (Options::get_opt (Options::PRINT_LOGIN_NAME))
    get_name = &Drwho_Node::get_login_name;
  else
    get_name = &Drwho_Node::get_real_name;

  for (Protocol_Record *prp;
       (prp = this->Protocol_Manager::get_each_friend ()) != 0;
       )
    {
      ACE_DEBUG ((LM_DEBUG,
                  "%-*s ",
                  this->max_key_length,
                  prp->get_host ()));

      for (Drwho_Node *np = prp->get_drwho_list (); ;)
	{
	  ACE_DEBUG ((LM_DEBUG,
                      "%s",
                      (np->*get_name) ()));

	  if (np->get_inactive_count () != 0)
	    {
	      if (np->get_active_count () != 0)
		ACE_DEBUG ((LM_DEBUG,
                            "*(%d)",
                            np->get_active_count ()));
	    }
	  else if (np->get_active_count () > 1)
	    ACE_DEBUG ((LM_DEBUG,
                        "*(%d)",
                        np->get_active_count ()));
	  else if (np->get_active_count () == 1)
            ACE_DEBUG ((LM_DEBUG,
                        "*"));

          np = np->next_;
	  if (np == 0)
	    break;
	  else if (Options::get_opt (Options::PRINT_LOGIN_NAME))
            ACE_DEBUG ((LM_DEBUG,
                        " "));
	  else
            ACE_DEBUG ((LM_DEBUG,
                        ", "));
	}

      ACE_DEBUG ((LM_DEBUG,
                  "\n"));
    }
}

PMC_Ruser::PMC_Ruser (void)
{
}