caecdc75ef554268896c749b0e159726ce111acf
[riece] / lisp / riece-irc.el
1 ;;; riece-irc.el --- IRC protocol
2 ;; Copyright (C) 1998-2004 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'riece-filter)
28 (require 'riece-server)
29
30 (defun riece-irc-open-server (server server-name)
31   (riece-server-keyword-bind server
32     (let (selective-display
33           (coding-system-for-read 'binary)
34           (coding-system-for-write 'binary)
35           process)
36       (if (equal server-name "")
37           (message "Connecting to IRC server...")
38         (message "Connecting to %s..." server-name))
39       (condition-case error
40           (setq process
41                 (funcall function (riece-server-process-name server-name)
42                          (concat " *IRC*" server-name)
43                          host service))
44         (error
45          (if (equal server-name "")
46              (message "Connecting to IRC server...failed: %S" error)
47            (message "Connecting to %s...failed: %S" server-name error))
48          (signal (car error) (cdr error))))
49       (if (equal server-name "")
50           (message "Connecting to IRC server...done")
51         (message "Connecting to %s...done" server-name))
52       (riece-reset-process-buffer process)
53       (with-current-buffer (process-buffer process)
54         (setq riece-server-name server-name))
55       (set-process-sentinel process 'riece-sentinel)
56       (set-process-filter process 'riece-filter)
57       (if (equal server-name "")
58           (message "Logging in to IRC server...")
59         (message "Logging in to %s..." server-name))
60       (if riece-reconnect-with-password ;password incorrect or not set.
61           (unwind-protect
62               (setq password
63                     (condition-case nil
64                         (let (inhibit-quit)
65                           (if (equal server-name "")
66                               (riece-read-passwd "Password: ")
67                             (riece-read-passwd (format "Password for %s: "
68                                                        server-name))))
69                       (quit
70                        (if (equal server-name "")
71                            (message "Password: Quit")
72                          (message "Password for %s: " server-name))
73                        'quit)))
74             (setq riece-reconnect-with-password nil)))
75       (if (eq password 'quit)
76           (delete-process process)
77         (if password
78             (riece-process-send-string process
79                                        (format "PASS %s\r\n" password)))
80         (riece-process-send-string process
81                                    (format "USER %s * * :%s\r\n"
82                                            (user-real-login-name)
83                                            (or username
84                                                "No information given")))
85         (riece-process-send-string process (format "NICK %s\r\n" nickname))
86         (with-current-buffer (process-buffer process)
87           (setq riece-last-nickname riece-real-nickname
88                 riece-nick-accepted 'sent
89                 riece-coding-system coding))
90         process))))
91
92 (defun riece-irc-quit-server-process (process &optional message)
93   (if riece-quit-timeout
94       (riece-run-at-time riece-quit-timeout nil
95                          (lambda (process)
96                            (if (rassq process riece-server-process-alist)
97                                (delete-process process)))
98                          process))
99   (riece-process-send-string process
100                              (if message
101                                  (format "QUIT :%s\r\n" message)
102                                "QUIT\r\n")))
103
104 (provide 'riece-irc)