summaryrefslogtreecommitdiff
path: root/apps/drwho/PMS_Ruser.cpp
blob: 99d3905f44b07e0f5b460ed3c857b21a9bff0bea (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
// $Id$

#include "Options.h"
#include "HT_Server.h"
#include "PMS_Ruser.h"

// This function packs the located friends userids, plus the machines
// they are logged into (along with the inactive and active counts on
// each machine) into a buffer that is subsequently transmitted back
// to the client across the network.  Note that this function encodes
// the REAL_NAME of the user in the packet.

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

  Protocol_Record *prp;
  char *buf_ptr = packet;

  sprintf (buf_ptr,
           "Users   %d",
           this->get_total_users ());
  buf_ptr += ACE_OS::strlen (buf_ptr) + 1;

  // We only send back info on hosts that we actually see.

  for (;
       (prp = this->get_next_friend ()) != 0;
       *buf_ptr++ = '\t')
    buf_ptr = this->handle_protocol_entries (ACE::strecpy (buf_ptr,
                                                           prp->get_host ()),
                                             prp->get_drwho_list ());
  
  *buf_ptr++ = '\n';
  packet_length = buf_ptr - packet;

  if (Options::get_opt (Options::DEBUG) != 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "packet_length = %d\n",
                  packet_length));
      ACE_OS::write (ACE_STDERR, packet, packet_length);
      ACE_DEBUG ((LM_DEBUG,
                  "\n"));
    }
  return 1;
}

// This function takes a packet received from the client and crusers
// the appropriate Protocol_Manager routine to build the local table
// of friends.

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

  if (*packet)
    Options::set_opt (Options::PRINT_LOGIN_NAME);

  ACE_NEW_RETURN (this->ss,
                  HT_Server,
                  -1);
  return 1;
}

Protocol_Record *
PMS_Ruser::insert_protocol_info (Protocol_Record &protocol_record)
{
  Drwho_Node *current_node = protocol_record.get_drwho_list ();
  Protocol_Record *prp = this->ss->insert (current_node->get_host_name (),
                                           MAXHOSTNAMELEN);
  Drwho_Node *np = this->get_drwho_node (ACE::strnnew (protocol_record.get_login (),
                                                       MAXUSERIDNAMELEN),
                                         prp->drwho_list_);

  if (Options::get_opt (Options::PRINT_LOGIN_NAME))
    np->set_real_name ("");
  else
    {
      passwd *pwent = getpwnam (np->get_login_name ());
      char *cp = 
        (char *) ACE_OS::strchr (np->set_real_name (pwent == 0 
                                                    ? np->get_login_name () 
                                                    : ACE::strnew (pwent->pw_gecos)),
                                 ',');
      if (cp != 0)
	*cp = '\0';
    }

  if (current_node->get_idle_time () >= MAX_USER_TIMEOUT)
    np->inactive_count_++;
  else
    np->active_count_++;
  
  return prp;
}

char *
PMS_Ruser::handle_protocol_entries (char *buf_ptr,
                                    Drwho_Node *np)
{
  for (; np != 0; np = np->next_)
    {
      sprintf (buf_ptr,
               "%d %d ",
               np->get_inactive_count (),
               np->get_active_count ());
      buf_ptr += ACE_OS::strlen (buf_ptr);

      buf_ptr = ACE::strecpy (ACE::strecpy (buf_ptr,
                                            np->get_login_name ()),
                              np->get_real_name ());
    }

  return buf_ptr;
}     

PMS_Ruser::PMS_Ruser (void)
{
}