File: ClientNNTP.cc

package info (click to toggle)
postman 2.1-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,248 kB
  • ctags: 3,249
  • sloc: cpp: 33,376; ansic: 2,014; sh: 768; makefile: 300
file content (209 lines) | stat: -rw-r--r-- 6,211 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
ClientNNTP.cc
*/

#include "ClientNNTP.h"
extern Language *L;
extern CCLIENT *thecclient;

ClientNNTP::ClientNNTP (const char *anntpserver, const char *adefaultipdomain, const char *auser, const char *apw)
  {
  xstrncpy (nntpserver, CMAXIPNAME, anntpserver);
  xstrncpy (user, CMAXBUFFER, auser);
  xstrncpy (pw, CMAXBUFFER, apw);  
  xstrncpy (defaultipdomain, CMAXIPNAME, adefaultipdomain);
  if (strchr (nntpserver, '.') == NULL) {xstrncat (nntpserver, CMAXIPNAME, defaultipdomain);}
  ReturnAllNNGroupsFileName (nntpserver, fnallnn);
  searchedgroups = "";
  SLAllGroups = new StringList;
  SLSearchedGroups = new StringList;
  }  

ClientNNTP::~ClientNNTP ()
  {
  delete SLAllGroups;
  delete SLSearchedGroups;
  }

void ClientNNTP::setNNTPserver (const char *anntpserver)
  {
  if (strcasecmp (nntpserver, anntpserver) != 0)
    {
    xstrncpy (nntpserver, CMAXIPNAME, anntpserver);
    if (strchr (nntpserver, '.') == NULL) {xstrncat (nntpserver, CMAXIPNAME, defaultipdomain);}
    ReturnAllNNGroupsFileName (nntpserver, fnallnn);
    searchedgroups = "";
    //Force get the list group
    SLAllGroups->Clear();
    SLSearchedGroups->Clear();
    SLAllGroups->Clear();
    retrieveAllGroupsList ();
    }  
  }

const char *ClientNNTP::getNNTPServer (void)
  {
  return nntpserver;
  }    

int ClientNNTP::retrieveAllGroupsList (void)
  {
  struct stat STAT;
  int res, dif;
  time_t tm, now;
  
  if (SLAllGroups->Count () > 0) 
    {
    //DEBUG ("NNTP AllGroups are already in memory.");
    return SLAllGroups->Count ();
    } 
  
  now = time (NULL);
  res = stat ((const char *)fnallnn, &STAT);  
  tm = STAT.st_mtime;      
  dif = (int)difftime(now, tm);
  //DEBUG ("res=%d,now=%ld--tm=%ld--dif=%d--MAX_TIME_NNFILE=%ld--", res, now, tm, dif, MAX_TIME_NNFILE);
  if ((dif > MAX_TIME_NNFILE) || (res != 0))
    {
    return retrieveAllGroupsListFromNet ();
    }
  else
    {
    return retrieveAllGroupsListFromFile ();
    }        
  }
  
int ClientNNTP::retrieveAllGroupsListFromNet (void)
  {
  TBuffer buffer, basura;
  int sock, cod, res, numgroups, possp;
  XSocket *nntpsocket;
  TBuffer errormsg;

  LOG ("NNTP retrieveAllGroupsListFromNet for server: %s, user: %s", nntpserver, user);
  SLAllGroups->Clear();
  nntpsocket = new XSocket ();
  nntpsocket->ignore_pipe();
  sock = nntpsocket->do_client_inet (nntpserver, NULL, DEFNNTPPORT, 240);
  if (sock == -1) {xstrncpy (errormsg, CMAXBUFFER, L->get (ERR_SERVERISDOWN)); return -1;}
  nntpsocket->SetSock (sock);
  
  /*LEO LA LINEA DE BIENVENIDA*/
  /*-1 by the possible buffer overflow resulting from the linefeed added below. */
  if (!nntpsocket->Gets (buffer, sizeof(buffer) -1)) {return -1;}
  sscanf (buffer, "%d %200s", &cod, basura);
  if ((cod < 200) || (cod > 400)) {xstrncpy (errormsg, CMAXBUFFER, buffer); return -1;}

  /*ENVIO LIST*/
  sprintf (buffer, "LIST\n");
  if (nntpsocket->Puts (buffer) == -1) {return -1;}

  /*LEO LO RECIBIDO*/
  numgroups = 0;
  res = nntpsocket->Gets (buffer, sizeof(buffer) - 1);
  sscanf (buffer, "%d %200s", &cod, basura);
  if (cod != 215) 
    {
    //Me pide usuario y pw
    if ((cod == 450) || (cod == 480))
      {
      snprintf (buffer, CMAXBUFFER, "AUTHINFO USER %s\n", user);
      if (nntpsocket->Puts (buffer) == -1) {return -1;}
      res = nntpsocket->Gets (buffer, sizeof(buffer) - 1);
      sscanf (buffer, "%d %200s", &cod, basura);
      if (cod != 381) {return -1;}
      
      snprintf (buffer, CMAXBUFFER, "AUTHINFO PASS %s\n", pw);
      if (nntpsocket->Puts (buffer) == -1) {return -1;}
      res = nntpsocket->Gets (buffer, sizeof(buffer) - 1);
      sscanf (buffer, "%d %200s", &cod, basura);
      if (cod != 281) {xstrncpy (errormsg, CMAXBUFFER, "User/pw invalid!"); return -1;}
      
      }
    else return -1;
    }  
  res = nntpsocket->Gets (buffer, sizeof(buffer) - 1);
  while ((res != -1) && (strcmp(buffer, ".") != 0) && (strcmp(buffer, "") != 0)) 
    {
    ++numgroups;
    possp = Cstrstr (buffer, " ");
    if (possp > 0) {buffer [possp - 1] = '\0';}
    /*DEBUG*/ //printf ("--%d--%s--\n", numgroups, buffer); fflush (stdout);
    SLAllGroups->Add (buffer);
    res = nntpsocket->Gets (buffer, sizeof(buffer) - 1);
    }  

  /*ENVIO QUIT*/
  sprintf (buffer, "QUIT\n");
  if (nntpsocket->Puts (buffer) == -1) {return -1;}

  /*LEO LA LINEA DE QUIT*/
  if (!nntpsocket->Gets (buffer, sizeof(buffer) -1)) {return -1;}
  sscanf (buffer, "%d %200s", &cod, basura);
  if ((cod < 200) || (cod > 400)) {return -1;}

  //sortAllGroupsList(); VERY SLOWLY WITH MANY GROUPS.

  SLAllGroups->SaveToFile (fnallnn, true);
  
  delete nntpsocket;
  
  //SORT THE BIG FILE FOR NNTP GROUPS
  if (SORT_NNTP_ALLGROUPS == 1)
    {
    TBuffer abuf, fnallnn_tmp;
    snprintf (fnallnn_tmp,  CMAXBUFFER, "%s.tmp", fnallnn);
    snprintf (abuf,  CMAXBUFFER, "%s -o %s %s", cmdSORT, fnallnn_tmp, fnallnn);
    if (system(abuf) == 0) {CopyFileOverWrite (fnallnn_tmp, fnallnn);}
    DeleteFile (fnallnn_tmp);
    }
    
  return numgroups;
  }

int ClientNNTP::retrieveAllGroupsListFromFile (void)
  {
  LOG ("NNTP retrieveAllGroupsListFromFile for server: %s, user: %s", nntpserver, user);
  SLAllGroups->LoadFromFile (fnallnn);
  return SLAllGroups->Count();
  }

const char *ClientNNTP::getSearchedGroups (void)
  {
  return searchedgroups.cstr();
  } 

int ClientNNTP::retrievePartialGroupsList (char *filter)
  {
  int i, res;  
  SLSearchedGroups->Clear();
  searchedgroups = "";
  if (IsEmpty (filter) == true) {return 0;}
  
  if (strstr (filter, "*") == NULL) 
    {
    for (int k = strlen(filter) - 1; k >= 0; --k) {filter[k + 1] = filter[k];}
    filter[0] = '*';
    filter[strlen(filter)] = '*';
    }
  
  res = 0;
  for (i = 0; i < SLAllGroups->Count (); ++i)
    {
    if (StringsLike (SLAllGroups->getString(i).cstr(), filter) == true) 
      {
      ++res;
      SLSearchedGroups->Add (SLAllGroups->getString(i).cstr());
      searchedgroups += SLAllGroups->getString(i).cstr(); searchedgroups += "\n";
      if (res >= MAX_NNTP_GROUPS_FOUND) {return res;}
      }
    }
  return res;
  }

void ClientNNTP::sortAllGroupsList (void)
  {
  if (SLAllGroups->Count() <= 0) {return;}
  SLAllGroups->SORT (CompareStringsNCS, FlipStrings);      
  }