]> cgit.babelmonkeys.de Git - mpdbot.git/blob - src/mpdbot.m
Use new callback API of objXMPP
[mpdbot.git] / src / mpdbot.m
1 /*
2  * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
3  * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
4  *
5  * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice is present in all copies.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
15  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 #import <ObjFW/ObjFW.h>
25 #import <ObjXMPP/ObjXMPP.h>
26
27 #import "PEPThread.h"
28
29 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
30
31 @interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
32 {
33         PEPThread *pepper;
34 }
35 @end
36
37 OF_APPLICATION_DELEGATE(AppDelegate)
38
39 @implementation AppDelegate
40 - (void)applicationDidFinishLaunching
41 {
42         XMPPConnection *conn;
43         OFArray *arguments = [OFApplication arguments];
44
45         conn = [[XMPPConnection alloc] init];
46         [conn addDelegate: self];
47
48         if (arguments.count != 3) {
49                 [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
50                     [OFApplication programName]];
51                 [OFApplication terminateWithStatus: 1];
52         }
53
54         conn.domain = [arguments objectAtIndex: 0];
55         conn.username = [arguments objectAtIndex: 1];
56         conn.password = [arguments objectAtIndex: 2];
57         conn.resource = @"ObjXMPP";
58
59         @try {
60                 [conn connect];
61                 [conn handleConnection];
62         } @catch (id e) {
63                 [of_stderr writeFormat: @"%@\n", e];
64         }
65 }
66
67 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
68 {
69         [of_stdout writeLine: @"Auth successful"];
70 }
71
72 - (void)connection: (XMPPConnection*)conn
73      wasBoundToJID: (XMPPJID*)jid
74 {
75         XMPPPresence *pres;
76         XMPPIQ *disco;
77         OFString *discoID;
78
79         [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
80
81         pres = [XMPPPresence presence];
82         [pres addPriority: 0];
83         [pres addStatus: @"Hello I'm MPDbot!"];
84         [conn sendStanza: pres];
85
86         discoID = [[conn generateStanzaID] retain];
87         disco = [XMPPIQ IQWithType: @"get"
88                                 ID: discoID];
89         disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
90         [disco addChild: [OFXMLElement
91             elementWithName: @"query"
92                   namespace: NS_DISCO_INFO]];
93
94         [conn       sendIQ: disco
95         withCallbackObject: self
96                   selector: @selector(mpdbot_handleDiscoForConnection:withIQ:)];
97 }
98
99 - (BOOL)mpdbot_handleDiscoForConnection: (XMPPConnection*)conn
100                                  withIQ: (XMPPIQ*)iq
101 {
102         OFXMLElement *query = [iq elementForName: @"query"
103                                        namespace: NS_DISCO_INFO];
104         for (OFXMLElement *identity in [query elementsForName: @"identity"
105                                                     namespace: NS_DISCO_INFO]) {
106                 if ([[[identity attributeForName: @"category"]
107                     stringValue] isEqual: @"pubsub"] &&
108                     [[[identity attributeForName: @"type"] stringValue]
109                     isEqual: @"pep"]) {
110                         pepper = [[PEPThread alloc]
111                             initWithObject: conn];
112                         [pepper start];
113
114                         return YES;
115                 }
116         }
117         [of_stderr writeLine: @"Server does NOT support PEP"];
118
119         return NO;
120 }
121
122 -   (void)connection: (XMPPConnection*)conn
123   didReceivePresence: (XMPPPresence*)pres
124 {
125         if ([pres.type isEqual: @"subscribe"]) {
126                 XMPPPresence *answer;
127                 answer = [XMPPPresence presenceWithType: @"subscribed"
128                                                      ID: pres.ID];
129                 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
130                 [conn sendStanza: answer];
131         }
132 }
133
134 - (void)connectionWasClosed: (XMPPConnection*)conn
135 {
136         [of_stdout writeLine: @"Connection was closed!"];
137 }
138 @end