View Javadoc

1   package paolomind.multitalk.irc;
2   
3   import org.jibble.pircbot.PircBot;
4   import paolomind.multitalk.netmessage.IMessageFactory;
5   import paolomind.multitalk.netmessage.IMessage;
6   import paolomind.multitalk.netmessage.MessageException;
7   import paolomind.multitalk.netmessage.Receiver;
8   import paolomind.multitalk.netmessage.Sender;
9   
10  /**
11   * Prima classe che implementa il protocollo di comunicazione.
12   * si preoccupa di ricevere ed inviare i messaggi,
13   * e delegare gli eventi IRC
14   *
15   * @author paolo
16   */
17  public class SenderIrcClient extends PircBot implements Sender {
18  
19    /** */
20    private Receiver receiver;
21  
22    /** */
23    private IrcListener irclistener;
24  
25    /** */
26    private IMessageFactory mfactory;
27  
28    /**
29     * @param pirclistener il listener IRC che riceve gli eventi IRC
30     */
31    public SenderIrcClient(final IrcListener pirclistener) {
32      super();
33      this.irclistener = pirclistener;
34    }
35  
36    /**
37     * This method is called whenever a message is sent to a channel.
38     * The implementation of this method in the PircBot abstract class
39     * performs no actions and may be overridden as required.
40     *
41     * @param channel The channel to which the message was sent.
42     * @param sender The nick of the person who sent the message.
43     * @param login The login of the person who sent the message.
44     * @param hostname The hostname of the person who sent the message.
45     * @param message The actual message sent to the channel.
46     */
47    protected final void onMessage(final String channel, final String sender,
48        final String login, final String hostname, final String message) {
49      try {
50        if (irclistener != null) {
51          irclistener.infoMessage(channel, sender, login, hostname, message);
52        }
53        if (receiver != null && mfactory != null) {
54          receiver.receive(mfactory.createMessage(message));
55        } else {
56          throw new MessageException(message);
57        }
58      } catch (MessageException e) {
59        if (irclistener != null) {
60          irclistener.messageUnparsed(message);
61        } else {
62          System.out.println(e.getMessage());
63        }
64      }
65    }
66  
67    /**
68     * metodo di invio messaggi.
69     *
70     * @param m messaggio da inviare da questo oggetto
71     */
72    public final void send(final IMessage m) {
73      if (irclistener != null) {
74        this.sendMessage(irclistener.getTarget(), m.toString());
75      } else {
76        this.sendRawLineViaQueue(m.toString());
77      }
78    }
79  
80    /**
81     * setta il receiver che riceverà ogni messaggio.
82     *
83     * @param preceiver parametro receiver
84     */
85    public final void setReceiver(final Receiver preceiver) {
86      this.receiver = preceiver;
87    }
88  
89    /**
90     * setta il listener che riceverà ogni messaggio.
91     *
92     * @param pirclistener parametro listener
93     */
94    public final void setIrcframe(final IrcListener pirclistener) {
95      this.irclistener = pirclistener;
96    }
97  
98    /**
99     * After calling the listChannels() method in PircBot, the server
100    * will start to send us information about each channel on the
101    * server.  You may override this method in order to receive the
102    * information about each channel as soon as it is received.
103    * Note that certain channels, such as those marked as hidden,
104    * may not appear in channel listings.
105    * The implementation of this method in the PircBot abstract class
106    * performs no actions and may be overridden as required.
107    *
108    * @param channel The name of the channel.
109    * @param userCount The number of users visible in this channel.
110    * @param topic The topic for this channel.
111    *
112    * @see #listChannels() listChannels
113    */
114   protected final void onChannelInfo(final String channel, final int userCount,
115       final String topic) {
116     super.onChannelInfo(channel, userCount, topic);
117     this.irclistener.showChannel(channel, userCount, topic);
118   }
119 
120   /**
121    * Sets the name of the bot, which will be used as its nick when it
122    * tries to join an IRC server.  This should be set before joining
123    * any servers, otherwise the default nick will be used.  You would
124    * typically call this method from the constructor of the class that
125    * extends PircBot.
126    * The changeNick method should be used if you wish to change your nick
127    * when you are connected to a server.
128    *
129    * @param name The new name of the Bot.
130    */
131   public final void setMyName(final String name) {
132     super.setName(name);
133   }
134 
135   /**
136    * This method is called once the PircBot has successfully connected to
137    * the IRC server.
138    * The implementation of this method in the PircBot abstract class
139    * performs no actions and may be overridden as required.
140    *
141    * @since PircBot 0.9.6
142    */
143   protected final void onConnect() {
144     super.onConnect();
145     irclistener.connectionEstablished();
146   }
147 
148   /**
149    * The actions to perform when a PING request comes from the server.
150    * This sends back a correct response, so if you override this method,
151    * be sure to either mimic its functionality or to call
152    * super.onServerPing(response);
153    *
154    * @param response The response that should be given back in your PONG.
155    */
156   protected final void onServerPing(final String response) {
157     super.onServerPing(response);
158     irclistener.serverPing(response);
159   }
160 
161   /**
162    * This method is called when we receive a numeric response from the
163    * IRC server.
164    * Numerics in the range from 001 to 099 are used for client-server
165    * connections only and should never travel between servers.  Replies
166    * generated in response to commands are found in the range from 200
167    * to 399.  Error replies are found in the range from 400 to 599.
168    * For example, we can use this method to discover the topic of a
169    * channel when we join it.  If we join the channel #test which
170    * has a topic of "I am King of Test" then the response
171    * will be &quot;<code>PircBot #test :I Am King of Test</code>&quot;
172    * with a code of 332 to signify that this is a topic.
173    * (This is just an example - note that overriding the
174    * <code>onTopic</code> method is an easier way of finding the
175    * topic for a channel). Check the IRC RFC for the full list of other
176    * command response codes.
177    * PircBot implements the interface ReplyConstants, which contains
178    * contstants that you may find useful here.
179    * The implementation of this method in the PircBot abstract class
180    * performs no actions and may be overridden as required.
181    *
182    * @param code The three-digit numerical code for the response.
183    * @param response The full response from the IRC server.
184    *
185    * @see ReplyConstants
186    */
187   protected final void onServerResponse(final int code, final String response) {
188     super.onServerResponse(code, response);
189     irclistener.serverResponse(code, response);
190   }
191 
192   /**
193    * This method is called whenever we receive a line from the server that
194    * the PircBot has not been programmed to recognise.
195    * The implementation of this method in the PircBot abstract class
196    * performs no actions and may be overridden as required.
197    *
198    * @param line The raw line that was received from the server.
199    */
200   protected final void onUnknown(final String line) {
201     super.onUnknown(line);
202     irclistener.onUnknown(line);
203   }
204 
205   /**
206    * setta il message factory per creare i messaggi dalle stringhe ricevute.
207    * @param pfactory il MessageFactory
208    */
209   public final void setMessageFactory(final IMessageFactory pfactory) {
210     this.mfactory = pfactory;
211   }
212 
213   /**
214    * ritorna il listener.
215    * @return ritorna il listener dell'oggetto
216    */
217   protected final IrcListener getIrclistener() {
218     return irclistener;
219   }
220 
221   /**
222    * setta il listener.
223    * @param pirclistener il listener da associare
224    */
225   protected final void setIrclistener(final IrcListener pirclistener) {
226     this.irclistener = pirclistener;
227   }
228 
229   /**
230    * ritorna il receiver.
231    * @return ritorna il receiver dell'oggetto
232    */
233   protected final Receiver getReceiver() {
234     return receiver;
235   }
236 
237   /**
238    * ritorna il MessageFactory.
239    * @return ritorna il MessageFactory dell'oggetto
240    */
241   protected final IMessageFactory getMessageFactory() {
242     return mfactory;
243   }
244 }