File: xIrcSocket.cpp

package info (click to toggle)
xirc 2.0-3
  • links: PTS
  • area: contrib
  • in suites: hamm, slink
  • size: 2,624 kB
  • ctags: 2,093
  • sloc: cpp: 21,094; makefile: 1,033
file content (432 lines) | stat: -rw-r--r-- 13,968 bytes parent folder | download
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/***************************************************************************
**    xIrcSocket.cpp  $Revision: 1.15 $ - $Name: V2-0 $ 
**    xSocket Class to parse IRC Messages
**
** Copyright (C) 1995, 1996  Joseph Croft <jcroft@unicomp.net>  
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
 ***************************************************************************/
#include <ctype.h>
#include "xIrcCommands.h"
#include "xIrcSocket.h"

static int dbg = 0;

extern xIrcCommands ircResponses;

xIrcSocket::xIrcSocket(xWidgetResInfo *pPRes, QObject *pParent, const char *pHost,
                       const char *pService, QObject *pSocketDialog, 
                       const char *pSlotStatus, const char *pSlotProgress, 
                       int &err) :
            xSocketTCP(pPRes, pParent, pHost, pService, pSocketDialog,
                       pSlotStatus, pSlotProgress, serr)
{
   if ((err = serr) == 0)
   {
      connect(this, SIGNAL(socketDataOut(QString)), 
              this, SLOT(socketDataIn(QString)));
      connect(this, SIGNAL(readFromSocket(char *)), 
              this, SLOT(rawSocketDataIn(char *)));
      connect(this, SIGNAL(sendToServer(const char *)), 
              this, SLOT(sendToSocket(const char *)));
   }
}

xIrcSocket::~xIrcSocket()
{
//   QObject::disconnect(this);
}

//
// This member function parses the Private messages is they
// are special, ei, ones that begin with a ^A. On exiting, if
// pRtn is not NULL, it will be set past the command found unless
// it is not recognized,  in which case pRtn will be set to the
// original value of cp.
//
// pMsg->pmsgType will be set to one of the values of the 
// enum xIrcPrvMsgType to indicate the type of private message
// found.
//
void xIrcSocket::parseMsgSpecial(xIrcMessage *pMsg, const char *pStr,
                     const char **pRtn)
{
   QString  tmpStr = "";
   const char *pSave, *pSave1;

   pSave = pStr;
   while (*pStr && !isspace(*pStr) && *pStr >= ' ')
      tmpStr += toupper(*(pStr++));

   if (strcmp(tmpStr, "PING") == 0)
      pMsg->pmsgTyp = ipmPing;
   else if (strcmp(tmpStr, "ACTION") == 0) 
      pMsg->pmsgTyp = ipmAction;
   else if (strcmp(tmpStr, "VERSION") == 0) 
      pMsg->pmsgTyp = ipmVersion;
   else if (strcmp(tmpStr, "USERINFO") == 0) 
      pMsg->pmsgTyp = ipmUserInfo;
   else if (strcmp(tmpStr, "CLIENTINFO") == 0) 
      pMsg->pmsgTyp = ipmClientInfo;
   else if (strcmp(tmpStr, "SOURCE") == 0) 
      pMsg->pmsgTyp = ipmSource;
   else if (strcmp(tmpStr, "FINGER") == 0) 
      pMsg->pmsgTyp = ipmFinger;
   else if (strcmp(tmpStr, "ERRMSG") == 0) 
      pMsg->pmsgTyp = ipmErrMsg;
   else if (strcmp(tmpStr, "TIME") == 0) 
      pMsg->pmsgTyp = ipmTime;
   else if (strcmp(tmpStr, "DCC") == 0)
   {
      pSave1 = pStr;
      while (*pStr && isspace(*pStr)) pStr++;
      tmpStr = "";
      while (*pStr && !isspace(*pStr))
         tmpStr += toupper(*(pStr++));
      if (strcmp(tmpStr, "SEND") == 0)
         pMsg->pmsgTyp = ipmDCCSend;
      else if (strcmp(tmpStr, "CHAT") == 0)
         pMsg->pmsgTyp = ipmDCCChat;
      else
      {
         pStr = pSave1;
         pMsg->pmsgTyp = ipmDCC;
      }
   }
   else
      pMsg->pmsgTyp = ipmUnknown;

   if (pMsg->pmsgTyp == ipmUnknown)
      *pRtn = pSave;
   else
      *pRtn = pStr;
}

//
// This member function parses the string pointed to by
// pText and emits a ircServerMessage signal passing the pointer
// to a xIrcMessage Structure to be handled by the higher levels.
//
void xIrcSocket::socketDataIn(QString msgStr)
{
   xIrcMessage msg;
   QString strTmp;
   bool isNumber;
   const char *cp;
   const char *pText = msgStr;
   int x;
   
   if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Enter\n");
   if (dbg) fflush(stdout);
   if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn():Have Raw message: |%s|\n",
                             pText);
   if (dbg) fflush(stdout);
   msg.srcNick = "";
   msg.srcAddr = "";
   msg.msgStr  = "";
   msg.rawMsg = pText;
   msg.rspCode = -1;
   //
   // Start by getting the nick, if present, then the address of the messages
   // source
   //
   if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Getting Nick\n");
   if (dbg) fflush(stdout);
   cp = pText;
   if (*pText != ':')
   {
      for (; *cp && *cp != '!' && *cp != ' '; cp++)
         strTmp += *(cp);
      msg.rspStr = strTmp;
   
      x = strTmp.toInt(&isNumber);
      if (isNumber)
      {
         if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Response was a code\n");
         if (dbg) fflush(stdout);
         msg.rspCode = x;
      }
      else
      {
         if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Response was a string\n");
         if (dbg) fflush(stdout);
         msg.rspCode = ircResponses.code(strTmp);
      }
      strTmp = "";
   }
   
   for (cp++; *cp && *cp != '!' && *cp != ' '; cp++)
      strTmp += *(cp);
   if (*cp == '!')
   {
      if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Getting Address\n");
      if (dbg) fflush(stdout);
      msg.srcNick = strTmp;
      strTmp = "";
      for (cp = cp+1; *cp && *cp != ' '; cp++)
         strTmp += *(cp);
   }      
   msg.srcAddr = strTmp;
   strTmp = "";
   
   //
   // Next either a numeric or text response will follow
   // Gather it, if its numeric, get its value and stuff it in msg.rspCode
   // Otherwise, get its text
   //
   if (msg.rspCode < 0)
   {
      if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Getting Response\n");
      if (dbg) fflush(stdout);
      for (cp++; *cp && *cp != ' '; cp++)
         strTmp += *(cp);
      msg.rspStr = strTmp;
      
      x = strTmp.toInt(&isNumber);
      if (isNumber)
      {
         if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Response was a code\n");
         if (dbg) fflush(stdout);
         msg.rspCode = x;
      }
      else
      {
         if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Response was a string\n");
         if (dbg) fflush(stdout);
         msg.rspCode = ircResponses.code(strTmp);
      }
      strTmp = "";
   }
   
   //
   // Now get the destination for the message, it should either be
   // a nick, or a channel name
   //
   if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Getting Destination\n");
   if (dbg) fflush(stdout);
   if (msg.rspCode >= 300 && msg.rspCode <= 369)
   {
      //
      // If this is a response in the range of 300 - 369, assume that the
      // Destination is the first word of the message and skip the destination
      // specified in the destination field
      //
      for (cp++; *cp && *cp != ':' && *cp != ' '; cp++);
      while (isspace(*cp) || *cp == ':' || *cp == '=' || *cp == '*' || *cp == '@') cp++;
      for (; *cp && *cp != ':' && *cp != ' '; cp++)
      {
         if (*cp != '\r' && *cp != '\n')
            strTmp += *(cp);
      }
      msg.dstStr = strTmp;
   }
   else
   {
      //
      // Otherwise, just use the destination as specified
      //
      while (*cp == ' ' || *cp == ':' || *cp == '=') cp++;
      for (; *cp && *cp != ':' && *cp != ' '; cp++)
      {
         if (*cp != '\r' && *cp != '\n')
            strTmp += *(cp);
      }
      msg.dstStr = strTmp;
   }
   strTmp = "";
   
   //
   // Just gather the remainder of the line and assume that it is the
   // message. This way it can be parsed as needed by the higher levels.
   // In any case, drop and '\r' encountered
   //
   if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Getting Message\n");
   if (dbg) fflush(stdout);
   while (isspace(*cp)) cp++;
   if (*cp == ':')
      while (isspace(*(++cp)));
   
   /*
   ** If this is a special private message, go parse it
   ** and set the pointer past the command. If its not a special
   ** private message, or even a private message for that matter,
   ** the pmsgTyp field of pMsg will be set to ipmMessage.
   */
   if ((ircResponses.is(msg.rspCode, "PRIVMSG")  ||
       ircResponses.is(msg.rspCode, "NOTICE")) &&
       *cp == 0x01)
      parseMsgSpecial(&msg, ++cp, &cp);
   else 
      msg.pmsgTyp = ipmMessage;

   /*
   ** Strip away any characters we don't want the calling code to have
   ** to mess with!
   */
   for (; *cp; cp++)
   {
      if (*cp != '\r' && *cp != 0x01)
         strTmp += *cp;
   }

   /*
   ** Set the message and go send it to the receiver!
   */
   msg.msgStr = strTmp;
   if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Sending data on via ircServerMessage\n");
   if (dbg) fflush(stdout);
   emit ircServerMessage(&msg);
   if (dbg) fprintf(stdout, "xIrcSocket::socketDataIn(): Exit\n");
   if (dbg) fflush(stdout);
}

void xIrcSocket::rawSocketDataIn(char *pText)
{
   int crFlag;
   QString tmpBuf;

   if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn(\"%s\"): Enter\n", pText);
   if (dbg > 3) fflush(stdout);
   tmpBuf = "";
   for (crFlag = 0; *pText; pText++)
   {
      if (crFlag)
      {
         if (*pText != '\n')
         {
            if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn(): Terminator 1\n");
            if (dbg > 3) fflush(stdout);
            socketBuffer += '\n';
            tmpBuf = socketBuffer.copy();
            socketBuffer.truncate(0);
            socketBuffer = "";
//            emit socketDataOut(socketBuffer);
//            emit socketDataOut(tmpBuf);
            if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn():socketBuffer set to |%s|\n",
                                 (const char *)socketBuffer);
            if (dbg > 3) fflush(stdout);
            socketBuffer += *pText;
         }
         else
         {
            if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn(): Terminator 2\n");
            if (dbg > 3) fflush(stdout);
            socketBuffer += *pText;
            tmpBuf = socketBuffer.copy();
            socketBuffer.truncate(0);
            socketBuffer = "";
//            emit socketDataOut(tmpBuf);
//            emit socketDataOut(socketBuffer);
            if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn():socketBuffer set to |%s|\n",
                                 (const char *)socketBuffer);
            if (dbg > 3) fflush(stdout);
         }
         if (*pText != '\r')
            crFlag = 0;
      }
      else
      {
         if (*pText == '\r')
            crFlag = 1;
         else
            socketBuffer += *pText;
         if (*pText == '\n')
         {
            if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn(): Terminator 3\n");
            if (dbg > 3) fflush(stdout);
            socketBuffer += *pText;
            tmpBuf = socketBuffer.copy();
            socketBuffer.truncate(0);
            socketBuffer = "";
//            emit socketDataOut(socketBuffer);
//            emit socketDataOut(tmpBuf);
            if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn():socketBuffer set to |%s|\n",
                                 (const char *)socketBuffer);
            if (dbg > 3) fflush(stdout);
         }
      }
   }
   if (strlen(tmpBuf) > 0)
      emit socketDataOut(tmpBuf);

   if (dbg > 4) fprintf(stdout, "xIrcSocket::rawSocketDataIn():socketBuffer left as |%s|\n",
                        (const char *)socketBuffer);
   if (dbg > 4) fflush(stdout);
   if (dbg > 3) fprintf(stdout, "xIrcSocket::rawSocketDataIn(): Exit\n");
   if (dbg > 3) fflush(stdout);
}

void xIrcSocket::sendIrcServerMessage(xIrcMessage *pMsg)
{
   QString  msgStr = "";
   
   if (dbg > 2) fprintf(stdout, "xIrcSocket::sendIrcServerMessage(): Sending message\n");
   if (dbg > 2) fprintf(stdout, "xIrcSocket::sendIrcServerMessage(): Response code = %d\n", pMsg->rspCode);
   if (dbg > 2) fprintf(stdout, "xIrcSocket::sendIrcServerMessage(): Destination =  |%s|\n",
                                (const char *)pMsg->dstStr);
   if (dbg > 2) fflush(stdout);
   
   msgStr = ircResponses.name(pMsg->rspCode);
   if (strlen(msgStr) == 0)
   {
      msgStr.setNum(pMsg->rspCode);
   }
   msgStr += ' ';
   if (strlen((const char *)pMsg->dstStr) > 0)
   {
      msgStr += pMsg->dstStr;
      if (strlen((const char *)pMsg->msgStr) > 0)
         msgStr += " :";
   }
   if (strlen((const char *)pMsg->msgStr) > 0)
      msgStr += pMsg->msgStr;
   msgStr += '\n';
   if (dbg > 2) fprintf(stdout, "xIrcSocket::sendIrcServerMessage(): Sending message %s\n", (const char *)msgStr);
   if (dbg > 2) fflush(stdout);
   emit sendToServer((const char *)msgStr);
   if (dbg > 2) fprintf(stdout, "xIrcSocket::sendIrcServerMessage(): Message sent\n");
   if (dbg > 2) fflush(stdout);
}

void xIrcSocket::socketError(int err)
{
   err = 0;
}

bool xIrcMessageList::is(xIrcMessage *pMsg)
{
   xIrcMessage *pMsg1;

   if (dbg) fprintf(stdout,"xIrcMessageList::is(%s): Enter\n",
                           (const char *)pMsg->srcNick);
   if (dbg) fflush(stdout);
   if ((pMsg1 = at(0)) != NULL)
   {
      if (dbg) fprintf(stdout, "xIrcMessageList::is(%s): Testing\n",
                              (const char *)pMsg1->srcNick);
      if (dbg) fflush(stdout);
      if (strcmp(pMsg->srcNick, pMsg1->srcNick) == 0)
      {
         if (dbg) fprintf(stdout,"xIrcMessageList::is(): Exit- Passed!\n");
         if (dbg) fflush(stdout);
         return(TRUE);
      }
   }
   if (dbg) fprintf(stdout,"xIrcMessageList::is(): Exit- Fail\n");
   if (dbg) fflush(stdout);
   return(FALSE);
}
#include "xIrcSocket.moc"