631a531ec56e9e3562cf77c01ac4860d042b10d9
[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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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: Quit" 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                                            (or username
83                                                (user-real-login-name))
84                                            (or realname
85                                                "No information given")))
86         (riece-process-send-string process (format "NICK %s\r\n" nickname))
87         (with-current-buffer (process-buffer process)
88           (setq riece-last-nickname riece-real-nickname
89                 riece-nick-accepted 'sent
90                 riece-coding-system coding))
91         process))))
92
93 (defun riece-irc-quit-server-process (process &optional message)
94   (if riece-quit-timeout
95       (riece-run-at-time riece-quit-timeout nil
96                          (lambda (process)
97                            (if (rassq process riece-server-process-alist)
98                                (delete-process process)))
99                          process))
100   (let ((server-name (with-current-buffer (process-buffer process)
101                        riece-server-name)))
102     (if (equal server-name "")
103         (message "Sending QUIT...")
104       (message "Sending QUIT to \"%s\"..." server-name))
105     (riece-process-send-string process
106                                (if message
107                                    (format "QUIT :%s\r\n" message)
108                                  "QUIT\r\n"))
109     (if (equal server-name "")
110         (message "Sending QUIT...done")
111       (message "Sending QUIT to \"%s\"...done" server-name))))
112
113 (provide 'riece-irc)
114
115 ;;; riece-irc.el ends here