1
(*
2
 *      PageSix.mod
3
 *
4
 *      Programmer:		T. Ross
5
 *	Started:
6
 *	Last edited:		27 May 2006
7
 *
8
 *      Copyright 2006 Terry K. Ross <terry@aliboom.com>
9
 *
10
 *      This program is free software; you can redistribute it 
11
 *      and/or modify it under the terms of the GNU General Public
12
 *	License as published by the Free Software Foundation; 
13
 *	either version 2 of the License, or (at your option) any 
14
 *	later version.
15
 *
16
 *      This program is distributed in the hope that it will be 
17
 *      useful,but WITHOUT ANY WARRANTY; without even the implied
18
 *	warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
19
 *	PURPOSE.  See theGNU General Public License for more details.
20
 *
21
 *      You should have received a copy of the GNU General Public 
22
 *      License along with this program; if not, write to the 
23
 *      Free Software Foundation, Inc., 675 Mass Ave, 
24
 *      Cambridge, MA 02139, USA.
25
 *)
26
<* GENHISTORY + *> 
27
<* GENDEBUG + *>
28
<* LINENO + *>
29
<*+ M2EXTENSIONS *>
30
<*+ M2ADDTYPES *>
31
<*+ CSTDLIB *>
32
<*+ NOHEADER *>
33
34
MODULE PageSix;
35
36
IMPORT COMPILER;
37
IMPORT Connect;
38
IMPORT Handler;
39
IMPORT FileSys;
40
IMPORT Message;
41
IMPORT RawIO;
42
IMPORT SeqFile;
43
IMPORT Sockets;
44
IMPORT STextIO;
45
IMPORT StreamFile;
46
IMPORT Strings, StringParse;
47
IMPORT SWholeIO;
48
IMPORT SYSTEM;
49
IMPORT SysClock;
50
IMPORT TextIO;
51
IMPORT TimeConv;
52
IMPORT Timers;
53
IMPORT types;
54
IMPORT util;
55
IMPORT WholeIO, WholeStr;
56
FROM Setup IMPORT check, strType;
57
 
58
IMPORT r;
59
60
CONST
61
  LF = 12C;
62
  CR = 15C;
63
  homechan = Message.homechan;
64
65
  (* for poll() *)
66
  POLLIN   = 1;
67
  POLLPRI  = 2;
68
  POLLOUT  = 4;
69
  POLLERR  = 8;
70
  POLLHUP  = 16;
71
  POLLNVAL = 32;
72
73
TYPE
74
  (* for poll() *)
75
  nfds_t = LONGCARD;
76
77
  pollfd = RECORD
78
    fd     : SYSTEM.int;    (*  File descriptor to poll.   *)
79
    events : SYSTEM.INT16;  (*  Types of events poller cares about.   *)
80
    revents: SYSTEM.INT16;  (*  Types of events that actually occurred.   *)
81
  END;
82
83
  Ptrpollfd = POINTER TO pollfd;
84
  
85
86
VAR 
87
  (* for poll() *)
88
  return       : INTEGER;
89
  tout         : SYSTEM.int;
90
  fd           : pollfd;
91
  nfds         : nfds_t;
92
  serv         : Sockets.Socket;
93
  found, done  : BOOLEAN;
94
  (* remainder *)
95
  parse	       : Message.Parse; 
96
  pid          : types.pid_t;
97
  file         : StreamFile.ChanId;
98
  res, iores   : StreamFile.OpenResults;
99
  string       : strType;
100
  temp         : ARRAY [0..80] OF CHAR;
101
102
PROCEDURE TimeStamp;
103
104
VAR
105
  dt: SysClock.DateTime;
106
  file : SeqFile.ChanId;
107
  res : SeqFile.OpenResults;
108
BEGIN  
109
  TimeConv.unpack(dt, COMPILER.TIMESTAMP);
110
  SeqFile.OpenWrite(file, "/home/terry/p6dat/dat/compiletime", SeqFile.raw+SeqFile.write+SeqFile.old, res);
111
  RawIO.Write(file, dt);
112
  SeqFile.Close(file);
113
END TimeStamp;
114
115
116
PROCEDURE ["C"] / poll ( fds: Ptrpollfd; nfds: nfds_t;
117
                         timeout: SYSTEM.int ): SYSTEM.int;
118
119
120
121
(* This will eventually be moved to the sockets definition file,
122
   once I have converted it from OS/2 to Linux. *)
123
(* PROCEDURE ["C"] / close (S: Sockets.Socket): BOOLEAN; *)
124
(* Attempting to use Sockets.close - if it works, delete this. *)
125
126
BEGIN (* PageSix.mod *)
127
  TimeStamp;
128
  tout := 50; (* set poll timeout to 50 milliseconds *)
129
 
130
  parse.botname := Connect.botName;
131
  
132
  (* Are we really starting, or did we rehash? *)
133
  IF FileSys.Exists("/home/terry/p6dat/tmp/rehash/yes") THEN 
134
     FileSys.Remove("/home/terry/p6dat/tmp/rehash/yes", done);
135
136
    (* Kill the old process *) (* This needs more work *)
137
    (* What should be happening here is that the bot is getting all of the info
138
       necessary to communicate with the server, and killing off the old version
139
       of the bot.
140
    *)   
141
    StreamFile.Open(file, "/home/terry/p6dat/tmp/rehash/pid", StreamFile.read,res);
142
    WholeIO.ReadInt(file, pid);
143
      (* Log it *)
144
      Strings.Assign("Closing pid P~", string);
145
      WholeStr.IntToStr(pid, temp);
146
      StringParse.Replace(string, "P~", temp);
147
      util.Log(string, parse);
148
149
    StreamFile.Close(file);
150
    (* Hmmmmm not working
151
    signal.kill(pid, signal.SIGTERM);
152
    *)
153
    StreamFile.Open(file, "/home/terry/p6dat/tmp/rehash/serv", StreamFile.read, res);
154
    WholeIO.ReadCard(file, parse.serv);
155
    STextIO.WriteString("<><><> Socket: ");
156
    SWholeIO.WriteCard(parse.serv,0);
157
    serv := parse.serv;
158
    STextIO.WriteLn;
159
    TextIO.SkipLine(file); 
160
    TextIO.ReadString(file, parse.server);
161
    STextIO.WriteString("<><><> Server: ");
162
    STextIO.WriteString(parse.server);
163
    STextIO.WriteLn;
164
    TextIO.SkipLine(file);
165
    TextIO.ReadString(file, temp);
166
    STextIO.WriteString("<><><> forumCheck: ");
167
    STextIO.WriteString(temp);
168
    STextIO.WriteLn;
169
    IF Strings.Equal(temp,"true") THEN
170
      check.trackercheck.on := TRUE;
171
    ELSE
172
      check.trackercheck.on := FALSE;
173
    END;
174
    (* get bug check status *)
175
    TextIO.SkipLine(file);
176
    TextIO.ReadString(file, temp);
177
    STextIO.WriteString("<><><> bugCheck: ");
178
    STextIO.WriteString(temp);
179
    STextIO.WriteLn;
180
    IF Strings.Equal(temp,"true") THEN
181
      check.bugcheck.on := TRUE;
182
    ELSE
183
      check.bugcheck.on := FALSE;
184
    END;  
185
    (* read linesSent *)
186
    TextIO.SkipLine(file);
187
    WholeIO.ReadCard(file, Message.linesSent);
188
    STextIO.WriteString("<><><> linesSent: ");
189
    SWholeIO.WriteCard(Message.linesSent, 0);
190
    STextIO.WriteLn;
191
    TextIO.SkipLine(file);
192
    (* read linesRead *)
193
    WholeIO.ReadCard(file, Message.linesRead);
194
    STextIO.WriteString("<><><> linesRead: ");
195
    SWholeIO.WriteCard(Message.linesRead, 0);
196
    STextIO.WriteLn;
197
    r.rehashReadTime;
198
    
199
    StreamFile.Close(file);
200
201
    parse := Message.DoRead(serv);
202
  ELSE (* We are actually firing up for the first time. *)
203
    (* *)
204
    serv := Connect.DoConnect("irc.freenode.net", 6667); 
205
    (* *)
206
    (* 
207
    serv := Connect.DoConnect("kornbluth.freenode.net", 6667);
208
    *)
209
    
210
    (*
211
    serv := Connect.DoConnect("niven.freenode.net", 6667); 
212
    *)
213
    
214
    Connect.DoLogin(parse.botname, parse.botname, "");
215
    Message.DoSend(serv, "JOIN #atari-3");
216
    
217
    parse := Message.DoRead(serv);
218
219
    check.trackercheck.on := FALSE;
220
    check.bugcheck. on := TRUE;
221
  END;
222
  (* set up poll() events *)
223
  fd.fd := serv;
224
  fd.events := SYSTEM.CAST(SYSTEM.INT16, POLLIN+POLLPRI);
225
  fd.revents := SYSTEM.CAST(SYSTEM.INT16, 0);
226
  
227
  REPEAT
228
    done := Timers.Timing();
229
    return := poll (fd, nfds, tout);
230
(*    IF (return > 0) THEN *)
231
    parse := Message.DoRead(serv);
232
    IF LENGTH(parse.raw) > 0 THEN
233
      Handler.Handler(parse);
234
    END;
235
    
236
237
    IF Message.InString("die bot",parse.raw) OR
238
       Message.InString("die", parse.raw) AND
239
       Message.InString(parse.message, parse.botname) THEN
240
         IF util.Auth(parse.raw, iores) THEN
241
	   found := TRUE;
242
	 ELSE
243
	   Message.PrintLine(parse.chan, "no.");
244
	 END;  
245
    END; (* IF *)
246
(*    END; (* IF (return > 0) *) *)
247
  UNTIL found; 
248
249
  (* OK, found is TRUE, so the die command was entered. Close down *)
250
  Connect.DoLogout("bye bye - qnr just killed me.");
251
252
  done := Sockets.close(serv);
253
EXCEPT
254
  RETURN;
255
END PageSix.